diff options
author | Joaquim <joaquim.org@gmail.com> | 2021-04-16 16:15:38 +0100 |
---|---|---|
committer | Joaquim <joaquim.org@gmail.com> | 2021-04-16 16:15:38 +0100 |
commit | 3c413bdd5283f6ef95d23a4b7274722da680f039 (patch) | |
tree | 54c2b4f658a82d5effc5f1a19f5937006aa748cd /src/components/battery/BatteryController.cpp | |
parent | c0c37877b5a67a484aa8676bf3c32290ae500997 (diff) |
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.
Diffstat (limited to 'src/components/battery/BatteryController.cpp')
-rw-r--r-- | src/components/battery/BatteryController.cpp | 23 |
1 files changed, 17 insertions, 6 deletions
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); -} |