From a9f7153fdf92b097b14143b77645f6608ac1bf9c Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 4 Oct 2021 01:41:38 +0300 Subject: [PATCH 1/2] Improve battery percentage calculation and reporting While charging, percentage should only go up, and while discharging, percentage should only go down. --- src/components/battery/BatteryController.cpp | 15 ++++++++++----- src/components/battery/BatteryController.h | 1 + src/systemtask/Messages.h | 2 +- src/systemtask/SystemTask.cpp | 8 ++------ src/systemtask/SystemTask.h | 2 -- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index b43b229f..0ef4ff1a 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -69,18 +69,23 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { // p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024 voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024; + uint8_t newPercent; if (isFull) { - percentRemaining = 100; + newPercent = 100; } else if (voltage < battery_min) { - percentRemaining = 0; + newPercent = 0; } else { - percentRemaining = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100); + newPercent = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100); + } + + if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) { + firstMeasurement = false; + percentRemaining = newPercent; + systemTask->PushMessage(System::Messages::BatteryPercentageUpdated); } nrfx_saadc_uninit(); isReading = false; - - systemTask->PushMessage(System::Messages::BatteryMeasurementDone); } } diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index c78ffb3f..55c26309 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -42,6 +42,7 @@ namespace Pinetime { bool isFull = false; bool isCharging = false; bool isPowerPresent = false; + bool firstMeasurement = true; void SaadcInit(); diff --git a/src/systemtask/Messages.h b/src/systemtask/Messages.h index bd1de234..5aa218d2 100644 --- a/src/systemtask/Messages.h +++ b/src/systemtask/Messages.h @@ -24,7 +24,7 @@ namespace Pinetime { SetOffAlarm, StopRinging, MeasureBatteryTimerExpired, - BatteryMeasurementDone, + BatteryPercentageUpdated, }; } } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 5441c162..0a53101f 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -349,14 +349,10 @@ void SystemTask::Work() { motorController.RunForDuration(15); break; case Messages::MeasureBatteryTimerExpired: - sendBatteryNotification = true; batteryController.Update(); break; - case Messages::BatteryMeasurementDone: - if (sendBatteryNotification) { - sendBatteryNotification = false; - nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining()); - } + case Messages::BatteryPercentageUpdated: + nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining()); break; default: diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 1fcfeb8a..879c1be8 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -133,14 +133,12 @@ namespace Pinetime { TimerHandle_t dimTimer; TimerHandle_t idleTimer; TimerHandle_t measureBatteryTimer; - bool sendBatteryNotification = false; bool doNotGoToSleep = false; void GoToRunning(); void UpdateMotion(); bool stepCounterMustBeReset = false; static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000); - TickType_t lastBatteryNotificationTime = 0; #if configUSE_TRACE_FACILITY == 1 SystemMonitor monitor; From 1777b9dee8f1189df29f627707b970995f0d4afe Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Sat, 9 Oct 2021 13:39:27 +0300 Subject: [PATCH 2/2] Don't measure and notify percentage on charging event. --- src/components/battery/BatteryController.cpp | 6 +++++- src/components/battery/BatteryController.h | 3 ++- src/systemtask/SystemTask.cpp | 7 ++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 0ef4ff1a..e807f033 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -13,7 +13,7 @@ Battery::Battery() { nrf_gpio_cfg_input(PinMap::Charging, static_cast GPIO_PIN_CNF_PULL_Disabled); } -void Battery::Update() { +void Battery::ReadPowerState() { isCharging = !nrf_gpio_pin_read(PinMap::Charging); isPowerPresent = !nrf_gpio_pin_read(PinMap::PowerPresent); @@ -22,6 +22,10 @@ void Battery::Update() { } else if (!isPowerPresent) { isFull = false; } +} + +void Battery::MeasureVoltage() { + ReadPowerState(); if (isReading) { return; diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 55c26309..5a7394c4 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -10,7 +10,8 @@ namespace Pinetime { public: Battery(); - void Update(); + void ReadPowerState(); + void MeasureVoltage(); void Register(System::SystemTask* systemTask); uint8_t PercentRemaining() const { diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 0a53101f..ec8b5e4e 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -136,7 +136,6 @@ void SystemTask::Work() { touchPanel.Init(); dateTimeController.Register(this); batteryController.Register(this); - batteryController.Update(); motorController.Init(); motionSensor.SoftReset(); timerController.Register(this); @@ -194,6 +193,8 @@ void SystemTask::Work() { nrf_gpio_cfg_sense_input(PinMap::PowerPresent, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH); } + batteryController.MeasureVoltage(); + idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback); dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback); measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback); @@ -345,11 +346,11 @@ void SystemTask::Work() { stepCounterMustBeReset = true; break; case Messages::OnChargingEvent: - batteryController.Update(); + batteryController.ReadPowerState(); motorController.RunForDuration(15); break; case Messages::MeasureBatteryTimerExpired: - batteryController.Update(); + batteryController.MeasureVoltage(); break; case Messages::BatteryPercentageUpdated: nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());