Class: Commands::SoundSensor

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

Overview

Implements the “Sound Sensor” block in NXT-G

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Sensor

#logic, #port=

Constructor Details

#initialize(nxt) ⇒ SoundSensor



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

def initialize(nxt)
  @nxt      = nxt
  
  # defaults the same as NXT-G
  @port           = 2
  @trigger_point  = 50
  @comparison     = ">"
  @mode           = "dba"
  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



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

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/sound_sensor.rb', line 26

def comparison
  @comparison
end

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#trigger_pointObject

Returns the value of attribute trigger_point.



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

def trigger_point
  @trigger_point
end

Instance Method Details

#raw_valueObject

returns the raw value of the sensor



50
51
52
# File 'lib/commands/sound_sensor.rb', line 50

def raw_value
  value_raw
end

#set_modeObject

sets up the sensor port



55
56
57
58
59
60
61
# File 'lib/commands/sound_sensor.rb', line 55

def set_mode
  @nxt.set_input_mode(
    NXTComm.const_get("SENSOR_#{@port}"),
    NXTComm.const_get("SOUND_#{@mode.upcase}"),
    NXTComm::PCTFULLSCALEMODE
  )
end

#sound_levelObject

scaled value read from sensor



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

def sound_level
  value_scaled
end