diff --git a/README.md b/README.md index 8b660d72..4052509d 100644 --- a/README.md +++ b/README.md @@ -73,9 +73,12 @@ const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { return MACRO_NONE; } -void setup() { - Kaleidoscope.use(&EEPROMKeymapProgrammer, &EEPROMKeymap, &Macros); +KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings, + EEPROMKeymapProgrammer, + EEPROMKeymap, + Macros); +void setup() { Kaleidoscope.setup(); Layer.getKey = EEPROMKeymap.getKey; diff --git a/examples/EEPROM-Keymap-Programmer/EEPROM-Keymap-Programmer.ino b/examples/EEPROM-Keymap-Programmer/EEPROM-Keymap-Programmer.ino index 0f7a937a..c0a77771 100644 --- a/examples/EEPROM-Keymap-Programmer/EEPROM-Keymap-Programmer.ino +++ b/examples/EEPROM-Keymap-Programmer/EEPROM-Keymap-Programmer.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-EEPROM-Keymap-Programmer -- On-the-fly reprogrammable keymap. - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,11 +17,12 @@ */ #include +#include #include #include #include - +// *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED (M(0), Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey, @@ -34,12 +35,13 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, - Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, + Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_NoKey), }; +// *INDENT-ON* const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { if (macroIndex == 0 && keyToggledOff(keyState)) { @@ -49,11 +51,14 @@ const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { return MACRO_NONE; } +KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings, + EEPROMKeymapProgrammer, + EEPROMKeymap, + Macros); + void setup() { Serial.begin(9600); - Kaleidoscope.use(&EEPROMKeymapProgrammer, &EEPROMKeymap, &Macros); - Kaleidoscope.setup(); Layer.getKey = EEPROMKeymap.getKey; diff --git a/src/Kaleidoscope/EEPROM-Keymap-Programmer.cpp b/src/Kaleidoscope/EEPROM-Keymap-Programmer.cpp index 824e4936..f033e82a 100644 --- a/src/Kaleidoscope/EEPROM-Keymap-Programmer.cpp +++ b/src/Kaleidoscope/EEPROM-Keymap-Programmer.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-EEPROM-Keymap-Programmer -- On-the-fly reprogrammable keymap. - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,13 +24,6 @@ EEPROMKeymapProgrammer::state_t EEPROMKeymapProgrammer::state_; EEPROMKeymapProgrammer::mode_t EEPROMKeymapProgrammer::mode; Key EEPROMKeymapProgrammer::new_key_; -EEPROMKeymapProgrammer::EEPROMKeymapProgrammer(void) { -} - -void EEPROMKeymapProgrammer::begin(void) { - Kaleidoscope.useEventHandlerHook(eventHandlerHook); -} - void EEPROMKeymapProgrammer::nextState(void) { switch (state_) { case INACTIVE: @@ -56,9 +49,9 @@ void EEPROMKeymapProgrammer::cancel(void) { state_ = INACTIVE; } -Key EEPROMKeymapProgrammer::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { +EventHandlerResult EEPROMKeymapProgrammer::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) { if (state_ == INACTIVE) - return mapped_key; + return EventHandlerResult::OK; if (state_ == WAIT_FOR_KEY) { if (keyToggledOn(key_state)) { @@ -68,7 +61,7 @@ Key EEPROMKeymapProgrammer::eventHandlerHook(Key mapped_key, byte row, byte col, if ((uint16_t)(Layer.top() * ROWS * COLS + row * COLS + col) == update_position_) nextState(); } - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } if (state_ == WAIT_FOR_SOURCE_KEY) { @@ -79,16 +72,17 @@ Key EEPROMKeymapProgrammer::eventHandlerHook(Key mapped_key, byte row, byte col, if (new_key_ == Layer.getKeyFromPROGMEM(Layer.top(), row, col)) nextState(); } - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } // WAIT_FOR_CODE state if (mapped_key < Key_1 || mapped_key > Key_0) - return mapped_key; + return EventHandlerResult::OK; - if (!keyToggledOn(key_state)) - return Key_NoKey; + if (!keyToggledOn(key_state)) { + return EventHandlerResult::EVENT_CONSUMED; + } uint8_t n; if (mapped_key.keyCode == Key_0.keyCode) @@ -98,7 +92,7 @@ Key EEPROMKeymapProgrammer::eventHandlerHook(Key mapped_key, byte row, byte col, new_key_.raw = new_key_.raw * 10 + n; - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } bool EEPROMKeymapProgrammer::focusHook(const char *command) { @@ -113,6 +107,20 @@ bool EEPROMKeymapProgrammer::focusHook(const char *command) { return true; } +// Legacy API +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void EEPROMKeymapProgrammer::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); +} + +Key EEPROMKeymapProgrammer::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::EEPROMKeymapProgrammer.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; +} +#endif + } kaleidoscope::EEPROMKeymapProgrammer EEPROMKeymapProgrammer; diff --git a/src/Kaleidoscope/EEPROM-Keymap-Programmer.h b/src/Kaleidoscope/EEPROM-Keymap-Programmer.h index d19d41b5..d87f6762 100644 --- a/src/Kaleidoscope/EEPROM-Keymap-Programmer.h +++ b/src/Kaleidoscope/EEPROM-Keymap-Programmer.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-EEPROM-Keymap-Programmer -- On-the-fly reprogrammable keymap. - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ #include namespace kaleidoscope { -class EEPROMKeymapProgrammer : public KaleidoscopePlugin { +class EEPROMKeymapProgrammer : public kaleidoscope::Plugin { public: typedef enum { CODE, @@ -30,9 +30,7 @@ class EEPROMKeymapProgrammer : public KaleidoscopePlugin { } mode_t; static mode_t mode; - EEPROMKeymapProgrammer(void); - - void begin(void) final; + EEPROMKeymapProgrammer(void) {} static void activate(void) { nextState(); @@ -42,6 +40,14 @@ class EEPROMKeymapProgrammer : public KaleidoscopePlugin { static bool focusHook(const char *command); + EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state); + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API + protected: + void begin(); + static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state); +#endif + private: typedef enum { INACTIVE, @@ -53,8 +59,6 @@ class EEPROMKeymapProgrammer : public KaleidoscopePlugin { static uint16_t update_position_; // layer, row, col static Key new_key_; - - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); }; }