diff --git a/Platformio/OmoteUI/OmoteUI.cpp b/Platformio/OmoteUI/OmoteUI.cpp index 703ed39..da7137f 100644 --- a/Platformio/OmoteUI/OmoteUI.cpp +++ b/Platformio/OmoteUI/OmoteUI.cpp @@ -9,15 +9,7 @@ std::shared_ptr OmoteUI::mInstance = nullptr; // #if defined(IS_SIMULATOR) && (IS_SIMULATOR == true) // #endif -OmoteUI::OmoteUI(std::shared_ptr aHardware) : mHardware(aHardware){ - mHardware->battery()->onBatteryStatusChange([this](int percent, bool isCharging){ - if(percent > 95) lv_label_set_text(objBattIcon, LV_SYMBOL_BATTERY_FULL); - else if(percent > 75) lv_label_set_text(objBattIcon, LV_SYMBOL_BATTERY_3); - else if(percent > 50) lv_label_set_text(objBattIcon, LV_SYMBOL_BATTERY_2); - else if(percent > 25) lv_label_set_text(objBattIcon, LV_SYMBOL_BATTERY_1); - else lv_label_set_text(objBattIcon, LV_SYMBOL_BATTERY_EMPTY); - }); -} +OmoteUI::OmoteUI(std::shared_ptr aHardware) : mHardware(aHardware){} // Set the page indicator scroll position relative to the tabview scroll // position @@ -123,6 +115,15 @@ void OmoteUI::create_status_bar(){ lv_label_set_text(this->objBattIcon, LV_SYMBOL_BATTERY_EMPTY); 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()](){ + 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); + else if(percent > 50) lv_label_set_text(batteryIcon, LV_SYMBOL_BATTERY_2); + else if(percent > 25) lv_label_set_text(batteryIcon, LV_SYMBOL_BATTERY_1); + else lv_label_set_text(batteryIcon, LV_SYMBOL_BATTERY_EMPTY); + }); } void OmoteUI::setup_settings(lv_obj_t* parent) diff --git a/Platformio/OmoteUI/OmoteUI.hpp b/Platformio/OmoteUI/OmoteUI.hpp index 460c907..822f548 100644 --- a/Platformio/OmoteUI/OmoteUI.hpp +++ b/Platformio/OmoteUI/OmoteUI.hpp @@ -75,6 +75,9 @@ public: private: static std::shared_ptr mInstance; std::shared_ptr mHardware; + + poller batteryPoller; + void reset_settings_menu(); void attach_keyboard(lv_obj_t* textarea); std::shared_ptr> found_wifi_networks; diff --git a/Platformio/OmoteUI/poller.cpp b/Platformio/OmoteUI/poller.cpp index 8d14181..4d2e6aa 100644 --- a/Platformio/OmoteUI/poller.cpp +++ b/Platformio/OmoteUI/poller.cpp @@ -5,19 +5,21 @@ using namespace std::chrono; -poller::poller(milliseconds aPollTime,std::function aCallback){ +poller::poller(std::function aOnPollCb, milliseconds aPollTime):mIntermittentCallback(std::move(aOnPollCb)){ mTimer = lv_timer_create(poller::onPoll,aPollTime.count(),this); lv_timer_set_repeat_count(mTimer,-1); // Call forever } poller::~poller(){ - lv_timer_del(mTimer); + if(mTimer){ + lv_timer_del(mTimer); + } } void poller::onPoll(_lv_timer_t* aTimer){ poller* currentPoller = reinterpret_cast(aTimer->user_data); - if(currentPoller->anIntermittentCallback){ - currentPoller->anIntermittentCallback(); + if(currentPoller->mIntermittentCallback){ + currentPoller->mIntermittentCallback(); } } \ No newline at end of file diff --git a/Platformio/OmoteUI/poller.hpp b/Platformio/OmoteUI/poller.hpp index 59fed2f..5fc888d 100644 --- a/Platformio/OmoteUI/poller.hpp +++ b/Platformio/OmoteUI/poller.hpp @@ -5,7 +5,8 @@ class poller{ public: - poller(std::chrono::milliseconds pollTime = std::chrono::seconds(5), std::function anIntermittentCallback = nullptr); + poller(){}; + poller(std::function aOnPollCb, std::chrono::milliseconds pollTime = std::chrono::seconds(5)); virtual ~poller(); void setPollPeriod(std::chrono::milliseconds aPollPeriod){ lv_timer_set_period(mTimer, aPollPeriod.count());} @@ -15,8 +16,8 @@ public: inline void runNext() { lv_timer_ready(mTimer);} private: - lv_timer_t* mTimer; - std::function anIntermittentCallback; + lv_timer_t* mTimer = nullptr; + std::function mIntermittentCallback = nullptr; // Static function registered to every timers callback to pass this object as context static void onPoll(_lv_timer_t* aTimer);