diff --git a/application/qml/components/BatteryGraph.qml b/application/qml/components/BatteryGraph.qml index 11c29ee..fdc0df1 100644 --- a/application/qml/components/BatteryGraph.qml +++ b/application/qml/components/BatteryGraph.qml @@ -45,6 +45,7 @@ Item { property int counter: 0 running: (enableLowBatteryAnimation && !battery.chargerConnected + && !battery.acConnected && _charge <= settings.lowAlert) repeat: true interval: 400 diff --git a/application/qml/pages/MainPage.qml b/application/qml/pages/MainPage.qml index 1b2e713..98749e5 100644 --- a/application/qml/pages/MainPage.qml +++ b/application/qml/pages/MainPage.qml @@ -132,8 +132,15 @@ Page { } MyDetailItem { + property bool connected: (battery.chargerConnected || battery.acConnected) + property string chargerType: { + if ( battery.chargerConnected && !battery.acConnected) { return qsTr("USB") } + else if (!battery.chargerConnected && battery.acConnected) { return qsTr("AC") } + else if ( battery.chargerConnected && battery.acConnected) { return qsTr("USB") + "/" + qsTr("AC") } + return "unknown power source" + } label: qsTr("Charger connected:") - value: battery.chargerConnected ? qsTr("yes") : qsTr("no") + value: connected ? (qsTr("yes") + " (" + chargerType + ")") : qsTr("no") } MyDetailItem { label: qsTr("State:") diff --git a/application/src/battery.cpp b/application/src/battery.cpp index 1f69dfb..780490a 100644 --- a/application/src/battery.cpp +++ b/application/src/battery.cpp @@ -79,6 +79,19 @@ 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"; + + 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 +183,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 +206,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 +253,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); }; diff --git a/service/src/battery.cpp b/service/src/battery.cpp index 5a3c0b6..ad97fc9 100644 --- a/service/src/battery.cpp +++ b/service/src/battery.cpp @@ -130,6 +130,19 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb logL("Charger status file: " + (chargerConnectedFile ? chargerConnectedFile->fileName() : notFound)); + // Charger connected, bool (number): 0 or 1 + filenames.clear(); + filenames << "/sys/class/power_supply/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" @@ -260,10 +273,19 @@ void Battery::updateData() chargerConnectedFile->close(); } + if(acConnectedFile && acConnectedFile->open(QIODevice::ReadOnly)) { + nextAcConnected = acConnectedFile->readLine().trimmed().toInt(); + if(nextAcConnected != acConnected) { + acConnected = nextAcConnected; + logM(QString("AC: %1").arg(acConnected ? "connected" : "disconnected")); + } + acConnectedFile->close(); + } + 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; @@ -507,6 +529,10 @@ bool Battery::getChargerConnected() { return chargerConnected; } +bool Battery::getAcConnected() { + return acConnected; +} + void Battery::shutdown() { logM("Shutting down..."); chargeNotification->close(); diff --git a/service/src/battery.h b/service/src/battery.h index f218a71..77bfd10 100644 --- a/service/src/battery.h +++ b/service/src/battery.h @@ -42,6 +42,7 @@ public: int getCharge(); bool getCharging(); bool getChargerConnected(); + bool getAcConnected(); QString getState(); bool getChargingEnabled(); @@ -78,6 +79,7 @@ private: Logger *logger; QFile *chargeFile = nullptr; QFile *chargerConnectedFile = nullptr; + QFile *acConnectedFile = nullptr; QFile *currentFile = nullptr; QFile *stateFile = nullptr; QFile *chargingEnabledFile = nullptr; @@ -97,6 +99,7 @@ private: int charge = 100; // 100% full int current = 0; // Charging/discharging current in microamps 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; @@ -114,6 +117,7 @@ private: bool invertDecided = false; bool nextChargerConnected = chargerConnected; + bool nextAcConnected = acConnected; QString nextState = state; bool nextChargingEnabled = chargingEnabled; int nextTemperature = temperature;