Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public final class I2cDevice
- Object
- I2cDevice
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
Methods
public int getBus() | The bus number. |
public int getAddress() | The 7-bit device address. |
public void write(byte[] data)
throws HardwareException | Writes data in one transaction. |
public byte[] read(int len)
throws HardwareException | Reads len bytes in one transaction. |
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. |
public int readRegister(int register)
throws HardwareException | Reads one 8-bit register. |
public byte[] readRegisters(int register, int len)
throws HardwareException | Reads len consecutive registers starting at register. |
public void writeRegister(int register, int value)
throws HardwareException | Writes one 8-bit register. |
public void close() | Releases the device. |
Inherited methods
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 HardwareExceptionWrites
data in one transaction.Throws
HardwareException- when the device does not acknowledge
read
public byte[] read(int len)
throws HardwareExceptionReads
len bytes in one transaction.Throws
HardwareException- when the device does not acknowledge
writeRead
public byte[] writeRead(byte[] write, int readLen)
throws HardwareExceptionWrites
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
readRegister
public int readRegister(int register)
throws HardwareExceptionReads one 8-bit register.
Throws
HardwareException- when the device does not acknowledge
readRegisters
public byte[] readRegisters(int register, int len)
throws HardwareExceptionReads
len consecutive registers starting at register.Throws
HardwareException- when the device does not acknowledge
writeRegister
public void writeRegister(int register, int value)
throws HardwareExceptionWrites one 8-bit register.
Throws
HardwareException- when the device does not acknowledge
close
public void close()Releases the device. Closing twice is harmless.