Class I2cDevice

java.lang.Object
com.codename1.hardware.I2cDevice

public final class I2cDevice extends Object

A device on an I2C bus, opened through Hardware.openI2c(int, int).

The register helpers cover the common sensor convention: write the register address, then read with a repeated start.

I2cDevice bme280 = Hardware.getInstance().openI2c(1, 0x76);
int chipId = bme280.readRegister(0xd0);   // 0x60 on a BME280
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Releases the device.
    int
    The 7-bit device address.
    int
    The bus number.
    byte[]
    read(int len)
    Reads len bytes in one transaction.
    int
    readRegister(int register)
    Reads one 8-bit register.
    byte[]
    readRegisters(int register, int len)
    Reads len consecutive registers starting at register.
    void
    write(byte[] data)
    Writes data in one transaction.
    byte[]
    writeRead(byte[] write, int readLen)
    Writes write, then reads readLen bytes after a repeated start, as a single transaction with no stop condition between them.
    void
    writeRegister(int register, int value)
    Writes one 8-bit register.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getBus

      public int getBus()
      The bus number.
    • getAddress

      public int getAddress()
      The 7-bit device address.
    • write

      public void write(byte[] data) throws HardwareException

      Writes data in one transaction.

      Throws
      • HardwareException: when the device does not acknowledge
      Throws:
      HardwareException
    • read

      public byte[] read(int len) throws HardwareException

      Reads len bytes in one transaction.

      Throws
      • HardwareException: when the device does not acknowledge
      Throws:
      HardwareException
    • writeRead

      public byte[] writeRead(byte[] write, int readLen) throws HardwareException

      Writes write, then reads readLen bytes after a repeated start, as a single transaction with no stop condition between them.

      Throws
      • HardwareException: when the device does not acknowledge
      Throws:
      HardwareException
    • readRegister

      public int readRegister(int register) throws HardwareException

      Reads one 8-bit register.

      Throws
      • HardwareException: when the device does not acknowledge
      Throws:
      HardwareException
    • readRegisters

      public byte[] readRegisters(int register, int len) throws HardwareException

      Reads len consecutive registers starting at register.

      Throws
      • HardwareException: when the device does not acknowledge
      Throws:
      HardwareException
    • writeRegister

      public void writeRegister(int register, int value) throws HardwareException

      Writes one 8-bit register.

      Throws
      • HardwareException: when the device does not acknowledge
      Throws:
      HardwareException
    • close

      public void close()
      Releases the device. Closing twice is harmless.