Class: Commands::TouchSensor

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

Overview

Implements the “Touch Sensor” block in NXT-G

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Sensor

#port=

Constructor Details

#initialize(nxt) ⇒ TouchSensor

Returns a new instance of TouchSensor.



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

def initialize(nxt)
  @nxt      = nxt
  
  # defaults the same as NXT-G
  @port   = 1
  @action = :pressed
  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



81
82
83
# File 'lib/commands/touch_sensor.rb', line 81

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

Instance Attribute Details

#actionObject

Returns the value of attribute action.



27
28
29
# File 'lib/commands/touch_sensor.rb', line 27

def action
  @action
end

#portObject (readonly)

Returns the value of attribute port.



27
28
29
# File 'lib/commands/touch_sensor.rb', line 27

def port
  @port
end

Instance Method Details

#comparison=(op) ⇒ Object

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/commands/touch_sensor.rb', line 44

def comparison=(op)
  raise NotImplementedError, "Cannot assign a comparison operator for this sensor type."
end

#logicObject

returns true or false based on action type



49
50
51
52
53
54
55
56
57
58
# File 'lib/commands/touch_sensor.rb', line 49

def logic
  case @action
    when :pressed
      value_scaled > 0 ? true : false
    when :released
      value_scaled > 0 ? false : true
    when :bumped
      value_scaled > 0 ? true : false
  end
end

#raw_valueObject

returns the raw value of the sensor



61
62
63
# File 'lib/commands/touch_sensor.rb', line 61

def raw_value
  value_raw
end

#resetObject

resets the value_scaled property, use this to reset the sensor when in :bumped mode



66
67
68
# File 'lib/commands/touch_sensor.rb', line 66

def reset
  @nxt.reset_input_scaled_value(NXTComm.const_get("SENSOR_#{@port}"))
end

#set_modeObject

sets up the sensor port



71
72
73
74
75
76
77
78
# File 'lib/commands/touch_sensor.rb', line 71

def set_mode
  @action == :bumped ? mode = NXTComm::PERIODCOUNTERMODE : mode = NXTComm::BOOLEANMODE
  @nxt.set_input_mode(
    NXTComm.const_get("SENSOR_#{@port}"),
    NXTComm::SWITCH,
    mode
  )
end