From fddbd75ff89f33a806188340710ca23381458e58 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 25 Nov 2019 15:56:45 +0100 Subject: [PATCH 1/2] New event handler: onLEDModeChange We'd like to be able to run custom code whenever the led mode changes, reliably, without having to resort to checking the mode every cycle. For this purpose, we introduce the `onLEDModeChange()` handler plugins can hook into. It will be called every time `LEDControl.set_mode()` is called, even if that just sets the mode to the currently active one. Signed-off-by: Gergely Nagy --- src/kaleidoscope/event_handlers.h | 12 ++++++++++++ src/kaleidoscope/hooks.h | 5 +++++ src/kaleidoscope/plugin/LEDControl.cpp | 2 ++ 3 files changed, 19 insertions(+) diff --git a/src/kaleidoscope/event_handlers.h b/src/kaleidoscope/event_handlers.h index 86c21db9..4bd8b38c 100644 --- a/src/kaleidoscope/event_handlers.h +++ b/src/kaleidoscope/event_handlers.h @@ -161,6 +161,14 @@ _CURRENT_IMPLEMENTATION, __NL__ \ _NOT_ABORTABLE, __NL__ \ (), (), ##__VA_ARGS__) __NL__ \ + /* Called when the LED mode changes. If one needs to know what */ __NL__ \ + /* from and what to the mode changed, they should track that */ __NL__ \ + /* themselves. */ __NL__ \ + OPERATION(onLEDModeChange, __NL__ \ + 1, __NL__ \ + _CURRENT_IMPLEMENTATION, __NL__ \ + _NOT_ABORTABLE, __NL__ \ + (), (), ##__VA_ARGS__) __NL__ \ /* Called before reporting our state to the host. This is the */ __NL__ \ /* last point in a cycle where a plugin can alter what gets */ __NL__ \ /* reported to the host. */ __NL__ \ @@ -219,6 +227,10 @@ OP(onLayerChange, 1) __NL__ \ END(onLayerChange, 1) __NL__ \ __NL__ \ + START(onLEDModeChange, 1) __NL__ \ + OP(onLEDModeChange, 1) __NL__ \ + END(onLEDModeChange, 1) __NL__ \ + __NL__ \ START(beforeReportingState, 1) __NL__ \ OP(beforeReportingState, 1) __NL__ \ END(beforeReportingState, 1) __NL__ \ diff --git a/src/kaleidoscope/hooks.h b/src/kaleidoscope/hooks.h index 4e154f49..abc6d5f1 100644 --- a/src/kaleidoscope/hooks.h +++ b/src/kaleidoscope/hooks.h @@ -33,6 +33,10 @@ extern void handleKeyswitchEvent(kaleidoscope::Key mappedKey, KeyAddr key_addr, DEPRECATED(ROW_COL_FUNC) extern void handleKeyswitchEvent(kaleidoscope::Key mappedKey, byte row, byte col, uint8_t keyState); namespace kaleidoscope { +namespace plugin { +// Forward declaration to enable friend declarations. +class LEDControl; +} // Forward declaration to enable friend declarations. class Layer_; @@ -55,6 +59,7 @@ class Hooks { // and Hooks::afterEachCycle. friend class Kaleidoscope_; friend class ::kaleidoscope::Layer_; + friend class ::kaleidoscope::plugin::LEDControl; // ::handleKeyswitchEvent(...) calls Hooks::onKeyswitchEvent. friend void ::handleKeyswitchEvent(kaleidoscope::Key mappedKey, diff --git a/src/kaleidoscope/plugin/LEDControl.cpp b/src/kaleidoscope/plugin/LEDControl.cpp index 9dc3b931..a5390072 100644 --- a/src/kaleidoscope/plugin/LEDControl.cpp +++ b/src/kaleidoscope/plugin/LEDControl.cpp @@ -68,6 +68,8 @@ LEDControl::set_mode(uint8_t mode_) { cur_led_mode_ = LEDModeManager::getLEDMode(mode_id); refreshAll(); + + kaleidoscope::Hooks::onLEDModeChange(); } void LEDControl::activate(LEDModeInterface *plugin) { From 9724e2eceaa620eb984a171f4c5c717365e54489 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 25 Nov 2019 16:02:17 +0100 Subject: [PATCH 2/2] plugin/PersistnetLEDMode: Use the new `onLEDModeChange()` hook Signed-off-by: Gergely Nagy --- src/kaleidoscope/plugin/PersistentLEDMode.cpp | 6 +++--- src/kaleidoscope/plugin/PersistentLEDMode.h | 7 +------ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/kaleidoscope/plugin/PersistentLEDMode.cpp b/src/kaleidoscope/plugin/PersistentLEDMode.cpp index 6480143b..312dbd85 100644 --- a/src/kaleidoscope/plugin/PersistentLEDMode.cpp +++ b/src/kaleidoscope/plugin/PersistentLEDMode.cpp @@ -33,8 +33,8 @@ EventHandlerResult PersistentLEDMode::onSetup() { Kaleidoscope.storage().get(settings_base_, cached_mode_index_); // If the index is max, assume an uninitialized EEPROM, and don't set the LED - // mode. We don't change the cached index here, `afterEachCycle()` will do - // that at the end of he cycle anyway. + // mode. We don't change the cached index here, `onLEDModeChange()` will do + // that whenever a led mode change happens. if (cached_mode_index_ != 0xff) return EventHandlerResult::OK; @@ -43,7 +43,7 @@ EventHandlerResult PersistentLEDMode::onSetup() { return EventHandlerResult::OK; } -EventHandlerResult PersistentLEDMode::afterEachCycle() { +EventHandlerResult PersistentLEDMode::onLEDModeChange() { if (cached_mode_index_ == ::LEDControl.get_mode_index()) return EventHandlerResult::OK; diff --git a/src/kaleidoscope/plugin/PersistentLEDMode.h b/src/kaleidoscope/plugin/PersistentLEDMode.h index 511c8939..07bc5c31 100644 --- a/src/kaleidoscope/plugin/PersistentLEDMode.h +++ b/src/kaleidoscope/plugin/PersistentLEDMode.h @@ -16,11 +16,6 @@ * this program. If not, see . */ -/* NOTE: This plugin is a workaround. It allows us to (optionally) save the LED - * mode to storage, and restore it on next boot, without having a way to hook - * into led mode change events. Once we can hook into that, this plugin shall be - * reworked to use it instead of keeping `afterEachCycle()` busy. */ - #pragma once #include @@ -33,7 +28,7 @@ class PersistentLEDMode: public kaleidoscope::Plugin { PersistentLEDMode() {} EventHandlerResult onSetup(); - EventHandlerResult afterEachCycle(); + EventHandlerResult onLEDModeChange(); private: static uint16_t settings_base_; static uint8_t cached_mode_index_;