Device I/O API
Proposal for Java ME 8

Package jdk.dio.i2cbus

Interfaces and classes for I2C (Inter-Integrated Circuit Bus) device access.

See: Description

Package jdk.dio.i2cbus Description

Interfaces and classes for I2C (Inter-Integrated Circuit Bus) device access.

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 numerical ID, name, type (interface) and/or properties:

Using its ID
 I2CDevice slave = (I2CDevice) DeviceManager.open(3);
 
Using its name and interface
 I2CDevice slave = DeviceManager.open("ADC1", I2CDevice.class, null);
 
Once the device opened, the application can exchange data with the I2C slave device using methods of the I2CDevice interface such as the write method.
 slave.write(sndBuf, 0, 1);
 
When the data exchange is over, the application should call the I2CDevice.close method to close the I2C slave device.
 slave.close();
 
The following samples code give 2 examples of using the I2C API to communicate with an I2C slave device:
 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
 }
 
Or,
 try (I2CDevice slave = DeviceManager.open("EEPROM", I2CDevice.class, (String) null)) {
     try {
         byte[] addr = new byte[]{/* Some address * /};
         ByteBuffer data = ByteBuffer.allocateDirect(4);
         slave.begin();
         slave.write(ByteBuffer.wrap(addr)); // Writes the address
         int count = slave.read(data); // Read the data at that EEPROM address
     } finally {
         slave.end();
     }
 }
 
Note that the preceding examples are using a try-with-resources statement and that 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, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown.

Device I/O API
Proposal for Java ME 8