dino 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ module Components
5
+ describe BaseComponent do
6
+ it 'should initialize with board and pin' do
7
+ pin = "a pin"
8
+ board = "a board"
9
+ component = BaseComponent.new(pin: pin, board: board)
10
+
11
+ component.pin.should == pin
12
+ component.board.should == board
13
+ end
14
+
15
+ it 'should assign pins' do
16
+ pins = {red: 'red', green: 'green', blue: 'blue'}
17
+ board = "a board"
18
+ component = BaseComponent.new(pins: pins, board: board)
19
+
20
+ component.pins.should == pins
21
+ end
22
+
23
+ it 'should require a pin or pins' do
24
+ expect {
25
+ BaseComponent.new(board: 'some board')
26
+ }.to raise_exception
27
+ end
28
+
29
+ it 'should require a board' do
30
+ expect {
31
+ BaseComponent.new(pin: 'some pin')
32
+ }.to raise_exception
33
+ end
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ module Components
5
+ describe Button do
6
+ describe '#initialize' do
7
+ it 'should raise if it does not receive a pin' do
8
+ expect {
9
+ Button.new(board: 'a board')
10
+ }.to raise_exception
11
+ end
12
+
13
+ it 'should raise if it does not receive a board' do
14
+ expect {
15
+ Button.new(pin: 'a pin')
16
+ }.to raise_exception
17
+ end
18
+
19
+ it 'should add itself to the board and start reading' do
20
+ board = mock(:board)
21
+ board.should_receive(:add_digital_hardware)
22
+ board.should_receive(:start_read)
23
+ Button.new(board: board, pin: 'a pin')
24
+ end
25
+ end
26
+
27
+ context 'callbacks' do
28
+ let(:board) { mock(:board, add_digital_hardware: true, start_read: true) }
29
+ let(:button) {Button.new(board: board, pin: mock)}
30
+ describe '#down' do
31
+ it 'should add a callback to the down_callbacks array' do
32
+ button.down(callback_mock = mock)
33
+ button.instance_variable_get(:@down_callbacks).should include callback_mock
34
+ end
35
+ end
36
+
37
+ describe '#up' do
38
+ it 'should add a callback to the up_callbacks array' do
39
+ button.up(callback_mock = mock)
40
+ button.instance_variable_get(:@up_callbacks).should include callback_mock
41
+ end
42
+ end
43
+
44
+ describe '#update' do
45
+ it 'should call the down callbacks' do
46
+ button.down(callback_mock1 = mock)
47
+ button.down(callback_mock2 = mock)
48
+ callback_mock1.should_receive(:call)
49
+ callback_mock2.should_receive(:call)
50
+ button.update(Button::DOWN)
51
+ end
52
+
53
+ it 'should call the up callbacks' do
54
+ button.up(callback_mock1 = mock)
55
+ button.up(callback_mock2 = mock)
56
+
57
+ button.instance_variable_set(:@state, Button::DOWN)
58
+
59
+ callback_mock1.should_receive(:call)
60
+ callback_mock2.should_receive(:call)
61
+ button.update(Button::UP)
62
+ end
63
+
64
+ it 'should not call the callbacks if the state has not changed' do
65
+ button.up(callback_mock = mock)
66
+
67
+ callback_mock.should_not_receive(:call)
68
+ button.update(Button::UP)
69
+ button.update(Button::UP)
70
+ end
71
+
72
+ it 'should not call the callbacks if the data is not UP or DOWN' do
73
+ button.up(callback_mock1 = mock)
74
+ button.down(callback_mock2 = mock)
75
+
76
+ callback_mock1.should_not_receive(:call)
77
+ callback_mock2.should_not_receive(:call)
78
+ button.update('foobarred')
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ module Components
5
+ describe Led do
6
+ let(:board) { mock(:board, digital_write: true, set_pin_mode: true) }
7
+
8
+ describe '#initialize' do
9
+ it 'should raise if it does not receive a pin' do
10
+ expect {
11
+ Led.new(board: board)
12
+ }.to raise_exception
13
+ end
14
+
15
+ it 'should raise if it does not receive a board' do
16
+ expect {
17
+ Led.new(pins: {})
18
+ }.to raise_exception
19
+ end
20
+
21
+ it 'should set the pin to out' do
22
+ board.should_receive(:set_pin_mode).with(13, :out)
23
+ Led.new(pin: 13, board: board)
24
+ end
25
+
26
+ it 'should set the pin to low' do
27
+ board.should_receive(:digital_write).with(13, Board::LOW)
28
+ Led.new(pin: 13, board: board)
29
+ end
30
+ end
31
+
32
+ describe '#on' do
33
+ it 'should send a high to the board with the pin' do
34
+ @led = Led.new(pin: 13, board: board)
35
+ board.should_receive(:digital_write).with(13, Board::HIGH)
36
+ @led.on
37
+ end
38
+ end
39
+
40
+ describe '#off' do
41
+ it 'should send a high to the board with the pin' do
42
+ @led = Led.new(pin: 13, board: board)
43
+ board.should_receive(:digital_write).with(13, Board::LOW)
44
+ @led.off
45
+ end
46
+ end
47
+
48
+ describe '#blink' do
49
+ it 'should turn the led off if it is on'
50
+ it 'should not block'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ module Components
5
+ describe RgbLed do
6
+ let(:board) { mock(:board, analog_write: true, set_pin_mode: true) }
7
+ let(:pins) { {red: 1, green: 2, blue: 3} }
8
+ let!(:rgb) {RgbLed.new(pins: pins, board: board)}
9
+
10
+ describe '#initialize' do
11
+ it 'should raise if it does not receive a pin' do
12
+ expect {
13
+ RgbLed.new(board: 'a board')
14
+ }.to raise_exception
15
+ end
16
+
17
+ it 'should raise if it does not receive a board' do
18
+ expect {
19
+ RgbLed.new(pins: pins)
20
+ }.to raise_exception
21
+ end
22
+
23
+ it 'should set the pin to out' do
24
+ board.should_receive(:set_pin_mode).with(1, :out)
25
+ board.should_receive(:set_pin_mode).with(2, :out)
26
+ board.should_receive(:set_pin_mode).with(3, :out)
27
+
28
+ RgbLed.new(pins: pins, board: board)
29
+ end
30
+
31
+ it 'should set the pin to low' do
32
+ board.should_receive(:analog_write).with(1, Board::LOW)
33
+ board.should_receive(:analog_write).with(2, Board::LOW)
34
+ board.should_receive(:analog_write).with(3, Board::LOW)
35
+
36
+ RgbLed.new(pins: pins, board: board)
37
+ end
38
+ end
39
+
40
+ describe '#red' do
41
+ it 'should set red to high, blue and green to low' do
42
+ board.should_receive(:analog_write).with(1, Board::HIGH)
43
+ board.should_receive(:analog_write).with(2, Board::LOW)
44
+ board.should_receive(:analog_write).with(3, Board::LOW)
45
+ rgb.red
46
+ end
47
+ end
48
+
49
+ describe '#green' do
50
+ it 'should set green to high, red and blue to low' do
51
+ board.should_receive(:analog_write).with(1, Board::LOW)
52
+ board.should_receive(:analog_write).with(2, Board::HIGH)
53
+ board.should_receive(:analog_write).with(3, Board::LOW)
54
+ rgb.green
55
+ end
56
+ end
57
+
58
+ describe '#blue' do
59
+ it 'should set blue to high, red and green to low' do
60
+ board.should_receive(:analog_write).with(1, Board::LOW)
61
+ board.should_receive(:analog_write).with(2, Board::LOW)
62
+ board.should_receive(:analog_write).with(3, Board::HIGH)
63
+ rgb.blue
64
+ end
65
+ end
66
+
67
+ describe '#blinky' do
68
+ it 'should set blue to high, red and green to low' do
69
+ Array.any_instance.should_receive(:cycle).and_yield(:red).and_yield(:green).and_yield(:blue)
70
+ rgb.should_receive(:red)
71
+ rgb.should_receive(:green)
72
+ rgb.should_receive(:blue)
73
+ rgb.blinky
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ module Components
5
+ describe Sensor do
6
+
7
+ let(:board){mock(:board).as_null_object}
8
+
9
+ describe '#initalize' do
10
+ it 'should raise if it does not receive a pin' do
11
+ expect {
12
+ Sensor.new(board: 'a board')
13
+ }.to raise_exception
14
+ end
15
+
16
+ it 'should raise if it does not receive a board' do
17
+ expect {
18
+ Sensor.new(pin: 'a pin')
19
+ }.to raise_exception
20
+ end
21
+
22
+ it 'should add itself to the board and start reading' do
23
+ board.should_receive(:add_analog_hardware)
24
+ board.should_receive(:start_read)
25
+ Sensor.new(board: board, pin: 'a pin')
26
+ end
27
+
28
+ it 'should initalize data_callbacks' do
29
+ sensor = Sensor.new(board: board, pin: 'a pin')
30
+ sensor.instance_variable_get(:@data_callbacks).should == []
31
+ end
32
+ end
33
+
34
+ describe '#when_data_received' do
35
+ it 'should add a callback back to the list of callbacks' do
36
+ sensor = Sensor.new(board: board, pin: 'a pin')
37
+ sensor.when_data_received 'Foo'
38
+ sensor.instance_variable_get(:@data_callbacks).should == ['Foo']
39
+ end
40
+ end
41
+
42
+ describe '#update' do
43
+ it 'should call all callbacks passing in the given data' do
44
+ first_callback, second_callback = mock, mock
45
+ first_callback.should_receive(:call).with('Some data')
46
+ second_callback.should_receive(:call).with('Some data')
47
+
48
+ sensor = Sensor.new(board: board, pin: 'a pin')
49
+
50
+ sensor.when_data_received first_callback
51
+ sensor.when_data_received second_callback
52
+
53
+ sensor.update('Some data')
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ module Components
5
+ describe Servo do
6
+ let(:board) { mock(:board, analog_write: true, set_pin_mode: true) }
7
+
8
+ describe '#initialize' do
9
+ it 'should raise if it does not receive a pin' do
10
+ expect {
11
+ Servo.new(board: board)
12
+ }.to raise_exception
13
+ end
14
+
15
+ it 'should raise if it does not receive a board' do
16
+ expect {
17
+ Servo.new(pin: 13)
18
+ }.to raise_exception
19
+ end
20
+
21
+ it 'should set the pins to out' do
22
+ board.should_receive(:set_pin_mode).with(13, :out)
23
+ Servo.new(pin: 13, board: board)
24
+ end
25
+
26
+ it 'should set the inital position to 0' do
27
+ servo = Servo.new(pin: 13, board: board)
28
+ servo.instance_variable_get(:@position).should == 0
29
+ end
30
+ end
31
+
32
+ describe '#position' do
33
+ let(:servo) {servo = Servo.new(pin: 13, board: board)}
34
+
35
+ it 'should set the position of the Servo' do
36
+ servo.position = 90
37
+ servo.instance_variable_get(:@position).should == 90
38
+ end
39
+
40
+ it 'should modulate the position at 180' do
41
+ servo.position = 190
42
+ servo.instance_variable_get(:@position).should == 10
43
+ end
44
+
45
+ it 'should write the new position to the board' do
46
+ servo.should_receive(:analog_write).with(10)
47
+ servo.position = 190
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ module Components
5
+ describe Stepper do
6
+ let(:board) { mock(:board, digital_write: true, set_pin_mode: true) }
7
+
8
+ describe '#initialize' do
9
+ it 'should raise if it does not receive a step pin' do
10
+ expect {
11
+ Stepper.new(board: board)
12
+ }.to raise_exception
13
+ end
14
+
15
+ it 'should raise if it does not receive a direction pin' do
16
+ expect {
17
+ Stepper.new(board: board)
18
+ }.to raise_exception
19
+ end
20
+
21
+ it 'should raise if it does not receive a board' do
22
+ expect {
23
+ Stepper.new(pins: {step: 12, direction: 13})
24
+ }.to raise_exception
25
+ end
26
+
27
+ it 'should set the pins to out' do
28
+ board.should_receive(:set_pin_mode).with(13, :out)
29
+ board.should_receive(:set_pin_mode).with(12, :out)
30
+ Stepper.new(pins: {step: 13, direction: 12}, board: board)
31
+ end
32
+
33
+ it 'should set the step pin to low' do
34
+ board.should_receive(:digital_write).with(13, Board::LOW)
35
+ Stepper.new(pins: {step: 13, direction: 12}, board: board)
36
+ end
37
+ end
38
+
39
+ describe '#step_cc' do
40
+ it 'should send a high to the board with the pin' do
41
+ @stepper = Stepper.new(pins: {step: 13, direction: 12}, board: board)
42
+ board.should_receive(:digital_write).with(12, Board::HIGH)
43
+ board.should_receive(:digital_write).with(13, Board::HIGH)
44
+ board.should_receive(:digital_write).with(13, Board::LOW)
45
+ @stepper.step_cc
46
+ end
47
+ end
48
+
49
+ describe '#step_cw' do
50
+ it 'should send a high to the board with the pin' do
51
+ @stepper = Stepper.new(pins: {step: 13, direction: 12}, board: board)
52
+ board.should_receive(:digital_write).with(12, Board::LOW)
53
+ board.should_receive(:digital_write).with(13, Board::HIGH)
54
+ board.should_receive(:digital_write).with(13, Board::LOW)
55
+ @stepper.step_cw
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ module Dino
4
+ describe TxRx do
5
+ it { should be }
6
+
7
+ describe '#initialize' do
8
+ it 'should set first_write to false' do
9
+ TxRx.new.instance_variable_get(:@first_write).should == true
10
+ end
11
+ end
12
+
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)
18
+
19
+ subject.io.should == mock_serial
20
+ end
21
+
22
+ it 'should use the existing io instance if set' do
23
+ subject.should_not_receive(:tty_devices)
24
+ SerialPort.stub(:new).and_return(mock_serial = mock)
25
+
26
+ subject.io = '/dev/tty1.usb'
27
+ subject.io.should == mock_serial
28
+ end
29
+ end
30
+
31
+ describe '#io=' do
32
+ it 'should set io to a new serial port with the specified device' do
33
+ SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::BAUD).and_return(mock_serial = mock)
34
+ subject.io = '/dev/tty1.usb'
35
+ subject.instance_variable_get(:@io).should == mock_serial
36
+ end
37
+ end
38
+
39
+ describe '#read' do
40
+ it 'should create a new thread' do
41
+ Thread.should_receive :new
42
+ subject.read
43
+ end
44
+
45
+ it 'should get messages from the device' do
46
+ subject.stub(:io).and_return(mock_serial = mock)
47
+
48
+ IO.should_receive(:select).and_return(true)
49
+ Thread.should_receive(:new).and_yield
50
+ subject.should_receive(:loop).and_yield
51
+ mock_serial.should_receive(:gets).and_return("foo::bar\n")
52
+ subject.should_receive(:changed).and_return(true)
53
+ subject.should_receive(:notify_observers).with('foo','bar')
54
+
55
+ subject.read
56
+ end
57
+ end
58
+
59
+ describe '#close_read' do
60
+ it 'should kill the reading thread' do
61
+ subject.instance_variable_set(:@thread, mock_thread = mock)
62
+ Thread.should_receive(:kill).with(mock_thread)
63
+ subject.read
64
+ subject.close_read
65
+ end
66
+ end
67
+
68
+ describe '#write' do
69
+ it 'should write to the device' do
70
+ IO.should_receive(:select).and_return(true)
71
+
72
+ subject.stub(:io).and_return(mock_serial = mock)
73
+ mock_serial.should_receive(:puts).with('a message')
74
+ subject.write('a message')
75
+ end
76
+ end
77
+ end
78
+ end