From 3c413bdd5283f6ef95d23a4b7274722da680f039 Mon Sep 17 00:00:00 2001 From: Joaquim Date: Fri, 16 Apr 2021 16:15:38 +0100 Subject: In order to stabilize the battery reading, I modified the process to make 5 consecutive readings, as the process is asynchronous, there is no interference in the main process. --- src/components/battery/BatteryController.cpp | 23 +++++++++++++++++------ src/components/battery/BatteryController.h | 7 +++++-- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'src/components') diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 4a7a2345..99afa55c 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -23,12 +23,20 @@ void Battery::Update() { isCharging = !nrf_gpio_pin_read(chargingPin); isPowerPresent = !nrf_gpio_pin_read(powerPresentPin); + if ( isReading ) return; // Non blocking read - SaadcInit(); - nrfx_saadc_sample(); + samples = 0; + isReading = true; + SaadcInit(); + + nrfx_saadc_sample(); } +void Battery::adcCallbackStatic(nrfx_saadc_evt_t const *event) { + instance->SaadcEventHandler(event); +} + void Battery::SaadcInit() { nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG; APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic)); @@ -68,10 +76,13 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const * p_event) { percentRemainingBuffer.insert(percentRemaining); - nrfx_saadc_uninit(); + samples++; + if ( samples > percentRemainingSamples ) { + nrfx_saadc_uninit(); + isReading = false; + } else { + nrfx_saadc_sample(); + } } } -void Battery::adcCallbackStatic(nrfx_saadc_evt_t const *event) { - instance->SaadcEventHandler(event); -} diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 2776687b..47d7a6d1 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -52,13 +52,13 @@ namespace Pinetime { float Voltage() const { return voltage; } bool IsCharging() const { return isCharging; } - bool IsPowerPresent() const { return isPowerPresent; } + bool IsPowerPresent() const { return isPowerPresent; } private: static Battery *instance; nrf_saadc_value_t saadc_value; - static constexpr uint8_t percentRemainingSamples = 10; + static constexpr uint8_t percentRemainingSamples = 5; CircBuffer percentRemainingBuffer {}; static constexpr uint32_t chargingPin = 12; @@ -74,6 +74,9 @@ namespace Pinetime { void SaadcEventHandler(nrfx_saadc_evt_t const * p_event); static void adcCallbackStatic(nrfx_saadc_evt_t const *event); + + bool isReading = false; + uint8_t samples = 0; }; } } \ No newline at end of file -- cgit v1.2.3