Display momentary current value

This commit is contained in:
Matti Viljanen 2020-11-15 22:28:00 +02:00
parent a6561ac135
commit f047f421fa
4 changed files with 30 additions and 0 deletions

View file

@ -153,6 +153,11 @@ Page {
label: qsTr("Charge:")
value: battery.charge + "%"
}
MyDetailItem {
label: qsTr("Current:")
value: Math.floor(battery.current / 1000) + " mA"
}
MyDetailItem {
label: qsTr("Charger connected:")
value: battery.chargerConnected ? qsTr("yes") : qsTr("no")

View file

@ -26,6 +26,10 @@ Battery::Battery(Settings* newSettings, QObject* parent) : QObject(parent)
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
qInfo() << "Reading capacity from" << chargeFile->fileName();
// Number: battery/charging current, e.g. -1450000 (-145mA)
currentFile = new QFile("/sys/class/power_supply/battery/current_now", this);
qInfo() << "Reading current from" << currentFile->fileName();
// String: charging, discharging, full, empty, unknown (others?)
stateFile = new QFile("/sys/class/power_supply/battery/status", this);
qInfo() << "Reading charge state from" << stateFile->fileName();
@ -105,6 +109,15 @@ void Battery::updateData()
}
chargeFile->close();
}
if(currentFile && currentFile->open(QIODevice::ReadOnly)) {
nextCurrent = currentFile->readLine().trimmed().toInt();
if(nextCurrent != current) {
current = nextCurrent;
emit currentChanged(current);
qDebug() << "Current:" << current;
}
currentFile->close();
}
if(chargerConnectedFile && chargerConnectedFile->open(QIODevice::ReadOnly)) {
nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt();
if(nextChargerConnected != chargerConnected) {
@ -127,6 +140,8 @@ void Battery::updateData()
int Battery::getCharge(){ return charge; }
int Battery::getCurrent(){ return current; }
QString Battery::getState() { return state; }
bool Battery::getChargingEnabled() { return chargingEnabled; }

View file

@ -31,6 +31,7 @@ class Battery : public QObject
{
Q_OBJECT
Q_PROPERTY(int charge READ getCharge NOTIFY chargeChanged)
Q_PROPERTY(int current READ getCurrent NOTIFY currentChanged)
Q_PROPERTY(bool chargerConnected READ getChargerConnected NOTIFY chargerConnectedChanged)
Q_PROPERTY(QString state READ getState NOTIFY stateChanged)
Q_PROPERTY(bool chargingEnabled READ getChargingEnabled WRITE setChargingEnabled NOTIFY chargingEnabledChanged)
@ -40,6 +41,7 @@ public:
~Battery();
int getCharge();
int getCurrent();
bool getCharging();
bool getChargerConnected();
QString getState();
@ -52,6 +54,7 @@ public slots:
private:
QFile* chargeFile = nullptr;
QFile* currentFile = nullptr;
QFile* chargerConnectedFile = nullptr;
QFile* stateFile = nullptr;
QFile* chargingEnabledFile = nullptr;
@ -59,6 +62,7 @@ private:
// Default values:
int charge = 100; // 100% full
int current = 0; // Not charging/discharging
bool chargerConnected = false; // Charger plugged in
QString state = "idle"; // dis/charging, idle, unknown
bool chargingEnabled = true; // Only ever disabled manually
@ -68,12 +72,14 @@ private:
bool chargerIsEnabled = true;
int nextCharge = charge;
int nextCurrent = current;
bool nextChargerConnected = chargerConnected;
QString nextState = state;
bool nextChargingEnabled = chargingEnabled;
signals:
void chargeChanged(int);
void currentChanged(int);
void stateChanged(QString);
void chargingEnabledChanged(bool);
void chargerConnectedChanged(bool);

View file

@ -235,6 +235,10 @@
<source>Stop</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Current:</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Settings</name>