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;import com.gmv.its.deepsy.hal.gpio.*;import com.gmv.its.deepsy.gpio.*;public class GetGpiosApp {public static void main(String[] args) {try (DeepsyGpioManager gpioManager = DeepsyGpioManager.getInstance("172.22.198.105")) {List<Gpio> gpioList = gpioManager.getGpios();System.out.println("There are " + gpioList.size() + " GPIOS");for (Gpio g : gpioList) {System.out.printf("Gpio id : %s\n", g.getId());System.out.printf("%20s : %s\n", "Alias", g.getConfiguration().getAlias());System.out.printf("%20s : %s\n", "Type", g.getInformation().getType());System.out.printf("%20s : %s\n", "Address", g.getInformation().getAddress());System.out.printf("%20s : %s\n", "Direction", g.getInformation().getDirection());System.out.printf("%20s : %s\n", "Default Value", g.getConfiguration().getDefaultvalue());System.out.printf("%20s : %s\n", "Active Low", g.getConfiguration().getActivelow());System.out.printf("%20s : %s\n", "Edge type", g.getConfiguration().getEdgetype());System.out.printf("%20s : %s\n", "Publish", g.getConfiguration().getPublish());System.out.printf("%20s : %s\n", "Bouncing Time", g.getConfiguration().getBouncingtime());}} catch (GpioNotAvailableException e) {e.printStackTrace();} catch (GpioLowLevelException e) {e.printStackTrace();}}}
-
Set Gpio Configuration
package com.gmv.its.dpyjavaexamples.gpio;import java.util.List;import com.gmv.its.deepsy.hal.gpio.*;import com.gmv.its.deepsy.gpio.*;public class SetGpioConfigurationApp {public static void main(String[] args) {try (DeepsyGpioManager gpioManager = DeepsyGpioManager.getInstance("172.22.198.105")) {List<Gpio> gpioList = gpioManager.getGpios();System.out.println("There are " + gpioList.size() + " GPIOS");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");System.out.printf("%20s : %s\n", "Alias", currentAlias);System.out.printf("%20s : %s\n", "Default Value", currentDefaultValue);System.out.printf("%20s : %d\n", "Bouncing Time", currentBouncingTime);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");System.out.printf("%20s : %s\n", "Alias", gpio2.getConfiguration().getAlias());System.out.printf("%20s : %s\n", "Default Value", gpio2.getConfiguration().getDefaultvalue());System.out.printf("%20s : %d\n", "Bouncing Time", gpio2.getConfiguration().getBouncingtime());//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.*;import com.gmv.its.deepsy.hal.CannotSubscribeException;import com.gmv.its.deepsy.hal.gpio.*;import com.gmv.its.deepsy.gpio.*;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);System.out.printf("%20s : %s\n", "New Alias", gpioConfiguration.getAlias());System.out.printf("%20s : %s\n", "New Default Value", gpioConfiguration.getDefaultvalue());System.out.printf("%20s : %s\n", "New Active Low", gpioConfiguration.getActivelow());System.out.printf("%20s : %s\n", "New Edge type", gpioConfiguration.getEdgetype());System.out.printf("%20s : %s\n", "New Publish", gpioConfiguration.getPublish());System.out.printf("%20s : %s\n", "New Bouncing Time", gpioConfiguration.getBouncingtime());}}public class SubscribeGpioEventsApp {public static void main(String[] args) {try (DeepsyGpioManager gpioManager = DeepsyGpioManager.getInstance("172.22.198.103")) {List<Gpio> gpioList = gpioManager.getGpios();System.out.println("There are " + gpioList.size() + " GPIOS");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;import com.gmv.its.deepsy.hal.gpio.*;import com.gmv.its.deepsy.gpio.*;public class GetGpiosValueApp {public static void main(String[] args) {try (DeepsyGpioManager gpioManager = DeepsyGpioManager.getInstance("172.22.198.105")) {List<Gpio> gpioList = gpioManager.getGpios();System.out.println("There are " + gpioList.size() + " GPIOS");for (Gpio g : gpioList) {if (g.getInformation().getDirection() == GpioDirection.INPUT) {System.out.printf("Gpio id : %s\n", g.getId());System.out.printf("%20s : %s\n", "Value", g.getStatus().getValue());System.out.printf("%20s : %s\n", "Counter", g.getStatus().getCounter());}}} catch (GpioNotAvailableException e) {e.printStackTrace();} catch (GpioLowLevelException e) {e.printStackTrace();}}}