• Gpio
  • Gpio Class

    The Gpio class allows to interact with the GPIOs. More...

    Header: #include <Gpio>

    Public Types

    enum Direction { DirectionInput, DirectionOutput, DirectionInvalid }
    enum Edge { EdgeFalling, EdgeRising, EdgeBoth, EdgeNone }
    enum Value { ValueLow, ValueHigh, ValueInvalid }

    Public Functions

    Gpio(const int & gpio, QObject * parent = nullptr)
    bool activeLow()
    Gpio::Direction direction()
    Gpio::Edge edgeInterrupt()
    bool exportGpio()
    QString gpioDirectory() const
    int gpioNumber() const
    bool setActiveLow(bool activeLow)
    bool setDirection(Gpio::Direction direction)
    bool setEdgeInterrupt(Gpio::Edge edge)
    bool setValue(Gpio::Value value)
    bool unexportGpio()
    Gpio::Value value()

    Static Public Members

    bool isAvailable()

    Detailed Description

    The Gpio class allows to interact with the GPIOs.

    A "General Purpose Input/Output" (GPIO) is a flexible software-controlled digital signal. They are provided from many kinds of chip, and are familiar to Linux developers working with embedded and custom hardware. Each GPIO represents a bit connected to a particular pin, or "ball" on Ball Grid Array (BGA) packages. Board schematics show which external hardware connects to which GPIOs. Drivers can be written generically, so that board setup code passes such pin configuration data to drivers (source).

    General Purpose Input/Output (a.k.a. GPIO) is a generic pin on a chip whose behavior (including whether it is an input or output pin) can be controlled through this class. An object of of the Gpio class represents a pin.

    Gpio *gpioOut = new Gpio(23, this);
    // Export Gpio
    if (!gpioOut->exportGpio()) {
        qWarning() << "Could not export Gpio" << gpioOut->gpioNumber();
    // Configure Gpio direction
    if (!gpioOut->setDirection(PiGpio::DirectionOutput)) {
        qWarning() << "Could not set direction of Gpio" << gpioOut->gpioNumber();
    Gpio *gpioIn = new Gpio(24, this);
    // Export Gpio
    if (!gpioIn->exportGpio()) {
        qWarning() << "Could not export Gpio" << gpioIn->gpioNumber();
    // Configure Gpio direction
    if (!gpioIn->setDirection(PiGpio::DirectionInput)) {
        qWarning() << "Could not set direction of Gpio" << gpioIn->gpioNumber();
    qDebug() << "Current value" << (bool)gpioIn->value();

    See also GpioMonitor.

    Member Type Documentation

    enum Gpio::Direction

    This enum type specifies the dirction a Gpio.

    Gpio::DirectionInput0The Gpio is configured as input.
    Gpio::DirectionOutput1The Gpio is configured as output.
    Gpio::DirectionInvalid2The direction is not valid.

    enum Gpio::Edge

    This enum type specifies the edge interrupt type of a Gpio.

    Gpio::EdgeFalling0The Gpio reacts on falling edge interrupt.
    Gpio::EdgeRising1The Gpio reacts on rising edge interrupt.
    Gpio::EdgeBoth2The Gpio reacts on both, rising and falling edge interrupt.
    Gpio::EdgeNone3The Gpio does not react on interrupts.

    enum Gpio::Value

    This enum type specifies the value a Gpio.

    Gpio::ValueLow0The Gpio is low.
    Gpio::ValueHigh1The Gpio is high.
    Gpio::ValueInvalid-1The value is not valid.

    Member Function Documentation

    Gpio::Gpio(const int & gpio, QObject * parent = nullptr)

    Constructs a Gpio object to represent a GPIO with the given gpio number and parent.


    Destroys and unexports the Gpio.

    bool Gpio::activeLow()

    Returns true if the logic of this Gpio is inverted (1 = low, 0 = high).

    See also setActiveLow().

    Gpio::Direction Gpio::direction()

    Returns the direction of this Gpio.

    See also setDirection().

    Gpio::Edge Gpio::edgeInterrupt()

    Returns the edge interrupt of this Gpio.

    See also setEdgeInterrupt().

    bool Gpio::exportGpio()

    Returns true if this Gpio could be exported in the system file /sys/class/gpio/export. If this Gpio is already exported, this function will return true.

    QString Gpio::gpioDirectory() const

    Returns the directory /sys/class/gpio/gpio<number> of this Gpio.

    int Gpio::gpioNumber() const

    Returns the number of this Gpio.

    Note: The Gpio number is mostly not equivalent with the pin number.

    [static] bool Gpio::isAvailable()

    Returns true if the directories /sys/class/gpio and /sys/class/gpio/export do exist.

    bool Gpio::setActiveLow(bool activeLow)

    This method allows to invert the logic of this Gpio. Returns true, if the GPIO could be set activeLow.

    See also activeLow().

    bool Gpio::setDirection(Gpio::Direction direction)

    Returns true if the direction of this GPIO could be set.

    See also direction() and Gpio::Direction.

    bool Gpio::setEdgeInterrupt(Gpio::Edge edge)

    Returns true if the edge of this GPIO could be set correctly. The edge parameter specifies, when an interrupt occurs.

    See also edgeInterrupt().

    bool Gpio::setValue(Gpio::Value value)

    Returns true if the digital value of this Gpio could be set correctly.

    See also value().

    bool Gpio::unexportGpio()

    Returns true if this Gpio could be unexported in the system file /sys/class/gpio/unexport.

    Gpio::Value Gpio::value()

    Returns the current digital value of this Gpio.

    See also setValue().