From e0b5bd72d4b7b910eb09b7aaaca04872f7f5e8b4 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 12:25:15 +0200 Subject: [PATCH 01/12] Update BatteryController.h --- src/components/battery/BatteryController.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 5a7394c4d4..334c6df121 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -49,6 +49,7 @@ namespace Pinetime { void SaadcEventHandler(nrfx_saadc_evt_t const* p_event); static void AdcCallbackStatic(nrfx_saadc_evt_t const* event); + static int GetBatteryPercentageFromVoltage(float voltage); bool isReading = false; From e206a0379b4fa0711b65c81b2c91492093665dd2 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 13:30:59 +0200 Subject: [PATCH 02/12] Implemented GetBatteryPercentageFromVoltage --- src/components/battery/BatteryController.cpp | 59 ++++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 300d097827..86163e1f33 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -60,8 +60,6 @@ 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 ) if (p_event->type == NRFX_SAADC_EVT_DONE) { @@ -77,10 +75,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(GetBatteryPercentageFromVoltage(voltage), isCharging ? 99 : 100); } if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) { @@ -94,6 +90,59 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { } } + void Battery::Register(Pinetime::System::SystemTask* systemTask) { this->systemTask = systemTask; } + +// The number of line segments used to approximate the battery discharge curve. +static const int LINE_SEGMENT_COUNT = 7; + +// The voltages at the endpoints of the line segments. Any two consecutive values +// represent the start and end voltage of a line segment. +static const float voltageOffsets[LINE_SEGMENT_COUNT + 1] { + 4.180, + 4.084, + 3.912, + 3.763, + 3.721, + 3.672, + 3.613, + 3.500 +}; + +// The battery percentages at the endpoints of the line segments. Note that last +// value is omitted: It is not needed because we only need the percentages at the +// start of each line segment. +static const float percentageOffsets[LINE_SEGMENT_COUNT] { + 100.000, + 96.362, + 76.664, + 51.908, + 40.905, + 19.343, + 9.139 + //0.000 +}; + +// The pre-calculated slopes (in battery percentage per volt) of the line segments. +static const float percentageSlopes[LINE_SEGMENT_COUNT] { + 31.940, + 119.893, + 166.148, + 261.976, + 440.041, + 191.537, + 76.271 +}; + +int Battery::GetBatteryPercentageFromVoltage(float voltage) { + if (voltage > voltageOffsets[0]) + return 100; + + for (int i = 0; i < LINE_SEGMENT_COUNT; i++) + if (voltage > voltageOffsets[i + 1]) + return percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]); + + return 0; +} From 4a88ce0e30c261e3c57fa048889b463d7b2767c5 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 13:33:00 +0200 Subject: [PATCH 03/12] Update BatteryController.h --- 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 334c6df121..2567da7606 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -49,7 +49,7 @@ namespace Pinetime { void SaadcEventHandler(nrfx_saadc_evt_t const* p_event); static void AdcCallbackStatic(nrfx_saadc_evt_t const* event); - static int GetBatteryPercentageFromVoltage(float voltage); + static uint8_t GetBatteryPercentageFromVoltage(float voltage); bool isReading = false; From 5aa12a22836fc2ca399dd155a65872aadadb3e28 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 13:37:47 +0200 Subject: [PATCH 04/12] Update BatteryController.cpp --- 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 86163e1f33..73a4e45202 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -142,7 +142,7 @@ int Battery::GetBatteryPercentageFromVoltage(float voltage) { for (int i = 0; i < LINE_SEGMENT_COUNT; i++) if (voltage > voltageOffsets[i + 1]) - return percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]); + return static_cast(round(percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]))); return 0; } From 65bc85bcdaa5902fca34921c7b00f516622fda4c Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 13:38:13 +0200 Subject: [PATCH 05/12] Update BatteryController.cpp --- 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 73a4e45202..48030c75c7 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -142,7 +142,7 @@ int Battery::GetBatteryPercentageFromVoltage(float voltage) { for (int i = 0; i < LINE_SEGMENT_COUNT; i++) if (voltage > voltageOffsets[i + 1]) - return static_cast(round(percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]))); + return static_cast(round(percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]))); return 0; } From 6a71809f6b84aa06abe8f8b40d45be4a3c323773 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 13:38:29 +0200 Subject: [PATCH 06/12] Update BatteryController.cpp --- 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 48030c75c7..a981774695 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -136,7 +136,7 @@ static const float percentageSlopes[LINE_SEGMENT_COUNT] { 76.271 }; -int Battery::GetBatteryPercentageFromVoltage(float voltage) { +uint8_t Battery::GetBatteryPercentageFromVoltage(float voltage) { if (voltage > voltageOffsets[0]) return 100; From d3b844600d093a849899f859b42ad09f4b49cf8a Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 13:43:15 +0200 Subject: [PATCH 07/12] Update BatteryController.cpp --- 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 a981774695..c7fb3672ca 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -142,7 +142,7 @@ uint8_t Battery::GetBatteryPercentageFromVoltage(float voltage) { for (int i = 0; i < LINE_SEGMENT_COUNT; i++) if (voltage > voltageOffsets[i + 1]) - return static_cast(round(percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]))); + return static_cast(roundf(percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]))); return 0; } From 34adecfe1b0e33e39df11df23ba7905a058c9862 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 14:52:04 +0200 Subject: [PATCH 08/12] Update BatteryController.cpp --- src/components/battery/BatteryController.cpp | 47 ++++++++++---------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index c7fb3672ca..8142ddd387 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -98,22 +98,22 @@ void Battery::Register(Pinetime::System::SystemTask* systemTask) { // The number of line segments used to approximate the battery discharge curve. static const int LINE_SEGMENT_COUNT = 7; -// The voltages at the endpoints of the line segments. Any two consecutive values -// represent the start and end voltage of a line segment. -static const float voltageOffsets[LINE_SEGMENT_COUNT + 1] { - 4.180, - 4.084, - 3.912, - 3.763, - 3.721, - 3.672, - 3.613, - 3.500 +// The voltages (mV) at the endpoints of the line segments. Any two consecutive +// values represent the start and end voltage of a line segment. +static const uint16_t voltageOffsets[LINE_SEGMENT_COUNT + 1] { + 4180, + 4084, + 3912, + 3763, + 3721, + 3672, + 3613, + 3500 }; // The battery percentages at the endpoints of the line segments. Note that last -// value is omitted: It is not needed because we only need the percentages at the -// start of each line segment. +// value is omitted: It is not needed because we only need the percentages at +// the start of each line segment. static const float percentageOffsets[LINE_SEGMENT_COUNT] { 100.000, 96.362, @@ -125,22 +125,23 @@ static const float percentageOffsets[LINE_SEGMENT_COUNT] { //0.000 }; -// The pre-calculated slopes (in battery percentage per volt) of the line segments. +// The pre-calculated slopes (in battery percentage points per millivolt) of the +// line segments. static const float percentageSlopes[LINE_SEGMENT_COUNT] { - 31.940, - 119.893, - 166.148, - 261.976, - 440.041, - 191.537, - 76.271 + 0.031940, + 0.119893, + 0.166148, + 0.261976, + 0.440041, + 0.191537, + 0.076271 }; -uint8_t Battery::GetBatteryPercentageFromVoltage(float voltage) { +uint8_t Battery::GetBatteryPercentageFromVoltage(uint16_t voltage) { if (voltage > voltageOffsets[0]) return 100; - for (int i = 0; i < LINE_SEGMENT_COUNT; i++) + for (uint8_t i = 0; i < LINE_SEGMENT_COUNT; i++) if (voltage > voltageOffsets[i + 1]) return static_cast(roundf(percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]))); From 0bc20376a7cebd811e7bd61af50784877d8ed6db Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 14:52:34 +0200 Subject: [PATCH 09/12] Update BatteryController.h --- 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 2567da7606..15559916a1 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -49,7 +49,7 @@ namespace Pinetime { void SaadcEventHandler(nrfx_saadc_evt_t const* p_event); static void AdcCallbackStatic(nrfx_saadc_evt_t const* event); - static uint8_t GetBatteryPercentageFromVoltage(float voltage); + static uint8_t GetBatteryPercentageFromVoltage(uint16_t voltage); bool isReading = false; From 3f078a5e4ec0d7bbe31c87a06f17528b485b62a2 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Sun, 15 Aug 2021 15:02:02 +0200 Subject: [PATCH 10/12] Update BatteryController.cpp --- 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 8142ddd387..8632538431 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -96,7 +96,7 @@ void Battery::Register(Pinetime::System::SystemTask* systemTask) { } // The number of line segments used to approximate the battery discharge curve. -static const int LINE_SEGMENT_COUNT = 7; +static const uint8_t LINE_SEGMENT_COUNT = 7; // The voltages (mV) at the endpoints of the line segments. Any two consecutive // values represent the start and end voltage of a line segment. From 350e6b2511f40080c161c8334afde7e584c48a22 Mon Sep 17 00:00:00 2001 From: hassless <85612141+hassless@users.noreply.github.com> Date: Wed, 1 Sep 2021 20:10:17 +0200 Subject: [PATCH 11/12] Update battery discharge curve tables --- src/components/battery/BatteryController.cpp | 40 ++++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 8632538431..67b500bc79 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -101,13 +101,13 @@ static const uint8_t LINE_SEGMENT_COUNT = 7; // The voltages (mV) at the endpoints of the line segments. Any two consecutive // values represent the start and end voltage of a line segment. static const uint16_t voltageOffsets[LINE_SEGMENT_COUNT + 1] { - 4180, - 4084, - 3912, - 3763, - 3721, - 3672, - 3613, + 4157, + 4063, + 3882, + 3747, + 3716, + 3678, + 3583, 3500 }; @@ -116,25 +116,25 @@ static const uint16_t voltageOffsets[LINE_SEGMENT_COUNT + 1] { // the start of each line segment. static const float percentageOffsets[LINE_SEGMENT_COUNT] { 100.000, - 96.362, - 76.664, - 51.908, - 40.905, - 19.343, - 9.139 + 95.197, + 70.429, + 48.947, + 35.158, + 18.971, + 5.801 //0.000 }; // The pre-calculated slopes (in battery percentage points per millivolt) of the // line segments. static const float percentageSlopes[LINE_SEGMENT_COUNT] { - 0.031940, - 0.119893, - 0.166148, - 0.261976, - 0.440041, - 0.191537, - 0.076271 + 0.05109, + 0.13684, + 0.15913, + 0.44481, + 0.42595, + 0.13863, + 0.06989 }; uint8_t Battery::GetBatteryPercentageFromVoltage(uint16_t voltage) { From 963bbdef056b0e02607ee519affeb0c64360bb15 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 7 Feb 2022 12:33:27 +0200 Subject: [PATCH 12/12] BatteryController fixes --- src/components/battery/BatteryController.cpp | 78 ++++++++------------ 1 file changed, 29 insertions(+), 49 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 67b500bc79..206b26ed4d 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -76,7 +76,7 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { if (isFull) { newPercent = 100; } else { - newPercent = std::min(GetBatteryPercentageFromVoltage(voltage), isCharging ? 99 : 100); + newPercent = std::min(GetBatteryPercentageFromVoltage(voltage), static_cast(isCharging ? 99 : 100)); } if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) { @@ -90,60 +90,40 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { } } - void Battery::Register(Pinetime::System::SystemTask* systemTask) { this->systemTask = systemTask; } -// The number of line segments used to approximate the battery discharge curve. -static const uint8_t LINE_SEGMENT_COUNT = 7; - -// The voltages (mV) at the endpoints of the line segments. Any two consecutive -// values represent the start and end voltage of a line segment. -static const uint16_t voltageOffsets[LINE_SEGMENT_COUNT + 1] { - 4157, - 4063, - 3882, - 3747, - 3716, - 3678, - 3583, - 3500 -}; - -// The battery percentages at the endpoints of the line segments. Note that last -// value is omitted: It is not needed because we only need the percentages at -// the start of each line segment. -static const float percentageOffsets[LINE_SEGMENT_COUNT] { - 100.000, - 95.197, - 70.429, - 48.947, - 35.158, - 18.971, - 5.801 - //0.000 -}; - -// The pre-calculated slopes (in battery percentage points per millivolt) of the -// line segments. -static const float percentageSlopes[LINE_SEGMENT_COUNT] { - 0.05109, - 0.13684, - 0.15913, - 0.44481, - 0.42595, - 0.13863, - 0.06989 -}; - uint8_t Battery::GetBatteryPercentageFromVoltage(uint16_t voltage) { - if (voltage > voltageOffsets[0]) + // The number of line segments used to approximate the battery discharge curve. + static const uint8_t LINE_SEGMENT_COUNT = 7; + + // The voltages (mV) at the endpoints of the line segments. Any two consecutive + // values represent the start and end voltage of a line segment. + static const uint16_t voltageOffsets[LINE_SEGMENT_COUNT + 1] {4157, 4063, 3882, 3747, 3716, 3678, 3583, 3500}; + + // The battery percentages at the endpoints of the line segments. Note that last + // value is omitted: It is not needed because we only need the percentages at + // the start of each line segment. + static const float percentageOffsets[LINE_SEGMENT_COUNT] {100.000, 95.197, 70.429, 48.947, 35.158, 18.971, 5.801}; + + // The pre-calculated slopes (in battery percentage points per millivolt) of the + // line segments. + static const float percentageSlopes[LINE_SEGMENT_COUNT] {0.05109, 0.13684, 0.15913, 0.44481, 0.42595, 0.13863, 0.06989}; + + if (voltage >= voltageOffsets[0]) { return 100; - - for (uint8_t i = 0; i < LINE_SEGMENT_COUNT; i++) - if (voltage > voltageOffsets[i + 1]) + } + + if (voltage <= voltageOffsets[7]) { + return 0; + } + + for (uint8_t i = 0; i < LINE_SEGMENT_COUNT; i++) { + if (voltage > voltageOffsets[i + 1]) { return static_cast(roundf(percentageOffsets[i] + percentageSlopes[i] * (voltage - voltageOffsets[i]))); - + } + } + return 0; }