Class: BrickPi::Sensor

Inherits:
Object
  • Object
show all
Defined in:
lib/brick_pi/sensor.rb

Constant Summary collapse

SENSOR_TYPES =
{
  touch: Native::TYPE_SENSOR_TOUCH,
  ultrasonic: Native::TYPE_SENSOR_ULTRASONIC_CONT,
  color: Native::TYPE_SENSOR_COLOR_FULL,
  color_red: Native::TYPE_SENSOR_COLOR_RED,
  color_green: Native::TYPE_SENSOR_COLOR_GREEN,
  color_blue: Native::TYPE_SENSOR_COLOR_BLUE,
  color_none: Native::TYPE_SENSOR_COLOR_NONE
}
PORT_MAP =
{
  port_1: Native::PORT_1,
  port_2: Native::PORT_2,
  port_3: Native::PORT_3,
  port_4: Native::PORT_4
}

Instance Method Summary collapse

Constructor Details

#initialize(port, sensor_type) ⇒ Sensor

So you can be all like ‘BrickPi::Sensor.new(:port_3, :ultrasonic)`



21
22
23
24
25
26
# File 'lib/brick_pi/sensor.rb', line 21

def initialize(port, sensor_type)
  @port = port
  @sensor_type = sensor_type
  Native::SensorType[PORT_MAP[@port]] = SENSOR_TYPES[@sensor_type]
  Native::BrickPiSetupSensors()
end

Instance Method Details

#distanceObject



38
39
40
# File 'lib/brick_pi/sensor.rb', line 38

def distance
  read if @sensor_type == :ultrasonic
end

#readObject



28
29
30
# File 'lib/brick_pi/sensor.rb', line 28

def read
  Native::Sensor[PORT_MAP[@port]] if @port
end

#touched?Boolean

Nice friendly access methods for different sensors types

Returns:

  • (Boolean)


34
35
36
# File 'lib/brick_pi/sensor.rb', line 34

def touched?
  @sensor_type == :touch && read == 1
end