From 94a95eceb5c2c6ddead64d0e48268669cfe79aaa Mon Sep 17 00:00:00 2001 From: Siegfried Siegert Date: Thu, 14 Nov 2024 13:57:52 +0100 Subject: [PATCH 1/4] dCArun: add DigitalOutputAbstraction --- dCArun/DigitalOutputAbstraction.cpp | 170 ++++++++++++++++++++++++++++ dCArun/DigitalOutputAbstraction.h | 37 ++++++ dCArun/dCArun.pro | 2 + 3 files changed, 209 insertions(+) create mode 100644 dCArun/DigitalOutputAbstraction.cpp create mode 100644 dCArun/DigitalOutputAbstraction.h diff --git a/dCArun/DigitalOutputAbstraction.cpp b/dCArun/DigitalOutputAbstraction.cpp new file mode 100644 index 0000000..1635293 --- /dev/null +++ b/dCArun/DigitalOutputAbstraction.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include + +#include + +#include "DigitalOutputAbstraction.h" +#include "plugin.h" + +/** + * this is based on a solution from: + * https://embeddeduse.com/2018/09/18/monitoring-sys-files-qfilesystemwatcher/ + * + */ + +DigitalOutputAbstraction::DigitalOutputAbstraction(hwinf *dc, QObject *parent) + : QObject(parent) + , dc(dc) +{ + auto fileMonitor = new QFileSystemWatcher(this); + + connect(fileMonitor, &QFileSystemWatcher::fileChanged, + this, &DigitalOutputAbstraction::fileChanged); + + qCritical() << "... init DigitalOutputAbstraction"; +} + + + + +bool DigitalOutputAbstraction::addCCWake(const QString file) +{ + // on PTU5: "/sys/class/leds/wakeupctrl_cc/brightness" + if (!QFileInfo::exists(file)) { + qCritical() << " ... create file: " << file; + QFile(file).open(QIODevice::ReadWrite | QIODevice::Text); + } + + qCritical() << " ... add file: " << file; + + this->ccWakePath = file; + return this->fileMonitor->addPath(file); +} + +bool DigitalOutputAbstraction::addCCPower(const QString file) +{ + if (!QFileInfo::exists(file)) { + qCritical() << " ... create file: " << file; + QFile(file).open(QIODevice::ReadWrite | QIODevice::Text); + } + + qCritical() << " ... add file: " << file; + + this->ccPowerPath = file; + return this->fileMonitor->addPath(file); +} + +bool DigitalOutputAbstraction::addCCModem(const QString file) +{ + if (!QFileInfo::exists(file)) { + qCritical() << " ... create file: " << file; + QFile(file).open(QIODevice::ReadWrite | QIODevice::Text); + } + + qCritical() << " ... add file: " << file; + + this->modemPowerPath = file; + return this->fileMonitor->addPath(file); +} + + +void DigitalOutputAbstraction::fileChanged(const QString &path) +{ + if (path == this->ccPowerPath) this->private_ccPowerChanged(); + if (path == this->ccWakePath) this->private_ccWakeChanged(); + if (path == this->modemPowerPath) this->private_ccWakeChanged(); +} + +void DigitalOutputAbstraction::private_modemPowerChanged() +{ + QFile modemPowerFile(this->modemPowerPath); + if (!modemPowerFile.open(QIODevice::ReadOnly)) { + qWarning() << "ERROR: Could not open modemPowerFile " << this->modemPowerPath; + return; + } + auto modemPower = modemPowerFile.readAll(); + if (!modemPower.isEmpty()) { + int state = modemPower.at(0); + //qCritical() << "INFO: modemPower = " << modemPower; + switch (state) { + case 0x30: // '0' + qCritical() << "INFO: modemPower -> off"; + this->dc->mod_switchWake(false); + this->dc->mod_switchPower(false); + break; + case 0x31: // '1' + qCritical() << "INFO: modemPower -> on"; + this->dc->mod_switchWake(true); + this->dc->mod_switchPower(true); + break; + } + } +} + +void DigitalOutputAbstraction::private_ccPowerChanged() +{ + QFile ccPowerFile(this->ccPowerPath); + if (!ccPowerFile.open(QIODevice::ReadOnly)) { + qWarning() << "ERROR: Could not open ccPowerFile file."; + return; + } + auto ccPower = ccPowerFile.readAll(); + if (!ccPower.isEmpty()) { + int state = ccPower.at(0); + + auto lambdaOn = [this]() -> void + { + this->dc->credit_switchPower(true); + this->dc->credit_switchWake(true); + }; + auto lambdaOff = [this]() -> void + { + this->dc->credit_switchPower(false); + this->dc->credit_switchWake(false); + }; + + + //qCritical() << "INFO: modemPower = " << modemPower; + switch (state) { + case 0x30: // '0' + qCritical() << "INFO: ccPower -> off"; + lambdaOff(); + break; + case 0x31: // '1' + qCritical() << "INFO: ccPower -> on"; + lambdaOn(); + break; + case 0x32: // '2' + qCritical() << "INFO: ccPower -> on / off"; + lambdaOff(); + QTimer::singleShot(500, this, lambdaOn); + break; + } + } +} + +void DigitalOutputAbstraction::private_ccWakeChanged() +{ + QFile ccWakeFile(this->ccWakePath); + if (!ccWakeFile.open(QIODevice::ReadOnly)) { + qWarning() << "ERROR: Could not open ccWakeFile " << this->ccWakePath; + return; + } + auto ccWake = ccWakeFile.readAll(); + if (!ccWake.isEmpty()) { + int state = ccWake.at(0); + //qCritical() << "INFO: ccWake = " << state; + switch (state) { + case 0x30: // '0' + qCritical() << "INFO: ccWake -> sleep"; + this->dc->credit_switchWake(true); // switch 'sleep' + break; + case 0x31: // '1' + qCritical() << "INFO: ccWake -> wake"; + this->dc->credit_switchWake(false); // switch 'wake' + break; + } + } +} diff --git a/dCArun/DigitalOutputAbstraction.h b/dCArun/DigitalOutputAbstraction.h new file mode 100644 index 0000000..f8f5910 --- /dev/null +++ b/dCArun/DigitalOutputAbstraction.h @@ -0,0 +1,37 @@ +#ifndef DIGITALOUTPUTABSTRACTION_H +#define DIGITALOUTPUTABSTRACTION_H + + +#include + +class hwinf; +class QFileSystemWatcher; + + +class DigitalOutputAbstraction : public QObject +{ + Q_OBJECT + +public: + DigitalOutputAbstraction(hwinf *dc, QObject *parent = nullptr); + bool addCCWake(const QString file); + bool addCCPower(const QString file); + bool addCCModem(const QString file); + + +private: + hwinf *dc; + QFileSystemWatcher *fileMonitor; + + QString modemPowerPath; + QString ccPowerPath; + QString ccWakePath; + + void fileChanged(const QString &path); + + void private_modemPowerChanged(); + void private_ccPowerChanged(); + void private_ccWakeChanged(); +}; + +#endif // DIGITALOUTPUTABSTRACTION_H diff --git a/dCArun/dCArun.pro b/dCArun/dCArun.pro index 676dd16..42abcfa 100644 --- a/dCArun/dCArun.pro +++ b/dCArun/dCArun.pro @@ -41,6 +41,7 @@ DEFINES+=APP_EXTENDED_VERSION=\\\"$$EXTENDED_VERSION\\\" SOURCES += \ CArun.cpp \ CCWakelineAbstraction.cpp \ + DigitalOutputAbstraction.cpp \ main.cpp \ tslib.cpp \ datei.cpp @@ -48,6 +49,7 @@ SOURCES += \ HEADERS += \ CArun.h \ CCWakelineAbstraction.h \ + DigitalOutputAbstraction.h \ guidefs.h \ tslib.h \ versionHistory.txt \ From 0ea9ac9af3349ebfc83e9881b2bfa1fef7c515b9 Mon Sep 17 00:00:00 2001 From: Siegfried Siegert Date: Thu, 14 Nov 2024 14:03:13 +0100 Subject: [PATCH 2/4] dCArun: use DigitalOutputAbstraction --- dCArun/CArun.cpp | 7 +++++-- dCArun/CArun.h | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/dCArun/CArun.cpp b/dCArun/CArun.cpp index 02b2558..19e8657 100644 --- a/dCArun/CArun.cpp +++ b/dCArun/CArun.cpp @@ -1,7 +1,7 @@ #include "CArun.h" #include "datei.h" -#include "CCWakelineAbstraction.h" +#include "DigitalOutputAbstraction.h" CArun::CArun(QObject *parent) @@ -20,7 +20,10 @@ CArun::CArun(QObject *parent) this->timerChainCtrl->start(); - this->ccWakelineAbstraction = new CCWakelineAbstraction(this->HWaccess, this); + this->digitalOutputAbstraction = new DigitalOutputAbstraction(this->HWaccess, this); + this->digitalOutputAbstraction->addCCWake("/sys/class/leds/wakeupctrl_cc/brightness"); + this->digitalOutputAbstraction->addCCPower("/run/powerctrl_cc"); + this->digitalOutputAbstraction->addCCModem("/run/powerctrl_modem"); } diff --git a/dCArun/CArun.h b/dCArun/CArun.h index 4be909e..402bcd8 100644 --- a/dCArun/CArun.h +++ b/dCArun/CArun.h @@ -22,7 +22,7 @@ enum class SETUP_STEP { }; -class CCWakelineAbstraction; +class DigitalOutputAbstraction; class CArun : public QObject { @@ -45,7 +45,7 @@ private: void openSerialPort(); - CCWakelineAbstraction* ccWakelineAbstraction; + DigitalOutputAbstraction* digitalOutputAbstraction; signals: From cd6b1fed00b00fccd035a669e243069c75a34894 Mon Sep 17 00:00:00 2001 From: Siegfried Siegert Date: Thu, 14 Nov 2024 14:04:30 +0100 Subject: [PATCH 3/4] Remove CCWakelineAbstraction --- dCArun/CCWakelineAbstraction.cpp | 51 -------------------------------- dCArun/CCWakelineAbstraction.h | 25 ---------------- dCArun/dCArun.pro | 2 -- 3 files changed, 78 deletions(-) delete mode 100644 dCArun/CCWakelineAbstraction.cpp delete mode 100644 dCArun/CCWakelineAbstraction.h diff --git a/dCArun/CCWakelineAbstraction.cpp b/dCArun/CCWakelineAbstraction.cpp deleted file mode 100644 index d5513d6..0000000 --- a/dCArun/CCWakelineAbstraction.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include - -#include - -#include "CCWakelineAbstraction.h" -#include "plugin.h" - -/** - * this is based on a solution from: - * https://embeddeduse.com/2018/09/18/monitoring-sys-files-qfilesystemwatcher/ - * - */ - -CCWakelineAbstraction::CCWakelineAbstraction(hwinf *dc, QObject *parent) - : QObject(parent) - , dc(dc) -{ - auto ccWakeMonitor = new QFileSystemWatcher(this); - - ccWakeMonitor->addPath("/sys/class/leds/wakeupctrl_cc/brightness"); - connect(ccWakeMonitor, &QFileSystemWatcher::fileChanged, - this, &CCWakelineAbstraction::ccWakeChanged); - - qCritical() << "... init CCWakelineAbstraction"; -} - - -void CCWakelineAbstraction::ccWakeChanged(const QString &path) -{ - QFile ccWakeFile(path); - if (!ccWakeFile.open(QIODevice::ReadOnly)) { - qWarning() << "ERROR: Could not open ccWakeFile file."; - return; - } - auto ccWake = ccWakeFile.readAll(); - if (!ccWake.isEmpty()) { - int state = ccWake.at(0); - //qCritical() << "INFO: ccWake = " << state; - switch (state) { - case 0x30: // '1' - qCritical() << "INFO: ccWake -> sleep"; - this->dc->credit_switchWake(true); // switch 'sleep' - break; - case 0x31: // '0' - qCritical() << "INFO: ccWake -> wake"; - this->dc->credit_switchWake(false); // switch 'wake' - break; - } - } -} diff --git a/dCArun/CCWakelineAbstraction.h b/dCArun/CCWakelineAbstraction.h deleted file mode 100644 index 9d3cb06..0000000 --- a/dCArun/CCWakelineAbstraction.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef CCWAKELINEABSTRACTION_H -#define CCWAKELINEABSTRACTION_H - - -#include - -class hwinf; -class QFileSystemWatcher; - - -class CCWakelineAbstraction : public QObject -{ - Q_OBJECT - -public: - CCWakelineAbstraction(hwinf *dc, QObject *parent = nullptr); - -private: - hwinf *dc; - QFileSystemWatcher *ccWakeMonitor; - - void ccWakeChanged(const QString &path); -}; - -#endif // CCWAKELINEABSTRACTION_H diff --git a/dCArun/dCArun.pro b/dCArun/dCArun.pro index 42abcfa..5ad0a58 100644 --- a/dCArun/dCArun.pro +++ b/dCArun/dCArun.pro @@ -40,7 +40,6 @@ DEFINES+=APP_EXTENDED_VERSION=\\\"$$EXTENDED_VERSION\\\" SOURCES += \ CArun.cpp \ - CCWakelineAbstraction.cpp \ DigitalOutputAbstraction.cpp \ main.cpp \ tslib.cpp \ @@ -48,7 +47,6 @@ SOURCES += \ HEADERS += \ CArun.h \ - CCWakelineAbstraction.h \ DigitalOutputAbstraction.h \ guidefs.h \ tslib.h \ From 2c09402bbb1977dfb8c35728f763873eedf10ded Mon Sep 17 00:00:00 2001 From: Siegfried Siegert Date: Thu, 14 Nov 2024 14:27:53 +0100 Subject: [PATCH 4/4] dCArun: Fix DigitalOutputAbstraction --- dCArun/CArun.cpp | 1 + dCArun/DigitalOutputAbstraction.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/dCArun/CArun.cpp b/dCArun/CArun.cpp index 19e8657..d94460b 100644 --- a/dCArun/CArun.cpp +++ b/dCArun/CArun.cpp @@ -20,6 +20,7 @@ CArun::CArun(QObject *parent) this->timerChainCtrl->start(); + this->digitalOutputAbstraction = new DigitalOutputAbstraction(this->HWaccess, this); this->digitalOutputAbstraction->addCCWake("/sys/class/leds/wakeupctrl_cc/brightness"); this->digitalOutputAbstraction->addCCPower("/run/powerctrl_cc"); diff --git a/dCArun/DigitalOutputAbstraction.cpp b/dCArun/DigitalOutputAbstraction.cpp index 1635293..9b0dd80 100644 --- a/dCArun/DigitalOutputAbstraction.cpp +++ b/dCArun/DigitalOutputAbstraction.cpp @@ -18,9 +18,9 @@ DigitalOutputAbstraction::DigitalOutputAbstraction(hwinf *dc, QObject *parent) : QObject(parent) , dc(dc) { - auto fileMonitor = new QFileSystemWatcher(this); + this->fileMonitor = new QFileSystemWatcher(this); - connect(fileMonitor, &QFileSystemWatcher::fileChanged, + connect(this->fileMonitor, &QFileSystemWatcher::fileChanged, this, &DigitalOutputAbstraction::fileChanged); qCritical() << "... init DigitalOutputAbstraction"; @@ -74,7 +74,7 @@ void DigitalOutputAbstraction::fileChanged(const QString &path) { if (path == this->ccPowerPath) this->private_ccPowerChanged(); if (path == this->ccWakePath) this->private_ccWakeChanged(); - if (path == this->modemPowerPath) this->private_ccWakeChanged(); + if (path == this->modemPowerPath) this->private_modemPowerChanged(); } void DigitalOutputAbstraction::private_modemPowerChanged() @@ -87,7 +87,7 @@ void DigitalOutputAbstraction::private_modemPowerChanged() auto modemPower = modemPowerFile.readAll(); if (!modemPower.isEmpty()) { int state = modemPower.at(0); - //qCritical() << "INFO: modemPower = " << modemPower; + // qCritical() << "INFO: modemPower = " << state; switch (state) { case 0x30: // '0' qCritical() << "INFO: modemPower -> off"; @@ -126,7 +126,7 @@ void DigitalOutputAbstraction::private_ccPowerChanged() }; - //qCritical() << "INFO: modemPower = " << modemPower; + //qCritical() << "INFO: ccPower = " << state; switch (state) { case 0x30: // '0' qCritical() << "INFO: ccPower -> off";