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..afc93e3 --- /dev/null +++ b/src/input/user_input_manager.cpp @@ -0,0 +1,81 @@ +#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; + } + Serial.print("getting next event (" + String(events.size()) + "->"); + InputEvent event = events.front(); + events.pop(); + Serial.println(String(events.size()) + ")"); + 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