add poller to simplify polling UI updates

This commit is contained in:
Matthew Colvin 2023-08-27 18:47:17 -05:00 committed by MatthewColvin
parent 47ccc214a0
commit f5f856ba63
3 changed files with 47 additions and 1 deletions

View file

@ -9,7 +9,7 @@
#include <memory> #include <memory>
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include "poller.hpp"
/// @brief Singleton to allow UI code to live separately from the Initialization /// @brief Singleton to allow UI code to live separately from the Initialization
/// of resources. /// of resources.

View file

@ -0,0 +1,23 @@
#include "poller.hpp"
#include <functional>
#include <memory>
using namespace std::chrono;
poller::poller(milliseconds aPollTime,std::function<void()> aCallback){
mTimer = lv_timer_create(poller::onPoll,aPollTime.count(),this);
lv_timer_set_repeat_count(mTimer,-1); // Call forever
}
poller::~poller(){
lv_timer_del(mTimer);
}
void poller::onPoll(_lv_timer_t* aTimer){
poller* currentPoller = reinterpret_cast<poller*>(aTimer->user_data);
if(currentPoller->anIntermittentCallback){
currentPoller->anIntermittentCallback();
}
}

View file

@ -0,0 +1,23 @@
#include <chrono>
#include <memory>
#include <functional>
#include "lvgl.h"
class poller{
public:
poller(std::chrono::milliseconds pollTime = std::chrono::seconds(5), std::function<void()> anIntermittentCallback = nullptr);
virtual ~poller();
void setPollPeriod(std::chrono::milliseconds aPollPeriod){ lv_timer_set_period(mTimer, aPollPeriod.count());}
inline void pause() { lv_timer_pause(mTimer);}
inline void resume() { lv_timer_resume(mTimer);}
inline void reset() { lv_timer_reset(mTimer);}
inline void runNext() { lv_timer_ready(mTimer);}
private:
lv_timer_t* mTimer;
std::function<void()> anIntermittentCallback;
// Static function registered to every timers callback to pass this object as context
static void onPoll(_lv_timer_t* aTimer);
};