hybridgroup-firmata 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ require 'bundler/setup'
2
+ require 'firmata'
3
+ require 'socket'
4
+
5
+ sp = TCPSocket.open 'localhost', 4567
6
+ #sp = '/dev/tty.usbserial-A700636n'
7
+ board = Firmata::Board.new(sp)
8
+
9
+ board.connect
10
+
11
+ puts "Firmware name #{board.firmware_name}"
12
+ puts "Firmata version #{board.version}"
13
+
14
+ pin_number = 2
15
+ rate = 0.5
16
+
17
+ listener = ->(pin, value) { puts("#{pin}:#{value}") }
18
+ board.on('digital-read', listener)
19
+ board.set_pin_mode(pin_number, Firmata::Board::INPUT)
20
+ board.toggle_pin_reporting(pin_number)
21
+
22
+ while true do
23
+ puts "waiting..."
24
+ board.read_and_process
25
+ sleep 0.5
26
+ end
@@ -0,0 +1,32 @@
1
+ require 'firmata'
2
+ require 'socket'
3
+
4
+ #sp = TCPSocket.open 'localhost', 4567
5
+ sp = "/dev/ttyACM0"
6
+ board = Firmata::Board.new(sp)
7
+
8
+ board.connect
9
+
10
+ puts "Firmware name #{board.firmware_name}"
11
+ puts "Firmata version #{board.version}"
12
+
13
+ rate = 0.5
14
+ address = 0x52
15
+
16
+ listener = ->(value) {
17
+ puts value
18
+ #value[:data].each do |n|
19
+ # puts "data: #{n}"
20
+ #nd
21
+ }
22
+ board.on("i2c_reply", listener)
23
+
24
+ board.i2c_config(0)
25
+ board.i2c_write_request(address, 0x40, 0x00)
26
+
27
+ while true do
28
+ board.i2c_write_request(address, 0x00, 0x00)
29
+ board.i2c_read_request(address, 6)
30
+ board.read_and_process
31
+ sleep 0.2
32
+ end
data/lib/firmata/board.rb CHANGED
@@ -57,8 +57,22 @@ module Firmata
57
57
  ANALOG_MAPPING_QUERY = 0x69
58
58
  # Internal: Fixnum byte sysex command for analog mapping response
59
59
  ANALOG_MAPPING_RESPONSE = 0x6A
60
+ # Internal: Fixnum byte sysex command for i2c request
61
+ I2C_REQUEST = 0x76
62
+ # Internal: Fixnum byte sysex command for i2c reply
63
+ I2C_REPLY = 0x77
64
+ # Internal: Fixnum byte sysex command for i2c config
65
+ I2C_CONFIG = 0x78
60
66
  # Internal: Fixnum byte sysex command for firmware query and response
61
67
  FIRMWARE_QUERY = 0x79
68
+ # Internal: Fixnum byte i2c mode write
69
+ I2C_MODE_WRITE = 0x00
70
+ # Internal: Fixnum byte i2c mode read
71
+ I2C_MODE_READ = 0x01
72
+ # Internal: Fixnum byte i2c mode continous read
73
+ I2C_MODE_CONTINUOUS_READ = 0x02
74
+ # Internal: Fixnum byte i2c mode stop reading
75
+ I2C_MODE_STOP_READING = 0x03
62
76
 
63
77
  # Public: Returns the SerialPort port the Arduino is attached to.
64
78
  attr_reader :serial_port
@@ -251,6 +265,30 @@ module Firmata
251
265
 
252
266
  pin.value |= (current_buffer[6] << 14) if current_buffer.size > 7
253
267
 
