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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e99b8d72a5ef6f50399e41d98bdb430ac6c389e
4
- data.tar.gz: 70b8875c3af5a261715c2e407d4c2ace7f498740
3
+ metadata.gz: ef22f258fe9188bda778e5d12dc5fed59304a739
4
+ data.tar.gz: 3cd46e7b4f6f46f6c0587f1c1f12b37c30eeafd4
5
5
  SHA512:
6
- metadata.gz: 8809c09890418839551d643e735b2ad89397357712b8ba0310cda7e3ec76e5def5facd9179c731afc5a461305d620a768296ae4450a7297472b855472361c142
7
- data.tar.gz: 0ae662eeeb16b7bc0a51d7855c750a5eb5985435bdab553b9882905ba0624f913590cc62486d12492db63416ffa42e69dc5440c18e798e87d3501f87665772d0
6
+ metadata.gz: 3d45b5612b574ac27c67ee28168d9930ea0fca8c89a3b4cab3fc2e0baa682d8f0abfa4b57f4a32a60b6a2b6615885d79526cae61f923b17d5d86a3dd3f2d4f0e
7
+ data.tar.gz: c56c99b937db47126b573e1901da2415ca430e531ba49a3c5072bf58f13b7348f07ba65457fdd60d628db9474f50794fcd8fb9f0a684b1d8762e8e8e8358b23c
data/Gemfile.lock CHANGED
@@ -1,58 +1,56 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- artoo-arduino (1.1.0)
5
- artoo (~> 1.1.0)
4
+ artoo-arduino (1.2.0)
5
+ artoo (~> 1.2.0)
6
+ artoo-gpio
7
+ artoo-i2c
6
8
  hybridgroup-firmata (~> 0.4.5)
7
- littlewire (~> 0.9.7)
8
9
 
9
10
  GEM
10
11
  remote: http://rubygems.org/
11
12
  specs:
12
- artoo (1.1.0)
13
+ artoo (1.2.1)
13
14
  celluloid (~> 0.15.0)
14
15
  celluloid-io (~> 0.15.0)
15
16
  multi_json (~> 1.6)
16
17
  pry (~> 0.9)
17
18
  rake (~> 10.0)
18
- reel (~> 0.4.0.pre)
19
+ reel (~> 0.4.0)
19
20
  thor (~> 0.18.1)
21
+ artoo-gpio (0.2.0)
22
+ artoo (~> 1.2.0)
23
+ artoo-i2c (0.2.0)
24
+ artoo (~> 1.2.0)
20
25
  celluloid (0.15.1)
21
26
  timers (~> 1.1.0)
22
27
  celluloid-io (0.15.0)
23
28
  celluloid (>= 0.15.0)
24
29
  nio4r (>= 0.5.0)
25
30
  coderay (1.0.9)
26
- colorist (0.0.2)
27
31
  event_emitter (0.2.5)
28
- ffi (1.9.0)
29
- http (0.5.0.pre)
32
+ http (0.5.0)
30
33
  http_parser.rb
31
34
  http_parser.rb (0.6.0.beta.2)
32
35
  hybridgroup-firmata (0.4.5)
33
36
  event_emitter
34
- libusb (0.3.4)
35
- ffi (>= 1.0)
36
- littlewire (0.9.7)
37
- colorist (>= 0.0.2)
38
- libusb (>= 0.2.0)
39
37
  metaclass (0.0.1)
40
38
  method_source (0.8.2)
41
39
  minitest (5.0.1)
42
40
  minitest-happy (1.0.0)
43
41
  mocha (0.14.0)
44
42
  metaclass (~> 0.0.1)
45
- multi_json (1.7.9)
43
+ multi_json (1.8.0)
46
44
  nio4r (0.5.0)
47
45
  pry (0.9.12.2)
48
46
  coderay (~> 1.0.5)
49
47
  method_source (~> 0.8)
50
48
  slop (~> 3.4)
51
49
  rake (10.1.0)
52
- reel (0.4.0.pre4)
53
- celluloid (>= 0.15.0)
50
+ reel (0.4.0)
51
+ celluloid (>= 0.15.1)
54
52
  celluloid-io (>= 0.15.0)
55
- http (>= 0.5.0.pre)
53
+ http (>= 0.5.0)
56
54
  http_parser.rb (>= 0.6.0.beta.2)
57
55
  websocket_parser (>= 0.1.4)
58
56
  slop (3.4.6)
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Artoo Adaptor For Arduino
2
2
 
