Skip to content

Commit

Permalink
Merge branch 'master' into ethernet-shield
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Feb 28, 2013
2 parents 2b86eb3 + 308a12d commit 9784361
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 57 deletions.
3 changes: 2 additions & 1 deletion examples/button/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/led/led.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@
# 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)
potentiometer = Dino::Components::Sensor.new(pin: 'A0', board: board)

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)
Expand Down
8 changes: 3 additions & 5 deletions examples/rgb_led.rb → examples/rgb_led/rgb_led.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)}"
Expand Down
9 changes: 4 additions & 5 deletions examples/sensor.rb → examples/sensor/sensor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions examples/servo.rb → examples/servo/servo.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/stepper/stepper.rb
Original file line number Diff line number Diff line change
@@ -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 })
Expand Down
32 changes: 17 additions & 15 deletions lib/dino/components/rgb_led.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/dino/components/sensor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/dino/tx_rx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions lib/dino/tx_rx/usb_serial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module TxRx
class USBSerial < Base
BAUD = 115200

def initialize
def initialize(device = nil)
@device = device
@first_write = true
end

Expand All @@ -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
Expand Down
45 changes: 45 additions & 0 deletions spec/lib/components/rgb_led_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 14 additions & 10 deletions spec/lib/components/sensor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/tx_rx/usb_serial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ 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)
SerialPort.should_receive(:new).with('COM3', TxRx::USBSerial::BAUD).and_return(mock)
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

Expand Down
11 changes: 10 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
require 'rspec'

require File.expand_path(File.join('../..', 'lib/dino'), __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

0 comments on commit 9784361

Please sign in to comment.