268
+ when I2C_REPLY
269
+ # I2C reply
270
+ # 0 START_SYSEX (0xF0) (MIDI System Exclusive)
271
+ # 1 I2C_REPLY (0x77)
272
+ # 2 slave address (LSB)
273
+ # 3 slave address (MSB)
274
+ # 4 register (LSB)
275
+ # 5 register (MSB)
276
+ # 6 data 0 LSB
277
+ # 7 data 0 MSB
278
+ # n END_SYSEX (0xF7)
279
+ i2c_reply = {
280
+ :slave_address => current_buffer[2,2].pack("CC").unpack("v").first,
281
+ :register => current_buffer[4,2].pack("CC").unpack("v").first,
282
+ :data => [current_buffer[6,2].pack("CC").unpack("v").first]
283
+ }
284
+ i = 8
285
+ while current_buffer[i] != "0xF7".hex do
286
+ break if !(!current_buffer[i,2].nil? && current_buffer[i,2].count == 2)
287
+ i2c_reply[:data].push(current_buffer[i,2].pack("CC").unpack("v").first)
288
+ i += 2
289
+ end
290
+ emit('i2c_reply', i2c_reply)
291
+
254
292
  when FIRMWARE_QUERY
255
293
  @firmware_name = current_buffer.slice(4, current_buffer.length - 5).reject { |b| b.zero? }.map(&:chr).join
256
294
  emit('firmware_query')
@@ -397,6 +435,55 @@ module Firmata
397
435
  def toggle_pin_reporting(pin, state = HIGH, mode = REPORT_DIGITAL)
398
436
  write(mode | pin, state)
399
437
  end
438
+ # Public: Make an i2c request.
439
+ #
440
+ # I2C read/write request
441
+ #
442
+ # 0 START_SYSEX (0xF0) (MIDI System Exclusive)
443
+ # 1 I2C_REQUEST (0x76)
444
+ # 2 slave address (LSB)
445
+ # 3 slave address (MSB) + read/write and address mode bits
446
+ # {7: always 0} + {6: reserved} + {5: address mode, 1 means 10-bit mode} +
447
+ # {4-3: read/write, 00 => write, 01 => read once, 10 => read continuously, 11 => stop reading} +
448
+ # {2-0: slave address MSB in 10-bit mode, not used in 7-bit mode}
449
+ # 4 data 0 (LSB)
450
+ # 5 data 0 (MSB)
451
+ # 6 data 1 (LSB)
452
+ # 7 data 1 (MSB)
453
+ # n END_SYSEX (0xF7)
454
+ # Returns nothing.
455
+ def i2c_read_request(slave_address, num_bytes)
456
+ address = [slave_address].pack("v")
457
+ write(START_SYSEX, I2C_REQUEST, address[0], (I2C_MODE_READ << 3), num_bytes & 0x7F, ((num_bytes >> 7) & 0x7F), END_SYSEX)
458
+ end
400
459
 
460
+ def i2c_write_request(slave_address, *data)
461
+ address = [slave_address].pack("v")
462
+ ret = [START_SYSEX, I2C_REQUEST, address[0], (I2C_MODE_WRITE << 3)]
463
+ data.each do |n|
464
+ ret.push([n].pack("v")[0])
465
+ ret.push([n].pack("v")[1])
466
+ end
467
+ ret.push(END_SYSEX)
468
+ write(*ret)
469
+ end
470
+ # Public: Set i2c config.
471
+ # I2C config
472
+ # 0 START_SYSEX (0xF0) (MIDI System Exclusive)
473
+ # 1 I2C_CONFIG (0x78)
474
+ # 2 Delay in microseconds (LSB)
475
+ # 3 Delay in microseconds (MSB)
476
+ # ... user defined for special cases, etc
477
+ # n END_SYSEX (0xF7)
478
+ # Returns nothing.
479
+ def i2c_config(*data)
480
+ ret = [START_SYSEX, I2C_CONFIG]
481
+ data.each do |n|
482
+ ret.push([n].pack("v")[0])
483
+ ret.push([n].pack("v")[1])
484
+ end
485
+ ret.push(END_SYSEX)
486
+ write(*ret)
487
+ end
401
488
  end
402
489
  end
@@ -1,3 +1,3 @@
1
1
  module Firmata
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hybridgroup-firmata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-12 00:00:00.000000000 Z
12
+ date: 2013-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry
@@ -56,6 +56,8 @@ files:
56
56
  - README.md
57
57
  - Rakefile
58
58
  - examples/blink_led.rb
59
+ - examples/digital_read.rb
60
+ - examples/wiichuck_read.rb
59
61
  - firmata.gemspec
60
62
  - lib/firmata.rb
61
63
  - lib/firmata/board.rb