diff --git a/README.md b/README.md index 2556a5a7..741879c4 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,9 @@ the box, without any further configuration: #include #include -void setup (void) { - Kaleidoscope.use (&TypingBreaks); +KALEIDOSCOPE_INIT_PLUGINS(TypingBreaks); +void setup (void) { Kaleidoscope.setup (); TypingBreaks.settings.idle_time_limit = 60; diff --git a/examples/TypingBreaks/TypingBreaks.ino b/examples/TypingBreaks/TypingBreaks.ino index 47ad8165..fe4c324d 100644 --- a/examples/TypingBreaks/TypingBreaks.ino +++ b/examples/TypingBreaks/TypingBreaks.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TypingBreaks -- Enforced typing breaks - * 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 @@ -19,6 +19,7 @@ #include #include +// *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED ( @@ -32,16 +33,17 @@ 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_skip), }; +// *INDENT-ON* -void setup() { - Kaleidoscope.use(&TypingBreaks); +KALEIDOSCOPE_INIT_PLUGINS(TypingBreaks); +void setup() { Kaleidoscope.setup(); TypingBreaks.settings.idle_time_limit = 60; diff --git a/src/Kaleidoscope/TypingBreaks.cpp b/src/Kaleidoscope/TypingBreaks.cpp index 898c0113..4f788951 100644 --- a/src/Kaleidoscope/TypingBreaks.cpp +++ b/src/Kaleidoscope/TypingBreaks.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TypingBreaks -- Enforced typing breaks - * 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 @@ -37,27 +37,22 @@ uint16_t TypingBreaks::left_hand_keys_; uint16_t TypingBreaks::right_hand_keys_; uint16_t TypingBreaks::settings_base_; -TypingBreaks::TypingBreaks(void) { -} - -void TypingBreaks::begin(void) { - Kaleidoscope.useEventHandlerHook(this->eventHandlerHook); -} - -Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { +EventHandlerResult TypingBreaks::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) { uint32_t lock_length = settings.lock_length * 1000; uint32_t idle_time_limit = settings.idle_time_limit * 1000; uint32_t lock_time_out = settings.lock_time_out * 1000; // If we are locked, and didn't time out yet, no key has to be pressed. - if (lock_start_time_ && (millis() - lock_start_time_ <= lock_length)) - return Key_NoKey; + if (lock_start_time_ && (millis() - lock_start_time_ <= lock_length)) { + return EventHandlerResult::EVENT_CONSUMED; + } // If we are locked... if (lock_start_time_) { // ...and the lock has not expired yet - if (millis() - lock_start_time_ <= lock_length) - return Key_NoKey; // remain locked + if (millis() - lock_start_time_ <= lock_length) { + return EventHandlerResult::EVENT_CONSUMED; // remain locked + } // ...otherwise clear the lock lock_start_time_ = 0; @@ -81,14 +76,14 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k if (settings.left_hand_max_keys && left_hand_keys_ >= settings.left_hand_max_keys) { lock_start_time_ = millis(); TypingBreak(true); - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } // If we have a limit on the right hand, and we reached it, lock up! if (settings.right_hand_max_keys && right_hand_keys_ >= settings.right_hand_max_keys) { lock_start_time_ = millis(); TypingBreak(true); - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } if (lock_time_out) { @@ -97,7 +92,7 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k // Yeah, it is. lock_start_time_ = last_key_time_; TypingBreak(true); - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } } @@ -112,7 +107,7 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k last_key_time_ = millis(); } - return mapped_key; + return EventHandlerResult::OK; } void TypingBreaks::enableEEPROM(void) { @@ -194,6 +189,22 @@ bool TypingBreaks::focusHook(const char *command) { EEPROM.put(settings_base_, settings); return true; } + +// Legacy V1 API + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void TypingBreaks::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); +} + +Key TypingBreaks::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::TypingBreaks.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; +} +#endif + } kaleidoscope::TypingBreaks TypingBreaks; diff --git a/src/Kaleidoscope/TypingBreaks.h b/src/Kaleidoscope/TypingBreaks.h index 0f5ea278..d07ccb42 100644 --- a/src/Kaleidoscope/TypingBreaks.h +++ b/src/Kaleidoscope/TypingBreaks.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TypingBreaks -- Enforced typing breaks - * 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,11 +22,9 @@ namespace kaleidoscope { -class TypingBreaks : public KaleidoscopePlugin { +class TypingBreaks : public kaleidoscope::Plugin { public: - TypingBreaks(void); - - void begin(void) final; + TypingBreaks(void) {} static void enableEEPROM(void); static bool focusHook(const char *command); @@ -41,6 +39,14 @@ class TypingBreaks : public KaleidoscopePlugin { static settings_t settings; + 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: static uint32_t session_start_time_; static uint32_t lock_start_time_; @@ -49,8 +55,6 @@ class TypingBreaks : public KaleidoscopePlugin { static uint16_t right_hand_keys_; static uint16_t settings_base_; - - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); }; }