Module diozero.core

Interface I2CDeviceInterface

All Superinterfaces:
AutoCloseable, DeviceInterface, I2CSMBusInterface
All Known Subinterfaces:
InternalI2CDeviceInterface
All Known Implementing Classes:
I2CDevice, NativeI2CDeviceJavaRaf, NativeI2CDeviceSMBus

public interface I2CDeviceInterface extends I2CSMBusInterface
Methods for interacting with I2C devices that do not use the SMBus interface. Inter-Integrated Circuit (I2C) Interface
  • Field Details

  • Method Details

    • readBytes

      int readBytes(byte[] buffer) throws RuntimeIOException
      Read the specified number of bytes from the device without the 32 byte limit imposed by SMBus. I2C commands:
       S Addr Rd [A] [Data] [A] [Data] [A] ... [A] [Data] NA P
       
      Parameters:
      buffer - byte array to populate, the length of the byte array indicates the number of bytes to read
      Returns:
      the number of bytes read
      Throws:
      RuntimeIOException - if an I/O error occurs
    • readBytesAsByteBuffer

      default ByteBuffer readBytesAsByteBuffer(int length) throws RuntimeIOException
      Utility method that wraps the response from readBytes(int) in a ByteBuffer that is configured to use the byte order specified in the constructor.
      Parameters:
      length - number of bytes to read
      Returns:
      A ByteBuffer containing the bytes read using the byte order specified in the constructor
      Throws:
      RuntimeIOException - if an I/O error occurs
      See Also:
    • readBytes

      default byte[] readBytes(int length) throws RuntimeIOException
      Utility method that wraps readBytes(byte[]) to read the specified number of bytes.
      Parameters:
      length - the number of bytes to read
      Returns:
      the bytes read from the device
      Throws:
      RuntimeIOException - if an I/O error occurs
      See Also:
    • writeBytes

      void writeBytes(byte... data) throws RuntimeIOException
      Write the specified byte array to the device without the 32 byte limit imposed by SMBus. I2C commands:
       S Addr Wr [A] [Data] [A] [Data] [A] ... [A] [Data] NA P
       
      Parameters:
      data - the data to write
      Throws:
      RuntimeIOException - if an I/O error occurs
    • writeBytes

      default void writeBytes(ByteBuffer buffer) throws RuntimeIOException
      Utility method that wraps I2CDevice.writeBytes(byte[]) to write the available bytes in the specified ByteBuffer. The byte order of data in the ByteBuffer is unchanged.
      Parameters:
      buffer - the ByteBuffer containing the data to write
      Throws:
      RuntimeIOException - if an I/O error occurs
      See Also:
    • readWrite

      default void readWrite(I2CDeviceInterface.Command... commands)
      Utility method to simplify the readWrite(I2CMessage[], byte[]) method at the cost of a bit of performance.
      Parameters:
      commands - list of logical commands to perform; note that for read commands the data read from the device will be copied into the command's data buffer
    • readWrite

      void readWrite(I2CDeviceInterface.I2CMessage[] messages, byte[] buffer)

      Low-level I2C interface to execute a series of commands in a single I2C transaction. Allows multiple read and write commands to be executed at the same time as well as control over the I2C flags that are sent with each command, e.g. NO-START.

      The data buffer MUST align with the commands that are being issued. For example, to read 2 bytes from register 0x40 and then write 3 bytes to register 0x50, the message array and buffer would be as follows:

       Message array and corresponding data buffer:
       Idx  Flags     Len  Buffer
       [0]  I2C_M_WR  1    [0] 0x40 - the register address to read from
       [1]  I2C_M_RD  2    [1..2] leave blank, will be overridden with the data read from the device
       [2]  I2C_M_WR  1    [3] 0x50 - the register address to write to
       [3]  I2C_M_WR  3    [4..6] the 3 bytes of data to write
       
      Parameters:
      messages - array of commands to send to the device
      buffer - the data buffer that is associated with the commands
    • readWrite

      default void readWrite(int registerWriteFlags, I2CDeviceInterface.Command... commands)
      Utility method to simplify the readWrite(I2CMessage[], byte[]) method at the cost of a bit of performance.
      Parameters:
      registerWriteFlags - flags to apply to all register address writes
      commands - list of logical commands to perform; note that for read commands the data read from the device will be copied into the command's data buffer
    • readNoStop

      default int readNoStop(byte registerAddress, byte[] rxData, boolean repeatedStart)
      Utility I2C read method that allows control over the NO-START flag.
      Parameters:
      registerAddress - the register address to read from
      rxData - buffer to hold the data read
      repeatedStart - whether or not to use repeated starts
      Returns:
      the number of bytes read