LogoLogo
v7.0.0
v7.0.0
  • TotalCross Overview
  • TotalCross Javadoc
  • TotalCross Changelog
  • Roadmap
  • Documentation
    • Getting Started
      • First embedded project with TotalCross
    • Components
      • Accordion
      • Aligned Labels
      • Button
      • Check
      • ComboBox
      • Dynamic Scroll
      • Edit
      • Floating Button
      • Gpiod
      • Grid
      • GridContainer
      • Image
      • ImageControl
      • ImageList
      • Label
      • Material Icons
      • Material Window
      • MessageBox
      • Multi Edit
      • Progress Bar
      • Progress Box
      • Radio
      • Radio Group
      • Scroll Container
      • Side Menu
      • Slider
      • Sliding Window
      • Spin List
      • Spinner
      • Switch
      • Tabbed Container
      • Velocimeter
    • APIs
      • API Overview
      • API Rest
      • Asynchronous Task
      • Camera
      • Control
        • Main Window
        • Window
        • Container
      • GPS
      • HTTPS and SSL
      • JSON
      • Maps
        • Maps - Deprecated
        • Static Map
      • Material Design Standards
      • Ninepath
      • Notifications
      • PrinterManager
      • Push Notification Firebase
      • Scanner
      • SOAP
      • Socket
      • SocketServer
      • SQLite Encryption
      • QR Code Generator
      • totalcross.sys
      • Youtube API
    • Creating an Issue
    • Contributing
      • Branch workflow
      • Writing documentation
    • Guides
      • App Architecture
        • Suggested Architecture
        • Why do Design Patterns help with the application's organization?
          • MVC Architecture Pattern
          • Template Pattern
          • Data Persistence: DAO Pattern.
        • Separation of concepts: What is the best way to create UI interfaces?
        • Positioning
          • Manual Positioning
        • Relative Positioning
        • Best practices to improve project maintenance
      • Device Simulator
      • Package your app from scratch
        • TotalCross SDK
        • Environment Variables in IDE
          • Eclipse
          • IntelliJ
        • Deploy your app with a dependecy TC
        • Deploy iOS
          • Using Development certificate to test your apps
      • Understanding TotalCross for Linux ARM
      • Running C++ applications with TotalCross
      • Web Services
    • Miscelaneous
      • Java JDK 8
      • Maven
      • Installing Visual Studio Code
    • FAQ
      • IMEI in Android 10
Powered by GitBook
On this page
  • Requirements
  • Output
  • Input
  • Behind the Class
  • Methods

Was this helpful?

  1. Documentation
  2. Components

Gpiod

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

PreviousFloating ButtonNextGrid

Last updated 4 years ago

Was this helpful?

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:

$ 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.

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);  
    }
}                               

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.

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();
    }
}

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