Device I/O API
Proposal for Java ME 8

Package jdk.dio.gpio

Interfaces and classes for reading and writing from/to GPIO (General Purpose Input Output) pins and ports of the device.

See: Description

Package jdk.dio.gpio Description

Interfaces and classes for reading and writing from/to GPIO (General Purpose Input Output) pins and ports of the device.

A GPIO port is a platform-defined grouping of GPIO pins that may be configured for output or input. Note that GPIO pins that are part of a GPIO port cannot be retrieved nor controlled individually as GPIOPin instances.

In order to use a specific pin or port, an application should first open and obtain and obtain a GPIOPin instance or GPIOPort instance, respectively, for the pin or port it wants to use using its numerical ID, name, type (interface) and/or properties:

Once a pin opened, an application can obtain the current value of a GPIO pin by calling the GPIOPin.getValue method and set its value by calling the GPIOPin.setValue method.
Once a port opened, an application can obtain the current value of a GPIO port by calling the GPIOPort.getValue method and set its value by calling the GPIOPort.setValue method.
 pin.setValue(true);
 port.setValue(0xFF);
 
When done, the application should call the GPIOPin.close or GPIOPort.close method to close the pin or port, respectively.
 pin.close();
 port.close();
 
The following sample code gives an example of using the GPIO API. It shows how to control GPIO Pins. It registers a pin listener for the GPIO input pin a switch button is attached to. When the button is pressed the listener is notified to turn the LED on or off by setting accordingly the GPIO output pin the LED is attached to.
 try (GPIOPin switchPin = (GPIOPin) DeviceManager.open(1); GPIOPin ledPin = (GPIOPin) DeviceManager.open(3)) {
     switchPin.setInputListener(new PinListener() {
 
         public void valueChanged(PinEvent event) {
             try {
                 ledPin.setValue(event.getValue()); // turn LED on or off
             } catch (IOException ioe) {
                 // handle exception
             }
         }
     });
     // perform some other computation
 } catch (IOException ioe) {
     // handle exception
 }
 
Note that the preceding example is using a try-with-resources statement and that the GPIOPin.close method is automatically invoked by the platform at the end of the statement.

Note that the underlying platform configuration may allow for some GPIO pins or ports to be set by an application for either output or input while others may be used for input only or output only and that their direction can not be changed by an application. Note also that asynchronous notification of pin or port value changes is only loosely tied to hardware-level interrupt requests. The platform does not guarantee notification in a deterministic/timely manner.

Because of performance issue, procedures handling GPIO pins, and especially event listeners, should be implemented to be as fast as possible.

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