Device I/O API
Proposal for Java ME 8

Package jdk.dio.generic

Interfaces and classes for controlling devices using generic I/O operations.

See: Description

Package jdk.dio.generic Description

Interfaces and classes for controlling devices using generic I/O operations.

The generic device API allows for accessing devices when there are no more specific standard Java API such as I2CDevice, SPIDevice, GPIOPin or GPIOPort...

This API offers 2 main interfaces:

GenericDevice
Device control operations and event listener registration. A device may implements this sole interface if it does not support any read and write operations.
GenericBufferIODevice
Device control operations and event listener registration as inherited from GenericBufferIODevice and byte buffer read and write operations.
In order to access a device using its generic interface, an application should first open and obtain a GenericDevice instance for the device using its numerical ID, name, type (interface) and/or properties:
Using its ID
 GenericDevice device = (GenericDevice) DeviceManager.open(17);
 
Using its name and interface
 GeneriBufferIODevice device = DeviceManager.open("STORAGE", GeneriBufferIODevice.class, null);
 
Once the device opened, the application can set and get controls, read and write data using methods of the GenericDevice or GenericBufferIODevice interfaces.
 device.read(buffer, 0, buffer.length);
 
When done, the application should call the GenericDevice.close method to close the Generic device.
 device.close();
 
The following sample codes give examples of using the Generic API to communicate Real Time Clock device:
 public static final int EVT_ALARM = 0;
 public static final GenericDeviceControl<Byte> SECONDS = new GenericDeviceControl<>(0, Byte.class);
 public static final GenericDeviceControl<Byte> SEC_ALARM = new GenericDeviceControl<>(1, Byte.class);
 public static final GenericDeviceControl<Byte> MINUTES = new GenericDeviceControl<>(2, Byte.class);
 public static final GenericDeviceControl<Byte> MIN_ALARM = new GenericDeviceControl<>(3, Byte.class);
 public static final GenericDeviceControl<Byte> HR_ALARM = new GenericDeviceControl<>(4, Byte.class);
 public static final GenericDeviceControl<Byte> HOURS = new GenericDeviceControl<>(5, Byte.class);
 public static final GenericDeviceControl<Boolean> ALARM_ENABLED = new GenericDeviceControl<>(6, Boolean.class);
 
 // Sets the daily alarm for after some delay
 public void setAlarm(byte delaySeconds, byte delayMinutes, byte delayHours) throws IOException, DeviceException {
     try (GenericDevice rtc = DeviceManager.open("RTC", GenericDevice.class, (String) null)) {
         byte currentSeconds = rtc.getControl(SECONDS);
         byte currentMinutes = rtc.getControl(MINUTES);
         byte currentHours = rtc.getControl(HOURS);
         byte i = (byte) ((currentSeconds + delaySeconds) % 60);
         byte j = (byte) ((currentSeconds + delaySeconds) / 60);
         rtc.setControl(SEC_ALARM, i);
         i = (byte) ((currentMinutes + delayMinutes + j) % 60);
         j = (byte) ((currentMinutes + delayMinutes + j) / 60);
         rtc.setControl(MIN_ALARM, i);
         i = (byte) ((currentHours + delayHours + j) % 24);
         rtc.setControl(HR_ALARM, i);
 
         rtc.setEventListener(EVT_ALARM, new GenericEventListener() {
 
             public void eventDispatched(GenericEvent event) {
                 GenericDevice rtc = event.getDevice();
                 // Notify application of alarm
             }
         });
         // Enable alarm.
         rtc.setControl(ALARM_ENABLED, true);
     }
 }
 
Note that the preceding example is using a try-with-resources statement and that the GenericDevice.close method is automatically invoked by the platform at the end of the statement.
 public static final int EVT_VOLUME_CHANGED = 0;
 public static final GenericDeviceControl<Float> MIC_VOLUME = new GenericDeviceControl<>(0, Float.class);
 public static final GenericDeviceControl<Float> MIC_SAMPLE_RATE = new GenericDeviceControl<>(1, Float.class);
 public static final GenericDeviceControl<Boolean> MIC_AUTOMATIC_GAIN = new GenericDeviceControl<>(2, Boolean.class);
 public static final GenericDeviceControl<Boolean> MIC_MUTE = new GenericDeviceControl<>(3, Boolean.class);
 
 public void audioCapture(ByteBuffer buffer, float sampleRate, boolean agc) throws IOException, DeviceException {
     try (GenericBufferIODevice mic = DeviceManager.open("MICROPHONE", GenericBufferIODevice.class, (String) null)) {
         mic.setControl(MIC_SAMPLE_RATE, sampleRate);
         mic.setControl(MIC_AUTOMATIC_GAIN, agc);
         mic.setControl(MIC_MUTE, false);
 
         mic.setEventListener(EVT_VOLUME_CHANGED, new GenericEventListener() {
 
             public void eventDispatched(GenericEvent event) {
                 GenericDevice mic = event.getDevice();
                 try {
                     float currentVolume = mic.getControl(MIC_VOLUME);
                     // ...
                 } catch (ClosedDeviceException ex) {
                     ex.printStackTrace();
                 } catch (IOException ex) {
                     ex.printStackTrace();
                 }
             }
         });
         mic.read(buffer);
     }
 }
 

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