From 2073c4f85537e07b14d44b589ef44d98a003776d Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Tue, 6 Apr 2021 19:02:20 -0500 Subject: [PATCH] Adapt miscellaneous LED mode plugins to KeyEvent handlers Signed-off-by: Michael Richters --- .../src/kaleidoscope/plugin/FingerPainter.cpp | 18 ++++++++------ .../src/kaleidoscope/plugin/FingerPainter.h | 2 +- .../src/kaleidoscope/plugin/Heatmap.cpp | 24 ++++++++++--------- .../src/kaleidoscope/plugin/Heatmap.h | 4 ++-- .../plugin/LED-AlphaSquare/Effect.cpp | 20 ++++++++-------- .../plugin/LED-AlphaSquare/Effect.h | 2 +- .../src/kaleidoscope/plugin/LED-Stalker.cpp | 11 +++++---- .../src/kaleidoscope/plugin/LED-Stalker.h | 2 +- .../src/kaleidoscope/plugin/LED-Wavepool.cpp | 15 ++++++------ .../src/kaleidoscope/plugin/LED-Wavepool.h | 4 ++-- .../src/kaleidoscope/plugin/TriColor.cpp | 2 +- 11 files changed, 56 insertions(+), 48 deletions(-) diff --git a/plugins/Kaleidoscope-FingerPainter/src/kaleidoscope/plugin/FingerPainter.cpp b/plugins/Kaleidoscope-FingerPainter/src/kaleidoscope/plugin/FingerPainter.cpp index fef12cdf..268b8801 100644 --- a/plugins/Kaleidoscope-FingerPainter/src/kaleidoscope/plugin/FingerPainter.cpp +++ b/plugins/Kaleidoscope-FingerPainter/src/kaleidoscope/plugin/FingerPainter.cpp @@ -50,20 +50,22 @@ void FingerPainter::toggle(void) { edit_mode_ = !edit_mode_; } -EventHandlerResult FingerPainter::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state) { +EventHandlerResult FingerPainter::onKeyEvent(KeyEvent &event) { if (!Runtime.has_leds || !edit_mode_) return EventHandlerResult::OK; - if (!keyToggledOn(key_state)) { - return EventHandlerResult::EVENT_CONSUMED; + if (keyToggledOff(event.state)) { + return EventHandlerResult::OK; } - if (!key_addr.isValid()) - return EventHandlerResult::EVENT_CONSUMED; + if (!event.addr.isValid()) + return EventHandlerResult::OK; // TODO(anyone): The following works only for keyboards with LEDs for each key. - uint8_t color_index = ::LEDPaletteTheme.lookupColorIndexAtPosition(color_base_, Runtime.device().getLedIndex(key_addr)); + uint8_t color_index = ::LEDPaletteTheme + .lookupColorIndexAtPosition(color_base_, + Runtime.device().getLedIndex(event.addr)); // Find the next color in the palette that is different. // But do not loop forever! @@ -80,7 +82,9 @@ EventHandlerResult FingerPainter::onKeyswitchEvent(Key &mapped_key, KeyAddr key_ new_color = ::LEDPaletteTheme.lookupPaletteColor(color_index); } - ::LEDPaletteTheme.updateColorIndexAtPosition(color_base_, Runtime.device().getLedIndex(key_addr), color_index); + ::LEDPaletteTheme.updateColorIndexAtPosition(color_base_, + Runtime.device().getLedIndex(event.addr), + color_index); return EventHandlerResult::EVENT_CONSUMED; } diff --git a/plugins/Kaleidoscope-FingerPainter/src/kaleidoscope/plugin/FingerPainter.h b/plugins/Kaleidoscope-FingerPainter/src/kaleidoscope/plugin/FingerPainter.h index d20cb06b..40dd2670 100644 --- a/plugins/Kaleidoscope-FingerPainter/src/kaleidoscope/plugin/FingerPainter.h +++ b/plugins/Kaleidoscope-FingerPainter/src/kaleidoscope/plugin/FingerPainter.h @@ -32,7 +32,7 @@ class FingerPainter : public LEDMode { static void toggle(void); - EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state); + EventHandlerResult onKeyEvent(KeyEvent &event); EventHandlerResult onFocusEvent(const char *command); EventHandlerResult onSetup(); EventHandlerResult onNameQuery(); diff --git a/plugins/Kaleidoscope-Heatmap/src/kaleidoscope/plugin/Heatmap.cpp b/plugins/Kaleidoscope-Heatmap/src/kaleidoscope/plugin/Heatmap.cpp index 2a492d2c..a67a3767 100644 --- a/plugins/Kaleidoscope-Heatmap/src/kaleidoscope/plugin/Heatmap.cpp +++ b/plugins/Kaleidoscope-Heatmap/src/kaleidoscope/plugin/Heatmap.cpp @@ -133,36 +133,38 @@ void Heatmap::TransientLEDMode::resetMap() { highest_ = 1; } -EventHandlerResult Heatmap::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state) { +// It may be better to use `onKeyswitchEvent()` here +EventHandlerResult Heatmap::onKeyEvent(KeyEvent &event) { + // If the keyboard has no LEDs, return if (!Runtime.has_leds) return EventHandlerResult::OK; - // this methode is called frequently by Kaleidoscope - // even if the module isn't activated + // If the event doesn't correspond to a physical key, skip it + if (!event.addr.isValid()) + return EventHandlerResult::OK; // if it is a synthetic key, skip it - if (key_state & INJECTED) + if (event.state & INJECTED) return EventHandlerResult::OK; // if the key is not toggled on, skip it - if (!keyToggledOn(key_state)) + if (!keyToggledOn(event.state)) return EventHandlerResult::OK; // if the LED mode is not current, skip it if (::LEDControl.get_mode_index() != led_mode_id_) return EventHandlerResult::OK; - return ::LEDControl.get_mode() - ->onKeyswitchEvent(mapped_key, key_addr, key_state); + return ::LEDControl.get_mode()->onKeyEvent(event); } -EventHandlerResult Heatmap::TransientLEDMode::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state) { +EventHandlerResult Heatmap::TransientLEDMode::onKeyEvent(KeyEvent &event) { // increment the heatmap_ value related to the key - heatmap_[key_addr.toInt()]++; + heatmap_[event.addr.toInt()]++; // check highest_ - if (highest_ < heatmap_[key_addr.toInt()]) { - highest_ = heatmap_[key_addr.toInt()]; + if (highest_ < heatmap_[event.addr.toInt()]) { + highest_ = heatmap_[event.addr.toInt()]; // if highest_ (and so heatmap_ value related to the key) // is close to overflow: call shiftStats diff --git a/plugins/Kaleidoscope-Heatmap/src/kaleidoscope/plugin/Heatmap.h b/plugins/Kaleidoscope-Heatmap/src/kaleidoscope/plugin/Heatmap.h index 53774b97..dafa8c03 100644 --- a/plugins/Kaleidoscope-Heatmap/src/kaleidoscope/plugin/Heatmap.h +++ b/plugins/Kaleidoscope-Heatmap/src/kaleidoscope/plugin/Heatmap.h @@ -33,7 +33,7 @@ class Heatmap : public Plugin, static uint8_t heat_colors_length; void resetMap(void); - EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state); + EventHandlerResult onKeyEvent(KeyEvent &event); EventHandlerResult beforeEachCycle(); // This class' instance has dynamic lifetime @@ -48,7 +48,7 @@ class Heatmap : public Plugin, explicit TransientLEDMode(const Heatmap *parent); void resetMap(); - EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state); + EventHandlerResult onKeyEvent(KeyEvent &event); EventHandlerResult beforeEachCycle(); protected: diff --git a/plugins/Kaleidoscope-LED-AlphaSquare/src/kaleidoscope/plugin/LED-AlphaSquare/Effect.cpp b/plugins/Kaleidoscope-LED-AlphaSquare/src/kaleidoscope/plugin/LED-AlphaSquare/Effect.cpp index 5667e4ca..45660d55 100644 --- a/plugins/Kaleidoscope-LED-AlphaSquare/src/kaleidoscope/plugin/LED-AlphaSquare/Effect.cpp +++ b/plugins/Kaleidoscope-LED-AlphaSquare/src/kaleidoscope/plugin/LED-AlphaSquare/Effect.cpp @@ -61,40 +61,40 @@ void AlphaSquareEffect::TransientLEDMode::refreshAt(KeyAddr key_addr) { ::LEDControl.setCrgbAt(key_addr, CRGB(0, 0, 0)); } -EventHandlerResult AlphaSquareEffect::onKeyswitchEvent(Key &mappedKey, KeyAddr key_addr, uint8_t keyState) { +EventHandlerResult AlphaSquareEffect::onKeyEvent(KeyEvent &event) { if (!Runtime.has_leds) return EventHandlerResult::OK; if (::LEDControl.get_mode_index() != led_mode_id_) return EventHandlerResult::OK; - if (keyState & INJECTED) + if (event.state & INJECTED) return EventHandlerResult::OK; - if (mappedKey < Key_A || mappedKey > Key_0) + if (event.key < Key_A || event.key > Key_0) return EventHandlerResult::OK; - if (!keyIsPressed(keyState)) - return EventHandlerResult::OK; + // if (!keyIsPressed(event.state)) + // return EventHandlerResult::OK; uint8_t display_col = 2; auto this_led_mode = ::LEDControl.get_mode(); Key prev_key = this_led_mode->last_key_left_; - if (key_addr.col() < Runtime.device().matrix_columns / 2) { - this_led_mode->last_key_left_ = mappedKey; + if (event.addr.col() < Runtime.device().matrix_columns / 2) { + this_led_mode->last_key_left_ = event.key; this_led_mode->start_time_left_ = Runtime.millisAtCycleStart(); } else { prev_key = this_led_mode->last_key_right_; - this_led_mode->last_key_right_ = mappedKey; + this_led_mode->last_key_right_ = event.key; this_led_mode->start_time_right_ = Runtime.millisAtCycleStart(); display_col = 10; } - if (prev_key != mappedKey) + if (prev_key != event.key) ::AlphaSquare.clear(prev_key, display_col); - ::AlphaSquare.display(mappedKey, display_col); + ::AlphaSquare.display(event.key, display_col); return EventHandlerResult::OK; } diff --git a/plugins/Kaleidoscope-LED-AlphaSquare/src/kaleidoscope/plugin/LED-AlphaSquare/Effect.h b/plugins/Kaleidoscope-LED-AlphaSquare/src/kaleidoscope/plugin/LED-AlphaSquare/Effect.h index fffe6861..dc197fa8 100644 --- a/plugins/Kaleidoscope-LED-AlphaSquare/src/kaleidoscope/plugin/LED-AlphaSquare/Effect.h +++ b/plugins/Kaleidoscope-LED-AlphaSquare/src/kaleidoscope/plugin/LED-AlphaSquare/Effect.h @@ -30,7 +30,7 @@ class AlphaSquareEffect : public Plugin, static uint16_t length; - EventHandlerResult onKeyswitchEvent(Key &mappedKey, KeyAddr key_addr, uint8_t keyState); + EventHandlerResult onKeyEvent(KeyEvent &event); // This class' instance has dynamic lifetime // diff --git a/plugins/Kaleidoscope-LED-Stalker/src/kaleidoscope/plugin/LED-Stalker.cpp b/plugins/Kaleidoscope-LED-Stalker/src/kaleidoscope/plugin/LED-Stalker.cpp index 9a59f93f..e8cc5ac3 100644 --- a/plugins/Kaleidoscope-LED-Stalker/src/kaleidoscope/plugin/LED-Stalker.cpp +++ b/plugins/Kaleidoscope-LED-Stalker/src/kaleidoscope/plugin/LED-Stalker.cpp @@ -34,19 +34,20 @@ StalkerEffect::TransientLEDMode::TransientLEDMode(const StalkerEffect *parent) map_{} {} -EventHandlerResult StalkerEffect::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t keyState) { +EventHandlerResult StalkerEffect::onKeyEvent(KeyEvent &event) { if (!Runtime.has_leds) return EventHandlerResult::OK; - if (!key_addr.isValid()) + if (!event.addr.isValid()) return EventHandlerResult::OK; if (::LEDControl.get_mode_index() != led_mode_id_) return EventHandlerResult::OK; - if (keyIsPressed(keyState)) { - ::LEDControl.get_mode()->map_[key_addr.toInt()] = 0xff; - } + // The simplest thing to do is trigger on both press and release. The color + // will fade while the key is held, and get restored to full brightness when + // it's released. + ::LEDControl.get_mode()->map_[event.addr.toInt()] = 0xff; return EventHandlerResult::OK; } diff --git a/plugins/Kaleidoscope-LED-Stalker/src/kaleidoscope/plugin/LED-Stalker.h b/plugins/Kaleidoscope-LED-Stalker/src/kaleidoscope/plugin/LED-Stalker.h index 8da6022a..6556e7c9 100644 --- a/plugins/Kaleidoscope-LED-Stalker/src/kaleidoscope/plugin/LED-Stalker.h +++ b/plugins/Kaleidoscope-LED-Stalker/src/kaleidoscope/plugin/LED-Stalker.h @@ -39,7 +39,7 @@ class StalkerEffect : public Plugin, static uint16_t step_length; static cRGB inactive_color; - EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t keyState); + EventHandlerResult onKeyEvent(KeyEvent &event); // This class' instance has dynamic lifetime // diff --git a/plugins/Kaleidoscope-LED-Wavepool/src/kaleidoscope/plugin/LED-Wavepool.cpp b/plugins/Kaleidoscope-LED-Wavepool/src/kaleidoscope/plugin/LED-Wavepool.cpp index 4f70e8c9..73e784a9 100644 --- a/plugins/Kaleidoscope-LED-Wavepool/src/kaleidoscope/plugin/LED-Wavepool.cpp +++ b/plugins/Kaleidoscope-LED-Wavepool/src/kaleidoscope/plugin/LED-Wavepool.cpp @@ -45,20 +45,21 @@ WavepoolEffect::TransientLEDMode::TransientLEDMode(const WavepoolEffect *parent) page_(0) {} -EventHandlerResult WavepoolEffect::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state) { - if (!key_addr.isValid()) +EventHandlerResult WavepoolEffect::onKeyEvent(KeyEvent &event) { + if (!event.addr.isValid()) return EventHandlerResult::OK; if (::LEDControl.get_mode_index() != led_mode_id_) return EventHandlerResult::OK; - return ::LEDControl.get_mode() - ->onKeyswitchEvent(mapped_key, key_addr, key_state); + return ::LEDControl.get_mode()->onKeyEvent(event); } -EventHandlerResult WavepoolEffect::TransientLEDMode::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state) { - if (keyIsPressed(key_state)) { - surface_[page_][pgm_read_byte(rc2pos + key_addr.toInt())] = 0x7f; +EventHandlerResult WavepoolEffect::TransientLEDMode::onKeyEvent(KeyEvent &event) { + // It might be better to trigger on both toggle-on and toggle-off, but maybe + // just the former. + if (keyIsPressed(event.state)) { + surface_[page_][pgm_read_byte(rc2pos + event.addr.toInt())] = 0x7f; frames_since_event_ = 0; } diff --git a/plugins/Kaleidoscope-LED-Wavepool/src/kaleidoscope/plugin/LED-Wavepool.h b/plugins/Kaleidoscope-LED-Wavepool/src/kaleidoscope/plugin/LED-Wavepool.h index 1ff8faf9..86e2de61 100644 --- a/plugins/Kaleidoscope-LED-Wavepool/src/kaleidoscope/plugin/LED-Wavepool.h +++ b/plugins/Kaleidoscope-LED-Wavepool/src/kaleidoscope/plugin/LED-Wavepool.h @@ -34,7 +34,7 @@ class WavepoolEffect : public Plugin, public: WavepoolEffect(void) {} - EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state); + EventHandlerResult onKeyEvent(KeyEvent &event); // ms before idle animation starts after last keypress static uint16_t idle_timeout; @@ -53,7 +53,7 @@ class WavepoolEffect : public Plugin, // explicit TransientLEDMode(const WavepoolEffect *parent); - EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state); + EventHandlerResult onKeyEvent(KeyEvent &event); protected: diff --git a/plugins/Kaleidoscope-LEDEffects/src/kaleidoscope/plugin/TriColor.cpp b/plugins/Kaleidoscope-LEDEffects/src/kaleidoscope/plugin/TriColor.cpp index 81534cd7..4da14b21 100644 --- a/plugins/Kaleidoscope-LEDEffects/src/kaleidoscope/plugin/TriColor.cpp +++ b/plugins/Kaleidoscope-LEDEffects/src/kaleidoscope/plugin/TriColor.cpp @@ -29,7 +29,7 @@ TriColor::TriColor(cRGB base_color, cRGB mod_color, cRGB esc_color) { void TriColor::TransientLEDMode::update(void) { for (auto key_addr : KeyAddr::all()) { - Key k = Layer.lookup(key_addr); + Key k = Layer.lookupOnActiveLayer(key_addr); // Special keys are always mod_color if (k.getFlags() != 0) {