dino 0.8 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ script: bundle exec rspec spec
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Welcome to Dino
2
+ [![Build Status](https://secure.travis-ci.org/austinbv/dino.png)](http://travis-ci.org/austinbv/dino)
2
3
 
3
4
  ## Get started in no time
4
5
  Dino was designed to help you start working with your Arduino in minutes.
5
6
 
7
+ > * `gem install dino`
6
8
  > * Burn the file `src/du.ino` to your Arduino
7
9
  > ** You can do this in the [Arduino IDE](http://www.arduino.cc/en/Main/software)
8
10
  > * Plug in your Arduino and wire the led like in `examples/led/led.png`
@@ -12,3 +14,6 @@ By now you should be blinking away
12
14
 
13
15
 
14
16
  Take a look in the example directory for more information
17
+
18
+
19
+ I gave a [talk on this library at RubyConf](http://confreaks.com/videos/1294-rubyconf2012-arduino-the-ruby-way), you can see the slides here: [Arudino The Ruby Way](https://speakerdeck.com/austinbv/arduino-the-ruby-way)
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ task :default => [:spec]
5
+
6
+ require 'rspec/core/rake_task'
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.pattern = 'spec/**/*_spec.rb'
10
+ end
@@ -4,10 +4,10 @@
4
4
  # sleep or in someway keep running or your program
5
5
  # will exit before any callbacks can be called
6
6
  #
7
- require '../lib/dino'
7
+ require File.expand_path('../../../lib/dino', __FILE__)
8
8
 
9
9
  board = Dino::Board.new(Dino::TxRx.new)
10
- button = Dino::Components::Button.new(pin: 2, board: board)
10
+ button = Dino::Components::Button.new(pin: 13, board: board)
11
11
 
12
12
  button_down = Proc.new do
13
13
  puts "button down"
@@ -4,7 +4,7 @@
4
4
  # sleep or in someway keep running or your program
5
5
  # will exit before any callbacks can be called
6
6
  #
7
- require '../lib/dino'
7
+ require File.expand_path('../../lib/dino', __FILE__)
8
8
 
9
9
  board = Dino::Board.new(Dino::TxRx.new)
10
10
  ir = Dino::Components::IrReceiver.new(pin: 2, board: board)
Binary file
@@ -3,7 +3,7 @@
3
3
  # every half a second
4
4
  #
5
5
 
6
- require '../lib/dino'
6
+ require File.expand_path('../../../lib/dino', __FILE__)
7
7
 
8
8
  board = Dino::Board.new(Dino::TxRx.new)
9
9
  led = Dino::Components::Led.new(pin: 13, board: board)
@@ -5,7 +5,7 @@
5
5
  # and changes the sleep delay for the LED on/off cycle.
6
6
  #
7
7
 
8
- require '../lib/dino'
8
+ require File.expand_path('../../lib/dino', __FILE__)
9
9
 
10
10
  board = Dino::Board.new(Dino::TxRx.new)
11
11
  led = Dino::Components::Led.new(pin: 13, board: board)
@@ -3,7 +3,7 @@
3
3
  # every half a second
4
4
  #
5
5
 
6
- require '../lib/dino'
6
+ require File.expand_path('../../lib/dino', __FILE__)
7
7
 
8
8
  board = Dino::Board.new(Dino::TxRx.new)
9
9
  led = Dino::Components::RgbLed.new(pins: {red: 11, green: 10, blue: 9}, board: board)
@@ -4,7 +4,7 @@
4
4
  # sleep or in someway keep running or your program
5
5
  # will exit before any callbacks can be called
6
6
  #
7
- require '../lib/dino'
7
+ require File.expand_path('../../lib/dino', __FILE__)
8
8
 
9
9
  board = Dino::Board.new(Dino::TxRx.new)
10
10
  sensor = Dino::Components::Sensor.new(pin: 'A0', board: board)
@@ -4,8 +4,8 @@
4
4
  # sleep or in someway keep running or your program
5
5
  # will exit before any callbacks can be called
6
6
  #
7
- $: << File.expand_path('../../lib', __FILE__)
8
- require '../lib/dino'
7
+
8
+ require File.expand_path('../../lib/dino', __FILE__)
9
9
 
10
10
  board = Dino::Board.new(Dino::TxRx.new)
11
11
  servo = Dino::Components::Servo.new(pin: 9, board: board)
@@ -2,7 +2,7 @@
2
2
  # This is a simple example to move a stepper motor using the sparkfun easydriver shield: https://www.sparkfun.com/products/10267?
3
3
  #
4
4
 
5
- require '../lib/dino'
5
+ require File.expand_path('../../../lib/dino', __FILE__)
6
6
 
7
7
  board = Dino::Board.new(Dino::TxRx.new)
8
8
  stepper = Dino::Components::Stepper.new(board: board, pins: { step: 10, direction: 8 })
@@ -1,15 +1,5 @@
1
+ require 'dino/board_not_found'
1
2
  require 'dino/version'
2
3
  require 'dino/tx_rx'
3
4
  require 'dino/board'
4
-
5
- require 'dino/components/base_component'
6
- require 'dino/components/led'
7
- require 'dino/components/button'
8
- require 'dino/components/sensor'
9
- require 'dino/components/rgb_led'
10
- require 'dino/components/servo'
11
- require 'dino/components/stepper'
12
- require 'dino/components/ir_receiver'
13
-
14
- module Dino
15
- end
5
+ require 'dino/components'
@@ -0,0 +1,3 @@
1
+ module Dino
2
+ class BoardNotFound < Exception; end
3
+ end
@@ -0,0 +1,12 @@
1
+ module Dino
2
+ module Components
3
+ require 'dino/components/base_component'
4
+ autoload :Led, 'dino/components/led'
5
+ autoload :Button, 'dino/components/button'
6
+ autoload :Sensor, 'dino/components/sensor'
7
+ autoload :RgbLed, 'dino/components/rgb_led'
8
+ autoload :Servo, 'dino/components/servo'
9
+ autoload :Stepper, 'dino/components/stepper'
10
+ autoload :IrReceiver, 'dino/components/ir_receiver'
11
+ end
12
+ end
@@ -9,8 +9,18 @@ module Dino
9
9
  self.board = options[:board]
10
10
 
11
11
  raise 'board and pin or pins are required for a component' if self.board.nil? || self.pin.nil?
12
+ after_initialize(options)
12
13
  end
13
14
 
15
+ #
16
+ # As BaseComponent does a lot of work for you with regarding to setting up, it is
17
+ # best not to override #initialize and instead define an #after_initialize method
18
+ # within your subclass.
19
+ #
20
+ # @note This method should be implemented in the BaseComponent subclass.
21
+ #
22
+ def after_initialize(options={}) ; end
23
+
14
24
  protected
15
25
 
16
26
  attr_writer :pin, :board
@@ -4,9 +4,7 @@ module Dino
4
4
  UP = "01"
5
5
  DOWN = "00"
6
6
 
7
- def initialize(options={})
8
- super(options)
9
-
7
+ def after_initialize(options={})
10
8
  @down_callbacks, @up_callbacks, @state = [], [], UP
11
9
 
12
10
  self.board.add_digital_hardware(self)
@@ -3,9 +3,7 @@ module Dino
3
3
  class IrReceiver < BaseComponent
4
4
  STABLE = "01"
5
5
 
6
- def initialize(options={})
7
- super(options)
8
-
6
+ def after_initialize(options={})
9
7
  @flash_callbacks = []
10
8
 
11
9
  self.board.add_digital_hardware(self)
@@ -1,11 +1,9 @@
1
1
  module Dino
2
2
  module Components
3
3
  class Led < BaseComponent
4
- def initialize(options={})
5
- super(options)
6
-
4
+ def after_initialize(options={})
7
5
  set_pin_mode(:out)
8
- digital_write(Board::LOW)
6
+ off
9
7
  end
10
8
 
11
9
  def on
@@ -2,9 +2,7 @@ module Dino
2
2
  module Components
3
3
  class RgbLed < BaseComponent
4
4
  # options = {board: my_board, pins: {red: red_pin, green: green_pin, blue: blue_pin}
5
- def initialize(options={})
6
- super(options)
7
-
5
+ def after_initialize(options={})
8
6
  raise 'missing pins[:red] pin' unless self.pins[:red]
9
7
  raise 'missing pins[:green] pin' unless self.pins[:green]
10
8
  raise 'missing pins[:blue] pin' unless self.pins[:blue]
@@ -1,9 +1,7 @@
1
1
  module Dino
2
2
  module Components
3
3
  class Sensor < BaseComponent
4
- def initialize(options={})
5
- super(options)
6
-
4
+ def after_initialize(options={})
7
5
  @data_callbacks = []
8
6
  @board.add_analog_hardware(self)
9
7
  @board.start_read
@@ -3,9 +3,7 @@ module Dino
3
3
  class Servo < BaseComponent
4
4
  attr_reader :position
5
5
 
6
- def initialize(options)
7
- super(options)
8
-
6
+ def after_initialize(options={})
9
7
  set_pin_mode(:out)
10
8
  self.position = 0
11
9
  end
@@ -2,9 +2,7 @@ module Dino
2
2
  module Components
3
3
  class Stepper < BaseComponent
4
4
 
5
- def initialize(options={})
6
- super(options)
7
-
5
+ def after_initialize(options={})
8
6
  raise 'missing pins[:step] pin' unless self.pins[:step]
9
7
  raise 'missing pins[:direction] pin' unless self.pins[:direction]
10
8
 
@@ -12,14 +12,8 @@ module Dino
12
12
  end
13
13
 
14
14
  def io
15
- @io ||= tty_devices.map do |device|
16
- next if device.match /^cu/
17
- begin
18
- SerialPort.new("/dev/#{device}", BAUD)
19
- rescue
20
- nil
21
- end
22
- end.compact.first
15
+ raise BoardNotFound unless @io ||= find_arduino
16
+ @io
23
17
  end
24
18
 
25
19
  def io=(device)
@@ -52,7 +46,22 @@ module Dino
52
46
  private
53
47
 
54
48
  def tty_devices
55
- `ls /dev | grep usb`.split(/\n/)
49
+ if RUBY_PLATFORM.include?("mswin") || RUBY_PLATFORM.include?("mingw")
50
+ ["COM1", "COM2", "COM3", "COM4"]
51
+ else
52
+ `ls /dev`.split("\n").grep(/usb|ACM/).map{|d| "/dev/#{d}"}
53
+ end
54
+ end
55
+
56
+ def find_arduino
57
+ tty_devices.map do |device|
58
+ next if device.match /^cu/
59
+ begin
60
+ SerialPort.new(device, BAUD)
61
+ rescue
62
+ nil
63
+ end
64
+ end.compact.first
56
65
  end
57
66
  end
58
- end
67
+ end
@@ -1,3 +1,3 @@
1
1
  module Dino
2
- VERSION = "0.8"
2
+ VERSION = "0.9"
3
3
  end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ describe BoardNotFound do
5
+ it { should be }
6
+ it { should be_a Exception }
7
+ end
8
+ end
@@ -2,7 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  module Dino
4
4
  module Components
5
+
5
6
  describe BaseComponent do
7
+
6
8
  it 'should initialize with board and pin' do
7
9
  pin = "a pin"
8
10
  board = "a board"
@@ -31,6 +33,33 @@ module Dino
31
33
  BaseComponent.new(pin: 'some pin')
32
34
  }.to raise_exception
33
35
  end
36
+
37
+ context "when subclassed #after_initialize should be executed" do
38
+
39
+ class SpecComponent < BaseComponent
40
+
41
+ def sucessfully_initialized? ; @success ; end
42
+
43
+ def options ; @options ; end
44
+
45
+ def after_initialize(options={})
46
+ @success = true
47
+ @options = options
48
+ end
49
+ end
50
+
51
+ let(:options) { { pin: pin, board: board } }
52
+ let(:pin) { "a pin" }
53
+ let(:board) { "a board" }
54
+
55
+ it "should call #after_initialize with options" do
56
+ component = SpecComponent.new(options)
57
+ component.should be_sucessfully_initialized
58
+ component.options.should eq options
59
+ end
60
+
61
+ end
62
+
34
63
  end
35
64
  end
36
65
  end
@@ -11,12 +11,30 @@ module Dino
11
11
  end
12
12
 
13
13
  describe '#io' do
14
- it 'should instantiate a new SerialPort for each usb tty device found' do
15
- subject.should_receive(:tty_devices).and_return(['cu.usb', 'tty1.usb', 'tty2.usb'])
16
- SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::BAUD).and_return(mock_serial = mock)
17
- SerialPort.should_receive(:new).with('/dev/tty2.usb', TxRx::BAUD).and_return(mock)
14
+ context "on windows" do
15
+ it 'should instantiate a new SerialPort for each usb tty device found' do
16
+ original_platform = RUBY_PLATFORM
17
+ RUBY_PLATFORM = "mswin"
18
+ subject.should_receive(:tty_devices).and_return(["COM1", "COM2", "COM3", "COM4"])
19
+ SerialPort.should_receive(:new).with('COM1', TxRx::BAUD).and_return(mock_serial = mock)
20
+ SerialPort.should_receive(:new).with('COM2', TxRx::BAUD).and_return(mock)
21
+ SerialPort.should_receive(:new).with('COM3', TxRx::BAUD).and_return(mock)
22
+ SerialPort.should_receive(:new).with('COM4', TxRx::BAUD).and_return(mock)
18
23
 
19
- subject.io.should == mock_serial
24
+ subject.io.should == mock_serial
25
+ RUBY_PLATFORM = original_platform
26
+ end
27
+ end
28
+
29
+ context "on unix" do
30
+ it 'should instantiate a new SerialPort for each usb tty device found' do
31
+ subject.should_receive(:tty_devices).and_return(['/dev/tty1.usb', '/dev/tty1.usb', '/dev/tty.ACM0'])
32
+ SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::BAUD).and_return(mock_serial = mock)
33
+ SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::BAUD).and_return(mock)
34
+ SerialPort.should_receive(:new).with('/dev/tty.ACM0', TxRx::BAUD).and_return(mock)
35
+
36
+ subject.io.should == mock_serial
37
+ end
20
38
  end
