In order to execute Gpiod methods at your embedded device, you will need to have the libgpiod-dev package installed in your board. You can do that by entering the following command at the device's terminal:
$ sudo apt-get install libgpiod-dev
Output
To activate and deactivate any external component the embedded board is to change the value of the GPIO pin that will activate it.
packagecom.totalcross.DocRpi;importtotalcross.ui.MainWindow;importtotalcross.ui.event.ControlEvent;importtotalcross.ui.event.PressListener;importtotalcross.ui.gfx.Color;importtotalcross.ui.Button;importtotalcross.io.device.gpiod.GpiodChip;importtotalcross.io.device.gpiod.GpiodLine;importtotalcross.sys.Settings;publicclassDocRpiextendsMainWindow {// Integers to store state of each LED pin, 0 (LOW) and 1 (HIGH)privateint stt;// Buttons to controlprivateButton btn;publicDocRpi(){setUIStyle(Settings.MATERIAL_UI); } @OverridepublicvoidinitUI(){// Board SetupGpiodChip gpioChip =GpiodChip.open(0); // GIPIO busGpiodLine pin =gpioChip.line(21); //// Set LED pins as outputs and default value sttpin.requestOutput("CONSUMER",stt);// The TotalCross button: btn =newButton("Pin"); // Button instantiation// without textbtn.setBackColor(Color.RED); // Set background color (red)btn.addPressListener(newPressListener(){ // Press event listener @OverridepublicvoidcontrolPressed(ControlEvent controlEvent) { stt =1- stt; // Invert pin state pin.setValue(stt); // Set value (HIGH or LOW) } });add(btn, CENTER, CENTER); }}
Input
In several embedded applications, it is necessary to receive digital signal from an external component such as sensors or even to activate another component indirectly.
packagecom.totalcross.DocRpi;importtotalcross.ui.MainWindow;importtotalcross.ui.event.ControlEvent;importtotalcross.ui.event.PressListener;importtotalcross.ui.gfx.Color;importtotalcross.ui.Button;importtotalcross.io.device.gpiod.GpiodChip;importtotalcross.io.device.gpiod.GpiodLine;importtotalcross.sys.Settings;importtotalcross.sys.Vm;publicclassDocRpiextendsMainWindow{// Integers to store state of each LED pin, 0 (LOW) and 1 (HIGH)privateint stt;// Buttons to control privateButton btn;publicDocRpi(){setUIStyle(Settings.MATERIAL_UI); } @OverridepublicvoidinitUI(){// Board SetupGpiodChip gpioChip =GpiodChip.open(0); // GIPIO busGpiodLine pin =gpioChip.line(21); //// Set LED pins as outputs and default value sttpin.requestOutput("CONSUMER", stt);GpiodLine pinPushButton =gpioChip.line(22);// Set Reset pin as inputpinPushButton.requestInput("CONSUMER");newThread(){ @Overridepublicvoidrun(){while(true){if(pinPushButton.getValue() ==1){//check pin status stt =1- stt; // Invert pin state pin.setValue(stt); // Set value (HIGH or LOW) }Vm.sleep(200); } } }.start(); }}
Behind the Class
Methods
Type
Name
Description
Constructor
open(int chip)
Defines which GPIO bus will be used.
Construtor
line(int pin)
Defines which pin of GPIO bus will be used.
Void
requestOutput(String consumer, int defaultValue)
Names the pin, defines as output and the initial value.