From 4b061994fc21d0306c6ddd7b88f77a903b756f6d Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 13 May 2018 10:09:22 +0200 Subject: [PATCH] Updated to use the new plugin APIs Signed-off-by: Gergely Nagy --- README.md | 4 ++-- src/Kaleidoscope-Macros.cpp | 44 +++++++++++++++++++++++++------------ src/Kaleidoscope-Macros.h | 19 +++++++++++----- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3bc1bbbf..c454766a 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,9 @@ const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { return MACRO_NONE; } +KALEIDOSCOPE_INIT_PLUGINS(Macros); + void setup() { - Kaleidoscope.use(&Macros); - Kaleidoscope.setup (); } ``` diff --git a/src/Kaleidoscope-Macros.cpp b/src/Kaleidoscope-Macros.cpp index 276b952e..55a2465d 100644 --- a/src/Kaleidoscope-Macros.cpp +++ b/src/Kaleidoscope-Macros.cpp @@ -183,21 +183,23 @@ const macro_t *Macros_::type(const char *string) { return MACRO_NONE; } -Key Macros_::handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState) { +kaleidoscope::EventHandlerResult Macros_::onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState) { if (mappedKey.flags != (SYNTHETIC | IS_MACRO)) - return mappedKey; + return kaleidoscope::EventHandlerResult::OK; byte key_id = (row * COLS) + col; addActiveMacroKey(mappedKey.keyCode, key_id, keyState); - return Key_NoKey; + + return kaleidoscope::EventHandlerResult::EVENT_CONSUMED; } -void Macros_::loopHook(bool post_clear) { - if (post_clear) { - active_macro_count = 0; - return; - } +kaleidoscope::EventHandlerResult Macros_::afterEachCycle() { + active_macro_count = 0; + + return kaleidoscope::EventHandlerResult::OK; +} +kaleidoscope::EventHandlerResult Macros_::beforeReportingState() { for (byte i = 0; i < active_macro_count; ++i) { if (active_macros[i].key_id == 0xFF) { // i.e. UNKNOWN_KEYSWITCH_LOCATION @@ -211,16 +213,30 @@ void Macros_::loopHook(bool post_clear) { active_macros[i].key_state); Macros.play(m); } + return kaleidoscope::EventHandlerResult::OK; } -Macros_::Macros_(void) { +// Legacy V1 API +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void Macros_::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); + Kaleidoscope.useLoopHook(legacyLoopHook); } -void -Macros_::begin(void) { - active_macro_count = 0; - Kaleidoscope.useEventHandlerHook(handleMacroEvent); - Kaleidoscope.useLoopHook(loopHook); +Key Macros_::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + kaleidoscope::EventHandlerResult r = Macros.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == kaleidoscope::EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; +} + +void Macros_::legacyLoopHook(bool is_post_clear) { + if (is_post_clear) { + Macros.afterEachCycle(); + } else { + Macros.beforeReportingState(); + } } +#endif Macros_ Macros; diff --git a/src/Kaleidoscope-Macros.h b/src/Kaleidoscope-Macros.h index 5c6d2658..d3c240e1 100644 --- a/src/Kaleidoscope-Macros.h +++ b/src/Kaleidoscope-Macros.h @@ -17,11 +17,9 @@ struct MacroKeyEvent { byte key_state; }; -class Macros_ : public KaleidoscopePlugin { +class Macros_ : public kaleidoscope::Plugin { public: - Macros_(void); - - void begin(void) final; + Macros_(void) {} static MacroKeyEvent active_macros[MAX_CONCURRENT_MACROS]; static byte active_macro_count; @@ -35,8 +33,10 @@ class Macros_ : public KaleidoscopePlugin { active_macros[active_macro_count].key_state = key_state; ++active_macro_count; } - static Key handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState); - static void loopHook(bool post_clear); + + kaleidoscope::EventHandlerResult onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState); + kaleidoscope::EventHandlerResult beforeReportingState(); + kaleidoscope::EventHandlerResult afterEachCycle(); void play(const macro_t *macro_p); @@ -57,6 +57,13 @@ class Macros_ : public KaleidoscopePlugin { static byte row, col; +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API + protected: + void begin(); + static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state); + static void legacyLoopHook(bool is_post_clear); +#endif + private: Key lookupAsciiCode(uint8_t ascii_code); };