OMOTE/Platformio/OmoteUI/UIs/LvglResourceManger.hpp
MatthewColvin 235d8e043f Convert Lvgl resource manager to recursive mutex to allow re locking in thread.
This helps with creation and getting values and should be safe since it still keeps lvgl on  one thread at a time.

Add OnLvglEvent to UIElement that allows foreasy place for UI Elements  to respond to LVGL events .

Add button  class that can react to being presssed via a  callback function.

Add GetBottom() api to allow easy grabbing of bottom Y coordinate.

use some new stuff in the settings  page to sort test it all out.
2023-10-06 22:11:15 -05:00

49 lines
1.1 KiB
C++

#pragma once
#include <functional>
#include <mutex>
#include <queue>
namespace UI {
class UIBase;
} // namespace UI
class LvglResourceManger {
friend UI::UIBase;
public:
static LvglResourceManger &GetInstance() {
static LvglResourceManger mInstance;
return mInstance;
}
[[nodiscard]] std::scoped_lock<std::recursive_mutex> scopeLock() {
return std::scoped_lock(mLvglMutex);
}
void AttemptNow(std::function<void()> aLvglModifierFunction) {
// Attempt to take mutex and preform op if you can't then queue up.
if (mLvglMutex.try_lock()) {
aLvglModifierFunction();
mLvglMutex.unlock();
} else {
QueueForLater(aLvglModifierFunction);
}
}
void QueueForLater(std::function<void()> aLvglModifierFunction) {
mLvglTasks.push(std::move(aLvglModifierFunction));
}
protected:
LvglResourceManger(){};
void HandleQueuedTasks() {
auto lock = scopeLock();
while (!mLvglTasks.empty()) {
mLvglTasks.front()();
mLvglTasks.pop();
}
}
std::queue<std::function<void()>> mLvglTasks;
std::recursive_mutex mLvglMutex;
};