From 0635518f9dec013c1e8748a0db87d3161b5b0c5f Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 13 May 2018 22:10:43 +0200 Subject: [PATCH] Updated to use the new plugin APIs Signed-off-by: Gergely Nagy --- README.md | 4 +- examples/TapDance/TapDance.ino | 10 +-- src/Kaleidoscope/TapDance.cpp | 111 ++++++++++++++++++++------------- src/Kaleidoscope/TapDance.h | 24 ++++--- 4 files changed, 89 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index 5c981278..80f56b6e 100644 --- a/README.md +++ b/README.md @@ -83,9 +83,9 @@ void tapDanceAction(uint8_t tap_dance_index, byte row, byte col, uint8_t tap_cou } } -void setup() { - Kaleidoscope.use(&TapDance); +KALEIDOSCOPE_INIT_PLUGINS(TapDance); +void setup() { Kaleidoscope.setup (); } ``` diff --git a/examples/TapDance/TapDance.ino b/examples/TapDance/TapDance.ino index a484f240..26babde0 100644 --- a/examples/TapDance/TapDance.ino +++ b/examples/TapDance/TapDance.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TapDance -- Tap-dance keys - * Copyright (C) 2016, 2017 Gergely Nagy + * Copyright (C) 2016, 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,12 +33,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, TD(1)), }; +// *INDENT-ON* static void tapDanceEsc(uint8_t tap_dance_index, uint8_t tap_count, kaleidoscope::TapDance::ActionType tap_dance_action) { tapDanceActionKeys(tap_count, tap_dance_action, Key_Escape, Key_Tab); @@ -52,9 +54,9 @@ void tapDanceAction(uint8_t tap_dance_index, byte row, byte col, uint8_t tap_cou } } -void setup() { - Kaleidoscope.use(&TapDance); +KALEIDOSCOPE_INIT_PLUGINS(TapDance); +void setup() { Kaleidoscope.setup(); } diff --git a/src/Kaleidoscope/TapDance.cpp b/src/Kaleidoscope/TapDance.cpp index 8d55ce6c..a90304c7 100644 --- a/src/Kaleidoscope/TapDance.cpp +++ b/src/Kaleidoscope/TapDance.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TapDance -- Tap-dance keys - * Copyright (C) 2016, 2017 Gergely Nagy + * Copyright (C) 2016, 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 @@ -73,37 +73,26 @@ void TapDance::timeout(void) { release(idx); } -Key TapDance::release(uint8_t tap_dance_index) { +void TapDance::release(uint8_t tap_dance_index) { end_time_ = 0; last_tap_dance_key_.raw = Key_NoKey.raw; bitClear(pressed_state_, tap_dance_index); bitClear(triggered_state_, tap_dance_index); bitWrite(release_next_state_, tap_dance_index, 1); - return Key_NoKey; } -Key TapDance::tap(void) { +void TapDance::tap(void) { uint8_t idx = last_tap_dance_key_.raw - ranges::TD_FIRST; tap_count_[idx]++; end_time_ = millis() + time_out; tapDanceAction(idx, last_tap_dance_row_, last_tap_dance_col_, tap_count_[idx], Tap); - - return Key_NoKey; } // --- api --- -TapDance::TapDance(void) { -} - -void TapDance::begin(void) { - Kaleidoscope.useEventHandlerHook(eventHandlerHook); - Kaleidoscope.useLoopHook(loopHook); -} - void TapDance::actionKeys(uint8_t tap_count, ActionType tap_dance_action, uint8_t max_keys, const Key tap_keys[]) { if (tap_count > max_keys) tap_count = max_keys; @@ -130,55 +119,63 @@ void TapDance::actionKeys(uint8_t tap_count, ActionType tap_dance_action, uint8_ // --- hooks --- -Key TapDance::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { - if (key_state & INJECTED) - return mapped_key; +EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t keyState) { + if (keyState & INJECTED) + return EventHandlerResult::OK; - if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) { - if (isTapDance(mapped_key)) - return Key_NoKey; + if (!keyIsPressed(keyState) && !keyWasPressed(keyState)) { + if (isTapDance(mapped_key)) { + return EventHandlerResult::EVENT_CONSUMED; + } - return mapped_key; + return EventHandlerResult::OK; } if (!isTapDance(mapped_key)) { if (!isActive()) - return mapped_key; + return EventHandlerResult::OK; - if (keyToggledOn(key_state)) + if (keyToggledOn(keyState)) interrupt(row, col); if (KeyboardHardware.isKeyMasked(row, col)) { KeyboardHardware.unMaskKey(row, col); - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } - return mapped_key; + return EventHandlerResult::OK; } uint8_t tap_dance_index = mapped_key.raw - ranges::TD_FIRST; - if (keyToggledOff(key_state)) + if (keyToggledOff(keyState)) bitClear(pressed_state_, tap_dance_index); if (!isInSeq(mapped_key)) { if (!isActive()) { if (bitRead(triggered_state_, tap_dance_index)) { - if (keyToggledOff(key_state)) - return release(tap_dance_index); - return Key_NoKey; + if (keyToggledOff(keyState)) { + release(tap_dance_index); + } + + return EventHandlerResult::EVENT_CONSUMED; } last_tap_dance_key_.raw = mapped_key.raw; last_tap_dance_row_ = row; last_tap_dance_col_ = col; - return tap(); + + tap(); + + return EventHandlerResult::EVENT_CONSUMED; } else { - if (keyToggledOff(key_state) && stillHeld(tap_dance_index)) { - return release(tap_dance_index); + if (keyToggledOff(keyState) && stillHeld(tap_dance_index)) { + release(tap_dance_index); + return EventHandlerResult::EVENT_CONSUMED; } - if (!keyToggledOn(key_state)) - return Key_NoKey; + if (!keyToggledOn(keyState)) { + return EventHandlerResult::EVENT_CONSUMED; + } interrupt(row, col); } @@ -186,28 +183,27 @@ Key TapDance::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_s // in sequence - if (keyToggledOff(key_state)) - return Key_NoKey; + if (keyToggledOff(keyState)) { + return EventHandlerResult::EVENT_CONSUMED; + } last_tap_dance_key_.raw = mapped_key.raw; last_tap_dance_row_ = row; last_tap_dance_col_ = col; bitSet(pressed_state_, tap_dance_index); - if (keyToggledOn(key_state)) - return tap(); + if (keyToggledOn(keyState)) { + tap(); + return EventHandlerResult::EVENT_CONSUMED; + } if (bitRead(triggered_state_, tap_dance_index)) tapDanceAction(tap_dance_index, row, col, tap_count_[tap_dance_index], Hold); - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } -void -TapDance::loopHook(bool is_post_clear) { - if (!is_post_clear) - return; - +EventHandlerResult TapDance::afterEachCycle() { for (uint8_t i = 0; i < 16; i++) { if (!bitRead(release_next_state_, i)) continue; @@ -218,11 +214,36 @@ TapDance::loopHook(bool is_post_clear) { } if (!isActive()) - return; + return EventHandlerResult::OK; if (end_time_ && millis() > end_time_) timeout(); + + return EventHandlerResult::OK; +} + +// Legacy V1 API + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void TapDance::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); + Kaleidoscope.useLoopHook(legacyLoopHook); +} + +Key TapDance::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::TapDance.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; +} + +void TapDance::legacyLoopHook(bool is_post_clear) { + if (!is_post_clear) + return; + + ::TapDance.afterEachCycle(); } +#endif } diff --git a/src/Kaleidoscope/TapDance.h b/src/Kaleidoscope/TapDance.h index 70d76f80..1d7b146c 100644 --- a/src/Kaleidoscope/TapDance.h +++ b/src/Kaleidoscope/TapDance.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-TapDance -- Tap-dance keys - * Copyright (C) 2016, 2017 Gergely Nagy + * Copyright (C) 2016, 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 @@ -30,7 +30,7 @@ }) namespace kaleidoscope { -class TapDance : public KaleidoscopePlugin { +class TapDance : public kaleidoscope::Plugin { public: typedef enum { Tap, @@ -40,13 +40,22 @@ class TapDance : public KaleidoscopePlugin { Release, } ActionType; - TapDance(void); + TapDance(void) {} - void begin(void) final; static uint16_t time_out; void actionKeys(uint8_t tap_count, ActionType tap_dance_action, uint8_t max_keys, const Key tap_keys[]); + EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t keyState); + EventHandlerResult afterEachCycle(); + +#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: static uint32_t end_time_; static uint8_t tap_count_[16]; @@ -57,13 +66,10 @@ class TapDance : public KaleidoscopePlugin { static byte last_tap_dance_row_; static byte last_tap_dance_col_; - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); - static void loopHook(bool is_post_clear); - - static Key tap(void); + static void tap(void); static void interrupt(byte row, byte col); static void timeout(void); - static Key release(uint8_t tap_dance_index); + static void release(uint8_t tap_dance_index); }; }