artoo-arduino 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +15 -17
  3. data/README.md +8 -63
  4. data/artoo-arduino.gemspec +4 -3
  5. data/examples/analog_sensor.rb +31 -0
  6. data/examples/{firmata_blink_led.rb → blink_led.rb} +1 -1
  7. data/examples/{firmata_blink_led_with_toggle.rb → blink_led_with_toggle.rb} +1 -1
  8. data/examples/{firmata_button_and_led.rb → button_and_led.rb} +0 -0
  9. data/examples/{firmata_dc_motor_speed.rb → dc_motor_speed.rb} +1 -1
  10. data/examples/{firmata_dc_motor_speed_h-bridge_2_pins.rb → dc_motor_speed_h-bridge_2_pins.rb} +1 -1
  11. data/examples/{firmata_dc_motor_switch.rb → dc_motor_switch.rb} +1 -1
  12. data/examples/{firmata_dc_motor_switch_h-bridge_2_pins.rb → dc_motor_switch_h-bridge_2_pins.rb} +1 -1
  13. data/examples/{firmata_led_brightness.rb → led_brightness.rb} +1 -1
  14. data/examples/led_brightness_with_analog_input.rb +29 -0
  15. data/examples/{firmata_led_with_button_toggle.rb → led_with_button_toggle.rb} +1 -1
  16. data/examples/{firmata_maxbotix.rb → maxbotix.rb} +2 -2
  17. data/examples/{firmata_motor.rb → motor.rb} +1 -1
  18. data/examples/motor_speed_with_analog_input.rb +30 -0
  19. data/examples/{firmata_servo.rb → servo.rb} +1 -1
  20. data/examples/{firmata_wiichuck.rb → wiichuck.rb} +0 -0
  21. data/lib/artoo-arduino/version.rb +1 -1
  22. data/lib/artoo/adaptors/firmata.rb +51 -9
  23. data/test/adaptors/firmata_test.rb +30 -0
  24. metadata +43 -48
  25. data/examples/littlewire_blink_led_with_toggle.rb +0 -17
  26. data/examples/littlewire_button_and_led.rb +0 -16
  27. data/examples/littlewire_dc_motor_speed.rb +0 -31
  28. data/examples/littlewire_maxbotix.rb +0 -19
  29. data/lib/artoo/adaptors/littlewire.rb +0 -63
  30. data/lib/artoo/drivers/board.rb +0 -10
  31. data/lib/artoo/drivers/button.rb +0 -43
  32. data/lib/artoo/drivers/led.rb +0 -59
  33. data/lib/artoo/drivers/maxbotix.rb +0 -44
  34. data/lib/artoo/drivers/motor.rb +0 -167
  35. data/lib/artoo/drivers/servo.rb +0 -49
  36. data/lib/artoo/drivers/wiichuck.rb +0 -61
  37. data/lib/artoo/drivers/wiiclassic.rb +0 -139
  38. data/lib/artoo/drivers/wiidriver.rb +0 -103
  39. data/test/adaptors/littlewire_test.rb +0 -25
  40. data/test/drivers/firmata_board_test.rb +0 -21
  41. data/test/drivers/led_test.rb +0 -92
  42. data/test/drivers/maxbotix_test.rb +0 -25
  43. data/test/drivers/motor_test.rb +0 -204
  44. data/test/drivers/servo_test.rb +0 -43
  45. data/test/drivers/wiichuck_test.rb +0 -11
  46. data/test/drivers/wiiclassic_test.rb +0 -11
  47. data/test/drivers/wiidriver_test.rb +0 -54
