The GpioMonitor class allows to monitor GPIOs. More...
Header: | #include <GpioMonitor> |
Inherits: |
GpioMonitor(int gpio, QObject * parent = nullptr) | |
void | disable() |
bool | enable(bool activeLow = false, Gpio::Edge edgeInterrupt = Gpio::EdgeBoth) |
Gpio * | gpio() |
bool | isRunning() const |
bool | value() const |
void | valueChanged(const bool & value) |
The GpioMonitor class allows to monitor GPIOs.
An instance of this class creates a thread, which monitors each of the added GPIOs. The object emits a signal if one of the added GPIOs changes its value. The GpioMonitor configures a GPIO as an input, with the edge interrupt EDGE_BOTH (setEdgeInterrupt).
Following example shows how to use the GpioMonitor class for a button on the Raspberry Pi. There are two possibilitys to connect a button. Following picture shows the schematics:
Button A represents a clean solutin with a 10 kΩ resistor (set up as activeLow = false). Button B represents a "dirty" solution, were the 3.3V will be directly connected to the GPIO if the button is pressed (set activeLow = true).
Here is the code example for a button class:
Button::Button(QObject *parent) : QObject(parent) { m_button = new GpioMonitor(110, this); connect(m_button, &GpioMonitor::valueChanged, this, &Button::stateChanged); } bool Button::init() { return m_button->enable(); } void Button::stateChanged(const bool &value) { if (m_pressed != value) { m_pressed = value; if (value) { emit buttonPressed(); } else { emit buttonReleased(); } } }
Constructs a GpioMonitor object with the given gpio number and parent.
Disables this GpioMonitor.
Returns true if this GpioMonitor could be enabled successfully. With the activeLow parameter the values can be inverted. With the edgeInterrupt parameter the interrupt type can be specified.
Returns the Gpio of this GpioMonitor.
Returns true if this GpioMonitor is running.
Returns the current value of this GpioMonitor.
[signal]
void GpioMonitor::valueChanged(const bool & value)This signal will be emitted, if the monitored Gpios changed his value.