Class I2cDevice
java.lang.Object
com.codename1.hardware.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
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases the device.intThe 7-bit device address.intgetBus()The bus number.byte[]read(int len) Readslenbytes in one transaction.intreadRegister(int register) Reads one 8-bit register.byte[]readRegisters(int register, int len) Readslenconsecutive registers starting atregister.voidwrite(byte[] data) Writesdatain one transaction.byte[]writeRead(byte[] write, int readLen) Writeswrite, then readsreadLenbytes after a repeated start, as a single transaction with no stop condition between them.voidwriteRegister(int register, int value) Writes one 8-bit register.
-
Method Details
-
getBus
public int getBus()The bus number. -
getAddress
public int getAddress()The 7-bit device address. -
write
Writes
datain one transaction.Throws
HardwareException: when the device does not acknowledge
- Throws:
HardwareException
-
read
Reads
lenbytes in one transaction.Throws
HardwareException: when the device does not acknowledge
- Throws:
HardwareException
-
writeRead
Writes
write, then readsreadLenbytes 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
Reads one 8-bit register.
Throws
HardwareException: when the device does not acknowledge
- Throws:
HardwareException
-
readRegisters
Reads
lenconsecutive registers starting atregister.Throws
HardwareException: when the device does not acknowledge
- Throws:
HardwareException
-
writeRegister
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.
-