QGpioLine Class
The QGpioLine class reads or drives one general-purpose I/O line. More…
| Header | #include <QGpioLine> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Gpio) |
| Module | Nt GPIO C++ Classes |
| Since | 6.0 |
| Inherits | QObject |
List of all members, including inherited members
Public Types
Public Functions
| QGpioLine(QObject *parent = nullptr) | |
| QGpioLine(const QString &name, QObject *parent = nullptr) | |
| ~QGpioLine() | |
| void | setLineName(const QString &name) |
| QString | lineName() const |
| void | setDirection(Direction direction) |
| Direction | direction() const |
| void | setActiveLow(bool activeLow) |
| bool | isActiveLow() const |
| void | setBias(Bias bias) |
| Bias | bias() const |
| void | setDebounceInterval(int msecs) |
| int | debounceInterval() const |
| void | setPollInterval(int msecs) |
| int | pollInterval() const |
| bool | open() |
| void | close() |
| bool | isOpen() const |
| bool | isEventDriven() const |
| bool | value() const |
| bool | setValue(bool value) |
| GpioError | error() const |
| QString | errorString() const |
| void | clearError() |
Signals
| void | valueChanged(bool value) |
| void | errorOccurred(QGpioLine::GpioError gpioError) |
Detailed Description
A QGpioLine is an input, such as a push-to-talk switch or a headset detect pin, or an output, such as an LED or the enable of an amplifier. Give it the name of the line, set it up, and call open():
QGpioLine ptt(QStringLiteral("gpiochip0:17"));
ptt.setActiveLow(true);
ptt.setBias(QGpioLine::PullUp);
ptt.setDebounceInterval(20);
if (!ptt.open())
qWarning() << ptt.errorString();
QObject::connect(&ptt, &QGpioLine::valueChanged, [](bool pressed) { qDebug() << pressed; });
Values are logical: on an active-low line, true means the line is low. An input line is watched from the event loop of the thread the object lives in, by edge where the hardware can and by polling where it cannot, and valueChanged() is emitted when the value changes. See Using Nt GPIO for how lines are named on each platform.
See also Using Nt GPIO.
Differences from Qt
Qt has no GPIO class; QGpioLine is an Nt addition. It works on Linux, through the GPIO character device (version 2 of its interface, Linux 5.10 or later), and on Zephyr. One object handles one line. Direction, bias and active low are applied by open() and need the line to be opened again to change; there is no open drain or open source drive, and no event timestamps. On Zephyr, line names are not looked up, and quick edges that come closer together than the thread can wake are seen as the level the line settled at.
Member Type Documentation
enum QGpioLine::Bias
This enum describes the internal resistor of an input line.
| Constant | Description |
|---|---|
QGpioLine::AsIs | The bias is left as it was set before, for example by the devicetree. |
QGpioLine::Disabled | No pull resistor. On Zephyr this is the same as AsIs. |
QGpioLine::PullUp | The line is pulled high when nothing drives it. |
QGpioLine::PullDown | The line is pulled low when nothing drives it. |
enum QGpioLine::Direction
This enum describes which way a line works.
| Constant | Description |
|---|---|
QGpioLine::Input | The line is read. valueChanged() reports its changes. |
QGpioLine::Output | The line is driven with setValue(). |
enum QGpioLine::GpioError
This enum describes the errors a QGpioLine can report.
| Constant | Description |
|---|---|
QGpioLine::NoError | No error occurred. |
QGpioLine::DeviceNotFoundError | The chip or the line does not exist, or the name is not valid. |
QGpioLine::PermissionError | The user may not open the GPIO chip. |
QGpioLine::BusyError | Another program or a driver holds the line. |
QGpioLine::OpenError | The line is already open in this object. |
QGpioLine::UnsupportedOperationError | The hardware cannot do what was asked, such as a bias, or setValue() was called on an input line. |
QGpioLine::ResourceError | The line went away while it was open, for example because its device was removed. The line has been closed. |
QGpioLine::NotOpenError | The operation needs the line to be open. |
QGpioLine::UnknownError | Any other error. |
Member Function Documentation
QGpioLine::QGpioLine(QObject *parent = nullptr)
Constructs a line object with the given parent. Set the line with setLineName() before open().
QGpioLine::QGpioLine(const QString &name, QObject *parent = nullptr)
Constructs a line object for the line called name, with the given parent. The line is not opened.
See also setLineName().
QGpioLine::~QGpioLine()
Destroys the object, closing the line first.
Bias QGpioLine::bias() const
Returns the bias of the line.
See also setBias().
void QGpioLine::clearError()
Sets error() to NoError and clears errorString().
void QGpioLine::close()
Releases the line. An output line keeps its last level on Zephyr; on Linux the kernel may set it back. Does nothing if the line is not open.
See also open().
int QGpioLine::debounceInterval() const
Returns the debounce interval in milliseconds.
See also setDebounceInterval().
Direction QGpioLine::direction() const
Returns whether the line is an input or an output.
See also setDirection().
GpioError QGpioLine::error() const
Returns the last error.
See also errorString() and clearError().
void QGpioLine::errorOccurred(QGpioLine::GpioError gpioError)
This signal is emitted when the error gpioError occurs. It is not emitted when the error is cleared.
See also error() and errorString().
QString QGpioLine::errorString() const
Returns a description of the last error, or an empty string if there is none.
See also error().
bool QGpioLine::isActiveLow() const
Returns true if the line is active low; otherwise returns false.
See also setActiveLow().
bool QGpioLine::isEventDriven() const
Returns true if the open input line reports its edges; returns false if it is polled, or not an open input.
See also setPollInterval().
bool QGpioLine::isOpen() const
Returns true if the line is open; otherwise returns false.
QString QGpioLine::lineName() const
Returns the name of the line.
See also setLineName().
bool QGpioLine::open()
Requests the line from the system and configures it. Returns true on success; otherwise returns false and sets error().
An input line is read at once, so value() is valid when open() returns. An output line is driven to the value set with setValue() beforehand.
See also close() and isOpen().
int QGpioLine::pollInterval() const
Returns the poll interval in milliseconds.
See also setPollInterval().
void QGpioLine::setActiveLow(bool activeLow)
Sets whether the line is active low to activeLow. On an active-low line, a logical true is a low level. It takes effect at the next open().
See also value().
void QGpioLine::setBias(Bias bias)
Sets the internal pull resistor of the line to bias. It takes effect at the next open(), which fails with UnsupportedOperationError if the hardware has no such bias.
void QGpioLine::setDebounceInterval(int msecs)
Sets the debounce interval of an input line to msecs milliseconds. A change of the line is reported once the line has kept its new value for this long. The default, 0, reports every change at once. It takes effect at once.
See also valueChanged().
void QGpioLine::setDirection(Direction direction)
Sets whether the line is an input or an output to direction. It takes effect at the next open(); a new line is an Input.
void QGpioLine::setLineName(const QString &name)
Sets the line this object opens to name, such as PTT or gpiochip0:17. It takes effect at the next open().
See also Naming a Line.
void QGpioLine::setPollInterval(int msecs)
Sets how often an input line that cannot report its edges is read to msecs milliseconds. The default is 10. A line that reports edges is not polled.
See also isEventDriven().
bool QGpioLine::setValue(bool value)
Drives an output line to the logical value. Returns true on success; otherwise returns false and sets error(). On a closed output line, value is kept and driven by the next open(). An input line cannot be set.
See also value() and valueChanged().
bool QGpioLine::value() const
Returns the logical value of the line. For an input, this is the value last reported by valueChanged(), or read by open(); it is updated from the event loop. For an output, it is the value last set.
See also setValue() and setActiveLow().
void QGpioLine::valueChanged(bool value)
This signal is emitted when the logical value of an input line changes to value, after any debounce interval has passed. It is also emitted when setValue() changes the value of an output line.
See also value() and setDebounceInterval().