Package com.pi4j.io.i2c.impl
Class I2CBusImpl
- java.lang.Object
-
- com.pi4j.io.i2c.impl.I2CBusImpl
-
- All Implemented Interfaces:
I2CBus
public class I2CBusImpl extends Object implements I2CBus
This is implementation of i2c bus. This class keeps underlying linux file descriptor of particular bus. As all reads and writes from/to i2c bus are blocked I/Os current implementation uses only one file per bus for all devices. Device implementations use this class file handle. Hint: For concurrency-locking the methods lock() and unlock() are provided. This requires that there is exactly one I2CBus-instance per bus-number what is guaranteed by the I2CFactory class. The locking is done by I2CDeviceImpl by using those methods. The reason for this is to enable other locking-strategies than the simple "lock before and release after access"-strategy.- Author:
- Daniel Sendula, refactored by RasPelikan
-
-
Field Summary
Fields Modifier and Type Field Description protected int
busNumber
Used to identifiy the i2c bus within Pi4Jprotected LinuxFile
file
File handle for this i2c busprotected String
filename
File name of this i2c busprotected int
lastAddress
protected long
lockAquireTimeout
protected TimeUnit
lockAquireTimeoutUnit
-
Constructor Summary
Constructors Modifier Constructor Description protected
I2CBusImpl(int busNumber, String fileName, long lockAquireTimeout, TimeUnit lockAquireTimeoutUnit)
Constructor of i2c bus implementation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this i2c bus.int
getBusNumber()
I2CDevice
getDevice(int address)
Returns i2c device implementation (I2CDeviceImpl
).void
ioctl(I2CDevice device, long command, int value)
void
ioctl(I2CDevice device, long command, ByteBuffer values, IntBuffer offsets)
protected void
open()
Opens the bus.int
readByte(I2CDevice device, int localAddress)
int
readByteDirect(I2CDevice device)
int
readBytes(I2CDevice device, int localAddress, int size, int offset, byte[] buffer)
int
readBytesDirect(I2CDevice device, int size, int offset, byte[] buffer)
<T> T
runBusLockedDeviceAction(I2CDevice device, Callable<T> action)
Selects a device on the bus for an action, and locks parallel access around file descriptor operations.protected void
selectBusSlave(I2CDevice device)
Selects the slave device if not already selected on this bus.protected void
testForProperOperationConditions(I2CDevice device)
String
toString()
int
writeAndReadBytesDirect(I2CDevice device, int writeSize, int writeOffset, byte[] writeBuffer, int readSize, int readOffset, byte[] readBuffer)
void
writeByte(I2CDevice device, int localAddress, byte data)
void
writeByteDirect(I2CDevice device, byte data)
void
writeBytes(I2CDevice device, int localAddress, int size, int offset, byte[] buffer)
void
writeBytesDirect(I2CDevice device, int size, int offset, byte[] buffer)
-
-
-
Field Detail
-
file
protected LinuxFile file
File handle for this i2c bus
-
lastAddress
protected int lastAddress
-
filename
protected String filename
File name of this i2c bus
-
busNumber
protected int busNumber
Used to identifiy the i2c bus within Pi4J
-
lockAquireTimeout
protected long lockAquireTimeout
-
lockAquireTimeoutUnit
protected TimeUnit lockAquireTimeoutUnit
-
-
Constructor Detail
-
I2CBusImpl
protected I2CBusImpl(int busNumber, String fileName, long lockAquireTimeout, TimeUnit lockAquireTimeoutUnit)
Constructor of i2c bus implementation.- Parameters:
busNumber
- used to identifiy the i2c bus within Pi4J- Throws:
IOException
- thrown in case that file cannot be opened
-
-
Method Detail
-
getDevice
public I2CDevice getDevice(int address) throws IOException
Returns i2c device implementation (I2CDeviceImpl
).- Specified by:
getDevice
in interfaceI2CBus
- Parameters:
address
- address of i2c device (7-bit address only: 0-127)- Returns:
- implementation of i2c device with given address
- Throws:
IOException
- never in this implementation
-
open
protected void open() throws IOException
Opens the bus.- Throws:
IOException
- thrown in case there are problems opening the i2c bus.
-
close
public void close() throws IOException
Closes this i2c bus. Can be used in a thread safe way during bus operations.- Specified by:
close
in interfaceI2CBus
- Throws:
IOException
- never in this implementation
-
readByteDirect
public int readByteDirect(I2CDevice device) throws IOException
- Throws:
IOException
-
readBytesDirect
public int readBytesDirect(I2CDevice device, int size, int offset, byte[] buffer) throws IOException
- Throws:
IOException
-
readByte
public int readByte(I2CDevice device, int localAddress) throws IOException
- Throws:
IOException
-
readBytes
public int readBytes(I2CDevice device, int localAddress, int size, int offset, byte[] buffer) throws IOException
- Throws:
IOException
-
writeByteDirect
public void writeByteDirect(I2CDevice device, byte data) throws IOException
- Throws:
IOException
-
writeBytesDirect
public void writeBytesDirect(I2CDevice device, int size, int offset, byte[] buffer) throws IOException
- Throws:
IOException
-
writeByte
public void writeByte(I2CDevice device, int localAddress, byte data) throws IOException
- Throws:
IOException
-
writeBytes
public void writeBytes(I2CDevice device, int localAddress, int size, int offset, byte[] buffer) throws IOException
- Throws:
IOException
-
writeAndReadBytesDirect
public int writeAndReadBytesDirect(I2CDevice device, int writeSize, int writeOffset, byte[] writeBuffer, int readSize, int readOffset, byte[] readBuffer) throws IOException
- Throws:
IOException
-
ioctl
public void ioctl(I2CDevice device, long command, int value) throws IOException
- Throws:
IOException
-
ioctl
public void ioctl(I2CDevice device, long command, ByteBuffer values, IntBuffer offsets) throws IOException
- Throws:
IOException
-
runBusLockedDeviceAction
public <T> T runBusLockedDeviceAction(I2CDevice device, Callable<T> action) throws IOException
Selects a device on the bus for an action, and locks parallel access around file descriptor operations. Multiple bus instances may be used in parallel, but a single bus instance must limit parallel access.The timeout used for the acquisition of the lock may be defined on getting the I2CBus from I2CFactory.
The 'run'-method of 'action' may throw an 'IOExceptionWrapperException' to wrap IOExceptions. The wrapped IOException is unwrapped by this method and rethrown as IOException.
- Type Parameters:
T
- The result-type of the method- Parameters:
device
- Device to be selected on the busaction
- The action to be run- Throws:
RuntimeException
- thrown by the custom codeIOException
- see method description above- See Also:
I2CFactory.getInstance(int, long, java.util.concurrent.TimeUnit)
-
selectBusSlave
protected void selectBusSlave(I2CDevice device) throws IOException
Selects the slave device if not already selected on this bus. Uses SharedSecrets to get the POSIX file descriptor, and runs the required ioctl's via JNI.- Parameters:
device
- Device to select- Throws:
IOException
-
testForProperOperationConditions
protected void testForProperOperationConditions(I2CDevice device) throws IOException
- Throws:
IOException
-
getBusNumber
public int getBusNumber()
- Specified by:
getBusNumber
in interfaceI2CBus
- Returns:
- The bus' number
-
-