From b8f943e73c84e368ebe6a380a7ca231e2a81f428 Mon Sep 17 00:00:00 2001 From: panky-codes Date: Thu, 14 Jan 2021 21:22:36 +0100 Subject: [PATCH 1/8] Implemented a circular buffer and added an average alg for batter remaining percentage. --- src/components/battery/BatteryController.cpp | 4 ++- src/components/battery/BatteryController.h | 32 ++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 3e3d65b4..e0042f5c 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -34,10 +34,12 @@ void Battery::Update() { // see https://forum.pine64.org/showthread.php?tid=8147 voltage = (value * 2.0f) / (1024/3.0f); - percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f; + float percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f; percentRemaining = std::max(percentRemaining, 0.0f); percentRemaining = std::min(percentRemaining, 100.0f); + percentRemainingBuffer.insert(percentRemaining); + // NRF_LOG_INFO("BATTERY " NRF_LOG_FLOAT_MARKER " %% - " NRF_LOG_FLOAT_MARKER " v", NRF_LOG_FLOAT(percentRemaining), NRF_LOG_FLOAT(voltage)); // NRF_LOG_INFO("POWER Charging : %d - Power : %d", isCharging, isPowerPresent); } diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 7cc964e4..a94adaad 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -1,14 +1,41 @@ #pragma once #include #include +#include +#include namespace Pinetime { namespace Controllers { + // A simple circular buffer that can be used to average + // out sensor values + template + class CircBuffer { + public: + CircBuffer() : arr{}, sz{}, cap{N}, loc{} {} + void insert(const float num) { + loc %= cap; + arr[loc++] = num; + if (sz != cap) { + sz++; + } + } + + float GetAverage() const { + float sum = std::accumulate(arr.begin(), arr.end(), 0); + return (sum / sz); + } + + private: + std::array arr; + uint8_t sz; + uint8_t cap; + uint8_t loc; + }; class Battery { public: void Init(); void Update(); - float PercentRemaining() const { return percentRemaining; } + float PercentRemaining() const { return percentRemainingBuffer.GetAverage(); } float Voltage() const { return voltage; } bool IsCharging() const { return isCharging; } bool IsPowerPresent() const { return isPowerPresent; } @@ -17,8 +44,9 @@ namespace Pinetime { static constexpr uint32_t chargingPin = 12; static constexpr uint32_t powerPresentPin = 19; static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7; + static constexpr uint8_t percentRemainingSamples = 10; static void SaadcEventHandler(nrfx_saadc_evt_t const * p_event); - float percentRemaining = 0.0f; + CircBuffer percentRemainingBuffer {}; float voltage = 0.0f; bool isCharging = false; bool isPowerPresent = false; From dece6c6bc8088be71e53df344f71b8457c4d4bc9 Mon Sep 17 00:00:00 2001 From: panky-codes Date: Thu, 14 Jan 2021 22:11:17 +0100 Subject: [PATCH 2/8] Adapted the accumulate function to return float. --- src/components/battery/BatteryController.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index a94adaad..21caffec 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -21,7 +21,7 @@ namespace Pinetime { } float GetAverage() const { - float sum = std::accumulate(arr.begin(), arr.end(), 0); + float sum = std::accumulate(arr.begin(), arr.end(), 0.0f); return (sum / sz); } @@ -31,6 +31,7 @@ namespace Pinetime { uint8_t cap; uint8_t loc; }; + class Battery { public: void Init(); From 11280d5f3704e174974805acd9e12542a3560207 Mon Sep 17 00:00:00 2001 From: panky-codes Date: Sat, 16 Jan 2021 14:55:13 +0100 Subject: [PATCH 3/8] Adjusted the firmware to display percent instead of gfx. --- src/displayapp/screens/Clock.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/displayapp/screens/Clock.cpp b/src/displayapp/screens/Clock.cpp index 57659141..5849f4cc 100644 --- a/src/displayapp/screens/Clock.cpp +++ b/src/displayapp/screens/Clock.cpp @@ -98,7 +98,8 @@ bool Clock::Refresh() { batteryPercentRemaining = batteryController.PercentRemaining(); if (batteryPercentRemaining.IsUpdated()) { auto batteryPercent = batteryPercentRemaining.Get(); - lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent)); + // lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent)); + lv_label_set_text(batteryIcon, std::to_string(batteryPercent).c_str()); auto isCharging = batteryController.IsCharging() || batteryController.IsPowerPresent(); lv_label_set_text(batteryPlug, BatteryIcon::GetPlugIcon(isCharging)); } From e2d1f8191563a6dc0232050b36e00c7b35c9bc21 Mon Sep 17 00:00:00 2001 From: panky-codes Date: Sat, 16 Jan 2021 19:51:32 +0100 Subject: [PATCH 4/8] Converted percent remaining calc to int. --- src/components/battery/BatteryController.cpp | 6 +++--- src/components/battery/BatteryController.h | 6 +++--- src/displayapp/screens/Clock.cpp | 3 +-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index e0042f5c..aaa245e4 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -34,9 +34,9 @@ void Battery::Update() { // see https://forum.pine64.org/showthread.php?tid=8147 voltage = (value * 2.0f) / (1024/3.0f); - float percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f; - percentRemaining = std::max(percentRemaining, 0.0f); - percentRemaining = std::min(percentRemaining, 100.0f); + int percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f; + percentRemaining = std::max(percentRemaining, 0); + percentRemaining = std::min(percentRemaining, 100); percentRemainingBuffer.insert(percentRemaining); diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 21caffec..0f2c2a42 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -20,8 +20,8 @@ namespace Pinetime { } } - float GetAverage() const { - float sum = std::accumulate(arr.begin(), arr.end(), 0.0f); + int GetAverage() const { + int sum = std::accumulate(arr.begin(), arr.end(), 0.0f); return (sum / sz); } @@ -36,7 +36,7 @@ namespace Pinetime { public: void Init(); void Update(); - float PercentRemaining() const { return percentRemainingBuffer.GetAverage(); } + int PercentRemaining() const { return percentRemainingBuffer.GetAverage(); } float Voltage() const { return voltage; } bool IsCharging() const { return isCharging; } bool IsPowerPresent() const { return isPowerPresent; } diff --git a/src/displayapp/screens/Clock.cpp b/src/displayapp/screens/Clock.cpp index 5849f4cc..57659141 100644 --- a/src/displayapp/screens/Clock.cpp +++ b/src/displayapp/screens/Clock.cpp @@ -98,8 +98,7 @@ bool Clock::Refresh() { batteryPercentRemaining = batteryController.PercentRemaining(); if (batteryPercentRemaining.IsUpdated()) { auto batteryPercent = batteryPercentRemaining.Get(); - // lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent)); - lv_label_set_text(batteryIcon, std::to_string(batteryPercent).c_str()); + lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent)); auto isCharging = batteryController.IsCharging() || batteryController.IsPowerPresent(); lv_label_set_text(batteryPlug, BatteryIcon::GetPlugIcon(isCharging)); } From 271ca78be55e2330b88bec90c7fe8f7a2e27bc87 Mon Sep 17 00:00:00 2001 From: panky-codes Date: Sat, 16 Jan 2021 20:18:55 +0100 Subject: [PATCH 5/8] Converted all the other float usage to int. --- src/components/battery/BatteryController.h | 2 +- src/displayapp/screens/BatteryIcon.cpp | 10 +++++----- src/displayapp/screens/BatteryIcon.h | 2 +- src/displayapp/screens/Clock.h | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 0f2c2a42..2329f162 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -7,7 +7,7 @@ namespace Pinetime { namespace Controllers { // A simple circular buffer that can be used to average - // out sensor values + // out the sensor values template class CircBuffer { public: diff --git a/src/displayapp/screens/BatteryIcon.cpp b/src/displayapp/screens/BatteryIcon.cpp index 4c102477..455b0c6d 100644 --- a/src/displayapp/screens/BatteryIcon.cpp +++ b/src/displayapp/screens/BatteryIcon.cpp @@ -3,11 +3,11 @@ using namespace Pinetime::Applications::Screens; -const char* BatteryIcon::GetBatteryIcon(float batteryPercent) { - if(batteryPercent > 90.0f) return Symbols::batteryFull; - if(batteryPercent > 75.0f) return Symbols::batteryThreeQuarter; - if(batteryPercent > 50.0f) return Symbols::batteryHalf; - if(batteryPercent > 25.0f) return Symbols::batteryOneQuarter; +const char* BatteryIcon::GetBatteryIcon(int batteryPercent) { + if(batteryPercent > 90) return Symbols::batteryFull; + if(batteryPercent > 75) return Symbols::batteryThreeQuarter; + if(batteryPercent > 50) return Symbols::batteryHalf; + if(batteryPercent > 25) return Symbols::batteryOneQuarter; return Symbols::batteryEmpty; } diff --git a/src/displayapp/screens/BatteryIcon.h b/src/displayapp/screens/BatteryIcon.h index f1001923..006e5621 100644 --- a/src/displayapp/screens/BatteryIcon.h +++ b/src/displayapp/screens/BatteryIcon.h @@ -6,7 +6,7 @@ namespace Pinetime { class BatteryIcon { public: static const char* GetUnknownIcon(); - static const char* GetBatteryIcon(float batteryPercent); + static const char* GetBatteryIcon(int batteryPercent); static const char* GetPlugIcon(bool isCharging); }; } diff --git a/src/displayapp/screens/Clock.h b/src/displayapp/screens/Clock.h index 4c5f60a0..c7f2d9db 100644 --- a/src/displayapp/screens/Clock.h +++ b/src/displayapp/screens/Clock.h @@ -62,7 +62,7 @@ namespace Pinetime { Pinetime::Controllers::DateTime::Days currentDayOfWeek = Pinetime::Controllers::DateTime::Days::Unknown; uint8_t currentDay = 0; - DirtyValue batteryPercentRemaining {0}; + DirtyValue batteryPercentRemaining {0}; DirtyValue bleState {false}; DirtyValue> currentDateTime; DirtyValue stepCount {0}; From 8697a06642a65df34dff4e97bbade736ee4402de Mon Sep 17 00:00:00 2001 From: panky-codes Date: Sat, 16 Jan 2021 20:36:34 +0100 Subject: [PATCH 6/8] Changed std accumulate to use int instead of a float. --- src/components/battery/BatteryController.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 2329f162..ecf8c91f 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -21,7 +21,7 @@ namespace Pinetime { } int GetAverage() const { - int sum = std::accumulate(arr.begin(), arr.end(), 0.0f); + int sum = std::accumulate(arr.begin(), arr.end(), 0); return (sum / sz); } From 952021cdb689f966fd745a37db0658f6787d9dc0 Mon Sep 17 00:00:00 2001 From: panky-codes Date: Sat, 16 Jan 2021 21:31:58 +0100 Subject: [PATCH 7/8] Changed float to int in battery controller file. --- src/components/battery/BatteryController.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index ecf8c91f..3138d243 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -12,7 +12,7 @@ namespace Pinetime { class CircBuffer { public: CircBuffer() : arr{}, sz{}, cap{N}, loc{} {} - void insert(const float num) { + void insert(const int num) { loc %= cap; arr[loc++] = num; if (sz != cap) { @@ -26,7 +26,7 @@ namespace Pinetime { } private: - std::array arr; + std::array arr; uint8_t sz; uint8_t cap; uint8_t loc; From b31c0e7e4571a2aef46672d49eedab0f31b820f4 Mon Sep 17 00:00:00 2001 From: panky-codes Date: Sun, 17 Jan 2021 11:06:24 +0100 Subject: [PATCH 8/8] Added more descriptive comments in doxygen format. --- src/components/battery/BatteryController.h | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 3138d243..86250a57 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -6,15 +6,21 @@ namespace Pinetime { namespace Controllers { - // A simple circular buffer that can be used to average - // out the sensor values + /** A simple circular buffer that can be used to average + out the sensor values. The total capacity of the CircBuffer + is given as the template parameter N. + */ template class CircBuffer { public: - CircBuffer() : arr{}, sz{}, cap{N}, loc{} {} + CircBuffer() : arr{}, sz{}, cap{N}, head{} {} + /** + insert member function overwrites the next data to the current + HEAD and moves the HEAD to the newly inserted value. + */ void insert(const int num) { - loc %= cap; - arr[loc++] = num; + head %= cap; + arr[head++] = num; if (sz != cap) { sz++; } @@ -26,10 +32,10 @@ namespace Pinetime { } private: - std::array arr; - uint8_t sz; - uint8_t cap; - uint8_t loc; + std::array arr; /**< internal array used to store the values*/ + uint8_t sz; /**< The current size of the array.*/ + uint8_t cap; /**< Total capacity of the CircBuffer.*/ + uint8_t head; /**< The current head of the CircBuffer*/ }; class Battery {