From e56ce7daa1065011ef75768517994d62e39134a8 Mon Sep 17 00:00:00 2001 From: Fritz Heiden Date: Mon, 26 May 2025 14:29:57 +0200 Subject: [PATCH] feat: add user input manager; add list --- src/input/encoder.cpp | 8 ++-- src/input/user_input_manager.cpp | 79 ++++++++++++++++++++++++++++++++ src/input/user_input_manager.hpp | 24 ++++++++++ src/main.cpp | 2 + src/ui/display.cpp | 59 +++++++++++++++++++++--- src/ui/views/main_view.cpp | 51 ++++++++++++--------- src/ui/views/main_view.hpp | 2 +- 7 files changed, 192 insertions(+), 33 deletions(-) create mode 100644 src/input/user_input_manager.cpp create mode 100644 src/input/user_input_manager.hpp diff --git a/src/input/encoder.cpp b/src/input/encoder.cpp index 93c187f..bb1dabb 100644 --- a/src/input/encoder.cpp +++ b/src/input/encoder.cpp @@ -57,7 +57,7 @@ namespace Encoder void handlePress() { - Serial.println("Encoder pressed"); + //Serial.println("Encoder pressed"); for (OnButtonPressCallback callback : onButtonPressCallbacks) { callback(); } @@ -65,7 +65,7 @@ namespace Encoder void handleRelease() { - Serial.println("Encoder released"); + //Serial.println("Encoder released"); for (OnButtonReleaseCallback callback : onButtonReleaseCallbacks) { callback(); } @@ -73,8 +73,8 @@ namespace Encoder void handleRotate(int8_t rotation) { - Serial.print("Encoder rotated by "); - Serial.println(rotation); + //Serial.print("Encoder rotated by "); + //Serial.println(rotation); for (OnRotateCallback callback : onRotateCallbacks) { callback(rotation); } diff --git a/src/input/user_input_manager.cpp b/src/input/user_input_manager.cpp new file mode 100644 index 0000000..15393ef --- /dev/null +++ b/src/input/user_input_manager.cpp @@ -0,0 +1,79 @@ +#include +#include +#include + +#include +#include +#include + +namespace UserInputManager +{ + using std::map; + using std::vector; + using std::queue; + + void handleEncoderButtonRelease(); + void handleEncoderButtonPress(); + void handleEncoderRotate(int); + + void pushEvent(InputEvent event); + + map> input_event_queues; + + void init() + { + Encoder::onButtonRelease(handleEncoderButtonRelease); + Encoder::onButtonPress(handleEncoderButtonPress); + Encoder::onRotate(handleEncoderRotate); + } + + void handleEncoderButtonPress() + { + pushEvent(EncoderButtonPress); + } + + void handleEncoderButtonRelease() + { + pushEvent(EncoderButtonRelease); + } + + void handleEncoderRotate(int rotation) + { + if (rotation > 0) + { + pushEvent(EncoderRotateRight); + } + else + { + pushEvent(EncoderRotateLeft); + } + } + + void pushEvent(InputEvent event) + { + Serial.println("pushing new event " + String(event)); + for (auto &queue : input_event_queues) + { + Serial.println("pushing event to queue " + queue.first + " (" + String(queue.second.size()) + ")"); + queue.second.push(event); + } + } + + InputEvent getNextEvent(String name) + { + queue &events = input_event_queues.at(name); + if (events.empty()) + { + return InputEvent::None; + } + InputEvent event = events.front(); + events.pop(); + return event; + } + + void registerForEvents(String name) + { + queue events = {}; + input_event_queues[name] = events; + } +} \ No newline at end of file diff --git a/src/input/user_input_manager.hpp b/src/input/user_input_manager.hpp new file mode 100644 index 0000000..1472cc8 --- /dev/null +++ b/src/input/user_input_manager.hpp @@ -0,0 +1,24 @@ +#ifndef DRUMZ_USER_INPUT_MANAGER_HPP +#define DRUMZ_USER_INPUT_MANAGER_HPP + +#include +#include + +namespace UserInputManager +{ + using std::vector; + + enum InputEvent + { + None, + EncoderButtonPress, + EncoderButtonRelease, + EncoderRotateLeft, + EncoderRotateRight, + }; + void init(); + InputEvent getNextEvent(String name); + void registerForEvents(String name); +} + +#endif // DRUMZ_USER_INPUT_MANAGER_HPP \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cf3bf23..0e0d7fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include