21
39
 
22
40
  it 'should use the existing io instance if set' do
@@ -26,6 +44,11 @@ module Dino
26
44
  subject.io = '/dev/tty1.usb'
27
45
  subject.io.should == mock_serial
28
46
  end
47
+
48
+ it 'should raise a BoardNotFound exception if there is no board connected' do
49
+ SerialPort.stub(:new).and_raise
50
+ expect { subject.io }.to raise_exception BoardNotFound
51
+ end
29
52
  end
30
53
 
31
54
  describe '#io=' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dino
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.8'
4
+ version: '0.9'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-01 00:00:00.000000000 Z
12
+ date: 2012-11-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: serialport
@@ -37,6 +37,7 @@ extra_rdoc_files: []
37
37
  files:
38
38
  - .gitignore
39
39
  - .rvmrc
40
+ - .travis.yml
40
41
  - Gemfile
41
42
  - LICENSE
42
43
  - README.md
@@ -45,14 +46,18 @@ files:
45
46
  - examples/button/button.png
46
47
  - examples/button/button.rb
47
48
  - examples/ir_receiver.rb
49
+ - examples/led/led.png
48
50
  - examples/led/led.rb
49
51
  - examples/potentiometer.rb
50
52
  - examples/rgb_led.rb
51
53
  - examples/sensor.rb
