From 557a328409b9a4d4617606c8b04679ebee885b1f Mon Sep 17 00:00:00 2001 From: minacode Date: Mon, 21 Nov 2022 00:03:09 +0100 Subject: [PATCH] changed to bool --- src/components/battery/BatteryController.cpp | 9 +++++++-- src/components/battery/BatteryController.h | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 79829360..cdcc766a 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -86,15 +86,20 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { newPercent = std::min(aprox.GetValue(voltage), isCharging ? uint8_t {99} : uint8_t {100}); } + if (isPowerPresent) { + batteryLowNotified = false; + } + if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) { firstMeasurement = false; - lastPercentRemaining = percentRemaining; percentRemaining = newPercent; systemTask->PushMessage(System::Messages::BatteryPercentageUpdated); + // warn about low battery when not charging and below threshold - if (!isPowerPresent && BatteryIsLow() && lastPercentRemaining > lowBatteryThreshold) { + if (BatteryIsLow() && !isPowerPresent && !batteryLowNotified) { systemTask->PushMessage(System::Messages::LowBattery); + batteryLowNotified = true; } } diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 3a7d73ce..627ca7d4 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -42,7 +42,7 @@ namespace Pinetime { static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7; uint16_t voltage = 0; uint8_t percentRemaining = 0; - uint8_t lastPercentRemaining = 0; + bool batteryLowNotified = false; bool isFull = false; bool isCharging = false;