I2C

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

Bases: builtins.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.

Constructor.

Parameters:
  • port – The I2C port the device is connected to.
  • deviceAddress – The address of the device on the I2C bus.
class Port[source]

Bases: builtins.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.broadcast(registerAddress, data)[source]

Send a broadcast write to all devices on the I2C bus.

Warning

This is not currently implemented!

Parameters:
  • registerAddress – The register to write on all devices on the bus.
  • data – The value to write to the devices.
I2C.read(registerAddress, count)[source]

Execute a read transaction with the device.

Read 1 to 7 bytes from a device. Most I2C devices will auto-increment the register pointer internally allowing you to read up to 7 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. [1..7]
Returns:

The data read from the device.

I2C.readOnly(count)[source]

Execute a read only transaction with the device.

Read 1 to 7 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. [1..7]
Returns:The data read from the device.
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 – Data to send as part of the transaction.
  • receiveSize – Number of bytes to read from the device. [0..7]
Returns:

Data received from the device.

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 – The data to write to the device.
Returns:Transfer Aborted... False for success, True for aborted.