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"
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; }

View file

@ -21,6 +21,7 @@
#include <QObject>
#include <QString>
#include <QFile>
#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;

View file

@ -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);