@@ -1,103 +0,0 @@
1
- require 'artoo/drivers/driver'
2
-
3
- module Artoo
4
- module Drivers
5
- # Wii-based controller shared driver behaviors for Firmata
6
- class Wiidriver < Driver
7
- attr_reader :joystick, :data
8
-
9
- def address; 0x52; end
10
-
11
- # Create new Wiidriver
12
- def initialize(params={})
13
- @joystick = get_defaults
14
- @data = {}
15
- super
16
- end
17
-
18
- # Starts drives and required connections
19
- def start_driver
20
- begin
21
- connection.i2c_config(0)
22
- every(interval) do
23
- connection.i2c_write_request(address, 0x40, 0x00)
24
- connection.i2c_write_request(address, 0x00, 0x00)
25
- connection.i2c_read_request(address, 6)
26
-
27
- connection.read_and_process
28
- handle_events
29
- end
30
-
31
- super
32
- rescue Exception => e
33
- Logger.error "Error starting wii driver!"
34
- Logger.error e.message
35
- Logger.error e.backtrace.inspect
36
- end
37
- end
38
-
39
- # Get and update data
40
- def update(value)
41
- if encrypted?(value)
42
- Logger.error "Encrypted bytes from wii device!"
43
- raise "Encrypted bytes from wii device!"
44
- end
45
-
46
- @data = parse(value)
47
- end
48
-
49
- def handle_events
50
- while i = find_event(:i2c_reply) do
51
- event = events.slice!(i)
52
- update(event.data.first) if !event.nil?
53
- end
54
- end
55
-
56
- protected
57
-
58
- def find_event(name)
59
- events.index {|e| e.name == name}
60
- end
61
-
62
- def events
63
- connection.async_events
64
- end
65
-
66
- def get_defaults
67
- {}
68
- end
69
-
70
- def parse
71
- {}
72
- end
73
-
74
- def set_joystick_default_value(joystick_axis, default_value)
75
- joystick[joystick_axis] = default_value if joystick[joystick_axis].nil?
76
- end
77
-
78
- def calculate_joystick_value(axis, origin)
79
- data[axis] - joystick[origin]
80
- end
81
-
82
- def encrypted?(value)
83
- [[0, 1], [2, 3], [4, 5]].all? {|a| get_value(value, a[0]) == get_value(value, a[1]) }
84
- end
85
-
86
- def decode(x)
87
- return ( x ^ 0x17 ) + 0x17
88
- end
89
-
90
- def decode_value(value, index)
91
- decode(get_value(value, index))
92
- end
93
-
94
- def get_value(value, index)
95
- value[:data][index]
96
- end
97
-
98
- def generate_bool(value)
99
- value == 0 ? true : false
100
- end
101
- end
102
- end
103
- end
@@ -1,25 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
- require 'artoo/adaptors/littlewire'
3
- require 'littlewire'
4
-
5
- describe Artoo::Adaptors::Littlewire do
6
- before do
7
- @port = Artoo::Port.new('/dev/awesome')
8
- @adaptor = Artoo::Adaptors::Littlewire.new(:port => @port)
9
- @adaptor.expects(:connect_to_usb)
10
- @littlewire = mock('littlewire')
11
- LittleWire.expects(:new).returns(@littlewire)
12
- end
13
-
14
- it 'Artoo::Adaptors::Littlewire#connect' do
15
- @adaptor.connect.must_equal true
16
- end
17
-
18
- it 'Artoo::Adaptors::Littlewire#disconnect' do
19
- @littlewire.expects(:finished)
20
- @adaptor.connect
21
- @adaptor.disconnect
22
-
23
- @adaptor.connected?.must_equal false
24
- end
25
- end
@@ -1,21 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
- require 'artoo/drivers/board'
3
-
4
- describe Artoo::Drivers::Board do
5
- before do
6
- @device = mock('device')
7
- @board = Artoo::Drivers::Board.new(:parent => @device)
8
- @connection = mock('connection')
9
- @device.stubs(:connection).returns(@connection)
10
- end
11
-
12
- it 'Board#firmware_name' do
13
- @connection.expects(:firmware_name).returns("awesomenessware")
14
- @board.firmware_name.must_equal "awesomenessware"
15
- end
16
-
17
- it 'Board#version' do
18
- @connection.expects(:version).returns("1.2.3")
19
- @board.version.must_equal "1.2.3"
20
- end
21
- end
@@ -1,92 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
- require 'artoo/drivers/led'
3
-
4
- describe Artoo::Drivers::Led do
5
- before do
6
- @device = mock('device')
7
- @pin = 13
8
- @device.stubs(:pin).returns(@pin)
9
- @led = Artoo::Drivers::Led.new(:parent => @device)
10
- @connection = mock('connection')
11
- @device.stubs(:connection).returns(@connection)
12
- end
13
-
14
- describe 'state switching' do
15
- describe '#on' do
16
- it 'must turn the led on' do
17
- @connection.expects(:digital_write).with(@pin, :high)
18
- @led.on
19
- end
20
- end
21
-
22
- describe '#off' do
23
- it 'must turn the led off' do
24
- @connection.expects(:digital_write).with(@pin, :low)
25
- @led.off
26
- end
27
- end
28
- end
29
-
30
- describe 'state checking' do
31
-
32
- before do
33
- @led.stubs(:change_state)
34
- end
35
-
36
- describe '#on?' do
37
- it 'must return true if led is on' do
38
- @led.on
39
- @led.on?.must_equal true
40
- end
41
-
42
- it 'must return false if led is off' do
43
- @led.off
44
- @led.on?.must_equal false
45
- end
46
- end
47
-
48
- describe '#off?' do
49
- it 'must return true if led is off' do
50
- @led.off
51
- @led.off?.must_equal true
52
- end
53
-
54
- it 'must return false if led is on' do
55
- @led.on
56
- @led.off?.must_equal false
57
- end
58
- end
59
- end
60
-
61
- describe '#toggle' do
62
- it 'must toggle the state of the led' do
63
- @led.stubs(:pin_state_on_board).returns(false)
64
- @connection.stubs(:set_pin_mode)
65
- @connection.stubs(:digital_write)
66
- @led.off?.must_equal true
67
- @led.toggle
68
- @led.on?.must_equal true
69
- @led.toggle
70
- @led.off?.must_equal true
71
- end
72
- end
73
-
74
- describe '#brightness' do
75
- it 'must change the brightness of the led' do
76
- val = 100
77
- @connection.expects(:pwm_write).with(@pin, val)
78
- @led.brightness(val)
79
- end
80
- end
81
-
82
- describe '#commands' do
83
- it 'must contain all the necessary commands' do
84
- @led.commands.must_include :on
85
- @led.commands.must_include :off
86
- @led.commands.must_include :toggle
87
- @led.commands.must_include :brightness
88
- @led.commands.must_include :on?
89
- @led.commands.must_include :off?
90
- end
91
- end
92
- end
@@ -1,25 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
- require 'artoo/drivers/maxbotix'
3
-
4
- describe Artoo::Drivers::Maxbotix do
5
- before do
6
- @device = mock('device')
7
- @pin = 0
8
- @device.stubs(:pin).returns(@pin)
9
- @maxbotix = Artoo::Drivers::Maxbotix.new(:parent => @device)
10
- @connection = mock('connection')
11
- @device.stubs(:connection).returns(@connection)
12
- end
13
-
14
- describe '#range' do
15
- it 'must have initial value' do
16
- @maxbotix.range.must_equal 0.0
17
- end
18
-
19
- it 'must adjust based on last_reading' do
20
- @maxbotix.last_reading = 13.5
21
- @maxbotix.range.must_equal 6.697265625
22
- @maxbotix.range_cm.must_equal 17.145
23
- end
24
- end
25
- end
@@ -1,204 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
- require 'artoo/drivers/motor'
3
-
4
- describe Artoo::Drivers::Motor do
5
-
6
- let(:connection) { mock('connection') }
7
-
8
- before do
9
- @device = mock('device')
10
- connection.stubs(:pwm_write)
11
- connection.stubs(:digital_write)
12
- @device.stubs(:connection).returns(connection)
13
- end
14
-
15
- describe 'mode predicate methods' do
16
-
17
- let(:motor) { Artoo::Drivers::Motor.new(:parent => @device, :additional_params => {}) }
18
-
19
- it '#digital?' do
20
- motor.digital?.must_equal true #initial state
21
- motor.speed(1)
22
- motor.digital?.must_equal false
23
- end
24
-
25
- it '#analog?' do
26
- motor.analog?.must_equal false
27
- motor.speed(1)
28
- motor.analog?.must_equal true
29
- end
30
-
31
- end
32
-
33
- describe 'when unidirectional' do
34
-
35
- describe 'when switching states (digital)' do
36
-
37
- let(:motor) { Artoo::Drivers::Motor.new(:parent => @device, :additional_params => {switch_pin: 1}) }
38
-
39
- it '#start' do
40
- motor.start
41
- motor.on?.must_equal true
42
- end
43
-
44
- it '#stop' do
45
- motor.stop
46
- motor.off?.must_equal true
47
- end
48
-
49
- it '#max' do
50
- motor.expects(:speed).with(255)
51
- motor.max
52
- end
53
-
54
- it '#min' do
55
- motor.min
56
- end
57
-
58
- describe '#toggle' do
59
- it 'toggles to on when off' do
60
- motor.start
61
- motor.toggle
62
- motor.off?.must_equal true
63
- end
64
-
65
- it 'toggles to off when on' do
66
- motor.stop
67
- motor.toggle
68
- motor.on?.must_equal true
69
- end
70
- end
71
- end
72
-
73
- describe 'when changing speed (analog)' do
74
-
75
- let(:motor) { Artoo::Drivers::Motor.new(:parent => @device, :additional_params => {speed_pin: 3}) }
76
-
77
- it '#speed' do
78
- connection.expects(:pwm_write).with(3, 255)
79
- motor.speed(255)
80
- end
81
-
82
- it '#start' do
83
- motor.speed(0)
84
- motor.start
85
- motor.current_speed.must_equal 255
86
- end
87
-
88
- it '#stop' do
89
- motor.speed(255)
90
- motor.stop
91
- motor.current_speed.must_equal 0
92
- end
93
-
94
- it '#max' do
95
- motor.speed(0)
96
- motor.max
97
- motor.current_speed.must_equal 255
98
- end
99
-
100
- it '#min' do
101
- motor.speed(255)
102
- motor.min
103
- motor.current_speed.must_equal 0
104
- end
105
-
106
- it '#on?' do
107
- motor.speed(1)
108
- motor.on?.must_equal true
109
- motor.speed(0)
110
- motor.on?.must_equal false
111
- end
112
-
113
- it '#off?' do
114
- motor.speed(1)
115
- motor.off?.must_equal false
116
- motor.speed(0)
117
- motor.off?.must_equal true
118
- end
119
- end
120
-
121
- end
122
-
123
- describe 'bididirectional' do
124
- let(:motor) { Artoo::Drivers::Motor.new(:parent => @device,
125
- :additional_params =>
126
- {:forward_pin => 1,
127
- :backward_pin => 2}) }
128
-
129
- describe '#forward' do
130
-
131
- before do
132
- connection.expects(:digital_write).with(1, :high)
133
- connection.expects(:digital_write).with(2, :low)
134
- end
135
-
136
- describe 'when no parameter' do
137
-
138
- it '#forward' do
139
- motor.expects(:start)
140
- motor.forward
141
- end
142
-
143
- end
144
-
145
- describe 'when speed parameter' do
146
-
147
- it '#forward' do
148
- motor.expects(:speed).with(255)
149
- motor.forward(255)
150
- end
151
-
152
- end
153
-
154
- end
155
-
156
- describe '#backward' do
157
-
158
- before do
159
- connection.expects(:digital_write).with(1, :low)
160
- connection.expects(:digital_write).with(2, :high)
161
- end
162
-
163
- describe 'when no parameter' do
164
-
165
- it '#backward' do
166
- motor.expects(:start)
167
- motor.backward
168
- end
169
-
170
- end
171
-
172
- describe 'when speed parameter' do
173
-
174
- it '#backward' do
175
- motor.expects(:speed).with(255)
176
- motor.backward(255)
177
- end
178
-
179
- end
180
-
181
- end
182
- end
183
-
184
-
185
- #it 'Motor#speed must be valid' do
186
- #invalid_speed = lambda { @motor2 = Artoo::Drivers::Motor.new(:parent => @device, :additional_params => {}); @motor2.speed("ads") }
187
- #invalid_speed.must_raise RuntimeError
188
- #error = invalid_speed.call rescue $!
189
- #error.message.must_equal 'Motor speed must be an integer between 0-255'
190
- #end
191
-
192
- #it 'Motor#forward' do
193
- #@motor.expects(:set_legs)
194
- #@motor.forward(100)
195
- #@motor.current_speed.must_equal 100
196
- #end
197
-
198
- #it 'Motor#backward' do
199
- #@motor.expects(:set_legs)
200
- #@motor.backward(100)
201
- #@motor.current_speed.must_equal 100
202
- #end
203
-
204
- end