How to Use GPIOs
Overview
GPIO access in ESF is granted through OpenJDK Device I/O, a third-party library that leverages standard Java ME Device I/O APIs to Java SE. ESF is distributed with the relevant native libraries, together with the default hardware configuration, for each platform on which it runs.
Default Configuration
Default hardware configuration for the hardware platform is defined in the jdk.dio.properties file. Standard configuration for complex devices can be added on a per-device basis as shown below.
NOTE: Depending on the size of your display window, you may need to scroll across to the right in order to view the configuration samples in their entirety.
#Default PIN configuration. To be overwritten in the following lines gpio.GPIOPin = initValue:0, deviceNumber:0, direction:3, mode:-1, trigger:3 #Standard PIN configuration 64 = deviceType: gpio.GPIOPin, pinNumber:64, name:RELAY1
APIs
ESF supports the full set of APIs for the listed device types. Refer to References for further information on the APIs.
Code Examples
Accessing a GPIO Pin with OpenJDK Device I/O
A GPIO Pin can be accessed by referencing its index in the properties file, or by creating a Pin configuration object and feeding it to the DeviceManager as shown in the examples below.
Accessing a GPIO Pin by its Index
#Accessing the GPIO Pin number 17. The default behaviour is defined in the #jdk.dio.properties file # #i.e.: # gpio.GPIOPin = initValue:0, deviceNumber:0, direction:3, mode:-1, trigger:3 # 17 = deviceType: gpio.GPIOPin, pinNumber:17, name:GPIO_USER_1 GPIOPin led = (GPIOPin)DeviceManager.open(17); led.setValue(true) //Turns the LED on led.setValue(false) //Turns the LED off boolean status = led.getValue() //true if the LED is on
Accessing a GPIO Pin Using a Device Configuration Object
#Accessing the Pin number 17 with custom configuration GPIOPinConfig pinConfig = new GPIOPinConfig( DeviceConfig.DEFAULT, //GPIO Controller number or name 17, //GPIO Pin number GPIOPinConfig.DIR_INPUT_ONLY, //Pin direction GPIOPinConfig.MODE_INPUT_PULL_DOWN, //Pin resistor GPIOPinConfig.TRIGGER_BOTH_EDGES, //Triggers false //initial value (for outputs) ); GPIOPin button = (GPIOPin) DeviceManager.open(GPIOPin.class, pinConfig); button.setInputListener(new PinListener(){ @Override public void valueChanged(PinEvent event) { System.out.println("PIN Status Changed!"); System.out.println(event.getLastTimeStamp() + " - " + event.getValue()); } });