52
54
  - examples/servo.rb
53
- - examples/stepper.rb
55
+ - examples/stepper/stepper.png
56
+ - examples/stepper/stepper.rb
54
57
  - lib/dino.rb
55
58
  - lib/dino/board.rb
59
+ - lib/dino/board_not_found.rb
60
+ - lib/dino/components.rb
56
61
  - lib/dino/components/base_component.rb
57
62
  - lib/dino/components/button.rb
58
63
  - lib/dino/components/ir_receiver.rb
@@ -63,6 +68,7 @@ files:
63
68
  - lib/dino/components/stepper.rb
64
69
  - lib/dino/tx_rx.rb
65
70
  - lib/dino/version.rb
71
+ - spec/lib/board_not_found_spec.rb
66
72
  - spec/lib/board_spec.rb
67
73
  - spec/lib/components/base_component_spec.rb
68
74
  - spec/lib/components/button_spec.rb
@@ -99,6 +105,7 @@ signing_key:
99
105
  specification_version: 3
100
106
  summary: Control your arduino through a serial port
101
107
  test_files:
108
+ - spec/lib/board_not_found_spec.rb
102
109
  - spec/lib/board_spec.rb
103
110
  - spec/lib/components/base_component_spec.rb
104
111
  - spec/lib/components/button_spec.rb