public class I2CBus extends Object implements AutoCloseable
It provides 3 sets of operations for doing IO :
read and
write.transactions (ie. uninterrupted reads and writes)smbus field.
SMBUS operations are a subset of what I2C can achieve but propose common interactions. However,
getFunctionnalities should be used in order to understand what operations the underlying
driver support.
I2C is a standard but your master device and the slaves devices it talks to probably do not support everything properly. For instance, as is, the Raspberry Pi only supports a few SMBUS operations and do not support transactions involving more than one message at a time (which defeats the purpose of having them in the first place). Furthermore, talking to an Arduino will result in timing issues if it is too slow to answer. Communication between those two is very much possible but requires more preparation and testing. This is an example of how things are not always that simple.
In this API, `command` is just another word for what is sometimes called `register`. Essentially, it means that before reading or writing bytes, the master writes a byte specifying a command, something to do with the following byte(s). If an SMBUS functionality is not available, this can often be emulated by explicitly sending the command byte and then reading or writing as needed.
| Modifier and Type | Field and Description |
|---|---|
SMBus |
smbus
For executing SMBus operations using this I2C bus.
|
| Constructor and Description |
|---|
I2CBus(int busNumber)
Opens an I2C bus by number, `/dev/i2c-$busNumber`.
|
I2CBus(String path)
Opens an I2C bus on the given path.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this bus.
|
void |
doTransaction(I2CTransaction transaction)
Do an uninterrupted transaction of several messages.
|
I2CFunctionalities |
getFunctionalities()
Finds out what this I2C bus can do.
|
void |
read(I2CBuffer buffer)
Directly reads bytes from the slave device (length is specified by the buffer).
|
void |
read(I2CBuffer buffer,
int length)
Directly reads length bytes from the slave device.
|
void |
selectSlave(int address)
Selects an available slave device using a regular 7-bit address.
|
void |
selectSlave(int address,
boolean force,
boolean isTenBit)
Selects an available slave device.
|
void |
setRetries(int nRetries)
Sets the number of time a slave should be polled when not acknowledging.
|
void |
setTimeout(int milliseconds)
Sets the timeout in milliseconds.
|
void |
write(I2CBuffer buffer)
Directly writes bytes to the slave device (length is specified by the buffer).
|
void |
write(I2CBuffer buffer,
int length)
Directly writes length bytes to the slave device.
|
public final SMBus smbus
public I2CBus(int busNumber)
throws IOException
busNumber - Number of the bus.IOException - When an unplanned error occured.public I2CBus(String path) throws IOException
path - Path to the bus such as `/dev/i2c-1`.FileNotFoundException - When the bus is not found or the user does not have permissions.IOException - When an unplanned error occured.public void close()
throws IOException
close in interface AutoCloseableIOException - When an unplanned error occured.public void doTransaction(I2CTransaction transaction) throws IOException
Not all devices support this feature and some only support one message at a time which is not very interesting.
transaction - Transaction that will be carried out.IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.public I2CFunctionalities getFunctionalities() throws IOException
IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.public void selectSlave(int address)
throws IOException
address - 1ddress of the needed slave.IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.selectSlave( int, boolean, boolean )public void selectSlave(int address,
boolean force,
boolean isTenBit)
throws IOException
This needs to be called only once if several IO operations are performed on the same
slave and has no effect on transactions.
address - 1ddress of the needed slave.force - Should the slave be selected even if it is already used somewhere else ?isTenBit - Is the given address in 10-bit resolution ?IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.public void setRetries(int nRetries)
throws IOException
Does not always have an effect depending on the underlying driver.
nRetries - Number of retries.IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.public void setTimeout(int milliseconds)
throws IOException
Does not always have an effect depending on the underlying driver.
milliseconds - Timeout in milliseconds.IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.public void read(I2CBuffer buffer) throws IOException
buffer - Buffer the answer will be written to.IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.read( I2CBuffer, int )public void read(I2CBuffer buffer, int length) throws IOException
buffer - Buffer the answer will be written to.length - Number of bytes requested.IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.public void write(I2CBuffer buffer) throws IOException
buffer - Buffer to write.IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.write( I2CBuffer, int )public void write(I2CBuffer buffer, int length) throws IOException
buffer - Buffer to write.length - Number of bytes.IllegalStateException - When the I2C bus has been closed.IOException - When the bus is not a proper I2C bus or an unplanned error occured.