From ee632664e60a140c923e6990fd56d0f3abe7929c Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Wed, 15 Dec 2021 22:47:28 +0100 Subject: [PATCH] Touch events & rotary events to host --- espmusicmouse/host_driver/host_driver.py | 45 ++++++++- espmusicmouse/host_driver/led_cmds.py | 4 +- espmusicmouse/host_driver/main.py | 58 +++++++++--- espmusicmouse/host_driver/player.py | 110 ++++++++++++++++++++-- espmusicmouse/lib/ledtl/effects/Static.h | 11 ++- espmusicmouse/src/Messages.h | 50 +++++++++- espmusicmouse/src/main.cpp | 112 +++++++++++------------ 7 files changed, 307 insertions(+), 83 deletions(-) diff --git a/espmusicmouse/host_driver/host_driver.py b/espmusicmouse/host_driver/host_driver.py index 10efdb8..fad8478 100644 --- a/espmusicmouse/host_driver/host_driver.py +++ b/espmusicmouse/host_driver/host_driver.py @@ -10,8 +10,9 @@ MAGIC_TOKEN_FW_TO_HOST = 0x10c65631 class MessageFwToHost(Enum): RFID_TOKEN_READ = 0 - BUTTON_NORMAL_PRESS = 1 - ROTARY_ENCODER = 2 + ROTARY_ENCODER = 1 + TOUCH_BUTTON_PRESS = 2 + TOUCH_BUTTON_RELEASE = 3 class MessageHostToFw(Enum): @@ -21,6 +22,13 @@ class MessageHostToFw(Enum): LED_WHEEL_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION = 3 +class TouchButton(Enum): + LEFT_FOOT = 0 + RIGHT_FOOT = 1 + LEFT_EAR = 2 + RIGHT_EAR = 3 + + outgoing_msg_map = { EffectStaticConfig: 0, EffectAlexaSwipeConfig: 1, @@ -38,7 +46,38 @@ class RfidTokenRead: return "RFID Token (" + " ".join(f"{v:02x}" for v in self.id) + ")" -incomingMsgMap = {0: RfidTokenRead} +class RotaryEncoderEvent: + def __init__(self, msg_content: bytes): + self.position, self.direction = struct.unpack(" str: + return "Pressed " + repr(self.touch_button) + + +class TouchButtonRelease: + def __init__(self, msg_content: bytes): + val = int(msg_content[0]) + self.touch_button = TouchButton(val) + + def __repr__(self) -> str: + return "Released " + repr(self.touch_button) + + +incomingMsgMap = { + 0: RfidTokenRead, + 1: RotaryEncoderEvent, + 2: TouchButtonPress, + 3: TouchButtonRelease, +} class MusicMouseProtocol(asyncio.Protocol): diff --git a/espmusicmouse/host_driver/led_cmds.py b/espmusicmouse/host_driver/led_cmds.py index db717b5..cb80115 100644 --- a/espmusicmouse/host_driver/led_cmds.py +++ b/espmusicmouse/host_driver/led_cmds.py @@ -44,9 +44,11 @@ class ColorHSV: @dataclass class EffectStaticConfig: color: ColorRGBW + begin: int = 0 + end: int = 0 def as_bytes(self) -> bytes: - return self.color.as_bytes() + return self.color.as_bytes() + struct.pack(" @@ -22,7 +27,11 @@ public: int operator()() { - setLedRGBW(ledStrip_, 0, NUM_LEDS, config_.color); + if (config_.begin == config_.end) + setLedRGBW(ledStrip_, 0, NUM_LEDS, config_.color); + else + setLedRGBW(ledStrip_, config_.begin, config_.end, config_.color); + return 10000; // nothing changing, return some large time to sleep } diff --git a/espmusicmouse/src/Messages.h b/espmusicmouse/src/Messages.h index 713a098..1c8be95 100644 --- a/espmusicmouse/src/Messages.h +++ b/espmusicmouse/src/Messages.h @@ -18,21 +18,67 @@ struct ClassToMessageType enum class MessageFwToHost : uint8_t { RFID_TOKEN_READ = 0, - BUTTON_NORMAL_PRESS = 1, - ROTARY_ENCODER = 2, + ROTARY_ENCODER = 1, + TOUCH_BUTTON_PRESS = 2, + TOUCH_BUTTON_RELEASE = 3, }; +enum class TouchButton : uint8_t +{ + LEFT_FOOT = 0, + RIGHT_FOOT = 1, + LEFT_EAR = 2, + RIGHT_EAR = 3 +}; + +#pragma pack(push, 1) struct MsgRfidTokenRead { uint8_t tagId[5]; }; +struct MsgRotaryEncoder +{ + int32_t position; + uint8_t direction; +}; + +struct MsgTouchButtonPress +{ + TouchButton button; +}; + +struct MsgTouchButtonRelease +{ + TouchButton button; +}; + +#pragma pack(pop) + template <> struct ClassToMessageType { static constexpr auto msgType = MessageFwToHost::RFID_TOKEN_READ; }; +template <> +struct ClassToMessageType +{ + static constexpr auto msgType = MessageFwToHost::ROTARY_ENCODER; +}; + +template <> +struct ClassToMessageType +{ + static constexpr auto msgType = MessageFwToHost::TOUCH_BUTTON_PRESS; +}; + +template <> +struct ClassToMessageType +{ + static constexpr auto msgType = MessageFwToHost::TOUCH_BUTTON_RELEASE; +}; + //---------------------------------------------------------------------------------------------------- enum class MessageHostToFw : uint8_t diff --git a/espmusicmouse/src/main.cpp b/espmusicmouse/src/main.cpp index a35528a..29bee52 100644 --- a/espmusicmouse/src/main.cpp +++ b/espmusicmouse/src/main.cpp @@ -58,6 +58,13 @@ void setupRotaryEncoder() ESP_ERROR_CHECK(rotary_encoder_set_queue(&info, eventQueueRotaryEncoder)); } +void handleRotaryEncoder() +{ + rotary_encoder_event_t event = {0}; + if (xQueueReceive(eventQueueRotaryEncoder, &event, 0) == pdTRUE) + sendMessageToHost(MsgRotaryEncoder{event.state.position, (uint8_t)(event.state.direction)}); +} + // -------------------------------------------------- Buttons ---------------------------------------- constexpr int BUTTON1_PIN = 25; @@ -79,7 +86,8 @@ void setupButtons() digitalWrite(BUTTON2_LED_PIN, 1); } -// -------------------------------------------------- Led circle ---------------------------------------- +// -------------------------------------------------- Led circle ------------------------------------------ + LedStripRGBW<51> ledStripCircle; Esp32DriverRGBW ledDriverCircle; LedTask ledTaskCircle; @@ -88,7 +96,20 @@ void setupLedCircle() { ledDriverCircle.begin(23, 0); ledTaskCircle.begin(ledStripCircle, ledDriverCircle); - ledTaskCircle.startEffect(EffectStaticConfig{ColorRGBW{0, 0, 0, 0}}); + ledTaskCircle.startEffect(EffectStaticConfig(ColorRGBW{0, 0, 0, 0})); +} + +// -------------------------------------------------- Mouse Leds -- ---------------------------------------- + +LedStripRGBW<12 + 16 + 17> ledStripMouse; +Esp32DriverRGBW ledDriverMouse; +LedTask ledTaskMouse; + +void setupMouseLeds() +{ + ledDriverMouse.begin(16, 1); + ledTaskMouse.begin(ledStripMouse, ledDriverMouse); + ledTaskMouse.startEffect(EffectStaticConfig{ColorRGBW{0, 0, 0, 0}, 0, 0}); } // -------------------------------------------------- Touch Buttons ---------------------------------------- @@ -107,10 +128,37 @@ void setupTouchButtons() touch_pad_filter_start(2); } -//------------------------------------------------------------------------------------------------------- +void handleTouchInputs() +{ + static bool previousState[4]; -LedStripRGBW<12 + 16 + 17> ledStripMouse; -Esp32DriverRGBW ledDriverMouse; + uint16_t touchLeftEar = 0; + uint16_t touchRightEar = 0; + uint16_t touchLeftFoot = 0; + uint16_t touchRightFoot = 0; + + touch_pad_read(TOUCH_PAD_LEFT_FOOT, &touchLeftFoot); + touch_pad_read(TOUCH_PAD_RIGHT_FOOT, &touchRightFoot); + touch_pad_read(TOUCH_PAD_LEFT_EAR, &touchLeftEar); + touch_pad_read(TOUCH_PAD_RIGHT_EAR, &touchRightEar); + + bool currentState[4]; + currentState[int(TouchButton::LEFT_FOOT)] = touchLeftFoot < 380; + currentState[int(TouchButton::RIGHT_FOOT)] = touchRightFoot < 380; + currentState[int(TouchButton::LEFT_EAR)] = touchLeftEar < 430; + currentState[int(TouchButton::RIGHT_EAR)] = touchRightEar < 430; + + for (int i = 0; i < 4; ++i) + { + if (previousState[i] == false && currentState[i] == true) + sendMessageToHost(MsgTouchButtonPress{TouchButton(i)}); + else if (previousState[i] == true && currentState[i] == false) + sendMessageToHost(MsgTouchButtonRelease{TouchButton(i)}); + previousState[i] = currentState[i]; + } +} + +//------------------------------------------------------------------------------------------------------- void setup() { @@ -120,61 +168,11 @@ void setup() setupLedCircle(); setupButtons(); setupTouchButtons(); - - ledDriverMouse.begin(16, 1); -} - -void handleTouchInputs() -{ } void loop() { handleIncomingMessagesFromHost(&ledTaskCircle); - - int btn1 = !digitalRead(BUTTON1_PIN); - int btn2 = !digitalRead(BUTTON2_PIN); - int rotaryBtn = !digitalRead(ROTARY_PRESS_PIN); - - uint16_t touchLeftEar = 1, touchRightEar = 1, touchLeftFoot = 1, touchRightFoot = 1; - - touch_pad_read(TOUCH_PAD_LEFT_FOOT, &touchLeftFoot); - touch_pad_read(TOUCH_PAD_RIGHT_FOOT, &touchRightFoot); - touch_pad_read(TOUCH_PAD_LEFT_EAR, &touchLeftEar); // 430 - touch_pad_read(TOUCH_PAD_RIGHT_EAR, &touchRightEar); // 430 - - //Serial.printf("%d, %d, %d | re %d, le %d, rf %d, lf %d\n", btn1, btn2, rotaryBtn, touchRightEar, touchLeftEar, touchRightFoot, touchLeftFoot); - - for (int i = 0; i < ledStripMouse.numLeds(); ++i) - ledStripMouse.set(i, 0, 0, 30, 0); - - bool rightFootPressed = touchRightFoot < 380; - bool leftFootPressed = touchLeftFoot < 380; - bool leftEarPressed = touchLeftEar < 430; - bool rightEarPressed = touchRightEar < 430; - - if (btn1 || rightFootPressed) - { - for (int i = 0; i < 6; ++i) - ledStripMouse.set(i, 180, 0, 255, 0); - } - - if (btn2 || leftFootPressed) - { - for (int i = 6; i < 12; ++i) - ledStripMouse.set(i, 180, 0, 255, 0); - } - - if (leftEarPressed) - for (int i = 12; i < 12 + 16; ++i) - ledStripMouse.set(i, 28, 241, 234, 0); - - if (rightEarPressed) - for (int i = 12 + 16; i < ledStripMouse.numLeds(); ++i) - ledStripMouse.set(i, 28, 241, 234, 0); - - ledDriverMouse.writeSync(ledStripMouse.rawData(), ledStripMouse.numLeds()); - delay(50); - - //delay(150); + handleTouchInputs(); + handleRotaryEncoder(); } \ No newline at end of file