See: Description
Interface | Description |
---|---|
I2CCombinedMessage |
The
I2CCombinedMessage interface provides methods for constructing a
combined message. |
I2CDevice |
The
I2CDevice interface provides methods for sending and receiving data to/from an I2C
slave device. |
I2CDevice.Bus |
The
Bus interface is a simplified abstraction of an I2C bus
providing methods for creating combined messages. |
Class | Description |
---|---|
I2CDeviceConfig |
The
I2CDeviceConfig class encapsulates the hardware addressing information, and static
and dynamic configuration parameters of an I2C slave device. |
I2CDeviceConfig.Builder |
The
Builder class allows for creating and initializing
I2CDeviceConfig objects. |
The functionalities supported by this API are those of an I2C master.
In order to communicate with a specific slave device, an application should first open and obtain
an I2CDevice
instance for the I2C slave device the
application wants to exchange data with, using its numeric ID, name, type (interface) and/or
properties:
Once the device opened, the application can exchange data with the I2C slave device using methods of the
- Using its ID
I2CDevice slave = (I2CDevice) DeviceManager.open(3);- Using its name and interface
I2CDevice slave = DeviceManager.open("ADC1", I2CDevice.class, null);
I2CDevice
interface such as the
write
method. When the data exchange is over, the application should call theslave.write(sndBuf, 0, 1);
I2CDevice.close
method to close the I2C
slave device. The following sample code gives an example of using the I2C API to communicate with an I2C slave device:slave.close();
try (I2CDevice slave = DeviceManager.open("LED_CONTROLLER", I2CDevice.class, null)) { ByteBuffer stopCmd = ByteBuffer.wrap(LED_STOP_COMMAND); ByteBuffer offCmd = ByteBuffer.wrap(LED_OFF_COMMAND); ByteBuffer onCmd = ByteBuffer.wrap(LED_ON_COMMAND); // Clear all status of the 'LED' slave device slave.write(ByteBuffer.wrap(stopCmd)); slave.write(ByteBuffer.wrap(offCmd)); for (int i = 0; i < LED_LOOP_COUNT; i++) { // turning 'LED' on and keeping it on for 1500ms slave.write(ByteBuffer.wrap(onCmd)); try { Thread.sleep(LED_BLINK_TIME); } catch (InterruptedException ex) { } // turning 'LED' off keeping it off for 1500ms slave.write(ByteBuffer.wrap(offCmd)); try { Thread.sleep(LED_BLINK_TIME); } catch (InterruptedException ex) { } } } catch (IOException ioe) { // handle exception }
The preceding example is using a try-with-resources statement;
the I2CDevice.close
method is
automatically invoked by the platform at the end of the statement.
Information about the I2C-bus specification can be found at http://www.nxp.com/documents/user_manual/UM10204.pdf.
Unless otherwise noted, permission and security checks that may cause
a SecurityException
to be thrown must be performed
in priority to any other checks or operations once performed the checking of the input parameters
from which the permission target names and action lists are retrieved and assembled.
Unless otherwise noted, passing a null
argument to a constructor or method in any class
or interface in this package will cause a NullPointerException
to be thrown.