Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' into ethernet-shield

  • Loading branch information...
commit 9784361daefdd36f4f587aa9c1b18a3a6e761465 2 parents 2b86eb3 + 308a12d
@vickash vickash authored
View
3  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)
View
3  examples/ir_receiver.rb → 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)
View
4 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)
View
8 examples/potentiometer.rb → 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)
View
8 examples/rgb_led.rb → 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)}"
View
9 examples/sensor.rb → 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
View
4 examples/servo.rb → 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)
View
4 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 })
View
32 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
View
4 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)
View
4 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
View
8 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
View
45 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)
View
24 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
View
4 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
View
11 spec/spec_helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.