3
- This repository contains the Artoo (http://artoo.io/) adaptor and drivers for Arduino and Arduino-compatible microcontrollers (http://arduino.cc/) using either the Firmata protocol (http://firmata.org) or the Little Wire protocol (http://littlewire.cc).
3
+ This repository contains the Artoo (http://artoo.io/) adaptor and drivers for Arduino and Arduino-compatible microcontrollers (http://arduino.cc/) using the Firmata protocol (http://firmata.org).
4
4
 
5
5
  Artoo is a open source micro-framework for robotics using Ruby.
6
6
 
@@ -10,8 +10,6 @@ For more information abut Artoo, check out our repo at https://github.com/hybrid
10
10
 
11
11
  This gem makes extensive use of the hybridgroup fork of the firmata gem (https://github.com/hybridgroup/firmata) thanks to [@hardbap](https://github.com/hardbap) with code borrrowed from the arduino_firmata gem (https://github.com/shokai/arduino_firmata) thanks to [@shokai](https://github.com/shokai)
12
12
 
13
- It also makes extensive use of the littlewire.rb gem (https://github.com/Bluebie/littlewire.rb) thanks to [@Bluebie](https://github.com/Bluebie)
14
-
15
13
  ## Installing
16
14
 
17
15
  ```
@@ -24,7 +22,7 @@ gem install artoo-arduino
24
22
  require 'artoo'
25
23
 
26
24
  connection :arduino, :adaptor => :firmata, :port => '127.0.0.1:8023'
27
- device :board
25
+ device :board, :driver => :device_info
28
26
  device :led, :driver => :led, :pin => 13
29
27
 
30
28
  work do
@@ -38,14 +36,17 @@ end
38
36
 
39
37
  ## Devices supported
40
38
 
41
- The following hardware devices have driver support:
39
+ The following hardware devices have driver support via the artoo-gpio gem:
42
40
  - Button
43
41
  - LED
44
42
  - Maxbotix ultrasonic range finder
43
+ - Analog sensor
45
44
  - Motor (DC)
46
45
  - Servo
47
- - Wiichuck controller (via i2c)
48
- - Wiiclassic controller (via i2c)
46
+
47
+ The following hardware devices have driver support via the artoo-i2c gem:
48
+ - Wiichuck controller
49
+ - Wiiclassic controller
49
50
 
50
51
  ## Connecting to Arduino
51
52
 
@@ -96,62 +97,6 @@ artoo connect serial ttyACM0 4567
96
97
 
97
98
  Someone please fill in the blanks here...
98
99
 
99
- ## Connecting to Digispark
100
-
101
- You can use Artoo with a Digispark (http://www.kickstarter.com/projects/digistump/digispark-the-tiny-arduino-enabled-usb-dev-board) ATTiny-based USB development board, that has the Little Wire (http://littlewire.cc/) protocol firmware installed. For instructions on how to install Little Wire on a Digispark check out http://digistump.com/board/index.php/topic,160.0.html
102
-
103
- ### OSX
104
-
105
- The main steps are:
106
- - Plug in the Digispark to the USB port
107
- - Connect to the device via Artoo
108
-
109
- First plug the Digispark into your computer via the USB port. Then... (directions go here)
110
-
111
- ### Ubuntu
112
-
113
- The main steps are:
114
- - Add a udev rule to allow access to the Digispark device
115
- - Plug in the Digispark to the USB port
116
- - Connect to the device via Artoo
117
-
118
- First, you must add a udev rule, so that Artoo can communicate with the USB device. Ubuntu and other modern Linux distibutions use udev to manage device files when USB devices are added and removed. By default, udev will create a device with read-only permission which will not allow to you download code. You must place the udev rules below into a file named /etc/udev/rules.d/49-micronucleus.rules.
119
-
120
- ```
121
- # UDEV Rules for Micronucleus boards including the Digispark.
122
- # This file must be placed at:
123
- #
124
- # /etc/udev/rules.d/49-micronucleus.rules (preferred location)
125
- # or
126
- # /lib/udev/rules.d/49-micronucleus.rules (req'd on some broken systems)
127
- #
128
- # After this file is copied, physically unplug and reconnect the board.
129
- #
130
- SUBSYSTEMS=="usb", ATTRS{idVendor}=="1781", ATTRS{idProduct}=="0c9f", MODE:="0666"
131
- KERNEL=="ttyACM*", ATTRS{idVendor}=="1781", ATTRS{idProduct}=="0c9f", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1"
132
- #
133
- # If you share your linux system with other users, or just don't like the
134
- # idea of write permission for everybody, you can replace MODE:="0666" with
135
- # OWNER:="yourusername" to create the device owned by you, or with
136
- # GROUP:="somegroupname" and mange access using standard unix groups.
137
- ```
138
-
139
- Thanks to [@bluebie](https://github.com/Bluebie) for these instructions! (https://github.com/Bluebie/micronucleus-t85/wiki/Ubuntu-Linux)
140
-
141
- Now plug the Digispark into your computer via the USB port.
142
-
143
- Once plugged in, use the `artoo connect scan` command with the `-t usb` option to verify your connection info:
144
-
145
- ```
146
- $ artoo connect scan -t usb
147
- ```
148
-
149
- Now use the `ID` info returned to find the `product` and `vendor` ID's for the connection info Digispark in your Artoo code.
150
-
151
- ### Windows
152
-
153
- Someone please fill in the blanks here...
154
-
155
100
  ## Contributing
156
101
 
157
102
  1. Fork it
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.name = "artoo-arduino"
7
7
  s.version = Artoo::Arduino::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Ron Evans", "Adrian Zankich", "Rafael Magaña"]
9
+ s.authors = ["Ron Evans", "Adrian Zankich", "Rafael Magaña", "Edgar Silva"]
10
10
  s.email = ["artoo@hybridgroup.com"]
11
11
  s.homepage = "https://github.com/hybridgroup/artoo-arduino"
12
12
  s.summary = %q{Artoo adaptor and driver for Arduino}
@@ -20,9 +20,10 @@ Gem::Specification.new do |s|
20
20
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
21
  s.require_paths = ["lib"]
22
22
 
23
- s.add_runtime_dependency 'artoo', '~> 1.1.0'
23
+ s.add_runtime_dependency 'artoo', '~> 1.2.0'
24
+ s.add_runtime_dependency 'artoo-gpio'
25
+ s.add_runtime_dependency 'artoo-i2c'
24
26
  s.add_runtime_dependency 'hybridgroup-firmata', '~> 0.4.5'
25
- s.add_runtime_dependency 'littlewire', '~> 0.9.7'
26
27
  s.add_development_dependency 'minitest', '~> 5.0'
27
28
  s.add_development_dependency 'minitest-happy'
28
29
  s.add_development_dependency 'mocha', '~> 0.14.0'
@@ -0,0 +1,31 @@
1
+ require 'artoo'
2
+
3
+ # Substitute the button with an analog sensor like a photoresistor and
4
+ # change to the correct analog input, in this case pin A0.
5
+ # Circuit and schematic here: http://arduino.cc/en/tutorial/button
6
+
7
+ #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
8
+ connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
9
+
10
+ # Analog inputs are based from 0 to 5 on the Arduino UNO, may vary on other arduino boards
11
+ device :sensor, driver: :analog_sensor, pin: 0, interval: 0.25, upper: 900, lower: 200
12
+ device :led, :driver => :led, :pin => 8
13
+
14
+ work do
15
+ puts
16
+ puts "Reading sensor in analog pin #{ sensor.pin }"
17
+ puts "Reading intervals every => #{ sensor.interval }"
18
+ puts "Initial sensor value => #{ sensor.analog_read(0) }"
19
+ puts "Sensor upper trigger set at value => #{ sensor.upper }"
20
+ puts "Sensor lower trigger set at value => #{ sensor.lower }"
21
+
22
+ on sensor, :upper => proc {
23
+ puts "UPPER LIMIT REACHED!"
24
+ led.off
25
+ }
26
+
27
+ on sensor, :lower => proc {
28
+ puts "LOWER SENSOR LIMIT REACHED!"
29
+ led.on
30
+ }
31
+ end
@@ -4,7 +4,7 @@ require 'artoo'
4
4
 
5
5
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
6
6
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:4567'
7
- device :board, :driver => :board
7
+ device :board, :driver => :device_info
8
8
  device :led, :driver => :led, :pin => 13
9
9
 
10
10
  work do
@@ -4,7 +4,7 @@ require 'artoo'
4
4
 
5
5
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
6
6
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
7
- device :board, :driver => :board
7
+ device :board, :driver => :device_info
8
8
  device :led, :driver => :led, :pin => 13
9
9
 
10
10
  work do
@@ -4,7 +4,7 @@ require 'artoo'
4
4
 
5
5
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
6
6
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:4567'
7
- device :board, :driver => :board
7
+ device :board, :driver => :device_info
8
8
  device :motor, :driver => :motor, :speed_pin => 3 # Use a PWM pin
9
9
 
10
10
  work do
@@ -5,7 +5,7 @@ require 'artoo'
5
5
 
6
6
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
7
7
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
8
- device :board, :driver => :board
8
+ device :board, :driver => :device_info
9
9
  device :motor, :driver => :motor,
10
10
  :forward_pin => 4, # Digital or PWM pin
11
11
  :backward_pin => 2, # Digital or PWM pin
@@ -4,7 +4,7 @@ require 'artoo'
4
4
 
5
5
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
6
6
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
7
- device :board, :driver => :board
7
+ device :board, :driver => :device_info
8
8
  device :motor, :driver => :motor, :switch_pin => 3 # Use a digital or PWM pin
9
9
 
10
10
  work do
@@ -6,7 +6,7 @@ require 'artoo'
6
6
 
7
7
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
8
8
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
9
- device :board, :driver => :board
9
+ device :board, :driver => :device_info
10
10
  device :motor, :driver => :motor, :forward_pin => 4, :backward_pin => 2
11
11
 
12
12
  work do
@@ -4,7 +4,7 @@ require 'artoo'
4
4
 
5
5
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
6
6
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
7
- device :board, :driver => :board
7
+ device :board, :driver => :device_info
8
8
  device :led, :driver => :led, :pin => 3
9
9
 
10
10
  brightness = 0
@@ -0,0 +1,29 @@
1
+ require 'artoo'
2
+
3
+ # For LED brightness:
4
+ # Circuit and schematic here: http://arduino.cc/en/Tutorial/Fade
5
+ #
6
+ # For Analog Input:
7
+ # Substitute the button with an analog sensor like a photoresistor and
8
+ # change to the correct analog input, in this case pin A0.
9
+ # Circuit and schematic here: http://arduino.cc/en/tutorial/button
10
+
11
+ #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
12
+ connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
13
+ device :sensor, driver: :analog_sensor, pin: 0, interval: 0
14
+ device :led, :driver => :led, :pin => 3
15
+
16
+ ai_pin = 0
17
+
18
+ work do
19
+ puts "Reading sensor in analog pin #{ sensor.pin }"
20
+ puts "Reading analog sensor intervals every => #{ sensor.interval }"
21
+
22
+ every(0.25) do
23
+ analog_read = sensor.analog_read(ai_pin)
24
+ brightness_val = analog_read.to_pwm_reverse
25
+ puts "Analog Read => #{ analog_read }"
26
+ puts "brightness val => #{ brightness_val }"
27
+ led.brightness(brightness_val)
28
+ end
29
+ end
@@ -4,7 +4,7 @@ require 'artoo'
4
4
 
5
5
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
6
6
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
7
- device :led, :driver => :led, :pin => 13
7
+ device :led, :driver => :device_info, :pin => 13
8
8
  device :button, :driver => :button, :pin => 2, :interval => 0.01
9
9
 
10
10
  work do
@@ -4,8 +4,8 @@ require 'artoo'
4
4
 
5
5
  #connection :firmata, :adaptor => :firmata, :port => '/dev/ttyACM0'
6
6
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:4567'
7
- device :sonar, :driver => :maxbotix, :pin => 14, :interval => 0.5
8
- device :board, :driver => :board
7
+ device :sonar, :driver => :maxbotix, :pin => 0, :interval => 0.5
8
+ device :board, :driver => :device_info
9
9
 
10
10
  work do
11
11
  on sonar, :range => :sonar_reading
@@ -9,7 +9,7 @@ forward = true
9
9
 
10
10
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
11
11
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
12
- device :board, :driver => :board
12
+ device :board, :driver => :device_info
13
13
  device :motor, :driver => :motor, :pin => [leg1_pin, leg2_pin, speed_pin]
14
14
 
15
15
  work do
@@ -0,0 +1,30 @@
1
+ require 'artoo'
2
+ # For motor speed output:
3
+ # Circuit's breadboard layout here: http://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/breadboard-layout
4
+ #
5
+ # For the analog input:
6
+ # Substitute the button with an analog sensor like a photoresistor and
7
+ # change to the correct analog input, in this case pin A0.
8
+ # Circuit and schematic here: http://arduino.cc/en/tutorial/button
9
+
10
+ #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
11
+ connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
12
+ device :sensor, driver: :analog_sensor, pin: 0, interval: 0
13
+ device :motor, :driver => :motor, :speed_pin => 3 # Use a PWM pin
14
+
15
+ ai_pin = 0
16
+
17
+ work do
18
+
19
+ puts "Reading sensor in analog pin #{ sensor.pin }"
20
+ puts "Reading intervals every => #{ sensor.interval }"
21
+
22
+ every(0.25) do
23
+ analog_read = sensor.analog_read(ai_pin)
24
+ motor_speed = analog_read.to_pwm
25
+ puts "Analog Read => #{ analog_read }"
26
+ puts "Motor Speed => #{ motor_speed }"
27
+ motor.speed(motor_speed)
28
+ end
29
+
30
+ end
@@ -4,7 +4,7 @@ require 'artoo'
4
4
 
5
5
  #connection :firmata, :adaptor => :firmata, :port => '/dev/tty*'
6
6
  connection :firmata, :adaptor => :firmata, :port => '127.0.0.1:8023'
7
- device :board, :driver => :board
7
+ device :board, :driver => :device_info
8
8
  device :servo, :driver => :servo, :pin => 3 # pin must be a PWM pin
9
9
 
10
10
  work do
File without changes
@@ -1,5 +1,5 @@
1
1
  module Artoo
2
2
  module Arduino
3
- VERSION = '1.1.0'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -5,7 +5,7 @@ module Artoo
5
5
  # Connect to Arduino using Firmata
6
6
  # @see http://rubydoc.info/gems/hybridgroup-firmata/0.3.0/Firmata/Board HybridGroup Firmata Documentation
7
7
  class Firmata < Adaptor
8
- attr_reader :firmata
8
+ attr_reader :firmata, :i2c_address
9
9
 
10
10
  # Creates connection with firmata board
11
11
  # @return [Boolean]
@@ -24,6 +24,16 @@ module Artoo
24
24
  super
25
25
  end
26
26
 
27
+ # device info interface
28
+ def firmware_name
29
+ firmata.firmware_name
30
+ end
31
+
32
+ def version
33
+ firmata.version
34
+ end
35
+
36
+ # GPIO - digital
27
37
  def digital_write(pin, level)
28
38
  firmata.set_pin_mode(pin, ::Firmata::PinModes::OUTPUT)
29
39
  firmata.digital_write(pin, convert_level(level))
@@ -41,34 +51,66 @@ module Artoo
41
51
  end
42
52
  value
43
53
  end
54
+
55
+ # GPIO - analog
56
+ # NOTE pins are numbered A0-A5, which translate to digital pins 14-19
57
+ def analog_read(pin)
58
+ firmata.set_pin_mode(digital_pin(pin), ::Firmata::PinModes::ANALOG)
59
+ firmata.toggle_pin_reporting(digital_pin(pin))
60
+ firmata.read_and_process
61
+
62
+ value = 0
63
+ while i = find_event("analog_read_#{pin}") do
64
+ event = events.slice!(i)
65
+ value = event.data.first if !event.nil?
66
+ end
67
+ value
68
+ end
44
69
 
70
+ def digital_pin(pin = 0)
71
+ pin + 14
72
+ end
73
+
74
+ # GPIO - PWM
45
75
  def pwm_write(pin, level)
46
76
  firmata.set_pin_mode(pin, ::Firmata::PinModes::PWM)
47
77
  firmata.analog_write(pin, level)
48
78
  end
49
79
 
80
+ # GPIO - servo
50
81
  def servo_write(pin, angle)
51
82
  firmata.set_pin_mode(pin, ::Firmata::PinModes::SERVO)
52
83
  firmata.analog_write(pin, angle)
53
84
  end
54
85
 
55
- def analog_read(pin)
56
- firmata.set_pin_mode(pin, ::Firmata::PinModes::ANALOG)
57
- firmata.toggle_pin_reporting(pin)
86
+ # i2c interface
87
+ def i2c_start(address)
88
+ @i2c_address = address
89
+
90
+ firmata.i2c_config(0)
91
+ end
92
+
93
+ def i2c_end
94
+
95
+ end
96
+
97
+ def i2c_read(size)
98
+ firmata.i2c_read_request(i2c_address, size)
58
99
  firmata.read_and_process
59
100
 
60
- value = 0
61
- while i = find_event("analog_read_#{to_analog_pin(pin)}") do
101
+ value = []
102
+ while i = find_event(:i2c_reply) do
62
103
  event = events.slice!(i)
63
- value = event.data.first if !event.nil?
104
+ value = event.data.first[:data] if !event.nil?
64
105
  end
65
106
  value
66
107
  end
67
108
 
68
- def to_analog_pin(pin)
69
- pin - 14
109
+ def i2c_write(*data)
110
+ firmata.i2c_write_request(i2c_address, *data)
70
111
  end
71
112
 
113
+ # helpers
72
114
  def find_event(name)
73
115
  events.index {|e| e.name == name.to_sym }
74
116
  end