Classes | |
interface | Gpio |
Gpio. More... | |
interface | GpioConfiguration |
Gpio Configuration. More... | |
interface | GpioConfigurationEvent |
Gpio Configuration event. More... | |
enum | GpioDirection |
enum | GpioEdgetype |
interface | GpioEvent |
interface | GpioInformation |
Gpio Information. More... | |
class | GpioLowLevelException |
Gpio low level exception. More... | |
interface | GpioManager |
Gpio Manager. More... | |
class | GpioNotAvailableException |
Gpio not available exception. More... | |
class | GpioObserver |
Gpio event. More... | |
interface | GpioStatus |
Gpio Status. More... | |
interface | GpioStatusEvent |
Gpio Status event. More... | |
Detailed Description
To work with Deepsy gpio service it is necessary to use DeepsyGpioManager located in com.gmv.its.deepsy.gpio for getting the initial GpioManager object.
DeepsyGpioManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:
GpioManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a list of Gpio objects:
If this object is not inside a try with resources block, it is recommended to call to gpioManager.close() in order to finalize properly;
Examples
-
Get Gpios
package com.gmv.its.dpyjavaexamples.gpio;import java.util.List;public class GetGpiosApp {public static void main(String[] args) {try (DeepsyGpioManager gpioManager = DeepsyGpioManager.getInstance("172.22.198.105")) {List<Gpio> gpioList = gpioManager.getGpios();for (Gpio g : gpioList) {System.out.printf("Gpio id : %s\n", g.getId());}} catch (GpioNotAvailableException e) {e.printStackTrace();} catch (GpioLowLevelException e) {e.printStackTrace();}}}
-
Set Gpio Configuration
package com.gmv.its.dpyjavaexamples.gpio;import java.util.List;public class SetGpioConfigurationApp {public static void main(String[] args) {try (DeepsyGpioManager gpioManager = DeepsyGpioManager.getInstance("172.22.198.105")) {List<Gpio> gpioList = gpioManager.getGpios();Gpio gpio = null;for (Gpio g : gpioList) {if (g.getInformation().getDirection() == GpioDirection.OUTPUT) {gpio = g;break;}}if (gpio != null) {String currentAlias = gpio.getConfiguration().getAlias();int currentDefaultValue = gpio.getConfiguration().getDefaultvalue();int currentBouncingTime = gpio.getConfiguration().getBouncingtime();System.out.println("Initial configuration");GpioConfiguration configuration = gpio.getConfiguration();configuration.setAlias("New Alias");configuration.setDefaultvalue(1);configuration.setBouncingtime(10);gpio.setConfiguration(configuration);List<Gpio> gpioList2 = gpioManager.getGpios();Gpio gpio2 = null;for (Gpio g : gpioList2) {if (g.getInformation().getDirection() == GpioDirection.OUTPUT) {gpio2 = g;break;}}System.out.println("New configuration");//Set configuration as it wasGpioConfiguration currentConfiguration = gpio2.getConfiguration();currentConfiguration.setAlias(currentAlias);currentConfiguration.setDefaultvalue(currentDefaultValue);currentConfiguration.setBouncingtime(currentBouncingTime);gpio2.setConfiguration(currentConfiguration);}} catch (GpioNotAvailableException e) {e.printStackTrace();} catch (GpioLowLevelException e) {e.printStackTrace();}}}
-
Subscribe to Gpio events
package com.gmv.its.dpyjavaexamples.gpio;import java.util.List;import java.io.*;class MyGpioObserver extends GpioObserver {@Overridepublic void notifyGpioStatus(GpioStatusEvent event) {String gpioid = event.getGpioid();GpioStatus gpioStatus = event.getStatus();System.out.printf("Status Event. Gpio %s. Value %s Counter %s\n", gpioid, gpioStatus.getValue(), gpioStatus.getCounter());}@Overridepublic void notifyGpioConfiguration(GpioConfigurationEvent event) {String gpioid = event.getGpioid();GpioConfiguration gpioConfiguration = event.getGpioconfiguration();System.out.printf("Configuration Event. Gpio %s\n", gpioid);}}public class SubscribeGpioEventsApp {public static void main(String[] args) {try (DeepsyGpioManager gpioManager = DeepsyGpioManager.getInstance("172.22.198.103")) {List<Gpio> gpioList = gpioManager.getGpios();for (Gpio g : gpioList) {g.subscribe(new MyGpioObserver());}System.out.println("Press any key for exit\n");InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);br.readLine();} catch (GpioNotAvailableException e) {e.printStackTrace();} catch (GpioLowLevelException e) {e.printStackTrace();} catch (CannotSubscribeException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}
-
Get Gpio value
package com.gmv.its.dpyjavaexamples.gpio;import java.util.List;public class GetGpiosValueApp {public static void main(String[] args) {try (DeepsyGpioManager gpioManager = DeepsyGpioManager.getInstance("172.22.198.105")) {List<Gpio> gpioList = gpioManager.getGpios();for (Gpio g : gpioList) {if (g.getInformation().getDirection() == GpioDirection.INPUT) {System.out.printf("Gpio id : %s\n", g.getId());}}} catch (GpioNotAvailableException e) {e.printStackTrace();} catch (GpioLowLevelException e) {e.printStackTrace();}}}