Package com.pi4j.io.i2c.impl
Class I2CDeviceImpl
- java.lang.Object
-
- com.pi4j.io.i2c.impl.I2CDeviceImpl
-
- All Implemented Interfaces:
I2CDevice
public class I2CDeviceImpl extends Object implements I2CDevice
Implementation of i2c device. This class only holds reference to i2c bus (so it can use its handle) and device address.- Author:
- Daniel Sendula, refactored by RasPelikan
-
-
Constructor Summary
Constructors Constructor Description I2CDeviceImpl(I2CBusImpl bus, int address)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getAddress()
void
ioctl(long command, int value)
Runs an ioctl on this device.void
ioctl(long command, ByteBuffer data, IntBuffer offsets)
Runs an ioctl on this device.protected String
makeDescription()
This helper method creates a string describing bus file name and device address (in hex).protected String
makeDescription(int address)
This helper method creates a string describing bus file name, device address (in hex) and local i2c address.int
read()
This method reads one byte from the i2c device.int
read(byte[] data, int offset, int size)
This method reads bytes from the i2c device to given buffer at asked offset.int
read(byte[] writeData, int writeOffset, int writeSize, byte[] readData, int readOffset, int readSize)
This method writes and reads bytes to/from the i2c device in a single method callint
read(int address)
This method reads one byte from the i2c device.int
read(int address, byte[] data, int offset, int size)
This method reads bytes from the i2c device to given buffer at asked offset.void
write(byte data)
This method writes one byte to i2c device.void
write(byte[] buffer)
This method writes all bytes included in the given buffer directly to the i2c device.void
write(byte[] data, int offset, int size)
This method writes several bytes to the i2c device from given buffer at given offset.void
write(int address, byte data)
This method writes one byte to i2c device.void
write(int address, byte[] buffer)
This method writes all bytes included in the given buffer directory to the register address on the i2c devicevoid
write(int address, byte[] data, int offset, int size)
This method writes several bytes to the i2c device from given buffer at given offset.
-
-
-
Constructor Detail
-
I2CDeviceImpl
public I2CDeviceImpl(I2CBusImpl bus, int address)
Constructor.- Parameters:
bus
- i2c busaddress
- i2c device address
-
-
Method Detail
-
getAddress
public int getAddress()
- Specified by:
getAddress
in interfaceI2CDevice
- Returns:
- The address for which this instance is constructed for.
-
write
public void write(byte data) throws IOException
This method writes one byte to i2c device.- Specified by:
write
in interfaceI2CDevice
- Parameters:
data
- byte to be written- Throws:
IOException
- thrown in case byte cannot be written to the i2c device or i2c bus
-
write
public void write(byte[] data, int offset, int size) throws IOException
This method writes several bytes to the i2c device from given buffer at given offset.- Specified by:
write
in interfaceI2CDevice
- Parameters:
data
- buffer of data to be written to the i2c device in one gooffset
- offset in buffersize
- number of bytes to be written- Throws:
IOException
- thrown in case byte cannot be written to the i2c device or i2c bus
-
write
public void write(byte[] buffer) throws IOException
This method writes all bytes included in the given buffer directly to the i2c device.- Specified by:
write
in interfaceI2CDevice
- Parameters:
buffer
- buffer of data to be written to the i2c device in one go- Throws:
IOException
- thrown in case byte cannot be written to the i2c device or i2c bus
-
write
public void write(int address, byte data) throws IOException
This method writes one byte to i2c device.- Specified by:
write
in interfaceI2CDevice
- Parameters:
address
- local address in the i2c devicedata
- byte to be written- Throws:
IOException
- thrown in case byte cannot be written to the i2c device or i2c bus
-
write
public void write(int address, byte[] data, int offset, int size) throws IOException
This method writes several bytes to the i2c device from given buffer at given offset.- Specified by:
write
in interfaceI2CDevice
- Parameters:
address
- local address in the i2c devicedata
- buffer of data to be written to the i2c device in one gooffset
- offset in buffersize
- number of bytes to be written- Throws:
IOException
- thrown in case byte cannot be written to the i2c device or i2c bus
-
write
public void write(int address, byte[] buffer) throws IOException
This method writes all bytes included in the given buffer directory to the register address on the i2c device- Specified by:
write
in interfaceI2CDevice
- Parameters:
address
- local address in the i2c devicebuffer
- buffer of data to be written to the i2c device in one go- Throws:
IOException
- thrown in case byte cannot be written to the i2c device or i2c bus
-
read
public int read() throws IOException
This method reads one byte from the i2c device. Result is between 0 and 255 if read operation was successful, else a negative number for an error.- Specified by:
read
in interfaceI2CDevice
- Returns:
- byte value read: positive number (or zero) to 255 if read was successful. Negative number if reading failed.
- Throws:
IOException
- thrown in case byte cannot be read from the i2c device or i2c bus
-
read
public int read(byte[] data, int offset, int size) throws IOException
This method reads bytes from the i2c device to given buffer at asked offset.
Note: Current implementation calls
read(int)
. That means for each read byte i2c bus will send (next) address to i2c device.- Specified by:
read
in interfaceI2CDevice
- Parameters:
data
- buffer of data to be read from the i2c device in one gooffset
- offset in buffersize
- number of bytes to be read- Returns:
- number of bytes read
- Throws:
IOException
- thrown in case byte cannot be read from the i2c device or i2c bus
-
read
public int read(int address) throws IOException
This method reads one byte from the i2c device. Result is between 0 and 255 if read operation was successful, else a negative number for an error.- Specified by:
read
in interfaceI2CDevice
- Parameters:
address
- local address in the i2c device- Returns:
- byte value read: positive number (or zero) to 255 if read was successful. Negative number if reading failed.
- Throws:
IOException
- thrown in case byte cannot be read from the i2c device or i2c bus
-
read
public int read(int address, byte[] data, int offset, int size) throws IOException
This method reads bytes from the i2c device to given buffer at asked offset.
Note: Current implementation calls
read(int)
. That means for each read byte i2c bus will send (next) address to i2c device.- Specified by:
read
in interfaceI2CDevice
- Parameters:
address
- local address in the i2c devicedata
- buffer of data to be read from the i2c device in one gooffset
- offset in buffersize
- number of bytes to be read- Returns:
- number of bytes read
- Throws:
IOException
- thrown in case byte cannot be read from the i2c device or i2c bus
-
ioctl
public void ioctl(long command, int value) throws IOException
Description copied from interface:I2CDevice
Runs an ioctl on this device.- Specified by:
ioctl
in interfaceI2CDevice
- Throws:
IOException
- See Also:
LinuxFile.ioctl(long, int)
-
ioctl
public void ioctl(long command, ByteBuffer data, IntBuffer offsets) throws IOException
Description copied from interface:I2CDevice
Runs an ioctl on this device.- Specified by:
ioctl
in interfaceI2CDevice
- Throws:
IOException
- See Also:
LinuxFile.ioctl(long, ByteBuffer, IntBuffer)
-
read
public int read(byte[] writeData, int writeOffset, int writeSize, byte[] readData, int readOffset, int readSize) throws IOException
This method writes and reads bytes to/from the i2c device in a single method call- Specified by:
read
in interfaceI2CDevice
- Parameters:
writeData
- buffer of data to be written to the i2c device in one gowriteOffset
- offset in write bufferwriteSize
- number of bytes to be written from bufferreadData
- buffer of data to be read from the i2c device in one goreadOffset
- offset in read bufferreadSize
- number of bytes to be read- Returns:
- number of bytes read
- Throws:
IOException
- thrown in case byte cannot be read from the i2c device or i2c bus
-
makeDescription
protected String makeDescription()
This helper method creates a string describing bus file name and device address (in hex).- Returns:
- string with all details
-
makeDescription
protected String makeDescription(int address)
This helper method creates a string describing bus file name, device address (in hex) and local i2c address.- Parameters:
address
- local address in i2c device- Returns:
- string with all details
-
-