diff --git a/src/kaleidoscope/plugin/LED-ActiveModColor.cpp b/src/kaleidoscope/plugin/LED-ActiveModColor.cpp index d6df13f9..e0dfb324 100644 --- a/src/kaleidoscope/plugin/LED-ActiveModColor.cpp +++ b/src/kaleidoscope/plugin/LED-ActiveModColor.cpp @@ -22,41 +22,30 @@ namespace kaleidoscope { namespace plugin { +uint8_t ActiveModColorEffect::mod_keys_[MAX_MODS_PER_LAYER]; +uint8_t ActiveModColorEffect::mod_key_count_; + cRGB ActiveModColorEffect::highlight_color = (cRGB) { 0xff, 0xff, 0xff }; cRGB ActiveModColorEffect::sticky_color = CRGB(0xff, 0x00, 0x00); -EventHandlerResult ActiveModColorEffect::beforeReportingState() { +EventHandlerResult ActiveModColorEffect::onLayerChange() { if (!Kaleidoscope.has_leds) return EventHandlerResult::OK; + mod_key_count_ = 0; + for (byte r = 0; r < ROWS; r++) { for (byte c = 0; c < COLS; c++) { - Key k = Layer.lookupOnActiveLayer(r, c); - - if (::OneShot.isOneShotKey(k)) { - if (::OneShot.isSticky(k)) - ::LEDControl.setCrgbAt(r, c, sticky_color); - else if (::OneShot.isActive(k)) - ::LEDControl.setCrgbAt(r, c, highlight_color); - else - ::LEDControl.refreshAt(r, c); - } else if (k.raw >= Key_LeftControl.raw && k.raw <= Key_RightGui.raw) { - if (hid::isModifierKeyActive(k)) - ::LEDControl.setCrgbAt(r, c, highlight_color); - else - ::LEDControl.refreshAt(r, c); - } else if (k.flags == (SYNTHETIC | SWITCH_TO_KEYMAP)) { - uint8_t layer = k.keyCode; - if (layer >= LAYER_SHIFT_OFFSET) - layer -= LAYER_SHIFT_OFFSET; - - if (Layer.isOn(layer)) - ::LEDControl.setCrgbAt(r, c, highlight_color); - else - ::LEDControl.refreshAt(r, c); + Key k = Layer.lookup(r, c); + + if (::OneShot.isOneShotKey(k) || + (k.raw >= Key_LeftControl.raw && k.raw <= Key_RightGui.raw) || + (k.flags == (SYNTHETIC | SWITCH_TO_KEYMAP))) { + uint8_t coords = r * COLS + c; + mod_keys_[mod_key_count_++] = coords; } } } @@ -64,6 +53,41 @@ EventHandlerResult ActiveModColorEffect::beforeReportingState() { return EventHandlerResult::OK; } +EventHandlerResult ActiveModColorEffect::beforeReportingState() { + for (uint8_t i = 0; i < mod_key_count_; i++) { + uint8_t coords = mod_keys_[i]; + byte c = coords % COLS; + byte r = (coords - c) / COLS; + + Key k = Layer.lookup(r, c); + + if (::OneShot.isOneShotKey(k)) { + if (::OneShot.isSticky(k)) + ::LEDControl.setCrgbAt(r, c, sticky_color); + else if (::OneShot.isActive(k)) + ::LEDControl.setCrgbAt(r, c, highlight_color); + else + ::LEDControl.refreshAt(r, c); + } else if (k.raw >= Key_LeftControl.raw && k.raw <= Key_RightGui.raw) { + if (hid::isModifierKeyActive(k)) + ::LEDControl.setCrgbAt(r, c, highlight_color); + else + ::LEDControl.refreshAt(r, c); + } else if (k.flags == (SYNTHETIC | SWITCH_TO_KEYMAP)) { + uint8_t layer = k.keyCode; + if (layer >= LAYER_SHIFT_OFFSET) + layer -= LAYER_SHIFT_OFFSET; + + if (Layer.isOn(layer)) + ::LEDControl.setCrgbAt(r, c, highlight_color); + else + ::LEDControl.refreshAt(r, c); + } + } + + return EventHandlerResult::OK; +} + } } diff --git a/src/kaleidoscope/plugin/LED-ActiveModColor.h b/src/kaleidoscope/plugin/LED-ActiveModColor.h index 6beb7889..ab2b94f5 100644 --- a/src/kaleidoscope/plugin/LED-ActiveModColor.h +++ b/src/kaleidoscope/plugin/LED-ActiveModColor.h @@ -20,6 +20,8 @@ #include #include +#define MAX_MODS_PER_LAYER 16 + namespace kaleidoscope { namespace plugin { class ActiveModColorEffect : public kaleidoscope::Plugin { @@ -30,6 +32,11 @@ class ActiveModColorEffect : public kaleidoscope::Plugin { static cRGB sticky_color; EventHandlerResult beforeReportingState(); + EventHandlerResult onLayerChange(); + + private: + static uint8_t mod_keys_[MAX_MODS_PER_LAYER]; + static uint8_t mod_key_count_; }; } }