diff options
author | JF <JF002@users.noreply.github.com> | 2021-10-09 20:11:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-09 20:11:31 +0200 |
commit | b969272c90bfbfa8ae7234809a896ba62411f797 (patch) | |
tree | 56e0d4f2af2e5b74d6050d74884a64076cdbdba1 /src/components/battery | |
parent | c99feeea312f911e17b133a93c208caa88cb7b03 (diff) | |
parent | 1777b9dee8f1189df29f627707b970995f0d4afe (diff) |
Merge pull request #719 from Riksu9000/improve_battery_reporting
Improve battery percentage calculation and reporting
Diffstat (limited to 'src/components/battery')
-rw-r--r-- | src/components/battery/BatteryController.cpp | 21 | ||||
-rw-r--r-- | src/components/battery/BatteryController.h | 4 |
2 files changed, 18 insertions, 7 deletions
diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index b43b229f..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<nrf_gpio_pin_pull_t> 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; @@ -69,18 +73,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..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 { @@ -42,6 +43,7 @@ namespace Pinetime { bool isFull = false; bool isCharging = false; bool isPowerPresent = false; + bool firstMeasurement = true; void SaadcInit(); |