modified poller to have callable first in constructor

This commit is contained in:
Matthew Colvin 2023-08-27 19:09:07 -05:00 committed by MatthewColvin
parent f5f856ba63
commit ae7a7d6dab
4 changed files with 23 additions and 16 deletions

View file

@ -9,15 +9,7 @@ std::shared_ptr<OmoteUI> OmoteUI::mInstance = nullptr;
// #if defined(IS_SIMULATOR) && (IS_SIMULATOR == true)
// #endif
OmoteUI::OmoteUI(std::shared_ptr<HardwareAbstract> 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<HardwareAbstract> 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)

View file

@ -75,6 +75,9 @@ public:
private:
static std::shared_ptr<OmoteUI> mInstance;
std::shared_ptr<HardwareAbstract> mHardware;
poller batteryPoller;
void reset_settings_menu();
void attach_keyboard(lv_obj_t* textarea);
std::shared_ptr<std::vector<WifiInfo>> found_wifi_networks;

View file

@ -5,19 +5,21 @@
using namespace std::chrono;
poller::poller(milliseconds aPollTime,std::function<void()> aCallback){
poller::poller(std::function<void()> 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<poller*>(aTimer->user_data);
if(currentPoller->anIntermittentCallback){
currentPoller->anIntermittentCallback();
if(currentPoller->mIntermittentCallback){
currentPoller->mIntermittentCallback();
}
}

View file

@ -5,7 +5,8 @@
class poller{
public:
poller(std::chrono::milliseconds pollTime = std::chrono::seconds(5), std::function<void()> anIntermittentCallback = nullptr);
poller(){};
poller(std::function<void()> 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<void()> anIntermittentCallback;
lv_timer_t* mTimer = nullptr;
std::function<void()> mIntermittentCallback = nullptr;
// Static function registered to every timers callback to pass this object as context
static void onPoll(_lv_timer_t* aTimer);