diff --git a/examples/TapDance/TapDance.ino b/examples/TapDance/TapDance.ino index 1c25a477..1835a3c6 100644 --- a/examples/TapDance/TapDance.ino +++ b/examples/TapDance/TapDance.ino @@ -20,44 +20,44 @@ #include const Key keymaps[][ROWS][COLS] PROGMEM = { - [0] = KEYMAP_STACKED - ( - Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey, - Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, - Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, - Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, - - Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, - TD(0), - - 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_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, - - Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, - TD(1) - ), + [0] = KEYMAP_STACKED + ( + Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey, + Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, + Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, + Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, + + Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, + TD(0), + + 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_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, + + Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, + TD(1) + ), }; -static void tapDanceEsc (uint8_t tapDanceIndex, uint8_t tapCount, KaleidoscopePlugins::TapDance::ActionType tapDanceAction) { - tapDanceActionKeys (tapCount, tapDanceAction, Key_Escape, Key_Tab); +static void tapDanceEsc(uint8_t tapDanceIndex, uint8_t tapCount, KaleidoscopePlugins::TapDance::ActionType tapDanceAction) { + tapDanceActionKeys(tapCount, tapDanceAction, Key_Escape, Key_Tab); } -void tapDanceAction (uint8_t tapDanceIndex, byte row, byte col, uint8_t tapCount, KaleidoscopePlugins::TapDance::ActionType tapDanceAction) { - switch (tapDanceIndex) { - case 0: - return tapDanceActionKeys (tapCount, tapDanceAction, Key_Tab, Key_Escape); - case 1: - return tapDanceEsc (tapDanceIndex, tapCount, tapDanceAction); - } +void tapDanceAction(uint8_t tapDanceIndex, byte row, byte col, uint8_t tapCount, KaleidoscopePlugins::TapDance::ActionType tapDanceAction) { + switch (tapDanceIndex) { + case 0: + return tapDanceActionKeys(tapCount, tapDanceAction, Key_Tab, Key_Escape); + case 1: + return tapDanceEsc(tapDanceIndex, tapCount, tapDanceAction); + } } -void setup () { - Kaleidoscope.setup (KEYMAP_SIZE); - Kaleidoscope.use (&TapDance, NULL); +void setup() { + Kaleidoscope.setup(KEYMAP_SIZE); + Kaleidoscope.use(&TapDance, NULL); } -void loop () { - Kaleidoscope.loop (); +void loop() { + Kaleidoscope.loop(); } diff --git a/src/Kaleidoscope/TapDance.cpp b/src/Kaleidoscope/TapDance.cpp index 0386ba72..d0329a84 100644 --- a/src/Kaleidoscope/TapDance.cpp +++ b/src/Kaleidoscope/TapDance.cpp @@ -40,191 +40,191 @@ byte TapDance::lastTapDanceCol; // --- actions --- void -TapDance::interrupt (void) { - uint8_t idx = lastTapDanceKey.raw - Ranges::TD_FIRST; +TapDance::interrupt(void) { + uint8_t idx = lastTapDanceKey.raw - Ranges::TD_FIRST; - tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Interrupt); - bitWrite (triggeredState, idx, 1); + tapDanceAction(idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Interrupt); + bitWrite(triggeredState, idx, 1); - endTime = 0; + endTime = 0; - if (bitRead (pressedState, idx)) - return; + if (bitRead(pressedState, idx)) + return; - release (idx); + release(idx); } void -TapDance::timeout (void) { - uint8_t idx = lastTapDanceKey.raw - Ranges::TD_FIRST; +TapDance::timeout(void) { + uint8_t idx = lastTapDanceKey.raw - Ranges::TD_FIRST; - tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Timeout); - bitWrite (triggeredState, idx, 1); + tapDanceAction(idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Timeout); + bitWrite(triggeredState, idx, 1); - if (bitRead (pressedState, idx)) - return; + if (bitRead(pressedState, idx)) + return; - lastTapDanceKey.raw = Key_NoKey.raw; + lastTapDanceKey.raw = Key_NoKey.raw; - release (idx); + release(idx); } Key -TapDance::release (uint8_t tapDanceIndex) { - endTime = 0; - lastTapDanceKey.raw = Key_NoKey.raw; - - bitClear (pressedState, tapDanceIndex); - bitClear (triggeredState, tapDanceIndex); - bitWrite (releaseNextState, tapDanceIndex, 1); - return Key_NoKey; +TapDance::release(uint8_t tapDanceIndex) { + endTime = 0; + lastTapDanceKey.raw = Key_NoKey.raw; + + bitClear(pressedState, tapDanceIndex); + bitClear(triggeredState, tapDanceIndex); + bitWrite(releaseNextState, tapDanceIndex, 1); + return Key_NoKey; } Key -TapDance::tap (void) { - uint8_t idx = lastTapDanceKey.raw - Ranges::TD_FIRST; +TapDance::tap(void) { + uint8_t idx = lastTapDanceKey.raw - Ranges::TD_FIRST; - tapCount[idx]++; - endTime = millis () + timeOut; + tapCount[idx]++; + endTime = millis() + timeOut; - tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Tap); + tapDanceAction(idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Tap); - return Key_NoKey; + return Key_NoKey; } // --- api --- -TapDance::TapDance (void) { - lastTapDanceKey.raw = Key_NoKey.raw; +TapDance::TapDance(void) { + lastTapDanceKey.raw = Key_NoKey.raw; } void -TapDance::begin (void) { - event_handler_hook_use (this->eventHandlerHook); - loop_hook_use (this->loopHook); +TapDance::begin(void) { + event_handler_hook_use(this->eventHandlerHook); + loop_hook_use(this->loopHook); } void -TapDance::actionKeys (uint8_t tapCount, ActionType tapDanceAction, uint8_t maxKeys, const Key tapKeys[]) { - if (tapCount > maxKeys) - tapCount = maxKeys; - - Key key; - key.raw = pgm_read_word (&(tapKeys[tapCount - 1].raw)); - - switch (tapDanceAction) { - case Tap: - break; - case Interrupt: - case Timeout: - handle_keyswitch_event (key, lastTapDanceRow, lastTapDanceCol, IS_PRESSED | INJECTED); - break; - case Hold: - handle_keyswitch_event (key, lastTapDanceRow, lastTapDanceCol, IS_PRESSED | WAS_PRESSED | INJECTED); - break; - case Release: - Keyboard.sendReport (); - handle_keyswitch_event (key, lastTapDanceRow, lastTapDanceCol, WAS_PRESSED | INJECTED); - break; - } +TapDance::actionKeys(uint8_t tapCount, ActionType tapDanceAction, uint8_t maxKeys, const Key tapKeys[]) { + if (tapCount > maxKeys) + tapCount = maxKeys; + + Key key; + key.raw = pgm_read_word(&(tapKeys[tapCount - 1].raw)); + + switch (tapDanceAction) { + case Tap: + break; + case Interrupt: + case Timeout: + handle_keyswitch_event(key, lastTapDanceRow, lastTapDanceCol, IS_PRESSED | INJECTED); + break; + case Hold: + handle_keyswitch_event(key, lastTapDanceRow, lastTapDanceCol, IS_PRESSED | WAS_PRESSED | INJECTED); + break; + case Release: + Keyboard.sendReport(); + handle_keyswitch_event(key, lastTapDanceRow, lastTapDanceCol, WAS_PRESSED | INJECTED); + break; + } } // --- hooks --- Key -TapDance::eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState) { - if (keyState & INJECTED) - return mappedKey; - - if (!key_is_pressed (keyState) && !key_was_pressed (keyState)) { - if (isTapDance (mappedKey)) - return Key_NoKey; - return mappedKey; - } +TapDance::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) { + if (keyState & INJECTED) + return mappedKey; - if (!isTapDance (mappedKey)) { - if (!isActive ()) - return mappedKey; + if (!key_is_pressed(keyState) && !key_was_pressed(keyState)) { + if (isTapDance(mappedKey)) + return Key_NoKey; + return mappedKey; + } - if (key_toggled_on (keyState)) - interrupt (); + if (!isTapDance(mappedKey)) { + if (!isActive()) + return mappedKey; - return mappedKey; - } + if (key_toggled_on(keyState)) + interrupt(); - uint8_t tapDanceIndex = mappedKey.raw - Ranges::TD_FIRST; - - if (key_toggled_off (keyState)) - bitClear (pressedState, tapDanceIndex); - - if (!isInSeq (mappedKey)) { - if (!isActive ()) { - if (bitRead (triggeredState, tapDanceIndex)) { - if (key_toggled_off (keyState)) - return release (tapDanceIndex); - return Key_NoKey; - } - - lastTapDanceKey.raw = mappedKey.raw; - lastTapDanceRow = row; - lastTapDanceCol = col; - return tap (); - } else { - if (key_toggled_off (keyState) && stillHeld (tapDanceIndex)) { - return release (tapDanceIndex); - } - - if (!key_toggled_on (keyState)) - return Key_NoKey; - - interrupt (); - } - } + return mappedKey; + } - // in sequence + uint8_t tapDanceIndex = mappedKey.raw - Ranges::TD_FIRST; - if (key_toggled_off (keyState)) - return Key_NoKey; + if (key_toggled_off(keyState)) + bitClear(pressedState, tapDanceIndex); - lastTapDanceKey.raw = mappedKey.raw; - lastTapDanceRow = row; - lastTapDanceCol = col; - bitSet (pressedState, tapDanceIndex); + if (!isInSeq(mappedKey)) { + if (!isActive()) { + if (bitRead(triggeredState, tapDanceIndex)) { + if (key_toggled_off(keyState)) + return release(tapDanceIndex); + return Key_NoKey; + } + + lastTapDanceKey.raw = mappedKey.raw; + lastTapDanceRow = row; + lastTapDanceCol = col; + return tap(); + } else { + if (key_toggled_off(keyState) && stillHeld(tapDanceIndex)) { + return release(tapDanceIndex); + } + + if (!key_toggled_on(keyState)) + return Key_NoKey; - if (key_toggled_on (keyState)) - return tap (); + interrupt(); + } + } - if (bitRead (triggeredState, tapDanceIndex)) - tapDanceAction (tapDanceIndex, row, col, tapCount[tapDanceIndex], Hold); + // in sequence + if (key_toggled_off(keyState)) return Key_NoKey; + + lastTapDanceKey.raw = mappedKey.raw; + lastTapDanceRow = row; + lastTapDanceCol = col; + bitSet(pressedState, tapDanceIndex); + + if (key_toggled_on(keyState)) + return tap(); + + if (bitRead(triggeredState, tapDanceIndex)) + tapDanceAction(tapDanceIndex, row, col, tapCount[tapDanceIndex], Hold); + + return Key_NoKey; } void -TapDance::loopHook (bool postClear) { - if (!postClear) - return; +TapDance::loopHook(bool postClear) { + if (!postClear) + return; - for (uint8_t i = 0; i < 16; i++) { - if (!bitRead (releaseNextState, i)) - continue; + for (uint8_t i = 0; i < 16; i++) { + if (!bitRead(releaseNextState, i)) + continue; - tapDanceAction (i, lastTapDanceRow, lastTapDanceCol, tapCount[i], Release); - tapCount[i] = 0; - bitClear (releaseNextState, i); - } + tapDanceAction(i, lastTapDanceRow, lastTapDanceCol, tapCount[i], Release); + tapCount[i] = 0; + bitClear(releaseNextState, i); + } - if (!isActive ()) - return; + if (!isActive()) + return; - if (endTime && millis () > endTime) - timeout(); + if (endTime && millis() > endTime) + timeout(); } } // namespace KaleidoscopePlugins __attribute__((weak)) void -tapDanceAction (uint8_t tapDanceIndex, byte row, byte col, uint8_t tapCount, KaleidoscopePlugins::TapDance::ActionType tapDanceAction) { +tapDanceAction(uint8_t tapDanceIndex, byte row, byte col, uint8_t tapCount, KaleidoscopePlugins::TapDance::ActionType tapDanceAction) { } KaleidoscopePlugins::TapDance TapDance; diff --git a/src/Kaleidoscope/TapDance.h b/src/Kaleidoscope/TapDance.h index 52af9dfe..a24540ab 100644 --- a/src/Kaleidoscope/TapDance.h +++ b/src/Kaleidoscope/TapDance.h @@ -31,42 +31,42 @@ namespace KaleidoscopePlugins { class TapDance : public KaleidoscopePlugin { - public: - typedef enum { - Tap, - Hold, - Interrupt, - Timeout, - Release, - } ActionType; + public: + typedef enum { + Tap, + Hold, + Interrupt, + Timeout, + Release, + } ActionType; - TapDance (void); + TapDance(void); - void begin (void) final; - static uint16_t timeOut; + void begin(void) final; + static uint16_t timeOut; - void actionKeys (uint8_t tapCount, ActionType tapDanceAction, uint8_t maxKeys, const Key tapKeys[]); + void actionKeys(uint8_t tapCount, ActionType tapDanceAction, uint8_t maxKeys, const Key tapKeys[]); - private: - static uint32_t endTime; - static uint8_t tapCount[16]; - static uint16_t pressedState; - static uint16_t triggeredState; - static uint16_t releaseNextState; - static Key lastTapDanceKey; - static byte lastTapDanceRow; - static byte lastTapDanceCol; + private: + static uint32_t endTime; + static uint8_t tapCount[16]; + static uint16_t pressedState; + static uint16_t triggeredState; + static uint16_t releaseNextState; + static Key lastTapDanceKey; + static byte lastTapDanceRow; + static byte lastTapDanceCol; - static Key eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState); - static void loopHook (bool postClear); + static Key eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState); + static void loopHook(bool postClear); - static Key tap (void); - static void interrupt (void); - static void timeout (void); - static Key release (uint8_t tapDanceIndex); + static Key tap(void); + static void interrupt(void); + static void timeout(void); + static Key release(uint8_t tapDanceIndex); }; } // namespace KaleidoscopePlugins -void tapDanceAction (uint8_t tapDanceIndex, byte row, byte col, uint8_t tapCount, KaleidoscopePlugins::TapDance::ActionType tapDanceAction); +void tapDanceAction(uint8_t tapDanceIndex, byte row, byte col, uint8_t tapCount, KaleidoscopePlugins::TapDance::ActionType tapDanceAction); extern KaleidoscopePlugins::TapDance TapDance;