public class I2C
extends java.lang.Object
I2C
object lets you
access physical module I2C interface to write and read from it.
Objects of this class are immutable. Operations on this class are performed on the underlying I2C-dev interface resulting in actions on the final hardware.
To obtain an instance of the I2C interface you have to use the
I2CManager.createI2C(int)
method.
I2CManager
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Attempts to close the I2C interface.
|
protected void |
finalize() |
|
int |
getInterfaceNumber() |
Retrieves the I2C interface number.
|
boolean |
isInterfaceOpen() |
Retrieves whether interface is open or not.
|
void |
open() |
Opens the I2C interface.
|
byte[] |
read(int deviceAddress,
int numBytes) |
Reads the given amount of bytes from the given slave device address.
|
byte |
readByte(int deviceAddress) |
Reads one byte from the given slave device address.
|
byte[] |
transfer(int deviceAddress,
byte[] data,
int numBytes) |
Simultaneously writes the given byte array in the given slave device
address and reads the given amount of bytes.
|
byte[] |
transfer(int deviceAddress,
byte data,
int numBytes) |
Simultaneously writes the given byte in the given slave device address
and reads the given amount of bytes.
|
void |
write(int deviceAddress,
byte wByte) |
Writes the given byte in the given slave device address.
|
void |
write(int deviceAddress,
byte[] data) |
Writes the given byte array in the given slave device address.
|
public void open() throws NoSuchInterfaceException, java.io.IOException
It does nothing if the interface is already open.
java.io.IOException
- If there is an error opening the I2C interface.NoSuchInterfaceException
- If the given interface does not exist.close()
,
isInterfaceOpen()
public void close() throws java.io.IOException
java.io.IOException
isInterfaceOpen()
,
open()
public boolean isInterfaceOpen()
public int getInterfaceNumber()
public byte readByte(int deviceAddress) throws java.io.IOException
deviceAddress
- Slave device address to read one byte from.java.lang.IllegalArgumentException
- If deviceAddress < 0
.java.io.IOException
- If the interface is closed or an error occurs while
reading data from the interface.read(int, int)
public byte[] read(int deviceAddress, int numBytes) throws java.io.IOException
deviceAddress
- Slave device address to read bytes from.numBytes
- Amount of bytes to read.null
if error.java.lang.IllegalArgumentException
- If deviceAddress < 0
or
if numBytes < 1
.java.io.IOException
- If the interface is closed or an error occurs while
reading data from the interface.readByte(int)
public void write(int deviceAddress, byte wByte) throws java.io.IOException
deviceAddress
- Slave device address to write the byte to.wByte
- Byte to write.java.lang.IllegalArgumentException
- If deviceAddress < 0
.java.io.IOException
- If the interface is closed or an error occurs while
writing data to the interface.write(int, byte[])
public void write(int deviceAddress, byte[] data) throws java.io.IOException
deviceAddress
- Slave device address to write the byte array to.data
- Byte array to write.java.lang.IllegalArgumentException
- If deviceAddress < 0
.java.io.IOException
- If the interface is closed or an error occurs while
writing data to the interface.java.lang.NullPointerException
- If data == null
.write(int, byte)
public byte[] transfer(int deviceAddress, byte data, int numBytes) throws java.io.IOException
deviceAddress
- Slave device address to write the byte to and read
from.data
- Byte to write.numBytes
- Number of bytes to read after write.null
if error.java.lang.IllegalArgumentException
- If deviceAddress < 0
or
if numBytes < 1
.java.io.IOException
- If the interface is closed or an error occurs while
writing or reading data from the interface.transfer(int, byte[], int)
public byte[] transfer(int deviceAddress, byte[] data, int numBytes) throws java.io.IOException
deviceAddress
- Slave device address to write bytes to and read
from.data
- Byte array to write.numBytes
- Number of bytes to read after write.null
if error.java.lang.IllegalArgumentException
- If deviceAddress < 0
or
if numBytes < 1
.java.io.IOException
- If the interface is closed or an error occurs while
writing or reading data from the interface.java.lang.NullPointerException
- If data == null
.transfer(int, byte, int)
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable