I2cDevice
public
class
I2cDevice
extends Object
implements
Closeable
java.lang.Object | |
↳ | com.google.android.things.pio.I2cDevice |
Controls an I2C device.
I2C is a bus protocol allowing a master (running Android headless) to control multiple slave
device connected to this bus. Each device must have a unique address to desambiguate the
commands sent.
Some devices use the notion of register (also called commands), allowing you to select a memory
address to read from/write to. The datasheet for your device will explain what each register
is used for.
Opening an I2C device takes ownership of it for the whole system, preventing anyone else from
opening/accessing this device until you call close()
. Forgetting to call close()
will prevent anyone (including the same process/app) from using the device.
Summary
Public methods | |
---|---|
void
|
close()
Close and release the device. |
void
|
read(byte[] buffer, int length)
Read data from the device. |
void
|
readRegBuffer(int reg, byte[] buffer, int length)
Read multiple bytes from a given register. |
byte
|
readRegByte(int reg)
Read a byte from a given register. |
short
|
readRegWord(int reg)
Read a 2-byte word from a given register. |
void
|
write(byte[] buffer, int length)
Write data to the device. |
void
|
writeRegBuffer(int reg, byte[] buffer, int length)
Write a byte array to a given register. |
void
|
writeRegByte(int reg, byte data)
Write a byte to a given register. |
void
|
writeRegWord(int reg, short data)
Write a 2-byte word to a given register. |
Inherited methods | |
---|---|
From
class
java.lang.Object
| |
From
interface
java.io.Closeable
| |
From
interface
java.lang.AutoCloseable
|
Public methods
close
</div>
void close ()
Close and release the device.
Must be called in order to make the device available to other users.</p>
Throws | |
---|---|
IOException |
if the I2cDevice is already closed. |
</div>
read
</div>
void read (byte[] buffer, int length)
Read data from the device.
Parameters | |
---|---|
buffer |
byte :
Buffer to read data into. |
length |
int :
Number of bytes to read, may not be larger than the buffer size. |
Throws | |
---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
See also:
readRegByte(int)
readRegWord(int)
readRegBuffer(int, byte[], int)
write(byte[], int)
</div>
readRegBuffer
</div>
void readRegBuffer (int reg, byte[] buffer, int length)
Read multiple bytes from a given register.
Parameters | |
---|---|
reg |
int :
The register to read from (0x00-0xFF). |
buffer |
byte :
Buffer to read data into. |
length |
int :
Number of bytes to read, may not be larger than the buffer size. |
Throws | |
---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
See also:
read(byte[], int)
readRegByte(int)
readRegWord(int)
writeRegBuffer(int, byte[], int)
</div>
readRegByte
</div>
byte readRegByte (int reg)
Read a byte from a given register.
Parameters | |
---|---|
reg |
int :
The register to read from (0x00-0xFF). |
Returns | |
---|---|
byte |
The value read from the device. |
Throws | |
---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
See also:
read(byte[], int)
readRegWord(int)
readRegBuffer(int, byte[], int)
writeRegByte(int, byte)
</div>
readRegWord
</div>
short readRegWord (int reg)
Read a 2-byte word from a given register.
Parameters | |
---|---|
reg |
int :
The register to read from (0x00-0xFF). |
Returns | |
---|---|
short |
The 2-byte value read from the device. |
Throws | |
---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
See also:
read(byte[], int)
readRegByte(int)
readRegBuffer(int, byte[], int)
writeRegWord(int, short)
</div>
write
</div>
void write (byte[] buffer, int length)
Write data to the device.
Parameters | |
---|---|
buffer |
byte :
Data to write. |
length |
int :
Number of bytes to write, may not be larger than the buffer size. |
Throws | |
---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
See also:
read(byte[], int)
writeRegByte(int, byte)
writeRegWord(int, short)
writeRegBuffer(int, byte[], int)
</div>
writeRegBuffer
</div>
void writeRegBuffer (int reg, byte[] buffer, int length)
Write a byte array to a given register.
Parameters | |
---|---|
reg |
int :
The register to write to (0x00-0xFF). |
buffer |
byte :
Data to write. |
length |
int :
Number of bytes to write, may not be larger than the buffer size. |
Throws | |
---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
See also:
readRegBuffer(int, byte[], int)
write(byte[], int)
writeRegByte(int, byte)
writeRegWord(int, short)
</div>
writeRegByte
</div>
void writeRegByte (int reg, byte data)
Write a byte to a given register.
Parameters | |
---|---|
reg |
int :
The register to write to (0x00-0xFF). |
data |
byte :
Value to write. |
Throws | |
---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
See also:
readRegByte(int)
write(byte[], int)
writeRegWord(int, short)
writeRegBuffer(int, byte[], int)
</div>
writeRegWord
</div>
void writeRegWord (int reg, short data)
Write a 2-byte word to a given register.
Parameters | |
---|---|
reg |
int :
The register to write to (0x00-0xFF). |
data |
short :
Value to write. |
Throws | |
---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
See also:
readRegWord(int)
write(byte[], int)
writeRegByte(int, byte)
writeRegBuffer(int, byte[], int)
</div>
</div>