diff --git a/application/src/battery.cpp b/application/src/battery.cpp index 1f69dfb..56599d4 100644 --- a/application/src/battery.cpp +++ b/application/src/battery.cpp @@ -79,6 +79,20 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO logL("Charger status file: " + (chargerConnectedFile ? chargerConnectedFile->fileName() : notFound)); + // Number: 0 or 1 + filenames.clear(); + filenames << "/sys/class/power_supply/ac/present" + << "/sys/class/power_supply/axp813-ac/present"; + + foreach(const QString& file, filenames) { + if(!acConnectedFile && QFile::exists(file)) { + acConnectedFile = new QFile(file, this); + break; + } + } + + logL("AC status file: " + (acConnectedFile ? acConnectedFile->fileName() : notFound)); + // Number: temperature filenames.clear(); filenames << "/sys/class/power_supply/battery/temp" @@ -170,6 +184,16 @@ void Battery::updateData() chargerConnectedFile->close(); } + if(acConnectedFile && acConnectedFile->open(QIODevice::ReadOnly)) { + nextAcConnected = acConnectedFile->readLine().trimmed().toInt(); + if(nextAcConnected != acConnected) { + acConnected = nextAcConnected; + emit acConnectedChanged(acConnected); + logM(QString("AC: %1").arg(acConnected ? "connected" : "disconnected")); + } + acConnectedFile->close(); + } + if(stateFile && stateFile->open(QIODevice::ReadOnly)) { nextState = (QString(stateFile->readLine().trimmed().toLower())); if(nextState != state) { @@ -183,7 +207,7 @@ void Battery::updateData() if(currentFile && currentFile->open(QIODevice::ReadOnly)) { current = currentFile->readLine().trimmed().toInt(); if(!invertDecided) { - invertCurrent = (!chargerConnected && current > 10); + invertCurrent = (!chargerConnected && !acConnected && current > 10); if(invertCurrent) logL("Battery current inverted"); else logL("Battery current not inverted"); invertDecided = true; @@ -230,3 +254,5 @@ int Battery::getTemperature(){ return temperature; } bool Battery::getChargingEnabled() { return chargingEnabled; } bool Battery::getChargerConnected() { return chargerConnected; } + +bool Battery::getAcConnected() { return acConnected; } diff --git a/application/src/battery.h b/application/src/battery.h index 508dfaa..b824891 100644 --- a/application/src/battery.h +++ b/application/src/battery.h @@ -33,6 +33,7 @@ class Battery : public QObject Q_PROPERTY(int current READ getCurrent NOTIFY currentChanged) Q_PROPERTY(int maxChargeCurrent READ getMaxChargeCurrent) Q_PROPERTY(bool chargerConnected READ getChargerConnected NOTIFY chargerConnectedChanged) + Q_PROPERTY(bool acConnected READ getAcConnected NOTIFY acConnectedChanged) Q_PROPERTY(QString state READ getState NOTIFY stateChanged) Q_PROPERTY(bool chargingEnabled READ getChargingEnabled NOTIFY chargingEnabledChanged) @@ -48,6 +49,7 @@ public: int getMaxChargeCurrent(); bool getCharging(); bool getChargerConnected(); + bool getAcConnected(); QString getState(); QString getHealth(); @@ -62,6 +64,7 @@ private: QFile* chargeFile = nullptr; QFile* currentFile = nullptr; QFile* chargerConnectedFile = nullptr; + QFile* acConnectedFile = nullptr; QFile* stateFile = nullptr; QFile* chargingEnabledFile = nullptr; QFile* maxChargeCurrentFile = nullptr; @@ -75,6 +78,7 @@ private: int charge = 100; // 100% full int current = 0; // Not charging/discharging bool chargerConnected = false; // Charger plugged in + bool acConnected = false; // AC plugged in QString state = "idle"; // dis/charging, idle, unknown bool chargingEnabled = true; // Only ever disabled manually int maxChargeCurrent = 0; // Charge current limit in micro amps @@ -91,9 +95,11 @@ private: bool invertDecided = false; bool nextChargerConnected = chargerConnected; + bool nextAcConnected = acConnected; QString nextState = state; bool nextChargingEnabled = chargingEnabled; + QString nextHealth = health; int nextTemperature = temperature; @@ -103,6 +109,7 @@ signals: void stateChanged(QString); void chargingEnabledChanged(bool); void chargerConnectedChanged(bool); + void acConnectedChanged(bool); void healthChanged(QString); void temperatureChanged(int); };