From f5f856ba638914a93abc07604477c53f9f356bb9 Mon Sep 17 00:00:00 2001 From: Matthew Colvin <35540398+Mc067415@users.noreply.github.com> Date: Sun, 27 Aug 2023 18:47:17 -0500 Subject: [PATCH] add poller to simplify polling UI updates --- Platformio/OmoteUI/OmoteUI.hpp | 2 +- Platformio/OmoteUI/poller.cpp | 23 +++++++++++++++++++++++ Platformio/OmoteUI/poller.hpp | 23 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 Platformio/OmoteUI/poller.cpp create mode 100644 Platformio/OmoteUI/poller.hpp diff --git a/Platformio/OmoteUI/OmoteUI.hpp b/Platformio/OmoteUI/OmoteUI.hpp index 2f8daea..460c907 100644 --- a/Platformio/OmoteUI/OmoteUI.hpp +++ b/Platformio/OmoteUI/OmoteUI.hpp @@ -9,7 +9,7 @@ #include #include #include - +#include "poller.hpp" /// @brief Singleton to allow UI code to live separately from the Initialization /// of resources. diff --git a/Platformio/OmoteUI/poller.cpp b/Platformio/OmoteUI/poller.cpp new file mode 100644 index 0000000..8d14181 --- /dev/null +++ b/Platformio/OmoteUI/poller.cpp @@ -0,0 +1,23 @@ +#include "poller.hpp" + +#include +#include + +using namespace std::chrono; + +poller::poller(milliseconds aPollTime,std::function 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(aTimer->user_data); + + if(currentPoller->anIntermittentCallback){ + currentPoller->anIntermittentCallback(); + } +} \ No newline at end of file diff --git a/Platformio/OmoteUI/poller.hpp b/Platformio/OmoteUI/poller.hpp new file mode 100644 index 0000000..59fed2f --- /dev/null +++ b/Platformio/OmoteUI/poller.hpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include "lvgl.h" + +class poller{ +public: + poller(std::chrono::milliseconds pollTime = std::chrono::seconds(5), std::function 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 anIntermittentCallback; + + // Static function registered to every timers callback to pass this object as context + static void onPoll(_lv_timer_t* aTimer); +}; \ No newline at end of file