diff --git a/README.md b/README.md index 500013ef..d23f3549 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,10 @@ void systerAction(kaleidoscope::Syster::action_t action, const char *symbol) { } } +KALEIDOSCOPE_INIT_PLUGINS(Unicode, Syster); + void setup() { Serial.begin(9600); - - Kaleidoscope.use(&Unicode, &Syster); - Kaleidoscope.setup (); } ``` diff --git a/examples/Syster/Syster.ino b/examples/Syster/Syster.ino index 76d13296..ea823f2a 100644 --- a/examples/Syster/Syster.ino +++ b/examples/Syster/Syster.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Syster -- Symbolic input system - * 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 @@ -23,6 +23,7 @@ #include #include +// *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED ( @@ -36,12 +37,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, SYSTER), }; +// *INDENT-ON* void systerAction(kaleidoscope::Syster::action_t action, const char *symbol) { switch (action) { @@ -64,9 +66,11 @@ void systerAction(kaleidoscope::Syster::action_t action, const char *symbol) { } } -void setup() { - Kaleidoscope.use(&Unicode, &Syster); +KALEIDOSCOPE_INIT_PLUGINS(HostOS, + Unicode, + Syster); +void setup() { Kaleidoscope.setup(); } diff --git a/src/Kaleidoscope/Syster.cpp b/src/Kaleidoscope/Syster.cpp index 396607c6..08818ef6 100644 --- a/src/Kaleidoscope/Syster.cpp +++ b/src/Kaleidoscope/Syster.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Syster -- Symbolic input system - * 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 @@ -32,14 +32,6 @@ bool Syster::is_active_; #define isSyster(k) (k == kaleidoscope::ranges::SYSTER) // --- api --- - -Syster::Syster(void) { -} - -void Syster::begin(void) { - Kaleidoscope.useEventHandlerHook(eventHandlerHook); -} - void Syster::reset(void) { symbol_pos_ = 0; symbol_[0] = 0; @@ -51,28 +43,30 @@ bool Syster::is_active(void) { } // --- hooks --- -Key Syster::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { +EventHandlerResult Syster::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t keyState) { if (!is_active_) { if (!isSyster(mapped_key)) - return mapped_key; + return EventHandlerResult::OK; - if (keyToggledOn(key_state)) { + if (keyToggledOn(keyState)) { is_active_ = true; systerAction(StartAction, NULL); } - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } - if (key_state & INJECTED) - return mapped_key; + if (keyState & INJECTED) + return EventHandlerResult::OK; - if (isSyster(mapped_key)) - return Key_NoKey; + if (isSyster(mapped_key)) { + return EventHandlerResult::EVENT_CONSUMED; + } - if (mapped_key == Key_Backspace && symbol_pos_ == 0) - return Key_NoKey; + if (mapped_key == Key_Backspace && symbol_pos_ == 0) { + return EventHandlerResult::EVENT_CONSUMED; + } - if (keyToggledOff(key_state)) { + if (keyToggledOff(keyState)) { if (mapped_key == Key_Spacebar) { for (uint8_t i = 0; i <= symbol_pos_; i++) { handleKeyswitchEvent(Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED); @@ -87,11 +81,11 @@ Key Syster::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_sta systerAction(SymbolAction, symbol_); reset(); - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } } - if (keyToggledOn(key_state)) { + if (keyToggledOn(keyState)) { if (mapped_key == Key_Backspace) { if (symbol_pos_ > 0) symbol_pos_--; @@ -102,8 +96,23 @@ Key Syster::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_sta } } - return mapped_key; + return EventHandlerResult::OK; +} + +// Legacy V1 API + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void Syster::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); +} + +Key Syster::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::Syster.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; } +#endif } diff --git a/src/Kaleidoscope/Syster.h b/src/Kaleidoscope/Syster.h index 83605be9..b9da0303 100644 --- a/src/Kaleidoscope/Syster.h +++ b/src/Kaleidoscope/Syster.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Syster -- Symbolic input system - * 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 @@ -27,7 +27,7 @@ namespace kaleidoscope { -class Syster : public KaleidoscopePlugin { +class Syster : public kaleidoscope::Plugin { public: typedef enum { StartAction, @@ -35,19 +35,24 @@ class Syster : public KaleidoscopePlugin { SymbolAction } action_t; - Syster(void); + Syster(void) {} - void begin(void) final; static void reset(void); bool is_active(void); + EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t keyState); + +#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: static char symbol_[SYSTER_MAX_SYMBOL_LENGTH + 1]; static uint8_t symbol_pos_; static bool is_active_; - - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); }; };