Class 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 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 interface I2CBus
        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 interface I2CBus
        Throws:
        IOException - never in this implementation
      • 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
      • 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 bus
        action - The action to be run
        Throws:
        RuntimeException - thrown by the custom code
        IOException - 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
      • getBusNumber

        public int getBusNumber()
        Specified by:
        getBusNumber in interface I2CBus
        Returns:
        The bus' number