edu.wpi.first.wpilibj

## Class I2C

- java.lang.Object
- - edu.wpi.first.wpilibj.SensorBase
  - - edu.wpi.first.wpilibj.I2C

-     public class I2C
      extends SensorBase

  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.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `I2C.Port`

  - ### Field Summary

    - ### Fields inherited from class edu.wpi.first.wpilibj.SensorBase

      `kAnalogInputChannels, kAnalogOutputChannels, kDigitalChannels, kPCMModules, kPDPChannels, kPDPModules, kPwmChannels, kRelayChannels, kSolenoidChannels, kSystemClockTicksPerMicrosecond`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `I2C(I2C.Port port, int deviceAddress)` Constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `addressOnly()` Attempt to address a device on the I2C bus.
    `void` | `free()` Destructor.
    `boolean` | `read(int registerAddress, int count, byte[] buffer)` Execute a read transaction with the device.
    `boolean` | `read(int registerAddress, int count, java.nio.ByteBuffer buffer)` Execute a read transaction with the device.
    `boolean` | `readOnly(byte[] buffer, int count)` Execute a read only transaction with the device.
    `boolean` | `readOnly(java.nio.ByteBuffer buffer, int count)` Execute a read only transaction with the device.
    `boolean` | `transaction(byte[] dataToSend, int sendSize, byte[] dataReceived, int receiveSize)` Generic transaction.
    `boolean` | `transaction(java.nio.ByteBuffer dataToSend, int sendSize, java.nio.ByteBuffer dataReceived, int receiveSize)` Generic transaction.
    `boolean` | `verifySensor(int registerAddress, int count, byte[] expected)` Verify that a device's registers contain expected values.
    `boolean` | `write(int registerAddress, int data)` Execute a write transaction with the device.
    `boolean` | `writeBulk(byte[] data)` Execute a write transaction with the device.
    `boolean` | `writeBulk(java.nio.ByteBuffer data, int size)` Execute a write transaction with the device.

    - ### Methods inherited from class edu.wpi.first.wpilibj.SensorBase

      `checkAnalogInputChannel, checkAnalogOutputChannel, checkDigitalChannel, checkPDPChannel, checkPDPModule, checkPWMChannel, checkRelayChannel, checkSolenoidChannel, checkSolenoidModule, getDefaultSolenoidModule, setDefaultSolenoidModule`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### I2C

          public I2C(I2C.Port port,
                     int deviceAddress)

      Constructor.
      Parameters:
        `port` - The I2C port the device is connected to.
        `deviceAddress` - The address of the device on the I2C bus.

  - ### Method Detail

    - #### free

          public void free()

      Destructor.
      Overrides:
        `free` in class `SensorBase`

    - #### transaction

          public boolean transaction(byte[] dataToSend,
                                     int sendSize,
                                     byte[] dataReceived,
                                     int receiveSize)

      Generic transaction.

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

      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.

    - #### transaction

          public boolean transaction(java.nio.ByteBuffer dataToSend,
                                     int sendSize,
                                     java.nio.ByteBuffer dataReceived,
                                     int receiveSize)

      Generic transaction.

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

      Parameters:
        `dataToSend` - Buffer of data to send as part of the transaction. Must be allocated using ByteBuffer.allocateDirect().
        `sendSize` - Number of bytes to send as part of the transaction.
        `dataReceived` - Buffer to read data into. Must be allocated using `ByteBuffer.allocateDirect(int)`.
        `receiveSize` - Number of bytes to read from the device.
      Returns:
        Transfer Aborted... false for success, true for aborted.

    - #### addressOnly

          public boolean addressOnly()

      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.

    - #### write

          public boolean write(int registerAddress,
                               int data)

      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

          public boolean writeBulk(byte[] data)

      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.

    - #### writeBulk

          public boolean writeBulk(java.nio.ByteBuffer data,
                                   int size)

      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. Must be created using ByteBuffer.allocateDirect().
      Returns:
        Transfer Aborted... false for success, true for aborted.

    - #### read

          public boolean read(int registerAddress,
                              int count,
                              byte[] buffer)

      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.
        `buffer` - A pointer to the array of bytes to store the data read from the device.
      Returns:
        Transfer Aborted... false for success, true for aborted.

    - #### read

          public boolean read(int registerAddress,
                              int count,
                              java.nio.ByteBuffer buffer)

      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.
        `buffer` - A buffer to store the data read from the device. Must be created using ByteBuffer.allocateDirect().
      Returns:
        Transfer Aborted... false for success, true for aborted.

    - #### readOnly

          public boolean readOnly(byte[] buffer,
                                  int count)

      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.

    - #### readOnly

          public boolean readOnly(java.nio.ByteBuffer buffer,
                                  int count)

      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. Must be created using ByteBuffer.allocateDirect().
        `count` - The number of bytes to read in the transaction.
      Returns:
        Transfer Aborted... false for success, true for aborted.

    - #### verifySensor

          public boolean verifySensor(int registerAddress,
                                      int count,
                                      byte[] expected)

      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.

      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.
      Returns:
        true if the sensor was verified to be connected
      Pre-Condition
        The device must support and be configured to use register auto-increment.