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:") label: qsTr("Charge:")
value: battery.charge + "%" value: battery.charge + "%"
} }
MyDetailItem {
label: qsTr("Current:")
value: Math.floor(battery.current / 1000) + " mA"
}
MyDetailItem { MyDetailItem {
label: qsTr("Charger connected:") label: qsTr("Charger connected:")
value: battery.chargerConnected ? qsTr("yes") : qsTr("no") 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); chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
qInfo() << "Reading capacity from" << chargeFile->fileName(); 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?) // String: charging, discharging, full, empty, unknown (others?)
stateFile = new QFile("/sys/class/power_supply/battery/status", this); stateFile = new QFile("/sys/class/power_supply/battery/status", this);
qInfo() << "Reading charge state from" << stateFile->fileName(); qInfo() << "Reading charge state from" << stateFile->fileName();
@ -105,6 +109,15 @@ void Battery::updateData()
} }
chargeFile->close(); 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)) { if(chargerConnectedFile && chargerConnectedFile->open(QIODevice::ReadOnly)) {
nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt(); nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt();
if(nextChargerConnected != chargerConnected) { if(nextChargerConnected != chargerConnected) {
@ -127,6 +140,8 @@ void Battery::updateData()
int Battery::getCharge(){ return charge; } int Battery::getCharge(){ return charge; }
int Battery::getCurrent(){ return current; }
QString Battery::getState() { return state; } QString Battery::getState() { return state; }
bool Battery::getChargingEnabled() { return chargingEnabled; } bool Battery::getChargingEnabled() { return chargingEnabled; }

View file

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

View file

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