Implement charger control logic

This commit is contained in:
Matti Viljanen 2020-03-20 20:58:01 +02:00
parent e1b59a98a5
commit d90c9c87da
No known key found for this signature in database
GPG key ID: CF32A1495158F888
3 changed files with 18 additions and 4 deletions

View file

@ -17,8 +17,10 @@
*/ */
#include "battery.h" #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 // Number: meaning percentage, e.g. 42
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this); chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
// String: charging, discharging, full, empty, unknown (others?) // String: charging, discharging, full, empty, unknown (others?)
@ -96,6 +98,15 @@ void Battery::updateData()
// } // }
// chargingEnabledFile->close(); // 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; } int Battery::getCharge(){ return charge; }

View file

@ -21,6 +21,7 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QFile> #include <QFile>
#include "settings.h"
class Battery : public QObject class Battery : public QObject
{ {
@ -31,7 +32,7 @@ class Battery : public QObject
Q_PROPERTY(bool chargingEnabled READ getChargingEnabled WRITE setChargingEnabled NOTIFY chargingEnabledChanged) Q_PROPERTY(bool chargingEnabled READ getChargingEnabled WRITE setChargingEnabled NOTIFY chargingEnabledChanged)
public: public:
Battery(QObject* parent = nullptr); Battery(Settings* newSettings, QObject* parent = nullptr);
~Battery(); ~Battery();
int getCharge(); int getCharge();
@ -50,6 +51,7 @@ private:
QFile* chargerConnectedFile; QFile* chargerConnectedFile;
QFile* stateFile; QFile* stateFile;
QFile* chargingEnabledFile; QFile* chargingEnabledFile;
Settings* settings;
// Default values: // Default values:
int charge = 100; // 100% full int charge = 100; // 100% full
@ -59,6 +61,7 @@ private:
int enableChargingValue = 1; int enableChargingValue = 1;
int disableChargingValue = 0; int disableChargingValue = 0;
bool chargerIsEnabled = true;
int nextCharge = charge; int nextCharge = charge;
bool nextChargerConnected = chargerConnected; bool nextChargerConnected = chargerConnected;

View file

@ -45,10 +45,10 @@ int main(int argc, char *argv[])
QGuiApplication* app = SailfishApp::application(argc, argv); QGuiApplication* app = SailfishApp::application(argc, argv);
QQuickView* view = SailfishApp::createView(); QQuickView* view = SailfishApp::createView();
Battery* battery = new Battery();
Settings* settings = new Settings(); 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())); QObject::connect(updater, SIGNAL(timeout()), battery, SLOT(updateData()));
updater->start(3000); updater->start(3000);