diff --git a/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp b/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp index 1db442d..a2cb19a 100644 --- a/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp +++ b/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp @@ -4,29 +4,27 @@ #include KeyPressSim::KeyPressSim() - : mKeyGrabberThread([this] { + : mKeyHandlerThread([this] { while (true) { HandleKeyPresses(); std::this_thread::sleep_for(std::chrono::milliseconds(50)); } - }), - mKeyHandlerThread([this] { - while (true) { - GrabKeys(); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } - }){}; + }) { + SDL_AddEventWatch(KeyPressSim::GrabKeyImpl, this); +}; -void KeyPressSim::GrabKeys() { - SDL_Event event; - while (SDL_PollEvent(&event)) { - if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { - auto keyEventType = event.type == SDL_KEYDOWN ? KeyEvent::Type::Press +int KeyPressSim::GrabKeyImpl(void *aSelf, SDL_Event *aEvent) { + reinterpret_cast(aSelf)->GrabKeys(aEvent); + return 0; +} + +void KeyPressSim::GrabKeys(SDL_Event *aEvent) { + if (aEvent->type == SDL_KEYDOWN || aEvent->type == SDL_KEYUP) { + auto keyEventType = aEvent->type == SDL_KEYDOWN ? KeyEvent::Type::Press : KeyEvent::Type::Release; - const auto SDLK_key = event.key.keysym.sym; - if (KeyMap.count(SDLK_key) > 0) { - QueueKeyEvent(KeyEvent(KeyMap.at(SDLK_key), keyEventType)); - } + const auto SDLK_key = aEvent->key.keysym.sym; + if (KeyMap.count(SDLK_key) > 0) { + QueueKeyEvent(KeyEvent(KeyMap.at(SDLK_key), keyEventType)); } } }; diff --git a/Platformio/HAL/Targets/Simulator/KeyPressSim.hpp b/Platformio/HAL/Targets/Simulator/KeyPressSim.hpp index 6954e20..1f7aff3 100644 --- a/Platformio/HAL/Targets/Simulator/KeyPressSim.hpp +++ b/Platformio/HAL/Targets/Simulator/KeyPressSim.hpp @@ -12,7 +12,9 @@ public: KeyPressSim(); - void GrabKeys(); + static int GrabKeyImpl(void *aSelf, SDL_Event *aEvent); + void GrabKeys(SDL_Event *aEvent); + void HandleKeyPresses() override; void QueueKeyEvent(KeyEvent aJustOccuredKeyEvent) override; diff --git a/Platformio/OmoteUI/UIs/LvglMutex.hpp b/Platformio/OmoteUI/UIs/LvglMutex.hpp new file mode 100644 index 0000000..d703cac --- /dev/null +++ b/Platformio/OmoteUI/UIs/LvglMutex.hpp @@ -0,0 +1,11 @@ +#pragma once +#include + +class LvglMutex { +public: + [[nodiscard]] static std::lock_guard lockScope() { + return std::lock_guard(mLvglMutex); + } + + inline static std::mutex mLvglMutex; +}; diff --git a/Platformio/OmoteUI/UIs/UIBase.cpp b/Platformio/OmoteUI/UIs/UIBase.cpp index 7acb1b4..d2ac817 100644 --- a/Platformio/OmoteUI/UIs/UIBase.cpp +++ b/Platformio/OmoteUI/UIs/UIBase.cpp @@ -1,4 +1,5 @@ #include "UIBase.hpp" +#include "LvglMutex.hpp" using namespace UI; @@ -7,5 +8,8 @@ UIBase::UIBase(std::shared_ptr aHardware) void UIBase::loopHandler() { lv_timer_handler(); - lv_task_handler(); + { + auto lock = LvglMutex::lockScope(); + lv_task_handler(); + } } \ No newline at end of file diff --git a/Platformio/OmoteUI/core/UIElement.cpp b/Platformio/OmoteUI/core/UIElement.cpp index bec4812..26bf16b 100644 --- a/Platformio/OmoteUI/core/UIElement.cpp +++ b/Platformio/OmoteUI/core/UIElement.cpp @@ -1,28 +1,34 @@ #include "UIElement.hpp" +#include "LvglMutex.hpp" namespace UI { UIElement::UIElement(lv_obj_t *aLvglSelf, ID aId) : mLvglSelf(aLvglSelf), mId(aId) { + auto lock = LvglMutex::lockScope(); mLvglSelf->user_data = this; } UIElement::~UIElement() { + auto lock = LvglMutex::lockScope(); if (lv_obj_is_valid(mLvglSelf)) { lv_obj_del(mLvglSelf); } } void UIElement::AddElement(UIElement *anUIElement) { + auto lock = LvglMutex::lockScope(); lv_obj_set_parent(anUIElement->mLvglSelf, mLvglSelf); } bool UIElement::IsVisible() { return lv_obj_is_visible(mLvglSelf); } void UIElement::SetWidth(lv_coord_t aWidth) { + auto lock = LvglMutex::lockScope(); lv_obj_set_width(mLvglSelf, aWidth); } void UIElement::SetHeight(lv_coord_t aHeight) { + auto lock = LvglMutex::lockScope(); lv_obj_set_height(mLvglSelf, aHeight); } @@ -36,8 +42,14 @@ lv_coord_t UIElement::GetWidth() { return lv_obj_get_width(mLvglSelf); } -void UIElement::SetY(lv_coord_t aY) { lv_obj_set_y(mLvglSelf, aY); } -void UIElement::SetX(lv_coord_t aX) { lv_obj_set_x(mLvglSelf, aX); } +void UIElement::SetY(lv_coord_t aY) { + auto lock = LvglMutex::lockScope(); + lv_obj_set_y(mLvglSelf, aY); +} +void UIElement::SetX(lv_coord_t aX) { + auto lock = LvglMutex::lockScope(); + lv_obj_set_x(mLvglSelf, aX); +} lv_coord_t UIElement::GetY() { lv_obj_update_layout(mLvglSelf); @@ -60,6 +72,7 @@ void UIElement::SetVisiblity(bool aVisible) { } void UIElement::SetBgColor(lv_color_t aColor, lv_style_selector_t aStyle) { + auto lock = LvglMutex::lockScope(); lv_obj_set_style_bg_color(mLvglSelf, aColor, aStyle); }; diff --git a/Platformio/OmoteUI/core/page/TabView.cpp b/Platformio/OmoteUI/core/page/TabView.cpp index 2bec6a3..417410f 100644 --- a/Platformio/OmoteUI/core/page/TabView.cpp +++ b/Platformio/OmoteUI/core/page/TabView.cpp @@ -21,6 +21,9 @@ bool Tab::KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { return mContent->KeyEvent(aKeyEvent); } +void Tab::OnShow() { mContent->OnShow(); }; +void Tab::OnHide() { mContent->OnHide(); }; + /////////////////////TabView///////////////////////////////////// TabView::TabView(ID aId) @@ -53,13 +56,11 @@ void TabView::HandleTabChange() { // Notify the page that it is now showing and the other that the are now // hidden for (int i = 0; i < mTabs.size(); i++) { - auto content = mTabs[i]->TakeContent(); if (GetCurrentTabIdx() == i) { - content->OnShow(); + mTabs[i]->OnShow(); } else { - content->OnHide(); + mTabs[i]->OnHide(); } - mTabs[i]->GiveContent(std::move(content)); } } diff --git a/Platformio/OmoteUI/core/page/TabView.hpp b/Platformio/OmoteUI/core/page/TabView.hpp index a977af2..28adb4a 100644 --- a/Platformio/OmoteUI/core/page/TabView.hpp +++ b/Platformio/OmoteUI/core/page/TabView.hpp @@ -18,6 +18,9 @@ public: bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; + void OnShow() override; + void OnHide() override; + private: Base::Ptr mContent; };