arduino_firmata 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.2.7 2013-03-20
2
+
3
+ * select Thread/EventMachine on connect
4
+
1
5
  === 0.2.6 2013-03-13
2
6
 
3
7
  * select blocking/nonblocking IO on connect
data/README.md CHANGED
@@ -16,7 +16,7 @@ Requirements
16
16
 
17
17
  * Ruby 1.8.7 or 1.9.2 or 1.9.3 or 2.0.0
18
18
  * Arduino (http://arduino.cc)
19
- * testing with Arduino Duemillanove, UNO, Micro, Seeduino v2.
19
+ * testing with Arduino Duemillanove, UNO, Leonardo, Micro, Seeduino v2.
20
20
  * Arduino Standard Firmata v2.2
21
21
  * Arduino IDE -> [File] -> [Examples] -> [Firmata] -> [StandardFirmata]
22
22
 
@@ -46,6 +46,7 @@ arduino = ArduinoFirmata.connect # use default arduino
46
46
  arduino = ArduinoFirmata.connect '/dev/tty.usb-device-name'
47
47
  arduino = ArduinoFirmata.connect '/dev/tty.usb-device-name', :bps => 57600
48
48
  arduino = ArduinoFirmata.connect '/dev/tty.usb-device-name', :nonblock_io => true
49
+ arduino = ArduinoFirmata.connect '/dev/tty.usb-device-name', :eventmachine => true
49
50
  ```
50
51
 
51
52
  Board Version
@@ -3,10 +3,13 @@ module ArduinoFirmata
3
3
  class Arduino
4
4
  include EventEmitter
5
5
 
6
- attr_reader :version, :status, :nonblock_io
6
+ attr_reader :version, :status, :nonblock_io, :eventmachine
7
7
 
8
8
  def initialize(serial_name, params)
9
9
  @nonblock_io = !!params[:nonblock_io]
10
+ @eventmachine = !!params[:eventmachine]
11
+ @read_byte_size = eventmachine ? 256 : 9600
12
+ @process_input_interval = eventmachine ? 0.0001 : 0.01
10
13
  @status = Status::CLOSE
11
14
  @wait_for_data = 0
12
15
  @execute_multi_byte_command = 0
@@ -44,14 +47,14 @@ module ArduinoFirmata
44
47
  end
45
48
 
46
49
  @thread_status = false
47
- Thread.new{
50
+ run do
48
51
  @thread_status = true
49
52
  while status == Status::OPEN do
50
53
  process_input
51
- sleep 0.01
54
+ sleep @process_input_interval
52
55
  end
53
56
  @thread_status = false
54
- }.run
57
+ end
55
58
 
56
59
  (0...6).each do |i|
57
60
  write(REPORT_ANALOG | i)
@@ -70,6 +73,15 @@ module ArduinoFirmata
70
73
  sleep 0.5
71
74
  end
72
75
 
76
+ def run(&block)
77
+ return unless block_given?
78
+ if eventmachine
79
+ EM::defer &block
80
+ else
81
+ Thread.new &block
82
+ end
83
+ end
84
+
73
85
  def close
74
86
  return if status == Status::CLOSE
75
87
  @status = Status::CLOSE
@@ -171,9 +183,9 @@ module ArduinoFirmata
171
183
  def read
172
184
  return if status == Status::CLOSE
173
185
  if nonblock_io
174
- @serial.read_nonblock 9600 rescue EOFError
186
+ @serial.read_nonblock @read_byte_size rescue EOFError
175
187
  else
176
- @serial.read 9600 rescue EOFError
188
+ @serial.read @read_byte_size rescue EOFError
177
189
  end
178
190
  end
179
191
 
@@ -7,7 +7,8 @@ module ArduinoFirmata
7
7
  :bit => 8,
8
8
  :parity => 0,
9
9
  :stopbit => 1,
10
- :nonblock_io => false
10
+ :nonblock_io => false,
11
+ :eventmachine => false
11
12
  }
12
13
  end
13
14
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module ArduinoFirmata
3
- VERSION = '0.2.6'
3
+ VERSION = '0.2.7'
4
4
  end
data/samples/README.md CHANGED
@@ -4,30 +4,39 @@ Digital/Analog IO
4
4
  -----------------
5
5
  - analog_read_write.rb
6
6
  - digital_read.rb
7
+ - eventmachine.rb
7
8
  - led_blink.rb
8
9
  - led_fade.rb
9
10
  - on_analog_read.rb
10
11
  - on_digital_read.rb
11
12
  - servo.rb
12
-
13
+ - tweet_temperature.rb
13
14
 
14
15
  Sysex Command
15
16
  -------------
16
17
  - sysex/sysex_led_blink.rb
17
18
 
18
19
 
19
- Tweet Temperature
20
- -----------------
20
+ EventMachine
21
+ ------------
21
22
 
22
- % gem install tw
23
- % tw --user:add
24
- % ruby tweet_temperature.rb
23
+ % gem install eventmachine
24
+ % ruby eventmachine.rb
25
25
 
26
26
 
27
27
  Sinatra Arduino
28
28
  ---------------
29
29
 
30
- % gem install sinatra
30
+ % gem install sinatra eventmachine
31
31
  % ruby sinatra_arduino.rb
32
32
 
33
33
  => http://localhost:4567
34
+
35
+
36
+ Tweet Temperature
37
+ -----------------
38
+
39
+ % gem install tw
40
+ % tw --user:add
41
+ % ruby tweet_temperature.rb
42
+
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'eventmachine'
5
+ require 'arduino_firmata'
6
+
7
+ EM::run do
8
+ arduino = ArduinoFirmata.connect ARGV.shift, :nonblock_io => true ,:eventmachine => true
9
+
10
+ arduino.on :analog_read do |pin, value|
11
+ puts "analog_read #{pin} => #{value}" if pin == 0
12
+ end
13
+
14
+ arduino.pin_mode 2, ArduinoFirmata::INPUT
15
+ arduino.on :digital_read do |pin, status|
16
+ puts "digital_read #{pin} => #{status}" if pin == 2
17
+ end
18
+
19
+ led_stat = false
20
+ EM::add_periodic_timer 1 do
21
+ puts led_stat
22
+ arduino.digital_write 13, led_stat
23
+ led_stat = !led_stat
24
+ end
25
+ end
@@ -2,10 +2,11 @@
2
2
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
3
3
  require 'rubygems'
4
4
  require 'sinatra'
5
+ require 'eventmachine'
5
6
  require 'arduino_firmata'
6
7
 
7
8
  # arduino = ArduinoFirmata.connect
8
- arduino = ArduinoFirmata.connect nil, :nonblock_io => true
9
+ arduino = ArduinoFirmata.connect nil, :nonblock_io => true, :eventmachine => true
9
10
 
10
11
  get '/' do
11
12
  redirect './on'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: arduino_firmata
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.6
5
+ version: 0.2.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sho Hashimoto
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-03-13 00:00:00 Z
13
+ date: 2013-03-19 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: serialport
@@ -72,6 +72,7 @@ files:
72
72
  - samples/analog_read_write.rb
73
73
  - samples/block.rb
74
74
  - samples/digital_read.rb
75
+ - samples/eventmachine.rb
75
76
  - samples/led_blink.rb
76
77
  - samples/led_fade.rb
77
78
  - samples/on_analog_read.rb