forked from GerhardHoffmann/DCLibraries
		
	
		
			
				
	
	
		
			171 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			171 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include <QFileSystemWatcher>
 | 
						|
#include <QFile>
 | 
						|
#include <QFileInfo>
 | 
						|
#include <QTimer>
 | 
						|
 | 
						|
#include <QDebug>
 | 
						|
 | 
						|
#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)
 | 
						|
{
 | 
						|
    this->fileMonitor = new QFileSystemWatcher(this);
 | 
						|
 | 
						|
    connect(this->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_modemPowerChanged();
 | 
						|
}
 | 
						|
 | 
						|
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 = " << state;
 | 
						|
        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: ccPower = " << state;
 | 
						|
        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;
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |