I2C

class wpilib.I2C(port, deviceAddress, simPort=None)[source]

Bases: object

I2C bus interface class.

This class is intended to be used by sensor (and other I2C device) drivers. It probably should not be used directly.

Example usage:

i2c = wpilib.I2C(wpilib.I2C.Port.kOnboard, 4)

# Write bytes 'text', and receive 4 bytes in data
data = i2c.transaction(b'text', 4)

Constructor.

Parameters:
  • port (I2C.Port) – The I2C port the device is connected to.
  • deviceAddress – The address of the device on the I2C bus.
  • simPort – This must be an object that implements all of the i2c* functions from hal_impl that you use. See test_i2c.py for an example.
class Port[source]

Bases: object

kMXP = 1
kOnboard = 0
I2C.addressOnly()[source]

Attempt to address a device on the I2C bus.

This allows you to figure out if there is a device on the I2C bus that responds to the address specified in the constructor.

Returns:Transfer Aborted... False for success, True for aborted.
I2C.free()[source]
I2C.port
I2C.read(registerAddress, count)[source]

Execute a read transaction with the device.

Read bytes from a device. Most I2C devices will auto-increment the register pointer internally allowing you to read consecutive registers on a device in a single transaction.

Parameters:
  • registerAddress – The register to read first in the transaction.
  • count – The number of bytes to read in the transaction.
Returns:

The data read from the device.

Return type:

iterable of bytes

I2C.readOnly(count)[source]

Execute a read only transaction with the device.

Read bytes from a device. This method does not write any data to prompt the device.

Parameters:count – The number of bytes to read in the transaction.
Returns:The data read from the device.
Return type:iterable of bytes
I2C.transaction(dataToSend, receiveSize)[source]

Generic transaction.

This is a lower-level interface to the I2C hardware giving you more control over each transaction.

Parameters:
  • dataToSend (iterable of bytes) – Buffer of data to send as part of the transaction.
  • receiveSize (int) – Number of bytes to read from the device.
Returns:

Data received from the device.

Return type:

iterable of bytes

I2C.verifySensor(registerAddress, expected)[source]

Verify that a device’s registers contain expected values.

Most devices will have a set of registers that contain a known value that can be used to identify them. This allows an I2C device driver to easily verify that the device contains the expected value.

The device must support and be configured to use register auto-increment.

Parameters:
  • registerAddress – The base register to start reading from the device.
  • expected – The values expected from the device.
Returns:

True if the sensor was verified to be connected

I2C.write(registerAddress, data)[source]

Execute a write transaction with the device.

Write a single byte to a register on a device and wait until the transaction is complete.

Parameters:
  • registerAddress – The address of the register on the device to be written.
  • data – The byte to write to the register on the device.
Returns:

Transfer Aborted... False for success, True for aborted.

I2C.writeBulk(data)[source]

Execute a write transaction with the device.

Write multiple bytes to a register on a device and wait until the transaction is complete.

Parameters:data (iterable of bytes) – The data to write to the device.
Returns:Transfer Aborted... False for success, True for aborted.

Usage:

# send byte string
failed = spi.writeBulk(b'stuff')

# send list of integers
failed = spi.write([0x01, 0x02])