From 65a4d0953ea643d40ec4fc666e60d15e895dcb2f Mon Sep 17 00:00:00 2001 From: hassless Date: Sat, 19 Jun 2021 09:59:22 +0200 Subject: [PATCH 1/3] Calculate battery percentage from voltage using lookup table --- src/components/battery/BatteryController.cpp | 43 ++++++++++++++++---- src/components/battery/BatteryController.h | 1 + 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index bc14645796..9d41f908cd 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -54,10 +54,6 @@ void Battery::SaadcInit() { } void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { - - const float battery_max = 4.18; // maximum voltage of battery ( max charging voltage is 4.21 ) - const float battery_min = 3.20; // minimum voltage of battery before shutdown ( depends on the battery ) - if (p_event->type == NRFX_SAADC_EVT_DONE) { APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1)); @@ -65,11 +61,7 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { voltage = (static_cast(p_event->data.done.p_buffer[0]) * 2.04f) / (1024 / 3.0f); voltage = roundf(voltage * 100) / 100; - percentRemaining = static_cast(((voltage - battery_min) / (battery_max - battery_min)) * 100); - - percentRemaining = std::max(percentRemaining, 0); - percentRemaining = std::min(percentRemaining, 100); - + percentRemaining = GetPercentageFromVoltage(voltage); percentRemainingBuffer.insert(percentRemaining); samples++; @@ -81,3 +73,36 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { } } } + +// Voltage-to-percentage conversion table. Indexed by voltage, where index 0 represents 2.98v, index 1 +// represents 2.99v, and so on. Valid indices range from 0 to 120 (or 2.98v to 4.18v). +static const uint8_t scaledVoltageToPercentage[] = { + 0,0,0,0,0,0,1,1,1,1,1,1,1,1,1, + 2,2,2,2,2,2,2,2,2,3,3,3,3,3,3, + 3,4,4,4,4,4,4,4,5,5,5,5,5,5,6, + 6,6,6,7,7,7,8,8,9,9,10,11,11,12,13, + 14,14,15,16,17,19,20,22,24,26,28,32,37,41,46, + 49,51,53,55,57,59,60,62,64,65,67,68,70,71,73, + 74,75,77,78,80,81,82,83,84,85,86,88,89,90,91, + 92,93,94,94,95,96,97,98,98,99,99,100,100,100,100, + 100 +}; + + +int Battery::GetPercentageFromVoltage(float voltage) { + const int scaledVoltageMinimum = 298; // Minimum observed voltage (x100) of battery before shutdown. + const int scaledVoltageMaximum = 418; // Maximum observed voltage (x100) of battery (when not charging). + + // Scale up the floating point voltage (assuming 2 decimal digits) to an integer. + int scaledVoltage = static_cast(voltage * 100); + + // If the voltage is not within range of our conversion table, return clamped percentage. + if (scaledVoltage < scaledVoltageMinimum) + return 0; + if (scaledVoltage > scaledVoltageMaximum) + return 100; + + // Calculate the index into the table and return the battery percentage corresponding to the voltage. + int index = scaledVoltage - scaledVoltageMinimum; + return scaledVoltageToPercentage[index]; +} \ No newline at end of file diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 04bcf6b899..4c11e88410 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -81,6 +81,7 @@ namespace Pinetime { void SaadcEventHandler(nrfx_saadc_evt_t const* p_event); static void adcCallbackStatic(nrfx_saadc_evt_t const* event); + static int GetPercentageFromVoltage(float voltage); bool isReading = false; uint8_t samples = 0; From 3f50ac8bbda5b7e1db46fdda9b3a39d61fc9da2d Mon Sep 17 00:00:00 2001 From: hassless Date: Sat, 19 Jun 2021 10:01:39 +0200 Subject: [PATCH 2/3] added newline at eof --- 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 9d41f908cd..bac1b21d67 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -105,4 +105,4 @@ int Battery::GetPercentageFromVoltage(float voltage) { // Calculate the index into the table and return the battery percentage corresponding to the voltage. int index = scaledVoltage - scaledVoltageMinimum; return scaledVoltageToPercentage[index]; -} \ No newline at end of file +} From 8f5bf37156ec5a5aeb7c245759bab3c01db266e0 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sat, 19 Jun 2021 16:26:48 +0200 Subject: [PATCH 3/3] Update BatteryController.cpp remove redundant newline --- src/components/battery/BatteryController.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index bac1b21d67..142d56f817 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -88,7 +88,6 @@ static const uint8_t scaledVoltageToPercentage[] = { 100 }; - int Battery::GetPercentageFromVoltage(float voltage) { const int scaledVoltageMinimum = 298; // Minimum observed voltage (x100) of battery before shutdown. const int scaledVoltageMaximum = 418; // Maximum observed voltage (x100) of battery (when not charging).