From 160114f54c775a2669cb88e0207cf8853f3d547c Mon Sep 17 00:00:00 2001 From: MatthewColvin Date: Fri, 29 Sep 2023 15:21:56 -0500 Subject: [PATCH] move from constructing keypress abstract with handler into setting the handler so that it can be set by the UI. --- Platformio/HAL/HardwareModules/KeyPressAbstract.cpp | 9 ++++++--- Platformio/HAL/HardwareModules/KeyPressAbstract.hpp | 10 ++++++---- .../HAL/Targets/Simulator/HardwareSimulator.cpp | 2 +- Platformio/HAL/Targets/Simulator/KeyPressSim.cpp | 11 +++++------ Platformio/HAL/Targets/Simulator/KeyPressSim.hpp | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Platformio/HAL/HardwareModules/KeyPressAbstract.cpp b/Platformio/HAL/HardwareModules/KeyPressAbstract.cpp index 5b12d38..d929c9f 100644 --- a/Platformio/HAL/HardwareModules/KeyPressAbstract.cpp +++ b/Platformio/HAL/HardwareModules/KeyPressAbstract.cpp @@ -1,5 +1,8 @@ #include "KeyPressAbstract.hpp" -KeyPressAbstract::KeyPressAbstract( - std::function aKeyEventHandler) - : mKeyEventHandler(std::move(aKeyEventHandler)) {} +KeyPressAbstract::KeyPressAbstract() {} + +void KeyPressAbstract::RegisterKeyPressHandler( + std::function aKeyEventHandler) { + mKeyEventHandler = std::move(aKeyEventHandler); +} \ No newline at end of file diff --git a/Platformio/HAL/HardwareModules/KeyPressAbstract.hpp b/Platformio/HAL/HardwareModules/KeyPressAbstract.hpp index 1bf6ede..2b10f0a 100644 --- a/Platformio/HAL/HardwareModules/KeyPressAbstract.hpp +++ b/Platformio/HAL/HardwareModules/KeyPressAbstract.hpp @@ -47,10 +47,12 @@ public: const KeyId mId; const Type mType; }; - /// @brief When Constructing pass a callable to be ran for Handling - /// KeyEvents - /// @param aKeyEventHandler - KeyPressAbstract(std::function aKeyEventHandler); + + KeyPressAbstract(); + + /// @brief Register a SINGLE handler to be used for proccessing keys + /// @param aKeyEventHandler - Callable the Handles KeyEvent + void RegisterKeyPressHandler(std::function aKeyEventHandler); protected: /// @brief Function ment to be called regularly to allow diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp index 74eae44..f39e434 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp @@ -13,7 +13,7 @@ HardwareSimulator::HardwareSimulator() mBattery(std::make_shared()), mDisplay(SDLDisplay::getInstance()), mWifiHandler(std::make_shared()), - mKeys(std::make_shared([](auto keypress) { return true; })) { + mKeys(std::make_shared()) { mHardwareStatusTitleUpdate = std::thread([this] { int dataToShow = 0; while (true) { diff --git a/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp b/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp index 1beab8c..1db442d 100644 --- a/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp +++ b/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp @@ -3,9 +3,8 @@ #include -KeyPressSim::KeyPressSim( - std::function aKeyHandler) - : KeyPressAbstract(std::move(aKeyHandler)), mKeyGrabberThread([this] { +KeyPressSim::KeyPressSim() + : mKeyGrabberThread([this] { while (true) { HandleKeyPresses(); std::this_thread::sleep_for(std::chrono::milliseconds(50)); @@ -35,9 +34,9 @@ void KeyPressSim::GrabKeys() { void KeyPressSim::HandleKeyPresses() { std::lock_guard lock(mQueueGaurd); while (!mKeyEventQueue.empty()) { - printf(mKeyEventQueue.front().mType == KeyEvent::Type::Release ? "release" - : "press"); - mKeyEventHandler(mKeyEventQueue.front()); + if (mKeyEventHandler) { + mKeyEventHandler(mKeyEventQueue.front()); + } mKeyEventQueue.pop(); } }; diff --git a/Platformio/HAL/Targets/Simulator/KeyPressSim.hpp b/Platformio/HAL/Targets/Simulator/KeyPressSim.hpp index 24c5d69..6954e20 100644 --- a/Platformio/HAL/Targets/Simulator/KeyPressSim.hpp +++ b/Platformio/HAL/Targets/Simulator/KeyPressSim.hpp @@ -10,7 +10,7 @@ class KeyPressSim : public KeyPressAbstract { public: static constexpr auto MaxQueueableKeyEvents = 3; - KeyPressSim(std::function aKeyHandler); + KeyPressSim(); void GrabKeys(); void HandleKeyPresses() override;