mirror of https://github.com/nymea/nymea-gpio
Update source formating using new clang format
parent
5e1654d783
commit
f151646295
|
|
@ -85,7 +85,6 @@
|
||||||
\sa GpioMonitor
|
\sa GpioMonitor
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\enum Gpio::Direction
|
\enum Gpio::Direction
|
||||||
This enum type specifies the dirction a Gpio.
|
This enum type specifies the dirction a Gpio.
|
||||||
|
|
@ -130,14 +129,13 @@
|
||||||
Q_LOGGING_CATEGORY(dcGpio, "Gpio")
|
Q_LOGGING_CATEGORY(dcGpio, "Gpio")
|
||||||
|
|
||||||
/*! Constructs a Gpio object to represent a GPIO with the given \a gpio number and \a parent. */
|
/*! Constructs a Gpio object to represent a GPIO with the given \a gpio number and \a parent. */
|
||||||
Gpio::Gpio(int gpio, QObject *parent) :
|
Gpio::Gpio(int gpio, QObject *parent)
|
||||||
QObject(parent),
|
: QObject(parent)
|
||||||
m_gpio(gpio),
|
, m_gpio(gpio)
|
||||||
m_direction(Gpio::DirectionInvalid),
|
, m_direction(Gpio::DirectionInvalid)
|
||||||
m_gpioDirectory(QDir(QString("/sys/class/gpio/gpio%1").arg(QString::number(gpio))))
|
, m_gpioDirectory(QDir(QString("/sys/class/gpio/gpio%1").arg(QString::number(gpio))))
|
||||||
{
|
{
|
||||||
qRegisterMetaType<Gpio::Value>();
|
qRegisterMetaType<Gpio::Value>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Destroys and unexports the Gpio. */
|
/*! Destroys and unexports the Gpio. */
|
||||||
|
|
@ -376,7 +374,6 @@ bool Gpio::activeLow()
|
||||||
/*! Returns true if the \a edge of this GPIO could be set correctly. The \a edge parameter specifies, when an interrupt occurs. */
|
/*! Returns true if the \a edge of this GPIO could be set correctly. The \a edge parameter specifies, when an interrupt occurs. */
|
||||||
bool Gpio::setEdgeInterrupt(Gpio::Edge edge)
|
bool Gpio::setEdgeInterrupt(Gpio::Edge edge)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (m_direction == Gpio::DirectionOutput) {
|
if (m_direction == Gpio::DirectionOutput) {
|
||||||
qCWarning(dcGpio()) << "Could not set edge interrupt, GPIO is configured as an output.";
|
qCWarning(dcGpio()) << "Could not set edge interrupt, GPIO is configured as an output.";
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -436,7 +433,6 @@ Gpio::Edge Gpio::edgeInterrupt()
|
||||||
return Gpio::EdgeNone;
|
return Gpio::EdgeNone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*! Prints the given \a gpio to \a debug. */
|
/*! Prints the given \a gpio to \a debug. */
|
||||||
QDebug operator<<(QDebug debug, Gpio *gpio)
|
QDebug operator<<(QDebug debug, Gpio *gpio)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,10 @@
|
||||||
#ifndef GPIO_H
|
#ifndef GPIO_H
|
||||||
#define GPIO_H
|
#define GPIO_H
|
||||||
|
|
||||||
#include <QDir>
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QObject>
|
#include <QDir>
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
Q_DECLARE_LOGGING_CATEGORY(dcGpio)
|
Q_DECLARE_LOGGING_CATEGORY(dcGpio)
|
||||||
|
|
||||||
|
|
@ -89,9 +89,8 @@ private:
|
||||||
int m_gpio = 0;
|
int m_gpio = 0;
|
||||||
Gpio::Direction m_direction = Gpio::DirectionOutput;
|
Gpio::Direction m_direction = Gpio::DirectionOutput;
|
||||||
QDir m_gpioDirectory;
|
QDir m_gpioDirectory;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QDebug operator<< (QDebug debug, Gpio *gpio);
|
QDebug operator<<(QDebug debug, Gpio *gpio);
|
||||||
|
|
||||||
#endif // GPIO_H
|
#endif // GPIO_H
|
||||||
|
|
|
||||||
|
|
@ -79,12 +79,10 @@
|
||||||
#include "gpiomonitor.h"
|
#include "gpiomonitor.h"
|
||||||
|
|
||||||
/*! Constructs a \l{GpioButton} object with the given \a gpio number and \a parent. */
|
/*! Constructs a \l{GpioButton} object with the given \a gpio number and \a parent. */
|
||||||
GpioButton::GpioButton(int gpio, QObject *parent) :
|
GpioButton::GpioButton(int gpio, QObject *parent)
|
||||||
QObject(parent),
|
: QObject(parent)
|
||||||
m_gpioNumber(gpio)
|
, m_gpioNumber(gpio)
|
||||||
{
|
{}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! Returns the gpio number for this GpioButton. */
|
/*! Returns the gpio number for this GpioButton. */
|
||||||
int GpioButton::gpioNumber() const
|
int GpioButton::gpioNumber() const
|
||||||
|
|
|
||||||
|
|
@ -29,8 +29,8 @@
|
||||||
#define GPIOBUTTON_H
|
#define GPIOBUTTON_H
|
||||||
|
|
||||||
#include <QElapsedTimer>
|
#include <QElapsedTimer>
|
||||||
#include <QTimer>
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
class GpioMonitor;
|
class GpioMonitor;
|
||||||
|
|
||||||
|
|
@ -79,10 +79,8 @@ private slots:
|
||||||
public slots:
|
public slots:
|
||||||
bool enable();
|
bool enable();
|
||||||
void disable();
|
void disable();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QDebug operator<< (QDebug debug, GpioButton *gpioButton);
|
QDebug operator<<(QDebug debug, GpioButton *gpioButton);
|
||||||
|
|
||||||
|
|
||||||
#endif // GPIOBUTTON_H
|
#endif // GPIOBUTTON_H
|
||||||
|
|
|
||||||
|
|
@ -71,9 +71,9 @@
|
||||||
#include "gpiomonitor.h"
|
#include "gpiomonitor.h"
|
||||||
|
|
||||||
/*! Constructs a \l{GpioMonitor} object with the given \a gpio number and \a parent. */
|
/*! Constructs a \l{GpioMonitor} object with the given \a gpio number and \a parent. */
|
||||||
GpioMonitor::GpioMonitor(int gpio, QObject *parent) :
|
GpioMonitor::GpioMonitor(int gpio, QObject *parent)
|
||||||
QObject(parent),
|
: QObject(parent)
|
||||||
m_gpioNumber(gpio)
|
, m_gpioNumber(gpio)
|
||||||
{
|
{
|
||||||
m_valueFile.setFileName("/sys/class/gpio/gpio" + QString::number(m_gpioNumber) + "/value");
|
m_valueFile.setFileName("/sys/class/gpio/gpio" + QString::number(m_gpioNumber) + "/value");
|
||||||
}
|
}
|
||||||
|
|
@ -86,10 +86,7 @@ bool GpioMonitor::enable(bool activeLow, Gpio::Edge edgeInterrupt)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_gpio = new Gpio(m_gpioNumber, this);
|
m_gpio = new Gpio(m_gpioNumber, this);
|
||||||
if (!m_gpio->exportGpio() ||
|
if (!m_gpio->exportGpio() || !m_gpio->setDirection(Gpio::DirectionInput) || !m_gpio->setActiveLow(activeLow) || !m_gpio->setEdgeInterrupt(edgeInterrupt)) {
|
||||||
!m_gpio->setDirection(Gpio::DirectionInput) ||
|
|
||||||
!m_gpio->setActiveLow(activeLow) ||
|
|
||||||
!m_gpio->setEdgeInterrupt(edgeInterrupt)) {
|
|
||||||
qCWarning(dcGpio()) << "GpioMonitor: Error while initializing GPIO" << m_gpio->gpioNumber();
|
qCWarning(dcGpio()) << "GpioMonitor: Error while initializing GPIO" << m_gpio->gpioNumber();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,10 @@
|
||||||
#ifndef GPIOMONITOR_H
|
#ifndef GPIOMONITOR_H
|
||||||
#define GPIOMONITOR_H
|
#define GPIOMONITOR_H
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QSocketNotifier>
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QSocketNotifier>
|
||||||
|
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
|
|
||||||
|
|
@ -48,7 +48,7 @@ public:
|
||||||
bool isRunning() const;
|
bool isRunning() const;
|
||||||
bool value() const;
|
bool value() const;
|
||||||
|
|
||||||
Gpio* gpio();
|
Gpio *gpio();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_gpioNumber;
|
int m_gpioNumber;
|
||||||
|
|
@ -62,7 +62,6 @@ signals:
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void readyReady(const int &ready);
|
void readyReady(const int &ready);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // GPIOMONITOR_H
|
#endif // GPIOMONITOR_H
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,12 @@
|
||||||
|
|
||||||
#include "application.h"
|
#include "application.h"
|
||||||
|
|
||||||
#include <QDebug>
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
static void catchUnixSignals(const std::vector<int>& quitSignals, const std::vector<int>& ignoreSignals = std::vector<int>())
|
static void catchUnixSignals(const std::vector<int> &quitSignals, const std::vector<int> &ignoreSignals = std::vector<int>())
|
||||||
{
|
{
|
||||||
auto handler = [](int sig) ->void {
|
auto handler = [](int sig) -> void {
|
||||||
switch (sig) {
|
switch (sig) {
|
||||||
case SIGQUIT:
|
case SIGQUIT:
|
||||||
qDebug() << "Cought SIGQUIT quit signal...";
|
qDebug() << "Cought SIGQUIT quit signal...";
|
||||||
|
|
@ -60,11 +60,10 @@ static void catchUnixSignals(const std::vector<int>& quitSignals, const std::vec
|
||||||
|
|
||||||
for (int sig : quitSignals)
|
for (int sig : quitSignals)
|
||||||
signal(sig, handler);
|
signal(sig, handler);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::Application(int &argc, char **argv) :
|
Application::Application(int &argc, char **argv)
|
||||||
QCoreApplication(argc, argv)
|
: QCoreApplication(argc, argv)
|
||||||
{
|
{
|
||||||
catchUnixSignals({SIGQUIT, SIGINT, SIGTERM, SIGHUP, SIGSEGV});
|
catchUnixSignals({SIGQUIT, SIGINT, SIGTERM, SIGHUP, SIGSEGV});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,15 +25,14 @@
|
||||||
#ifndef APPLICATION_H
|
#ifndef APPLICATION_H
|
||||||
#define APPLICATION_H
|
#define APPLICATION_H
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
class Application : public QCoreApplication
|
class Application : public QCoreApplication
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit Application(int &argc, char **argv);
|
explicit Application(int &argc, char **argv);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // APPLICATION_H
|
#endif // APPLICATION_H
|
||||||
|
|
|
||||||
|
|
@ -42,19 +42,23 @@ int main(int argc, char *argv[])
|
||||||
"Copyright (C) 2013 - 2024 nymea GmbH\n"
|
"Copyright (C) 2013 - 2024 nymea GmbH\n"
|
||||||
"Copyright (C) 2024 - 2025 chargebyte austria GmbH\n\n"
|
"Copyright (C) 2024 - 2025 chargebyte austria GmbH\n\n"
|
||||||
"Released under the GNU General Public License v3.0 or (at your option) any later version.\n")
|
"Released under the GNU General Public License v3.0 or (at your option) any later version.\n")
|
||||||
.arg(application.applicationVersion());
|
.arg(application.applicationVersion());
|
||||||
parser.setApplicationDescription(applicationDescription);
|
parser.setApplicationDescription(applicationDescription);
|
||||||
|
|
||||||
QCommandLineOption gpioOption(QStringList() << "g" << "gpio", "The gpio number to use.", "GPIO");
|
QCommandLineOption gpioOption(QStringList() << "g" << "gpio", "The gpio number to use.", "GPIO");
|
||||||
parser.addOption(gpioOption);
|
parser.addOption(gpioOption);
|
||||||
|
|
||||||
QCommandLineOption interruptOption(QStringList() << "i" << "interrupt", "Configure the input GPIO to the given interrupt. This option is only allowed for monitoring. Allowerd interrupts are: [rising, falling, both, none]. Default is \"both\".", "INTERRUPT");
|
QCommandLineOption interruptOption(
|
||||||
|
QStringList() << "i" << "interrupt",
|
||||||
|
"Configure the input GPIO to the given interrupt. This option is only allowed for monitoring. Allowerd interrupts are: [rising, falling, both, none]. Default is \"both\".",
|
||||||
|
"INTERRUPT");
|
||||||
parser.addOption(interruptOption);
|
parser.addOption(interruptOption);
|
||||||
|
|
||||||
QCommandLineOption valueOption(QStringList() << "s" << "set-value", "Configure the GPIO to output and set the value. Allowerd values are: [0, 1].", "VALUE");
|
QCommandLineOption valueOption(QStringList() << "s" << "set-value", "Configure the GPIO to output and set the value. Allowerd values are: [0, 1].", "VALUE");
|
||||||
parser.addOption(valueOption);
|
parser.addOption(valueOption);
|
||||||
|
|
||||||
QCommandLineOption monitorOption(QStringList() << "m" << "monitor", "Monitor the given GPIO. The GPIO will automatically configured as input and print any change regarding to the given interrupt behaviour.");
|
QCommandLineOption monitorOption(QStringList() << "m" << "monitor",
|
||||||
|
"Monitor the given GPIO. The GPIO will automatically configured as input and print any change regarding to the given interrupt behaviour.");
|
||||||
parser.addOption(monitorOption);
|
parser.addOption(monitorOption);
|
||||||
|
|
||||||
QCommandLineOption activeLowOption(QStringList() << "l" << "active-low", "Configure the pin as active low (default is active high).");
|
QCommandLineOption activeLowOption(QStringList() << "l" << "active-low", "Configure the pin as active low (default is active high).");
|
||||||
|
|
@ -149,9 +153,7 @@ int main(int argc, char *argv[])
|
||||||
GpioMonitor *monitor = new GpioMonitor(gpioNumber);
|
GpioMonitor *monitor = new GpioMonitor(gpioNumber);
|
||||||
|
|
||||||
// Inform about interrupt
|
// Inform about interrupt
|
||||||
QObject::connect(monitor, &GpioMonitor::valueChanged, [gpioNumber](bool value) {
|
QObject::connect(monitor, &GpioMonitor::valueChanged, [gpioNumber](bool value) { qDebug() << "GPIO" << gpioNumber << "value changed:" << (value ? "1" : "0"); });
|
||||||
qDebug() << "GPIO" << gpioNumber << "value changed:" << (value ? "1" : "0");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Enable the monitor
|
// Enable the monitor
|
||||||
if (!monitor->enable(activeLow, edge)) {
|
if (!monitor->enable(activeLow, edge)) {
|
||||||
|
|
@ -160,9 +162,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up the gpio once done
|
// Clean up the gpio once done
|
||||||
QObject::connect(&application, &Application::aboutToQuit, [monitor](){
|
QObject::connect(&application, &Application::aboutToQuit, [monitor]() { delete monitor; });
|
||||||
delete monitor;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return application.exec();
|
return application.exec();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue