From c0b7c4ddce627a11f8e5a59249da3bfa80ad6a70 Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Wed, 10 Aug 2022 00:12:13 +0300 Subject: [PATCH] Invert the charge current if necessary, assume the value always changes --- application/src/battery.cpp | 14 +++++++++----- application/src/battery.h | 4 +++- service/src/battery.cpp | 12 ++++++++---- service/src/battery.h | 4 +++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/application/src/battery.cpp b/application/src/battery.cpp index 165a739..aa97732 100644 --- a/application/src/battery.cpp +++ b/application/src/battery.cpp @@ -187,12 +187,16 @@ void Battery::updateData() } if(currentFile && currentFile->open(QIODevice::ReadOnly)) { - nextCurrent = currentFile->readLine().trimmed().toInt(); - if(nextCurrent != current) { - current = nextCurrent; - emit currentChanged(current); - logD(QString("Current: %1mA").arg(current / 1000)); + current = currentFile->readLine().trimmed().toInt(); + if(!invertDecided) { + invertCurrent = (!chargerConnected && current > 10); + if(invertCurrent) logL("Battery current inverted"); + else logL("Battery current not inverted"); + invertDecided = true; } + current = current * (invertCurrent ? -1 : 1); + emit currentChanged(current); + logH(QString("Current: %1mA").arg(current / 1000)); currentFile->close(); } diff --git a/application/src/battery.h b/application/src/battery.h index 71b90aa..07f73ff 100644 --- a/application/src/battery.h +++ b/application/src/battery.h @@ -83,7 +83,9 @@ private: bool chargerIsEnabled = true; int nextCharge = charge; - int nextCurrent = current; + bool invertCurrent = false; + bool invertDecided = false; + bool nextChargerConnected = chargerConnected; QString nextState = state; bool nextChargingEnabled = chargingEnabled; diff --git a/service/src/battery.cpp b/service/src/battery.cpp index 84a2c62..70f3f6e 100644 --- a/service/src/battery.cpp +++ b/service/src/battery.cpp @@ -224,11 +224,15 @@ void Battery::updateData() } if(currentFile && currentFile->open(QIODevice::ReadOnly)) { - nextCurrent = currentFile->readLine().trimmed().toInt(); - if(nextCurrent != current) { - current = nextCurrent; - logV(QString("Current: %1mA").arg(current / 1000)); + current = currentFile->readLine().trimmed().toInt(); + if(!invertDecided) { + invertCurrent = (!chargerConnected && current > 10); + if(invertCurrent) logL("Battery current inverted"); + else logL("Battery current not inverted"); + invertDecided = true; } + current = current * (invertCurrent ? -1 : 1); + logH(QString("Current: %1mA").arg(current / 1000)); currentFile->close(); } diff --git a/service/src/battery.h b/service/src/battery.h index efeff37..3d1dd64 100644 --- a/service/src/battery.h +++ b/service/src/battery.h @@ -107,7 +107,9 @@ private: bool chargerIsEnabled = true; int nextCharge = charge; - int nextCurrent = current; + bool invertCurrent = false; + bool invertDecided = false; + bool nextChargerConnected = chargerConnected; QString nextState = state; bool nextChargingEnabled = chargingEnabled;