diff options
Diffstat (limited to 'src/components/battery')
| -rw-r--r-- | src/components/battery/BatteryController.cpp | 37 | ||||
| -rw-r--r-- | src/components/battery/BatteryController.h | 18 |
2 files changed, 39 insertions, 16 deletions
diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index f8a64ecd..e807f033 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -1,4 +1,5 @@ #include "BatteryController.h" +#include "drivers/PinMap.h" #include <hal/nrf_gpio.h> #include <nrfx_saadc.h> #include <algorithm> @@ -9,15 +10,22 @@ Battery* Battery::instance = nullptr; Battery::Battery() { instance = this; + nrf_gpio_cfg_input(PinMap::Charging, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled); } -void Battery::Init() { - nrf_gpio_cfg_input(chargingPin, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Pullup); +void Battery::ReadPowerState() { + isCharging = !nrf_gpio_pin_read(PinMap::Charging); + isPowerPresent = !nrf_gpio_pin_read(PinMap::PowerPresent); + + if (isPowerPresent && !isCharging) { + isFull = true; + } else if (!isPowerPresent) { + isFull = false; + } } -void Battery::Update() { - isCharging = !nrf_gpio_pin_read(chargingPin); - isPowerPresent = !nrf_gpio_pin_read(powerPresentPin); +void Battery::MeasureVoltage() { + ReadPowerState(); if (isReading) { return; @@ -65,15 +73,26 @@ 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; - if (voltage > battery_max) { - percentRemaining = 100; + uint8_t newPercent; + if (isFull) { + newPercent = 100; } else if (voltage < battery_min) { - percentRemaining = 0; + newPercent = 0; } else { - percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min); + 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; } } + +void Battery::Register(Pinetime::System::SystemTask* systemTask) { + this->systemTask = systemTask; +} diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 6f09b737..5a7394c4 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -1,8 +1,7 @@ #pragma once #include <cstdint> #include <drivers/include/nrfx_saadc.h> -#include <array> -#include <numeric> +#include <systemtask/SystemTask.h> namespace Pinetime { namespace Controllers { @@ -11,8 +10,9 @@ namespace Pinetime { public: Battery(); - void Init(); - void Update(); + void ReadPowerState(); + void MeasureVoltage(); + void Register(System::SystemTask* systemTask); uint8_t PercentRemaining() const { return percentRemaining; @@ -23,7 +23,9 @@ namespace Pinetime { } bool IsCharging() const { - return isCharging; + // isCharging will go up and down when fully charged + // isFull makes sure this returns false while fully charged. + return isCharging && !isFull; } bool IsPowerPresent() const { @@ -34,14 +36,14 @@ namespace Pinetime { static Battery* instance; nrf_saadc_value_t saadc_value; - static constexpr uint32_t chargingPin = 12; - static constexpr uint32_t powerPresentPin = 19; static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7; uint16_t voltage = 0; uint8_t percentRemaining = 0; + bool isFull = false; bool isCharging = false; bool isPowerPresent = false; + bool firstMeasurement = true; void SaadcInit(); @@ -49,6 +51,8 @@ namespace Pinetime { static void AdcCallbackStatic(nrfx_saadc_evt_t const* event); bool isReading = false; + + Pinetime::System::SystemTask* systemTask = nullptr; }; } } |
