diff --git a/examples/button/button.rb b/examples/button/button.rb index ea18e421..171a6cab 100644 --- a/examples/button/button.rb +++ b/examples/button/button.rb @@ -4,7 +4,8 @@ # sleep or in someway keep running or your program # will exit before any callbacks can be called # -require File.expand_path('../../../lib/dino', __FILE__) +require 'bundler/setup' +require 'dino' board = Dino::Board.new(Dino::TxRx.new) button = Dino::Components::Button.new(pin: 13, board: board) diff --git a/examples/ir_receiver.rb b/examples/ir_receiver/ir_receiver.rb similarity index 91% rename from examples/ir_receiver.rb rename to examples/ir_receiver/ir_receiver.rb index 56801125..5d279a28 100644 --- a/examples/ir_receiver.rb +++ b/examples/ir_receiver/ir_receiver.rb @@ -4,7 +4,8 @@ # sleep or in someway keep running or your program # will exit before any callbacks can be called # -require File.expand_path('../../lib/dino', __FILE__) +require 'bundler/setup' +require 'dino' board = Dino::Board.new(Dino::TxRx.new) ir = Dino::Components::IrReceiver.new(pin: 2, board: board) diff --git a/examples/led/led.rb b/examples/led/led.rb index 4759026f..86ea5046 100644 --- a/examples/led/led.rb +++ b/examples/led/led.rb @@ -2,8 +2,8 @@ # This is a simple example to blink an led # every half a second # - -require File.expand_path('../../../lib/dino', __FILE__) +require 'bundler/setup' +require 'dino' board = Dino::Board.new(Dino::TxRx.new) led = Dino::Components::Led.new(pin: 13, board: board) diff --git a/examples/potentiometer.rb b/examples/potentiometer/potentiometer.rb similarity index 81% rename from examples/potentiometer.rb rename to examples/potentiometer/potentiometer.rb index 97310012..d7712289 100644 --- a/examples/potentiometer.rb +++ b/examples/potentiometer/potentiometer.rb @@ -4,8 +4,8 @@ # LED. The set_delay callback reads from the potentiometer # and changes the sleep delay for the LED on/off cycle. # - -require File.expand_path('../../lib/dino', __FILE__) +require 'bundler/setup' +require 'dino' board = Dino::Board.new(Dino::TxRx.new) led = Dino::Components::Led.new(pin: 13, board: board) @@ -13,12 +13,10 @@ delay = 500.0 -set_delay = Proc.new do |data| +potentiometer.when_data_received do |data| puts "DATA: #{delay = data.to_i}" end -potentiometer.when_data_received(set_delay) - [:on, :off].cycle do |switch| puts "DELAY: #{seconds = (delay / 1000.0)}" led.send(switch) diff --git a/examples/rgb_led.rb b/examples/rgb_led/rgb_led.rb similarity index 82% rename from examples/rgb_led.rb rename to examples/rgb_led/rgb_led.rb index 16fe5241..ce2b5add 100644 --- a/examples/rgb_led.rb +++ b/examples/rgb_led/rgb_led.rb @@ -2,8 +2,8 @@ # This is a simple example to blink an led # every half a second # - -require File.expand_path('../../lib/dino', __FILE__) +require 'bundler/setup' +require 'dino' board = Dino::Board.new(Dino::TxRx.new) led = Dino::Components::RgbLed.new(pins: {red: 11, green: 10, blue: 9}, board: board) @@ -12,13 +12,11 @@ delay = 500.0 -set_delay = Proc.new do |data| +potentiometer.when_data_received do |data| sleep 0.5 puts "DATA: #{delay = data.to_i}" end -potentiometer.when_data_received(set_delay) - sleep(2) loop do puts "DELAY: #{seconds = (delay / 1000.0)}" diff --git a/examples/sensor.rb b/examples/sensor/sensor.rb similarity index 71% rename from examples/sensor.rb rename to examples/sensor/sensor.rb index a119e67d..1b4693b6 100644 --- a/examples/sensor.rb +++ b/examples/sensor/sensor.rb @@ -4,15 +4,14 @@ # sleep or in someway keep running or your program # will exit before any callbacks can be called # -require File.expand_path('../../lib/dino', __FILE__) +require 'bundler/setup' +require 'dino' board = Dino::Board.new(Dino::TxRx.new) sensor = Dino::Components::Sensor.new(pin: 'A0', board: board) -on_data = Proc.new do |data| - puts data +sensor.when_data_received do |data| + puts data end -sensor.when_data_received(on_data) - sleep diff --git a/examples/servo.rb b/examples/servo/servo.rb similarity index 78% rename from examples/servo.rb rename to examples/servo/servo.rb index 938204d5..41d95189 100644 --- a/examples/servo.rb +++ b/examples/servo/servo.rb @@ -1,8 +1,8 @@ # # This is an example of how to use the servo class # - -require File.expand_path('../../lib/dino', __FILE__) +require 'bundler/setup' +require 'dino' board = Dino::Board.new(Dino::TxRx.new) servo = Dino::Components::Servo.new(pin: 9, board: board) diff --git a/examples/stepper/stepper.rb b/examples/stepper/stepper.rb index 8017f982..9181a9c9 100644 --- a/examples/stepper/stepper.rb +++ b/examples/stepper/stepper.rb @@ -1,8 +1,8 @@ # # This is a simple example to move a stepper motor using the sparkfun easydriver shield: https://www.sparkfun.com/products/10267? # - -require File.expand_path('../../../lib/dino', __FILE__) +require 'bundler/setup' +require 'dino' board = Dino::Board.new(Dino::TxRx.new) stepper = Dino::Components::Stepper.new(board: board, pins: { step: 10, direction: 8 }) diff --git a/lib/dino/components/rgb_led.rb b/lib/dino/components/rgb_led.rb index e536958d..787b4fe4 100644 --- a/lib/dino/components/rgb_led.rb +++ b/lib/dino/components/rgb_led.rb @@ -13,22 +13,24 @@ def after_initialize(options={}) end end - def blue - analog_write(pins[:red], Board::LOW) - analog_write(pins[:green], Board::LOW) - analog_write(pins[:blue], Board::HIGH) - end - - def red - analog_write(pins[:red], Board::HIGH) - analog_write(pins[:green], Board::LOW) - analog_write(pins[:blue], Board::LOW) - end + # Format: [R, G, B] + COLORS = { + red: [255, 000, 000], + green: [000, 255, 000], + blue: [000, 000, 255], + cyan: [000, 255, 255], + yellow: [255, 255, 000], + magenta: [255, 000, 255], + white: [255, 255, 255], + off: [000, 000, 000] + } - def green - analog_write(pins[:red], Board::LOW) - analog_write(pins[:green], Board::HIGH) - analog_write(pins[:blue], Board::LOW) + COLORS.each_key do |color| + define_method(color) do + analog_write(COLORS[color][0], pins[:red]) + analog_write(COLORS[color][1], pins[:green]) + analog_write(COLORS[color][2], pins[:blue]) + end end def blinky diff --git a/lib/dino/components/sensor.rb b/lib/dino/components/sensor.rb index aab25794..485f389d 100644 --- a/lib/dino/components/sensor.rb +++ b/lib/dino/components/sensor.rb @@ -7,8 +7,8 @@ def after_initialize(options={}) @board.start_read end - def when_data_received(callback) - @data_callbacks << callback + def when_data_received(&block) + @data_callbacks << block end def update(data) diff --git a/lib/dino/tx_rx.rb b/lib/dino/tx_rx.rb index 0c541459..39bde2cb 100644 --- a/lib/dino/tx_rx.rb +++ b/lib/dino/tx_rx.rb @@ -4,8 +4,8 @@ module TxRx require 'dino/tx_rx/usb_serial' require 'dino/tx_rx/tcp' - def self.new - self::USBSerial.new + def self.new(device = nil) + self::USBSerial.new(device) end end end \ No newline at end of file diff --git a/lib/dino/tx_rx/usb_serial.rb b/lib/dino/tx_rx/usb_serial.rb index 46198be0..b23d253b 100644 --- a/lib/dino/tx_rx/usb_serial.rb +++ b/lib/dino/tx_rx/usb_serial.rb @@ -5,7 +5,8 @@ module TxRx class USBSerial < Base BAUD = 115200 - def initialize + def initialize(device = nil) + @device = device @first_write = true end @@ -21,8 +22,11 @@ def io=(device) private def tty_devices + return [@device] if @device if RUBY_PLATFORM.include?("mswin") || RUBY_PLATFORM.include?("mingw") - ["COM1", "COM2", "COM3", "COM4"] + com_ports = [] + 1.upto(9) { |n| com_ports << "COM#{n}" } + com_ports else `ls /dev`.split("\n").grep(/usb|ACM/).map{|d| "/dev/#{d}"} end diff --git a/spec/lib/components/rgb_led_spec.rb b/spec/lib/components/rgb_led_spec.rb index 6d9082d9..45ac741f 100644 --- a/spec/lib/components/rgb_led_spec.rb +++ b/spec/lib/components/rgb_led_spec.rb @@ -64,6 +64,51 @@ module Components end end + describe '#cyan' do + it 'should set blue and green to high, red to low' do + board.should_receive(:analog_write).with(1, Board::LOW) + board.should_receive(:analog_write).with(2, Board::HIGH) + board.should_receive(:analog_write).with(3, Board::HIGH) + rgb.cyan + end + end + + describe '#yellow' do + it 'should set red and green to high, blue to low' do + board.should_receive(:analog_write).with(1, Board::HIGH) + board.should_receive(:analog_write).with(2, Board::HIGH) + board.should_receive(:analog_write).with(3, Board::LOW) + rgb.yellow + end + end + + describe '#magenta' do + it 'should set red and blue to high, green to low' do + board.should_receive(:analog_write).with(1, Board::HIGH) + board.should_receive(:analog_write).with(2, Board::LOW) + board.should_receive(:analog_write).with(3, Board::HIGH) + rgb.magenta + end + end + + describe '#white' do + it 'should set all to high' do + board.should_receive(:analog_write).with(1, Board::HIGH) + board.should_receive(:analog_write).with(2, Board::HIGH) + board.should_receive(:analog_write).with(3, Board::HIGH) + rgb.white + end + end + + describe '#off' do + it 'should set all to low' do + board.should_receive(:analog_write).with(1, Board::LOW) + board.should_receive(:analog_write).with(2, Board::LOW) + board.should_receive(:analog_write).with(3, Board::LOW) + rgb.off + end + end + describe '#blinky' do it 'should set blue to high, red and green to low' do Array.any_instance.should_receive(:cycle).and_yield(:red).and_yield(:green).and_yield(:blue) diff --git a/spec/lib/components/sensor_spec.rb b/spec/lib/components/sensor_spec.rb index 53097327..7767ab92 100644 --- a/spec/lib/components/sensor_spec.rb +++ b/spec/lib/components/sensor_spec.rb @@ -32,25 +32,29 @@ module Components end describe '#when_data_received' do - it 'should add a callback back to the list of callbacks' do + + it "should add a callback to the list of callbacks" do sensor = Sensor.new(board: board, pin: 'a pin') - sensor.when_data_received 'Foo' - sensor.instance_variable_get(:@data_callbacks).should == ['Foo'] + sensor.when_data_received { "this is a block" } + sensor.instance_variable_get(:@data_callbacks).should_not be_empty end end describe '#update' do it 'should call all callbacks passing in the given data' do - first_callback, second_callback = mock, mock - first_callback.should_receive(:call).with('Some data') - second_callback.should_receive(:call).with('Some data') - sensor = Sensor.new(board: board, pin: 'a pin') - - sensor.when_data_received first_callback - sensor.when_data_received second_callback + + first_block_data = nil + second_block_data = nil + sensor.when_data_received do |data| + first_block_data = data + end + sensor.when_data_received do |data| + second_block_data = data + end sensor.update('Some data') + [first_block_data, second_block_data].each { |block_data| block_data.should == "Some data" } end end end diff --git a/spec/lib/tx_rx/usb_serial_spec.rb b/spec/lib/tx_rx/usb_serial_spec.rb index 78158e8c..a83aaf22 100644 --- a/spec/lib/tx_rx/usb_serial_spec.rb +++ b/spec/lib/tx_rx/usb_serial_spec.rb @@ -14,7 +14,7 @@ module Dino context "on windows" do it 'should instantiate a new SerialPort for each usb tty device found' do original_platform = RUBY_PLATFORM - RUBY_PLATFORM = "mswin" + Constants.redefine(:RUBY_PLATFORM, "mswin", :on => Object) subject.should_receive(:tty_devices).and_return(["COM1", "COM2", "COM3", "COM4"]) SerialPort.should_receive(:new).with('COM1', TxRx::USBSerial::BAUD).and_return(mock_serial = mock) SerialPort.should_receive(:new).with('COM2', TxRx::USBSerial::BAUD).and_return(mock) @@ -22,7 +22,7 @@ module Dino SerialPort.should_receive(:new).with('COM4', TxRx::USBSerial::BAUD).and_return(mock) subject.io.should == mock_serial - RUBY_PLATFORM = original_platform + Constants.redefine(:RUBY_PLATFORM, original_platform, :on => Object) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6eb43d1b..3ed69bc3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,12 @@ require 'rspec' -require File.expand_path(File.join('../..', 'lib/dino'), __FILE__) \ No newline at end of file +require File.expand_path(File.join('../..', 'lib/dino'), __FILE__) + +# Nice little helper module to redefine constants quietly. +module Constants + def self.redefine(const, value, opts={}) + opts = {:on => self.class}.merge(opts) + opts[:on].send(:remove_const, const) if self.class.const_defined?(const) + opts[:on].const_set(const, value) + end +end \ No newline at end of file