diff --git a/espmusicmouse/host_driver/host_driver.py b/espmusicmouse/host_driver/host_driver.py index dcf1740..d4c4c39 100644 --- a/espmusicmouse/host_driver/host_driver.py +++ b/espmusicmouse/host_driver/host_driver.py @@ -15,13 +15,6 @@ class MessageFwToHost(Enum): TOUCH_BUTTON_RELEASE = 3 -class MessageHostToFw(Enum): - LED_WHEEL_EFFECT_STATIC = 0 - LED_WHEEL_EFFECT_ALEXA_SWIPE = 1 - LED_WHEEL_EFFECT_CIRCULAR = 2 - LED_WHEEL_EFFECT_RANDOM_TWO_COLOR_INTERPOLATION = 3 - - class TouchButton(Enum): LEFT_FOOT = 0 RIGHT_FOOT = 1 @@ -46,6 +39,9 @@ mouse_led_effect_to_message_id = { EffectReverseSwipe: 10, } +PREV_BUTTON_LED_MSG = 20 +NEXT_BUTTON_LED_MSG = 21 + class RfidTokenRead: def __init__(self, id: bytes): @@ -135,6 +131,16 @@ class MusicMouseProtocol(asyncio.Protocol): mouse_led_effect_to_message_id[type(effect_cfg)], len(msg_content)) self.transport.write(header + msg_content) + def button_background_led_prev(self, val): + msg_content = struct.pack(" @@ -155,7 +158,7 @@ void sendMessageToHost(const TMessage &msg) } template -inline void handleIncomingMessagesFromHost(LedTask1 *ledTaskCircle, LedTask2 *ledTaskMouse) +inline void handleIncomingMessagesFromHost(LedTask1 *ledTaskCircle, LedTask2 *ledTaskMouse, uint8_t ledChannelLeft, uint8_t ledChannelRight) { if (Serial.available() < sizeof(MAGIC_TOKEN_FW_TO_HOST) + sizeof(MessageHostToFw) + sizeof(uint16_t)) return; @@ -235,6 +238,16 @@ inline void handleIncomingMessagesFromHost(LedTask1 *ledTaskCircle, LedTask2 *le auto cfg = reinterpret_cast(msgBuffer); ledTaskMouse->startEffect(*cfg); } + else if (msgType == MessageHostToFw::PREV_BUTTON_LED) + { + float *val = reinterpret_cast(msgBuffer); + ledcWrite(ledChannelLeft, uint32_t(255 * (*val))); + } + else if (msgType == MessageHostToFw::NEXT_BUTTON_LED) + { + float *val = reinterpret_cast(msgBuffer); + ledcWrite(ledChannelRight, uint32_t(255 * (*val))); + } else Serial.println("Unknown message type"); } diff --git a/espmusicmouse/src/main.cpp b/espmusicmouse/src/main.cpp index b9b4ead..7312f51 100644 --- a/espmusicmouse/src/main.cpp +++ b/espmusicmouse/src/main.cpp @@ -74,6 +74,8 @@ constexpr int BUTTON_LEFT_PIN = 14; constexpr int ROTARY_PRESS_PIN = 13; constexpr int BUTTON_RIGHT_LED_PIN = 33; constexpr int BUTTON_LEFT_LED_PIN = 12; +constexpr int PWM_FREQ = 5000; +constexpr int PWM_RESOLUTION = 8; using ace_button::AceButton; AceButton buttonLeft(BUTTON_LEFT_PIN); @@ -104,6 +106,12 @@ void setupButtons() buttonLeft.setEventHandler(handleButtonEvent); buttonRight.setEventHandler(handleButtonEvent); buttonRotary.setEventHandler(handleButtonEvent); + + ledcSetup(0, PWM_FREQ, PWM_RESOLUTION); + ledcAttachPin(BUTTON_LEFT_LED_PIN, 0); + + ledcSetup(1, PWM_FREQ, PWM_RESOLUTION); + ledcAttachPin(BUTTON_RIGHT_LED_PIN, 1); } void handleButtons() @@ -201,7 +209,7 @@ void setup() void loop() { - handleIncomingMessagesFromHost(&ledTaskCircle, &ledTaskMouse); + handleIncomingMessagesFromHost(&ledTaskCircle, &ledTaskMouse, 0, 1); handleTouchInputs(); handleRotaryEncoder(); handleButtons();