Class: LightSensor

Inherits:
Sensor show all
Defined in:
lib/sensors/light_sensor.rb

Constant Summary

Constants inherited from Sensor

Sensor::POLL_INTERVAL

Instance Attribute Summary

Attributes inherited from Brick

#log, #port

Instance Method Summary collapse

Methods inherited from Sensor

#name, #off, #read_data, #set_input_mode, #wait_for_event

Constructor Details

#initialize(nxt, port = NXTComm::SENSOR_3) ⇒ LightSensor

Returns a new instance of LightSensor.



23
24
25
26
# File 'lib/sensors/light_sensor.rb', line 23

def initialize(nxt, port = NXTComm::SENSOR_3)
  super(nxt, port)
  use_illuminated_mode
end

Instance Method Details

#get_light_levelObject

Get the current light level as a float from 0 to 1.0. 1.0 is maximum, 0 is minimum.



30
31
32
33
# File 'lib/sensors/light_sensor.rb', line 30

def get_light_level
  # TODO: probably need to calibrate this... light level never really reaches 1023
  (read_data[:value_scaled]).to_f / 1023.to_f
end

#use_ambient_modeObject



41
42
43
# File 'lib/sensors/light_sensor.rb', line 41

def use_ambient_mode
  set_input_mode(NXTComm::LIGHT_INACTIVE, NXTComm::RAWMODE)
end

#use_illuminated_modeObject



36
37
38
# File 'lib/sensors/light_sensor.rb', line 36

def use_illuminated_mode
  set_input_mode(NXTComm::LIGHT_ACTIVE, NXTComm::RAWMODE)
end