From 9546275d22bc2a9667ee3f915d34c6dcef95dffd Mon Sep 17 00:00:00 2001 From: Alex Dolzhenkov Date: Sat, 29 Oct 2022 12:20:44 +1300 Subject: [PATCH 1/8] Add linear approximation and use it for improving battery percentage --- src/components/battery/BatteryController.cpp | 14 ++++--- src/components/utility/LinearApproximation.h | 39 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/components/utility/LinearApproximation.h diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 300d097827..cdcb1c8489 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -1,4 +1,5 @@ #include "components/battery/BatteryController.h" +#include "components/utility/LinearApproximation.h" #include "drivers/PinMap.h" #include #include @@ -60,8 +61,13 @@ void Battery::SaadcInit() { } void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { - const uint16_t battery_max = 4180; // maximum voltage of battery ( max charging voltage is 4.21 ) - const uint16_t battery_min = 3200; // minimum voltage of battery before shutdown ( depends on the battery ) + static const Utility::LinearApproximation aprox {{{ + {3200, 0}, // minimum voltage of battery before shutdown ( depends on the battery ) + {3600, 10}, // keen point corresponded to 10% of battery + {3700, 25}, + {3800, 50}, + {4180, 100} // maximum voltage of battery ( max charging voltage is 4.21 ) + }}}; if (p_event->type == NRFX_SAADC_EVT_DONE) { @@ -77,10 +83,8 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { uint8_t newPercent; if (isFull) { newPercent = 100; - } else if (voltage < battery_min) { - newPercent = 0; } else { - newPercent = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100); + newPercent = std::min(aprox.GetValue(voltage), (isCharging ? uint8_t{99} : uint8_t{100})); } if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) { diff --git a/src/components/utility/LinearApproximation.h b/src/components/utility/LinearApproximation.h new file mode 100644 index 0000000000..181fabb53b --- /dev/null +++ b/src/components/utility/LinearApproximation.h @@ -0,0 +1,39 @@ +#pragma once + +#include // for std::array +#include // for std::pair + +namespace Pinetime { + namespace Utility { + + // based on: https://github.com/SHristov92/LinearApproximation/blob/main/Linear.h + template + class LinearApproximation { + using Point = struct { + Key key; + Value value; + }; + + public: + LinearApproximation(const std::array &&points) + : points{points} {} + + Value GetValue(Key key) const { + if (key <= points[0].key) { + return points[0].value; + } + + for (size_t i = 1; i < Size; i++) { + if (key < points[i].key) { + return points[i-1].value + (key - points[i-1].key) * (points[i].value - points[i-1].value) / (points[i].key - points[i-1].key); + } + } + + return points[Size - 1].value; + } + + private: + std::array points; + }; + } +} From 4a83a3c4fdbe1f6d32816b64f882aed29620025d Mon Sep 17 00:00:00 2001 From: Alex Dolzhenkov Date: Sat, 29 Oct 2022 12:45:27 +1300 Subject: [PATCH 2/8] Tidied up --- src/components/utility/LinearApproximation.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/utility/LinearApproximation.h b/src/components/utility/LinearApproximation.h index 181fabb53b..bfa8e5d0e7 100644 --- a/src/components/utility/LinearApproximation.h +++ b/src/components/utility/LinearApproximation.h @@ -1,7 +1,6 @@ #pragma once -#include // for std::array -#include // for std::pair +#include namespace Pinetime { namespace Utility { From 6251b5d4b65740267c0d77c6efb607067a4fbbd6 Mon Sep 17 00:00:00 2001 From: Alex Dolzhenkov Date: Tue, 1 Nov 2022 08:32:44 +1300 Subject: [PATCH 3/8] Fixed style --- src/components/battery/BatteryController.cpp | 5 +++-- src/components/utility/LinearApproximation.h | 12 ++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index cdcb1c8489..86c2026fc6 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -61,11 +61,12 @@ void Battery::SaadcInit() { } void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { - static const Utility::LinearApproximation aprox {{{ + static const Utility::LinearApproximation aprox {{{ {3200, 0}, // minimum voltage of battery before shutdown ( depends on the battery ) {3600, 10}, // keen point corresponded to 10% of battery {3700, 25}, - {3800, 50}, + {3750, 50}, + {3900, 75}, {4180, 100} // maximum voltage of battery ( max charging voltage is 4.21 ) }}}; diff --git a/src/components/utility/LinearApproximation.h b/src/components/utility/LinearApproximation.h index bfa8e5d0e7..da501a4fdc 100644 --- a/src/components/utility/LinearApproximation.h +++ b/src/components/utility/LinearApproximation.h @@ -14,8 +14,9 @@ namespace Pinetime { }; public: - LinearApproximation(const std::array &&points) - : points{points} {} + LinearApproximation(const std::array&& sorted_points) + : points{sorted_points} { + } Value GetValue(Key key) const { if (key <= points[0].key) { @@ -23,8 +24,11 @@ namespace Pinetime { } for (size_t i = 1; i < Size; i++) { - if (key < points[i].key) { - return points[i-1].value + (key - points[i-1].key) * (points[i].value - points[i-1].value) / (points[i].key - points[i-1].key); + const auto& p = points[i]; + const auto& p_prev = points[i - 1]; + + if (key < p.key) { + return p_prev.value + (key - p_prev.key) * (p.value - p_prev.value) / (p.key - p_prev.key); } } From 269fb66b75e6e6ee337c71aa02d52b7001fb970a Mon Sep 17 00:00:00 2001 From: Alex Dolzhenkov Date: Tue, 1 Nov 2022 08:39:22 +1300 Subject: [PATCH 4/8] Fixed style --- src/components/battery/BatteryController.cpp | 2 +- src/components/utility/LinearApproximation.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 86c2026fc6..10d15f38c1 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -61,7 +61,7 @@ void Battery::SaadcInit() { } void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { - static const Utility::LinearApproximation aprox {{{ + static const Utility::LinearApproximation aprox{{{ {3200, 0}, // minimum voltage of battery before shutdown ( depends on the battery ) {3600, 10}, // keen point corresponded to 10% of battery {3700, 25}, diff --git a/src/components/utility/LinearApproximation.h b/src/components/utility/LinearApproximation.h index da501a4fdc..d7c2797077 100644 --- a/src/components/utility/LinearApproximation.h +++ b/src/components/utility/LinearApproximation.h @@ -1,5 +1,6 @@ #pragma once +#include #include namespace Pinetime { @@ -23,7 +24,7 @@ namespace Pinetime { return points[0].value; } - for (size_t i = 1; i < Size; i++) { + for (std::size_t i = 1; i < Size; i++) { const auto& p = points[i]; const auto& p_prev = points[i - 1]; From 00f788007abbe8dbb96d63c20ea2b0a30b87b6d5 Mon Sep 17 00:00:00 2001 From: Alex Dolzhenkov Date: Tue, 1 Nov 2022 22:13:37 +1300 Subject: [PATCH 5/8] Changed the minimum voltage level to 3.5V --- src/components/battery/BatteryController.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 10d15f38c1..750cad5b15 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -62,7 +62,7 @@ void Battery::SaadcInit() { void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { static const Utility::LinearApproximation aprox{{{ - {3200, 0}, // minimum voltage of battery before shutdown ( depends on the battery ) + {3500, 0}, // minimum voltage of battery before shutdown ( depends on the battery ) {3600, 10}, // keen point corresponded to 10% of battery {3700, 25}, {3750, 50}, From bfc6efb5d9682ed054f25b489bf4f5b0a9aca053 Mon Sep 17 00:00:00 2001 From: Alex Dolzhenkov Date: Fri, 4 Nov 2022 08:07:18 +1300 Subject: [PATCH 6/8] Fix style to match InfinityTime coding standard --- src/components/battery/BatteryController.cpp | 4 ++-- src/components/utility/LinearApproximation.h | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 750cad5b15..05ee9fcb39 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -61,7 +61,7 @@ void Battery::SaadcInit() { } void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { - static const Utility::LinearApproximation aprox{{{ + static const Utility::LinearApproximation aprox {{{ {3500, 0}, // minimum voltage of battery before shutdown ( depends on the battery ) {3600, 10}, // keen point corresponded to 10% of battery {3700, 25}, @@ -85,7 +85,7 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { if (isFull) { newPercent = 100; } else { - newPercent = std::min(aprox.GetValue(voltage), (isCharging ? uint8_t{99} : uint8_t{100})); + newPercent = std::min(aprox.GetValue(voltage), (isCharging ? uint8_t {99} : uint8_t {100})); } if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) { diff --git a/src/components/utility/LinearApproximation.h b/src/components/utility/LinearApproximation.h index d7c2797077..f7104ced28 100644 --- a/src/components/utility/LinearApproximation.h +++ b/src/components/utility/LinearApproximation.h @@ -7,16 +7,14 @@ namespace Pinetime { namespace Utility { // based on: https://github.com/SHristov92/LinearApproximation/blob/main/Linear.h - template - class LinearApproximation { + template class LinearApproximation { using Point = struct { Key key; Value value; }; public: - LinearApproximation(const std::array&& sorted_points) - : points{sorted_points} { + LinearApproximation(const std::array&& sorted_points) : points {sorted_points} { } Value GetValue(Key key) const { From cd83d8b21f54045f1ffa313d5a83508d8e127e17 Mon Sep 17 00:00:00 2001 From: Alex Dolzhenkov Date: Mon, 7 Nov 2022 21:58:36 +1300 Subject: [PATCH 7/8] Update src/components/battery/BatteryController.cpp Removed redundant brackets Co-authored-by: NeroBurner --- src/components/battery/BatteryController.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 05ee9fcb39..b29b400750 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -85,7 +85,7 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { if (isFull) { newPercent = 100; } else { - newPercent = std::min(aprox.GetValue(voltage), (isCharging ? uint8_t {99} : uint8_t {100})); + newPercent = std::min(aprox.GetValue(voltage), isCharging ? uint8_t {99} : uint8_t {100}); } if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) { From 090e046a6176aaf4ce8132347d416d297b630a2f Mon Sep 17 00:00:00 2001 From: Avamander Date: Thu, 10 Nov 2022 22:58:43 +0200 Subject: [PATCH 8/8] Minor improvements to the comment --- src/components/battery/BatteryController.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index b29b400750..b61f0ce3df 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -62,12 +62,12 @@ void Battery::SaadcInit() { void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { static const Utility::LinearApproximation aprox {{{ - {3500, 0}, // minimum voltage of battery before shutdown ( depends on the battery ) - {3600, 10}, // keen point corresponded to 10% of battery + {3500, 0}, // Minimum voltage before shutdown (depends on the battery) + {3600, 10}, // Keen point that corresponds to 10% {3700, 25}, {3750, 50}, {3900, 75}, - {4180, 100} // maximum voltage of battery ( max charging voltage is 4.21 ) + {4180, 100} // Maximum voltage during charging is 4.21V }}}; if (p_event->type == NRFX_SAADC_EVT_DONE) {