Gpiod
This library serves to control the digital pins of the embedded GPIO.
Last updated
Was this helpful?
Was this helpful?
package com.totalcross.DocRpi;
import totalcross.ui.MainWindow;
import totalcross.ui.event.ControlEvent;
import totalcross.ui.event.PressListener;
import totalcross.ui.gfx.Color;
import totalcross.ui.Button;
import totalcross.io.device.gpiod.GpiodChip;
import totalcross.io.device.gpiod.GpiodLine;
import totalcross.sys.Settings;
public class DocRpi extends MainWindow {
// Integers to store state of each LED pin, 0 (LOW) and 1 (HIGH)
private int stt;
// Buttons to control
private Button btn;
public DocRpi(){
setUIStyle(Settings.MATERIAL_UI);
}
@Override
public void initUI(){
// Board Setup
GpiodChip gpioChip = GpiodChip.open(0); // GIPIO bus
GpiodLine pin = gpioChip.line(21); //
// Set LED pins as outputs and default value stt
pin.requestOutput("CONSUMER",stt);
// The TotalCross button:
btn = new Button("Pin"); // Button instantiation
// without text
btn.setBackColor(Color.RED); // Set background color (red)
btn.addPressListener(new PressListener(){ // Press event listener
@Override
public void controlPressed(ControlEvent controlEvent) {
stt = 1 - stt; // Invert pin state
pin.setValue(stt); // Set value (HIGH or LOW)
}
});
add(btn, CENTER, CENTER);
}
} package com.totalcross.DocRpi;
import totalcross.ui.MainWindow;
import totalcross.ui.event.ControlEvent;
import totalcross.ui.event.PressListener;
import totalcross.ui.gfx.Color;
import totalcross.ui.Button;
import totalcross.io.device.gpiod.GpiodChip;
import totalcross.io.device.gpiod.GpiodLine;
import totalcross.sys.Settings;
import totalcross.sys.Vm;
public class DocRpi extends MainWindow{
// Integers to store state of each LED pin, 0 (LOW) and 1 (HIGH)
private int stt;
// Buttons to control
private Button btn;
public DocRpi(){
setUIStyle(Settings.MATERIAL_UI);
}
@Override
public void initUI(){
// Board Setup
GpiodChip gpioChip = GpiodChip.open(0); // GIPIO bus
GpiodLine pin = gpioChip.line(21); //
// Set LED pins as outputs and default value stt
pin.requestOutput("CONSUMER", stt);
GpiodLine pinPushButton = gpioChip.line(22);
// Set Reset pin as input
pinPushButton.requestInput("CONSUMER");
new Thread(){
@Override
public void run(){
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();
}
}