From 3c413bdd5283f6ef95d23a4b7274722da680f039 Mon Sep 17 00:00:00 2001 From: Joaquim Date: Fri, 16 Apr 2021 16:15:38 +0100 Subject: [PATCH] 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 ++++-- src/displayapp/DisplayApp.cpp | 2 +- src/displayapp/screens/BatteryInfo.cpp | 15 ++++++++----- src/systemtask/SystemTask.cpp | 9 ++++++-- 5 files changed, 40 insertions(+), 16 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); -} 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 diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 773ae9c0..36f93a91 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -132,7 +132,7 @@ void DisplayApp::Refresh() { // clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected : Screens::Clock::BleConnectionStates::NotConnected); break; case Messages::UpdateBatteryLevel: -// clockScreen.SetBatteryPercentRemaining(batteryController.PercentRemaining()); + batteryController.Update(); break; case Messages::NewNotification: LoadApp( Apps::NotificationsPreview, DisplayApp::FullRefreshDirections::Down ); diff --git a/src/displayapp/screens/BatteryInfo.cpp b/src/displayapp/screens/BatteryInfo.cpp index ae39138f..e616d67f 100644 --- a/src/displayapp/screens/BatteryInfo.cpp +++ b/src/displayapp/screens/BatteryInfo.cpp @@ -84,7 +84,7 @@ void BatteryInfo::UpdateAnim() { batteryPercent = batteryController.PercentRemaining(); if ( batteryPercent >= 0 ) { - if ( batteryController.IsCharging() ) { + if ( batteryController.IsCharging() and batteryPercent < 100 ) { animation +=1; if (animation >= 100) { animation = 0; @@ -111,12 +111,17 @@ void BatteryInfo::UpdateScreen() { batteryVoltage = batteryController.Voltage(); if ( batteryPercent >= 0 ) { - if ( batteryController.IsCharging() ) { - lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, lv_color_hex(0xFF0000)); + if ( batteryController.IsCharging() and batteryPercent < 100 ) { + lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_RED); lv_label_set_text_static(status,"Battery charging"); - + } else if ( batteryPercent == 100 ) { + lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_BLUE); + lv_label_set_text_static(status,"Battery is fully charged"); + } else if ( batteryPercent < 10 ) { + lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_YELLOW); + lv_label_set_text_static(status,"Battery is low"); } else { - lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, lv_color_hex(0x00FF00)); + lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_GREEN); lv_label_set_text_static(status,"Battery discharging"); } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 42a4e844..b540fcd8 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -98,7 +98,6 @@ void SystemTask::Work() { heartRateController, settingsController, motionController); displayApp->Start(); - batteryController.Update(); displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel); heartRateSensor.Init(); @@ -106,7 +105,6 @@ void SystemTask::Work() { heartRateApp = std::make_unique(heartRateSensor, heartRateController); heartRateApp->Start(); - nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t)GPIO_PIN_CNF_SENSE_High); nrf_gpio_cfg_output(15); nrf_gpio_pin_set(15); @@ -141,7 +139,14 @@ void SystemTask::Work() { uint8_t msg; if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) { + + // call the battery controller or use the MSG in DisplayApp to get the battery status ??? + // it is necessary to validate which is the most efficient batteryController.Update(); + //displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel); + // analyze a more efficient way to do this refreshment + // this and the UpdateMotion(); can be called on a timer to be independent of the main process ??? + Messages message = static_cast(msg); switch(message) { case Messages::EnableSleeping: