diff --git a/espmusicmouse/host_driver/host_driver.py b/espmusicmouse/host_driver/host_driver.py index fad8478..cdb82ff 100644 --- a/espmusicmouse/host_driver/host_driver.py +++ b/espmusicmouse/host_driver/host_driver.py @@ -29,7 +29,7 @@ class TouchButton(Enum): RIGHT_EAR = 3 -outgoing_msg_map = { +led_ring_effect_to_message_id = { EffectStaticConfig: 0, EffectAlexaSwipeConfig: 1, EffectCircularConfig: 2, @@ -37,6 +37,12 @@ outgoing_msg_map = { EffectSwipeAndChange: 4, } +mouse_led_effect_to_message_id = { + EffectStaticConfig: 5, + EffectCircularConfig: 6, + EffectRandomTwoColorInterpolationConfig: 7 +} + class RfidTokenRead: def __init__(self, id: bytes): @@ -72,11 +78,33 @@ class TouchButtonRelease: return "Released " + repr(self.touch_button) +class ButtonEvent: + button_name = {1: 'left', 2: 'right', 3: 'rotary'} + event_name = { + 0: 'pressed', + 1: 'released', + 2: 'clicked', + 3: 'double_clicked', + 4: 'long_pressed', + 5: 'repeat_pressed', + 6: 'long_released' + } + + def __init__(self, msg_content: bytes): + button_nr, event_nr = struct.unpack(" str: + return f"Button {self.button} {self.event}" + + incomingMsgMap = { 0: RfidTokenRead, 1: RotaryEncoderEvent, 2: TouchButtonPress, 3: TouchButtonRelease, + 4: ButtonEvent, } @@ -92,10 +120,16 @@ class MusicMouseProtocol(asyncio.Protocol): self.transport = transport self.in_buff = bytes() - def send_message(self, message): - msg_content = message.as_bytes() - header = struct.pack(" @@ -79,6 +86,12 @@ struct ClassToMessageType static constexpr auto msgType = MessageFwToHost::TOUCH_BUTTON_RELEASE; }; +template <> +struct ClassToMessageType +{ + static constexpr auto msgType = MessageFwToHost::BUTTON_EVENT; +}; + //---------------------------------------------------------------------------------------------------- enum class MessageHostToFw : uint8_t @@ -87,7 +100,11 @@ enum class MessageHostToFw : uint8_t LED_WHEEL_EFFECT_ALEXA_SWIPE = 1, LED_WHEEL_EFFECT_CIRCULAR = 2, LED_WHEEL_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION = 3, - LED_WHEEL_EFFECT_SWIPE_AND_CHANGE = 4 + LED_WHEEL_EFFECT_SWIPE_AND_CHANGE = 4, + MOUSE_LED_EFFECT_STATIC = 5, + MOUSE_LED_EFFECT_CIRCULAR = 6, + MOUSE_LED_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION = 7, + }; template <> @@ -134,8 +151,8 @@ void sendMessageToHost(const TMessage &msg) Serial.write((uint8_t *)&msg, sizeof(msg)); } -template -inline void handleIncomingMessagesFromHost(LedTask *ledTask) +template +inline void handleIncomingMessagesFromHost(LedTask1 *ledTaskCircle, LedTask2 *ledTaskMouse) { if (Serial.available() < sizeof(MAGIC_TOKEN_FW_TO_HOST) + sizeof(MessageHostToFw) + sizeof(uint16_t)) return; @@ -162,27 +179,42 @@ inline void handleIncomingMessagesFromHost(LedTask *ledTask) if (msgType == MessageHostToFw::LED_WHEEL_EFFECT_STATIC) { auto cfg = reinterpret_cast(msgBuffer); - ledTask->startEffect(*cfg); + ledTaskCircle->startEffect(*cfg); } else if (msgType == MessageHostToFw::LED_WHEEL_EFFECT_ALEXA_SWIPE) { auto cfg = reinterpret_cast(msgBuffer); - ledTask->startEffect(*cfg); + ledTaskCircle->startEffect(*cfg); } else if (msgType == MessageHostToFw::LED_WHEEL_EFFECT_CIRCULAR) { auto cfg = reinterpret_cast(msgBuffer); - ledTask->startEffect(*cfg); + ledTaskCircle->startEffect(*cfg); } else if (msgType == MessageHostToFw::LED_WHEEL_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION) { auto cfg = reinterpret_cast(msgBuffer); - ledTask->startEffect(*cfg); + ledTaskCircle->startEffect(*cfg); } else if (msgType == MessageHostToFw::LED_WHEEL_EFFECT_SWIPE_AND_CHANGE) { auto cfg = reinterpret_cast(msgBuffer); - ledTask->startEffect(*cfg); + ledTaskCircle->startEffect(*cfg); + } + else if (msgType == MessageHostToFw::MOUSE_LED_EFFECT_STATIC) + { + auto cfg = reinterpret_cast(msgBuffer); + ledTaskMouse->startEffect(*cfg); + } + else if (msgType == MessageHostToFw::MOUSE_LED_EFFECT_CIRCULAR) + { + auto cfg = reinterpret_cast(msgBuffer); + ledTaskMouse->startEffect(*cfg); + } + else if (msgType == MessageHostToFw::MOUSE_LED_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION) + { + auto cfg = reinterpret_cast(msgBuffer); + ledTaskMouse->startEffect(*cfg); } else Serial.println("Unknown message type"); diff --git a/espmusicmouse/src/main.cpp b/espmusicmouse/src/main.cpp index 29bee52..b9b4ead 100644 --- a/espmusicmouse/src/main.cpp +++ b/espmusicmouse/src/main.cpp @@ -11,6 +11,8 @@ #include "effects/AlexaSwipe.h" #include "effects/RandomTwoColorInterpolation.h" +#include "AceButton.h" + #include "driver/touch_pad.h" #include "Messages.h" @@ -67,23 +69,48 @@ void handleRotaryEncoder() // -------------------------------------------------- Buttons ---------------------------------------- -constexpr int BUTTON1_PIN = 25; -constexpr int BUTTON2_PIN = 14; +constexpr int BUTTON_RIGHT_PIN = 25; +constexpr int BUTTON_LEFT_PIN = 14; constexpr int ROTARY_PRESS_PIN = 13; -constexpr int BUTTON1_LED_PIN = 33; -constexpr int BUTTON2_LED_PIN = 12; +constexpr int BUTTON_RIGHT_LED_PIN = 33; +constexpr int BUTTON_LEFT_LED_PIN = 12; + +using ace_button::AceButton; +AceButton buttonLeft(BUTTON_LEFT_PIN); +AceButton buttonRight(BUTTON_RIGHT_PIN); +AceButton buttonRotary(ROTARY_PRESS_PIN); + +void handleButtonEvent(AceButton *button, uint8_t eventType, uint8_t /*buttonState*/) +{ + uint8_t buttonNr = 0; + if (button == &buttonLeft) + buttonNr = 1; + else if (button == &buttonRight) + buttonNr = 2; + else if (button == &buttonRotary) + buttonNr = 3; + sendMessageToHost(MsgButtonEvent{buttonNr, eventType}); +} void setupButtons() { - pinMode(BUTTON1_PIN, INPUT_PULLUP); - pinMode(BUTTON2_PIN, INPUT_PULLUP); + pinMode(BUTTON_RIGHT_PIN, INPUT_PULLUP); + pinMode(BUTTON_LEFT_PIN, INPUT_PULLUP); pinMode(ROTARY_PRESS_PIN, INPUT_PULLUP); - pinMode(BUTTON1_LED_PIN, OUTPUT); - pinMode(BUTTON2_LED_PIN, OUTPUT); + pinMode(BUTTON_RIGHT_LED_PIN, OUTPUT); + pinMode(BUTTON_LEFT_LED_PIN, OUTPUT); - digitalWrite(BUTTON1_LED_PIN, 1); - digitalWrite(BUTTON2_LED_PIN, 1); + buttonLeft.setEventHandler(handleButtonEvent); + buttonRight.setEventHandler(handleButtonEvent); + buttonRotary.setEventHandler(handleButtonEvent); +} + +void handleButtons() +{ + buttonLeft.check(); + buttonRight.check(); + buttonRotary.check(); } // -------------------------------------------------- Led circle ------------------------------------------ @@ -113,6 +140,7 @@ void setupMouseLeds() } // -------------------------------------------------- Touch Buttons ---------------------------------------- + constexpr auto TOUCH_PAD_RIGHT_EAR = TOUCH_PAD_NUM0; constexpr auto TOUCH_PAD_LEFT_EAR = TOUCH_PAD_NUM9; constexpr auto TOUCH_PAD_RIGHT_FOOT = TOUCH_PAD_NUM2; @@ -168,11 +196,13 @@ void setup() setupLedCircle(); setupButtons(); setupTouchButtons(); + setupMouseLeds(); } void loop() { - handleIncomingMessagesFromHost(&ledTaskCircle); + handleIncomingMessagesFromHost(&ledTaskCircle, &ledTaskMouse); handleTouchInputs(); handleRotaryEncoder(); + handleButtons(); } \ No newline at end of file