I2C

class wpilib.I2C(port: wpilib._wpilib.I2C.Port, deviceAddress: int)

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

The Onboard I2C port is subject to system lockups. See <a href=”https://docs.wpilib.org/en/stable/docs/yearly-overview/known-issues.html#onboard-i2c-causing-system-lockups”> WPILib Known Issues</a> page for details.

Constructor.

Parameters:
  • port – The I2C port to which the device is connected.

  • deviceAddress – The address of the device on the I2C bus.

class Port(value: int)

Bases: pybind11_object

Members:

kOnboard

kMXP

kMXP = <Port.kMXP: 1>
kOnboard = <Port.kOnboard: 0>
property name
property value
addressOnly() bool

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.

getDeviceAddress() int
getPort() wpilib._wpilib.I2C.Port
read(registerAddress: int, data: buffer) bool

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.

  • data – A pointer to the array of bytes to store the data read from the device.

Returns:

Transfer Aborted… false for success, true for aborted.

readOnly(buffer: buffer) bool

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:
  • buffer – A pointer to the array of bytes to store the data read from the device.

  • count – The number of bytes to read in the transaction.

Returns:

Transfer Aborted… false for success, true for aborted.

transaction(dataToSend: buffer, dataReceived: buffer) bool

Generic transaction.

This is a lower-level interface to the I2C hardware giving you more control over each transaction. If you intend to write multiple bytes in the same transaction and do not plan to receive anything back, use writeBulk() instead. Calling this with a receiveSize of 0 will result in an error.

Parameters:
  • dataToSend – Buffer of data to send as part of the transaction.

  • sendSize – Number of bytes to send as part of the transaction.

  • dataReceived – Buffer to read data into.

  • receiveSize – Number of bytes to read from the device.

Returns:

Transfer Aborted… false for success, true for aborted.

verifySensor(registerAddress: int, expected: buffer) bool

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.

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

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

  • count – The size of the field to be verified.

  • expected – A buffer containing the values expected from the device.

write(registerAddress: int, data: int) bool

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.

writeBulk(data: buffer) bool

Execute a bulk write transaction with the device.

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

Parameters:
  • data – The data to write to the register on the device.

  • count – The number of bytes to be written.

Returns:

Transfer Aborted… false for success, true for aborted.