Search…
Gpiod
This library serves to control the digital pins of the embedded GPIO.

Requirements

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:
1
$ sudo apt-get install libgpiod-dev
Copied!

Output

To activate and deactivate any external component the embedded board is to change the value of the GPIO pin that will activate it.
1
package com.totalcross.DocRpi;
2
import totalcross.ui.MainWindow;
3
import totalcross.ui.event.ControlEvent;
4
import totalcross.ui.event.PressListener;
5
import totalcross.ui.gfx.Color;
6
import totalcross.ui.Button;
7
import totalcross.io.device.gpiod.GpiodChip;
8
import totalcross.io.device.gpiod.GpiodLine;
9
import totalcross.sys.Settings;
10
11
public class DocRpi extends MainWindow {
12
// Integers to store state of each LED pin, 0 (LOW) and 1 (HIGH)
13
private int stt;
14
// Buttons to control
15
private Button btn;
16
public DocRpi(){
17
setUIStyle(Settings.MATERIAL_UI);
18
}
19
@Override
20
public void initUI(){
21
// Board Setup
22
GpiodChip gpioChip = GpiodChip.open(0); // GIPIO bus
23
GpiodLine pin = gpioChip.line(21); //
24
// Set LED pins as outputs and default value stt
25
pin.requestOutput("CONSUMER",stt);
26
// The TotalCross button:
27
btn = new Button("Pin"); // Button instantiation
28
// without text
29
btn.setBackColor(Color.RED); // Set background color (red)
30
btn.addPressListener(new PressListener(){ // Press event listener
31
@Override
32
public void controlPressed(ControlEvent controlEvent) {
33
stt = 1 - stt; // Invert pin state
34
pin.setValue(stt); // Set value (HIGH or LOW)
35
}
36
});
37
add(btn, CENTER, CENTER);
38
}
39
}
Copied!

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.
1
package com.totalcross.DocRpi;
2
import totalcross.ui.MainWindow;
3
import totalcross.ui.event.ControlEvent;
4
import totalcross.ui.event.PressListener;
5
import totalcross.ui.gfx.Color;
6
import totalcross.ui.Button;
7
import totalcross.io.device.gpiod.GpiodChip;
8
import totalcross.io.device.gpiod.GpiodLine;
9
import totalcross.sys.Settings;
10
import totalcross.sys.Vm;
11
12
public class DocRpi extends MainWindow{
13
// Integers to store state of each LED pin, 0 (LOW) and 1 (HIGH)
14
private int stt;
15
// Buttons to control
16
private Button btn;
17
public DocRpi(){
18
setUIStyle(Settings.MATERIAL_UI);
19
}
20
@Override
21
public void initUI(){
22
23
// Board Setup
24
GpiodChip gpioChip = GpiodChip.open(0); // GIPIO bus
25
GpiodLine pin = gpioChip.line(21); //
26
// Set LED pins as outputs and default value stt
27
pin.requestOutput("CONSUMER", stt);
28
GpiodLine pinPushButton = gpioChip.line(22);
29
// Set Reset pin as input
30
pinPushButton.requestInput("CONSUMER");
31
new Thread(){
32
@Override
33
public void run(){
34
while(true){
35
if(pinPushButton.getValue() == 1){//check pin status
36
stt = 1 - stt; // Invert pin state
37
pin.setValue(stt); // Set value (HIGH or LOW)
38
}
39
Vm.sleep(200);
40
}
41
}
42
}.start();
43
}
44
}
45
Copied!

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.
Void
setValue(int value)
Changes pin value.
Void
requestInput(String consumer)
Names the pin and defines as input.
Int
getValue()
Returns pin status
Last modified 2yr ago