firmata 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,4 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- .rvmrc
18
+ .rvmrc
19
+ harness.rb
data/README.md CHANGED
@@ -26,6 +26,9 @@ Or install it yourself as:
26
26
  4. Click the Upload button
27
27
  5. Make note of the serial port: Tools > Serial Port
28
28
 
29
+ There have been reports of [issues](https://github.com/jgautier/firmata/issues/8) with Firmata 2.3.
30
+ Try downgrading to [Firmata 2.2](http://at.or.at/hans/pd/Firmata-2.2.zip) if you're having a problem.
31
+
29
32
  ## Usage
30
33
 
31
34
  Here is a simple example using IRB that will turn pin 13 on and off.
data/firmata.gemspec CHANGED
@@ -15,6 +15,8 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Firmata::VERSION
17
17
 
18
+ gem.add_development_dependency("pry")
19
+
18
20
  gem.add_runtime_dependency("serialport", ["~> 1.1.0"])
19
21
  gem.add_runtime_dependency("event_spitter")
20
22
  end
data/lib/firmata/board.rb CHANGED
@@ -69,6 +69,9 @@ module Firmata
69
69
  # Public: Returns the String firmware name of Arduion.
70
70
  attr_reader :firmware_name
71
71
 
72
+ # Public: Initialize a Board
73
+ #
74
+ # port - a String port or an Object that responds to read and write.
72
75
  def initialize(port)
73
76
  @serial_port = port.is_a?(String) ? SerialPort.new(port, 57600, 8, 1, SerialPort::NONE) : port
74
77
  @serial_port.read_timeout = 2
@@ -79,10 +82,16 @@ module Firmata
79
82
  @connected = false
80
83
  end
81
84
 
85
+ # Pubilc: Check if a connection to Arduino has been made.
86
+ #
87
+ # Returns Boolean connected state.
82
88
  def connected?
83
89
  @connected
84
90
  end
85
91
 
92
+ # Public: Make connection to Arduio.
93
+ #
94
+ # Returns Firmata::Board board.
86
95
  def connect
87
96
  unless @connected
88
97
  once('report_version', ->() do
@@ -117,22 +126,24 @@ module Firmata
117
126
  #
118
127
  # Returns nothing.
119
128
  def write(*commands)
120
- serial_port.write(commands.map(&:chr).join)
129
+ serial_port.write_nonblock(commands.map(&:chr).join)
121
130
  end
122
131
 
123
132
  # Internal: Read data from the underlying serial port.
124
133
  #
125
- # Returns Enumerator of bytes.
134
+ # Returns String data read for serial port.
126
135
  def read
127
- serial_port.bytes
136
+ serial_port.read_nonblock(4096)
137
+ rescue EOFError
128
138
  end
129
139
 
130
140
  # Internal: Process a series of bytes.
131
141
  #
132
- # bytes: An Enumerator of bytes
142
+ # data: The String data to process.
133
143
  #
134
144
  # Returns nothing.
135
- def process(bytes)
145
+ def process(data)
146
+ bytes = StringIO.new(String(data)).bytes
136
147
  bytes.each do |byte|
137
148
  case byte
138
149
  when REPORT_VERSION
@@ -152,6 +163,21 @@ module Firmata
152
163
  pins[analog_pin].value = value
153
164
  end
154
165
 
166
+ when DIGITAL_MESSAGE_RANGE
167
+ port = byte & 0x0F
168
+ first_bitmask = bytes.next
169
+ second_bitmask = bytes.next
170
+ port_value = first_bitmask | (second_bitmask << 7)
171
+
172
+ 8.times do |i|
173
+ pin_number = 8 * port + i
174
+ if pin = pins[pin_number] and pin.mode == INPUT
175
+ value = (port_value >> (i & 0x07)) & 0x01
176
+ pin.value = value
177
+ emit('digital-read', pin_number, value)
178
+ end
179
+ end
180
+
155
181
  when START_SYSEX
156
182
  current_buffer = [byte]
157
183
  begin
@@ -215,7 +241,7 @@ module Firmata
215
241
  emit('firmware_query')
216
242
 
217
243
  else
218
- # TODO decide what to do with unknown message
244
+ puts 'bad byte'
219
245
  end
220
246
  end
221
247
  end
@@ -244,10 +270,10 @@ module Firmata
244
270
  #
245
271
  # Examples
246
272
  #
247
- # pin_mode(13, OUTPUT)
273
+ # set_pin_mode(13, OUTPUT)
248
274
  #
249
275
  # Returns nothing.
250
- def pin_mode(pin, mode)
276
+ def set_pin_mode(pin, mode)
251
277
  pins[pin].mode = mode
252
278
  write(PIN_MODE, pin, mode)
253
279
  end
@@ -325,6 +351,7 @@ module Firmata
325
351
  write(START_SYSEX, ANALOG_MAPPING_QUERY, END_SYSEX)
326
352
  end
327
353
 
354
+
328
355
  # Internal: Toggle the pin analog and digtal reporting off and on.
329
356
  #
330
357
  # state - The Integer to turn the pin on (1) or off (0).
@@ -340,14 +367,14 @@ module Firmata
340
367
  # Public: Turn pin analog and digital reporting on.
341
368
  #
342
369
  # Returns nothing.
343
- def turn_pin_reporting_on
370
+ def start_pin_reporting
344
371
  toggle_pin_reporting(1)
345
372
  end
346
373
 
347
374
  # Public: Turn pin analog and digital reporting off.
348
375
  #
349
376
  # Returns nothing.
350
- def turn_pin_reporting_off
377
+ def stop_pin_reporting
351
378
  toggle_pin_reporting(0)
352
379
  end
353
380
  end
@@ -1,3 +1,3 @@
1
1
  module Firmata
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/test/board_test.rb CHANGED
@@ -15,7 +15,7 @@ class BoardTest < MiniTest::Unit::TestCase
15
15
  yield mock_port
16
16
  else
17
17
  expected = args.map(&:chr).join
18
- mock_port.expect(:write, 1, [expected])
18
+ mock_port.expect(:write_nonblock, 1, [expected])
19
19
  end
20
20
 
21
21
  mock_port
@@ -75,15 +75,33 @@ class BoardTest < MiniTest::Unit::TestCase
75
75
  assert_equal 6, board.analog_pins.size
76
76
  end
77
77
 
78
- def test_write_pin_mode
78
+ def test_processing_digital_message
79
+ board = Firmata::Board.new(FakeSerialPort.new)
80
+
81
+ board.query_capabilities
82
+ board.read_and_process
83
+
84
+ board.query_analog_mapping
85
+ board.read_and_process
86
+
87
+ pin = board.pins[8]
88
+ pin.mode = Firmata::Board::INPUT
89
+
90
+ board.process("\x91\x01\x00")
91
+
92
+ assert_equal Firmata::Board::HIGH, pin.value
93
+ end
94
+
95
+ def test_set_pin_mode
79
96
  mock_sp = mock_serial_port(Firmata::Board::PIN_MODE, 13, Firmata::Board::OUTPUT)
80
97
 
81
98
  board = Firmata::Board.new(mock_sp)
82
99
  board.pins[13] = Firmata::Board::Pin.new([0, 1, 4], 0, 0, nil)
83
100
 
84
- board.pin_mode(13, Firmata::Board::OUTPUT)
101
+ board.set_pin_mode(13, Firmata::Board::OUTPUT)
85
102
 
86
103
  assert_equal Firmata::Board::OUTPUT, board.pins[13].mode
104
+ mock_sp.verify
87
105
  end
88
106
 
89
107
  def test_write_pin_state_query
@@ -108,30 +126,30 @@ class BoardTest < MiniTest::Unit::TestCase
108
126
  assert_equal Firmata::Board::OUTPUT, board.pins[13].mode
109
127
  end
110
128
 
111
- def test_write_turn_pin_reporting_on
129
+ def test_start_pin_reporting
112
130
  mock_sp = mock_serial_port do |mock|
113
131
  16.times do |i|
114
- mock.expect(:write, 2, [[Firmata::Board::REPORT_DIGITAL | i, 1].map(&:chr).join])
115
- mock.expect(:write, 2, [[(Firmata::Board::REPORT_ANALOG | i), 1].map(&:chr).join])
132
+ mock.expect(:write_nonblock, 2, [[Firmata::Board::REPORT_DIGITAL | i, 1].map(&:chr).join])
133
+ mock.expect(:write_nonblock, 2, [[(Firmata::Board::REPORT_ANALOG | i), 1].map(&:chr).join])
116
134
  end
117
135
  end
118
136
 
119
137
  board = Firmata::Board.new(mock_sp)
120
- board.turn_pin_reporting_on
138
+ board.start_pin_reporting
121
139
 
122
140
  mock_sp.verify
123
141
  end
124
142
 
125
- def test_turn_pin_reporting_off
143
+ def test_stop_pin_reporting
126
144
  mock_sp = mock_serial_port do |mock|
127
145
  16.times do |i|
128
- mock.expect(:write, 2, [[Firmata::Board::REPORT_DIGITAL | i, 0].map(&:chr).join])
129
- mock.expect(:write, 2, [[(Firmata::Board::REPORT_ANALOG | i), 0].map(&:chr).join])
146
+ mock.expect(:write_nonblock, 2, [[Firmata::Board::REPORT_DIGITAL | i, 0].map(&:chr).join])
147
+ mock.expect(:write_nonblock, 2, [[(Firmata::Board::REPORT_ANALOG | i), 0].map(&:chr).join])
130
148
  end
131
149
  end
132
150
 
133
151
  board = Firmata::Board.new(mock_sp)
134
- board.turn_pin_reporting_off
152
+ board.stop_pin_reporting
135
153
 
136
154
  mock_sp.verify
137
155
  end
@@ -40,6 +40,7 @@ class FakeSerialPort
40
40
  @buffer << val
41
41
  val.length
42
42
  end
43
+ alias_method :write_nonblock, :write
43
44
 
44
45
  def bytes
45
46
  bytes = StringIO.new(@buffer).bytes
@@ -47,7 +48,8 @@ class FakeSerialPort
47
48
  bytes
48
49
  end
49
50
 
50
- def read
51
- @buffer.read
51
+ def read(size)
52
+ @buffer
52
53
  end
54
+ alias_method :read_nonblock, :read
53
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firmata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-18 00:00:00.000000000 Z
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pry
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: serialport
16
32
  requirement: !ruby/object:Gem::Requirement