More Ruby Syntax Fun

July 9, 2008

With the addition of Brian’s revised lcd and servo libraries, we’ve been working to ensure consistent syntax across the sketch.  The original screencast featured the following method calls:

 1 class Quattro < ArduinoSketch
 2
 3   input_pin 3, :as => :sensor
 4   output_pin 13, :as => :led
 5
 6
 7   def loop
 8     reading = add_hysteresis sensor, 8
 9     blink led, 100 if reading > 100
10     blink led, 1000 if reading <= 100
11   end
12
13 end

However, isn’t dot notation more intuitive:

 1 class Quattro < ArduinoSketch
 2
 3   input_pin 3, :as => :sensor
 4   output_pin 13, :as => :led
 5
 6
 7   def loop
 8     reading = add_hysteresis sensor, 8
 9     led.blink 100 if reading > 100
10     led.blink 1000 if reading <= 100
11   end
12
13 end

Today both work.

How do we make input and output pin declarations simple and straightforward?  The first pass used options to trigger pin behavior.  In the following example, setting a min and max sets up a servo, and the latch option sets up a button:

1 class InOutDemo < ArduinoSketch
2
3   input_pin 6, :as => :button_one, :latch => :off
4   output_pin 7, :as => :my_servo, :min => 700, :max => 2200
5

After working through this, the notion of a device option seems like a better choice:

1 class InOutDemo < ArduinoSketch
2
3   input_pin 6, :as => :button_one, :device => :button
4   output_pin 7, :as => :my_servo, :device => :servo
5   output_pin 8, :as => :my_lcd, :device => :sf_lcd
6

Declaring a device will setup default settings for that device. For example, the servo will default to a min and max of 544 and 2400. For LCD screens, the default serial rate and driver is setup.