See: Description
| Interface | Description |
|---|---|
| I2CDevice |
The
I2CDevice interface provides methods for sending and receiving data to/from an I2C slave device. |
| Class | Description |
|---|---|
| I2CDeviceConfig |
The
I2CDeviceConfig class encapsulates the hardware addressing information, and static and dynamic
configuration parameters of an I2C slave device. |
I2CDevice instance for the I2C slave device the application wants to exchange
data with, using its numerical ID, name, type (interface) and/or properties:
I2CDevice slave = (I2CDevice) PeripheralManager.open(3);
I2CDevice slave = (I2CDevice) PeripheralManager.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);
Peripheral.close() method to release I2C slave device. The following samples code give 2 examples of using the I2C API to communicate with an I2C slave device:slave.close();
public static final String LED_SLAVE_NAME = "LED_CONTROLLER";
public static final byte[] LED_STOP_COMMAND = null;
public static final byte[] LED_OFF_COMMAND = null;
public static final byte[] LED_ON_COMMAND = null;
public static int LED_LOOP_COUNT = 10;
public static long LED_BLINK_TIME = 1500;
I2CDevice slave = null;
try {
slave = (I2CDevice) PeripheralManager.open(LED_SLAVE_NAME, I2CDevice.class, (String[]) null);
// Clear all status of the 'LED' slave device
slave.write(LED_STOP_COMMAND, 0, LED_STOP_COMMAND.length);
slave.write(LED_OFF_COMMAND, 0, LED_OFF_COMMAND.length);
for (int i = 0; i < LED_LOOP_COUNT; i++) {
// turning 'LED' on and keeping it on for 1500ms
slave.write(LED_ON_COMMAND, 0, LED_ON_COMMAND.length);
try {
Thread.sleep(LED_BLINK_TIME);
} catch (InterruptedException ex) {
}
// turning 'LED' off keeping it off for 1500ms
slave.write(LED_OFF_COMMAND, 0, LED_OFF_COMMAND.length);
try {
Thread.sleep(LED_BLINK_TIME);
} catch (InterruptedException ex) {
}
}
} catch (IOException ex) {
// Handle exception
} catch (PeripheralException ex) {
// Handle exception
} finally {
if (slave != null) {
try {
slave.close();
} catch (IOException ex) {
}
}
}
Or,
I2CDevice slave = null;
try {
slave = (I2CDevice) PeripheralManager.open("EEPROM", I2CDevice.class, (String[]) null);
byte[] addr = new byte[]{/ * Somme address * /};
byte[] data = new byte[4];
try {
slave.begin();
slave.write(addr, 0, 2); // Writes the address
int count = slave.read(data, 0, 1); // Read the data at that EEPROM address
} finally {
slave.end();
}
} finally {
if (slave != null) {
slave.close();
}
}
Information about the I2C-bus specification can be found at http://www.nxp.com/documents/user_manual/UM10204.pdf.
com.oracle.deviceaccess.PeripheralManager.open methods. The "com.oracle.deviceaccess.i2c" permission allows
access to be granted to I2C (slave) devices as a whole.Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved. Use of this specification is subject to license terms.