Class: Commands::LightSensor

Inherits:
Object
  • Object
show all
Includes:
Mixins::Sensor
Defined in:
lib/commands/light_sensor.rb

Overview

Implements (and extens) the “Light Sensor” block in NXT-G

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Sensor

#logic, #port=

Constructor Details

#initialize(nxt) ⇒ LightSensor

Returns a new instance of LightSensor.



28
29
30
31
32
33
34
35
36
37
# File 'lib/commands/light_sensor.rb', line 28

def initialize(nxt)
  @nxt      = nxt
  
  # defaults the same as NXT-G
  @port           = 3
  @trigger_point  = 50
  @comparison     = ">"
  @generate_light = true
  set_mode
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(cmd) ⇒ Object

attempt to return the input_value requested



79
80
81
# File 'lib/commands/light_sensor.rb', line 79

def method_missing(cmd)
  @nxt.get_input_values(NXTComm.const_get("SENSOR_#{@port}"))[cmd]
end

Instance Attribute Details

#comparisonObject

Returns the value of attribute comparison.



26
27
28
# File 'lib/commands/light_sensor.rb', line 26

def comparison
  @comparison
end

#generate_lightObject

Returns the value of attribute generate_light.



25
26
27
# File 'lib/commands/light_sensor.rb', line 25

def generate_light
  @generate_light
end

#portObject (readonly)

Returns the value of attribute port.



25
26
27
# File 'lib/commands/light_sensor.rb', line 25

def port
  @port
end

#trigger_pointObject

Returns the value of attribute trigger_point.



26
27
28
# File 'lib/commands/light_sensor.rb', line 26

def trigger_point
  @trigger_point
end

Instance Method Details

#ambient_modeObject

Turns off the sensor’s LED light.



40
41
42
# File 'lib/commands/light_sensor.rb', line 40

def ambient_mode
  self.generate_light = false
end

#illuminated_modeObject

Turns on the sensor’s LED light.



45
46
47
# File 'lib/commands/light_sensor.rb', line 45

def illuminated_mode
  self.generate_light = true
end

#intensityObject Also known as: light_level

intensity of light detected 0-100 in %



58
59
60
# File 'lib/commands/light_sensor.rb', line 58

def intensity
  value_scaled
end

#raw_valueObject

returns the raw value of the sensor



64
65
66
# File 'lib/commands/light_sensor.rb', line 64

def raw_value
  value_raw
end

#set_modeObject

sets up the sensor port



69
70
71
72
73
74
75
76
# File 'lib/commands/light_sensor.rb', line 69

def set_mode
  @generate_light ? mode = NXTComm::LIGHT_ACTIVE : mode = NXTComm::LIGHT_INACTIVE
  @nxt.set_input_mode(
    NXTComm.const_get("SENSOR_#{@port}"),
    mode,
    NXTComm::PCTFULLSCALEMODE
  )
end