From 65162049b3fbd21be80a4fb54c50779828465bba Mon Sep 17 00:00:00 2001 From: Matthew Colvin <35540398+Mc067415@users.noreply.github.com> Date: Sun, 27 Aug 2023 20:27:46 -0500 Subject: [PATCH] convert notification driven battery to lvgl timer based polling using new poller class to simplify the battery interface and downstream usages of battery. tweaked poller destructor and remove the default constructor in preference of using a unique pointer convert simulator to use a std::thread for lvgl Tick api because the SDL thread sleep was not true to time. (and is simplified the code...) --- .../HAL/HardwareModules/BatteryInterface.cpp | 9 ------ .../HAL/HardwareModules/BatteryInterface.h | 16 +++------- Platformio/HAL/Targets/ESP32/HardwareRevX.cpp | 14 +-------- Platformio/HAL/Targets/ESP32/HardwareRevX.hpp | 4 --- .../Targets/Simulator/HardwareSimulator.cpp | 29 +++++-------------- .../Targets/Simulator/HardwareSimulator.hpp | 4 +++ .../Targets/Simulator/batterySimulator.hpp | 17 ++--------- Platformio/OmoteUI/OmoteUI.cpp | 2 +- Platformio/OmoteUI/OmoteUI.hpp | 2 +- Platformio/OmoteUI/poller.cpp | 1 + Platformio/OmoteUI/poller.hpp | 1 - 11 files changed, 22 insertions(+), 77 deletions(-) delete mode 100644 Platformio/HAL/HardwareModules/BatteryInterface.cpp diff --git a/Platformio/HAL/HardwareModules/BatteryInterface.cpp b/Platformio/HAL/HardwareModules/BatteryInterface.cpp deleted file mode 100644 index ed93972..0000000 --- a/Platformio/HAL/HardwareModules/BatteryInterface.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "BatteryInterface.h" - -void BatteryInterface::NotifyCurrentStatus(){ - mBatteryNotification.notify(getPercentage(),isCharging()); -} - -void BatteryInterface::onBatteryStatusChange(std::function batteryChangeHandler){ - mBatteryNotification.onNotify(std::move(batteryChangeHandler)); -} \ No newline at end of file diff --git a/Platformio/HAL/HardwareModules/BatteryInterface.h b/Platformio/HAL/HardwareModules/BatteryInterface.h index 6ebae91..1fd8e86 100644 --- a/Platformio/HAL/HardwareModules/BatteryInterface.h +++ b/Platformio/HAL/HardwareModules/BatteryInterface.h @@ -2,16 +2,8 @@ #include "Notification.hpp" class BatteryInterface { - public: - BatteryInterface() = default; - virtual int getPercentage() = 0; - virtual bool isCharging() = 0; - - /// @brief Notify on Current battery status. - void NotifyCurrentStatus(); - /// @brief Register Handler to be ran on battery status change - /// @param Callable to be ran on status change (percentage, IsCharging) - void onBatteryStatusChange(std::function); - private: - Notification mBatteryNotification; +public: + BatteryInterface() = default; + virtual int getPercentage() = 0; + virtual bool isCharging() = 0; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index 9e830cf..88217cd 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -322,19 +322,7 @@ void HardwareRevX::setupIR() { IrReceiver.enableIRIn(); // Start the receiver } -void HardwareRevX::startTasks() { - if (xTaskCreate(&HardwareRevX::updateBatteryTask, "Battery Percent Update", - 1024, nullptr, 5, &batteryUpdateTskHndl) != pdPASS) { - debugPrint("ERROR Could not Create Battery Update Task!"); - } -} - -void HardwareRevX::updateBatteryTask(void*){ - while(true){ - mInstance->battery()->NotifyCurrentStatus(); - vTaskDelay(5000 / portTICK_PERIOD_MS); - } -} +void HardwareRevX::startTasks() {} void HardwareRevX::loopHandler() { standbyTimer < 2000 ? mDisplay->sleep() : mDisplay->wake(); diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index 34668d3..464b524 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -54,10 +54,6 @@ protected: // Tasks void startTasks(); - /// @brief Send Battery Notification every 5 Seconds - static void updateBatteryTask(void *); - - TaskHandle_t batteryUpdateTskHndl = nullptr; private: HardwareRevX(); diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp index 03d8074..d7b4c1f 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp @@ -9,32 +9,17 @@ #include "SDLDisplay.hpp" - -/** - * A task to measure the elapsed time for LittlevGL - * @param data unused - * @return never return - */ -static int tick_thread(void * data) +HardwareSimulator::HardwareSimulator(): HardwareAbstract(), + mTickThread([](){ + while(true){ + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + lv_tick_inc(2); /*Tell lvgl that 2 milliseconds were elapsed*/ + }}) { - (void)data; - - while(1) { - SDL_Delay(5); /*Sleep for 5 millisecond*/ - lv_tick_inc(5); /*Tell LittelvGL that 5 milliseconds were elapsed*/ - } - - return 0; -} - -HardwareSimulator::HardwareSimulator(): HardwareAbstract(){ - /* Tick init. - * You have to call 'lv_tick_inc()' in periodically to inform LittelvGL about how much time were elapsed - * Create an SDL thread to do this*/ mBattery = std::make_shared(); mDisplay = SDLDisplay::getInstance(); mWifiHandler = std::make_shared(); - SDL_CreateThread(tick_thread, "tick", NULL); + } std::shared_ptr HardwareSimulator::battery(){ diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp index c2c14ee..7e3177b 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp @@ -5,6 +5,8 @@ #include "SDLDisplay.hpp" #include "wifiHandlerSim.hpp" +#include + class HardwareSimulator : public HardwareAbstract { public: HardwareSimulator(); @@ -23,6 +25,8 @@ public: virtual std::shared_ptr wifi() override; private: + std::thread mTickThread; + std::shared_ptr mBattery; std::shared_ptr mDisplay; std::shared_ptr mWifiHandler; diff --git a/Platformio/HAL/Targets/Simulator/batterySimulator.hpp b/Platformio/HAL/Targets/Simulator/batterySimulator.hpp index e5b52ec..dea050a 100644 --- a/Platformio/HAL/Targets/Simulator/batterySimulator.hpp +++ b/Platformio/HAL/Targets/Simulator/batterySimulator.hpp @@ -1,17 +1,13 @@ #include "BatteryInterface.h" #include -#include #include class BatterySimulator: public BatteryInterface{ public: BatterySimulator() : - mCreationTime(std::chrono::high_resolution_clock::now()), - mBattNotifier(std::thread(&BatterySimulator::batteryNotifyThread,this)) + mCreationTime(std::chrono::high_resolution_clock::now()) {}; + ~BatterySimulator(){} - ~BatterySimulator(){ - mBattNotifier.join(); - } virtual int getPercentage() override { auto now = std::chrono::high_resolution_clock::now(); auto batteryRunTime = std::chrono::duration_cast(now - mCreationTime); @@ -20,15 +16,8 @@ class BatterySimulator: public BatteryInterface{ return std::floor(fakeBattPercentage < 100 ? fakeBattPercentage : 0); } - virtual bool isCharging() override { return true; } + virtual bool isCharging() override { return false; } private: - void batteryNotifyThread(){ - while (true){ - NotifyCurrentStatus(); - std::this_thread::sleep_for(std::chrono::seconds(5)); - } - } std::chrono::_V2::system_clock::time_point mCreationTime; - std::thread mBattNotifier; }; \ No newline at end of file diff --git a/Platformio/OmoteUI/OmoteUI.cpp b/Platformio/OmoteUI/OmoteUI.cpp index da7137f..a77e52d 100644 --- a/Platformio/OmoteUI/OmoteUI.cpp +++ b/Platformio/OmoteUI/OmoteUI.cpp @@ -116,7 +116,7 @@ void OmoteUI::create_status_bar(){ lv_obj_align(this->objBattIcon, LV_ALIGN_RIGHT_MID, 8, 0); lv_obj_set_style_text_font(this->objBattIcon, &lv_font_montserrat_16, LV_PART_MAIN); - batteryPoller = poller([&batteryIcon = objBattIcon, battery = mHardware->battery()](){ + batteryPoller = std::make_unique([&batteryIcon = objBattIcon, battery = mHardware->battery()](){ auto percent = battery->getPercentage(); if(percent > 95) lv_label_set_text(batteryIcon, LV_SYMBOL_BATTERY_FULL); else if(percent > 75) lv_label_set_text(batteryIcon, LV_SYMBOL_BATTERY_3); diff --git a/Platformio/OmoteUI/OmoteUI.hpp b/Platformio/OmoteUI/OmoteUI.hpp index 822f548..5b40d0c 100644 --- a/Platformio/OmoteUI/OmoteUI.hpp +++ b/Platformio/OmoteUI/OmoteUI.hpp @@ -76,7 +76,7 @@ private: static std::shared_ptr mInstance; std::shared_ptr mHardware; - poller batteryPoller; + std::unique_ptr batteryPoller; void reset_settings_menu(); void attach_keyboard(lv_obj_t* textarea); diff --git a/Platformio/OmoteUI/poller.cpp b/Platformio/OmoteUI/poller.cpp index 4d2e6aa..e49198d 100644 --- a/Platformio/OmoteUI/poller.cpp +++ b/Platformio/OmoteUI/poller.cpp @@ -13,6 +13,7 @@ poller::poller(std::function aOnPollCb, milliseconds aPollTime):mIntermi poller::~poller(){ if(mTimer){ lv_timer_del(mTimer); + mTimer = nullptr; } } diff --git a/Platformio/OmoteUI/poller.hpp b/Platformio/OmoteUI/poller.hpp index 5fc888d..3e291a1 100644 --- a/Platformio/OmoteUI/poller.hpp +++ b/Platformio/OmoteUI/poller.hpp @@ -5,7 +5,6 @@ class poller{ public: - poller(){}; poller(std::function aOnPollCb, std::chrono::milliseconds pollTime = std::chrono::seconds(5)); virtual ~poller();