From d90c9c87dade8a8a0b5b332b72928dd439e22ddf Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Fri, 20 Mar 2020 20:58:01 +0200 Subject: [PATCH] Implement charger control logic --- src/battery.cpp | 13 ++++++++++++- src/battery.h | 5 ++++- src/harbour-batterybuddy.cpp | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/battery.cpp b/src/battery.cpp index 1cf9973..5e5f43e 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -17,8 +17,10 @@ */ #include "battery.h" -Battery::Battery(QObject* parent) : QObject(parent) +Battery::Battery(Settings* newSettings, QObject* parent) : QObject(parent) { + settings = newSettings; + // Number: meaning percentage, e.g. 42 chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this); // String: charging, discharging, full, empty, unknown (others?) @@ -96,6 +98,15 @@ void Battery::updateData() // } // chargingEnabledFile->close(); // } + + if(settings->getLimitEnabled()) { + if(chargingEnabled && charge >= settings->getHighLimit()) { + setChargingEnabled(false); + } + else if(!chargingEnabled && charge <= settings->getLowLimit()) { + setChargingEnabled(true); + } + } } int Battery::getCharge(){ return charge; } diff --git a/src/battery.h b/src/battery.h index fcbf82f..eeb0a31 100644 --- a/src/battery.h +++ b/src/battery.h @@ -21,6 +21,7 @@ #include #include #include +#include "settings.h" class Battery : public QObject { @@ -31,7 +32,7 @@ class Battery : public QObject Q_PROPERTY(bool chargingEnabled READ getChargingEnabled WRITE setChargingEnabled NOTIFY chargingEnabledChanged) public: - Battery(QObject* parent = nullptr); + Battery(Settings* newSettings, QObject* parent = nullptr); ~Battery(); int getCharge(); @@ -50,6 +51,7 @@ private: QFile* chargerConnectedFile; QFile* stateFile; QFile* chargingEnabledFile; + Settings* settings; // Default values: int charge = 100; // 100% full @@ -59,6 +61,7 @@ private: int enableChargingValue = 1; int disableChargingValue = 0; + bool chargerIsEnabled = true; int nextCharge = charge; bool nextChargerConnected = chargerConnected; diff --git a/src/harbour-batterybuddy.cpp b/src/harbour-batterybuddy.cpp index 8823a9c..30e66fd 100644 --- a/src/harbour-batterybuddy.cpp +++ b/src/harbour-batterybuddy.cpp @@ -45,10 +45,10 @@ int main(int argc, char *argv[]) QGuiApplication* app = SailfishApp::application(argc, argv); QQuickView* view = SailfishApp::createView(); - Battery* battery = new Battery(); Settings* settings = new Settings(); - QTimer* updater = new QTimer(); + Battery* battery = new Battery(settings); + QTimer* updater = new QTimer(); QObject::connect(updater, SIGNAL(timeout()), battery, SLOT(updateData())); updater->start(3000);