tino / pyFirmata

Python interface for the Firmata (http://firmata.org/) protocol. It is compliant with Firmata 2.1. Any help with updating to 2.2 is welcome. The Capability Query is implemented, but the Pin State Query feature not yet.
MIT License
578 stars 191 forks source link

Read/write I2C #37

Closed zawilliams closed 9 years ago

zawilliams commented 9 years ago

Is there a way to read and write over I2C? I may be totally missing it.

tino commented 9 years ago

Not directly, but I2C message are just sysex messages (https://github.com/firmata/protocol/blob/master/i2c.md), so you should be able to do something like:

my_board.send_sysex(pyfirmata.I2C_REQUEST, my_data)

You also want to write a method that does something with the reply. Subclass Board and add a command handler for the I2C_REPLY command:

class MyBoard(pyfirmata.Board):
    def _handle_i2c(self, *data):
        # do your thing

# instantiate and register the cmd handler
my_board = MyBoard()
my_board.add_cmd_handler(pyfirmata.I2C_REPLY, my_board._handle_i2c)
# use my_board
zawilliams commented 9 years ago

Got it - thought that was the case. Thanks @tino!