Class: Bucaneer::Protocol::I2C

Inherits:
Object
  • Object
show all
Defined in:
lib/bucaneer/protocol/i2c.rb

Overview

I2C bitbang on the BusPirate:

http://dangerousprototypes.com/docs/I2C_(binary)

I2C protocol:

http://en.wikipedia.org/wiki/I²C

Constant Summary collapse

I2C_MODE =
0x02
SET_SPEED =
0x62
START =
0x02
STOP =
0x03
BULK_WRITE =
0x10
I2C_WRITE_BIT =
0x00
I2C_READ_BIT =
0x01
ACK =
0x00
NACK =
0x01
CHUNK_SIZE =
16

Instance Method Summary collapse

Constructor Details

#initialize(controller, options = {}) ⇒ I2C

Returns a new instance of I2C.



23
24
25
26
27
# File 'lib/bucaneer/protocol/i2c.rb', line 23

def initialize(controller, options = {})
  @controller = controller
  enter_i2c_mode
  @controller.tx(SET_SPEED)
end

Instance Method Details

#tx(address, *bytes) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bucaneer/protocol/i2c.rb', line 29

def tx(address, *bytes)
  bytes.flatten!

  @controller.tx(START)

  bytes.to_enum.each_slice(CHUNK_SIZE) do |chunk|
    start_bulk_write(chunk.length + 1)
    @controller.tx((address << 1) | I2C_WRITE_BIT, ACK)
    chunk.each {|byte| @controller.tx(byte, ACK) }
  end

  @controller.tx(STOP)
end