Class: Rubotz::UltrasonicSensor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubotz/ultrasonic_sensor.rb

Overview

The UltrasonicSensor class is a code generator for the NXT ultrasonic sensor. It provides simple value comparison methods, value assignment NXC code, and an activation class method. Its variable is currently hard-coded; this is planned to change.

Ultimately all XyzSensors will inherit from a base Sensor class. This class will very likely be the only one to override Sensor’s #port method (see NXC docs for why).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUltrasonicSensor

Create the UltrasonicSensor, with a variable declaration and relevant NXC API code.



10
11
12
# File 'lib/rubotz/ultrasonic_sensor.rb', line 10

def initialize
  @code = "ultrasonic_sensor = SensorUS(S4);\n"
end

Class Method Details

.port(number) ⇒ Object

Activation code for initialization blocks.



15
16
17
# File 'lib/rubotz/ultrasonic_sensor.rb', line 15

def port(number)
  "SetSensorLowspeed(S#{number});\n"
end

Instance Method Details

#<(number, &block) ⇒ Object

UltrasonicSensor#< generates an if block around the <, the sensor itself (as an NXC variable), and the value to be compared against. It then executes arbitrary Ruby within the context of that if block. In practice this means turning Rubotz code into NXC code. (The instance method approach to value comparison is definitely a bit of a kludge, but it was a lot less work for me personally than implementing a parser.)



45
46
47
# File 'lib/rubotz/ultrasonic_sensor.rb', line 45

def < (number, &block)
  @code += "if (ultrasonic_sensor < #{number}) {\n" + Rubotz.generate(&block) + "}\n"
end

#<=(number, &block) ⇒ Object

UltrasonicSensor#<= generates an if block around the <=, the sensor itself (as an NXC variable), and the value to be compared against. It then executes arbitrary Ruby within the context of that if block. In practice this means turning Rubotz code into NXC code. (The instance method approach to value comparison is definitely a bit of a kludge, but it was a lot less work for me personally than implementing a parser.)



53
54
55
# File 'lib/rubotz/ultrasonic_sensor.rb', line 53

def <= (number, &block)
  @code += "if (ultrasonic_sensor <= #{number}) {\n" + Rubotz.generate(&block) + "}\n"
end

#>(number, &block) ⇒ Object

UltrasonicSensor#> generates an if block around the >, the sensor itself (as an NXC variable), and the value to be compared against. It then executes arbitrary Ruby within the context of that if block. In practice this means turning Rubotz code into NXC code. (The instance method approach to value comparison is definitely a bit of a kludge, but it was a lot less work for me personally than implementing a parser.)



29
30
31
# File 'lib/rubotz/ultrasonic_sensor.rb', line 29

def > (number, &block)
  @code += "if (ultrasonic_sensor > #{number}) {\n" + Rubotz.generate(&block) + "}\n"
end

#>=(number, &block) ⇒ Object

UltrasonicSensor#>= generates an if block around the >=, the sensor itself (as an NXC variable), and the value to be compared against. It then executes arbitrary Ruby within the context of that if block. In practice this means turning Rubotz code into NXC code. (The instance method approach to value comparison is definitely a bit of a kludge, but it was a lot less work for me personally than implementing a parser.)



37
38
39
# File 'lib/rubotz/ultrasonic_sensor.rb', line 37

def >= (number, &block)
  @code += "if (ultrasonic_sensor >= #{number}) {\n" + Rubotz.generate(&block) + "}\n"
end

#to_sObject

Utility method for variable interpolation in code which refers to this sensor.



21
22
23
# File 'lib/rubotz/ultrasonic_sensor.rb', line 21

def to_s
  "ultrasonic_sensor"
end