See: Description
Interface | Description |
---|---|
I2CDevice |
The
I2CDevice interface provides methods for sending and receiving data to/from an I2C slave device. |
Class | Description |
---|---|
I2CCombinedMessage |
The
I2CCombinedMessage class allows for constructing a combined message. |
I2CDeviceConfig |
The
I2CDeviceConfig class encapsulates the hardware addressing information, and static and dynamic
configuration parameters of an I2C slave device. |
I2CPermission |
The
I2CPermission class defines permissions for I2C slave device access. |
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 = 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 close the I2C slave device. The following samples code give 2 examples of using the I2C API to communicate with an I2C slave device:slave.close();
Or,try (I2CDevice slave = PeripheralManager.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 }
Note that the preceding examples are using a try-with-resources statement and that thetry (I2CDevice slave = PeripheralManager.open("EEPROM", I2CDevice.class, (String) null)) { try { byte[] addr = new byte[]{/*Somme 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(); } }
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.Copyright © 2012, 2013, Oracle and/or its affiliates. All rights reserved.
Legal Notices