diff --git a/examples/OneShot/OneShot.ino b/examples/OneShot/OneShot.ino index efea6c8f..8901ebfd 100644 --- a/examples/OneShot/OneShot.ino +++ b/examples/OneShot/OneShot.ino @@ -20,52 +20,52 @@ #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, + [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, - OSM(LeftControl), Key_Backspace, OSM(LeftGui), Key_LeftShift, - Key_Keymap1_Momentary, + OSM(LeftControl), Key_Backspace, OSM(LeftGui), Key_LeftShift, + Key_Keymap1_Momentary, - 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_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, OSM(RightAlt), Key_Spacebar, OSM(RightControl), - OSL(1) - ), + Key_RightShift, OSM(RightAlt), Key_Spacebar, OSM(RightControl), + OSL(1) + ), - [1] = KEYMAP_STACKED - ( - ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___, ___, + [1] = KEYMAP_STACKED + ( + ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, - ___, + ___, ___, ___, ___, + ___, - ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, Key_UpArrow, Key_DownArrow, Key_LeftArrow, Key_RightArrow, ___, ___, - ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, - ___ - ), + ___, ___, ___, ___, + ___ + ), }; void setup () { - Kaleidoscope.setup (); + Kaleidoscope.setup (); - USE_PLUGINS (&OneShot); + USE_PLUGINS (&OneShot); } void loop () { - Kaleidoscope.loop (); + Kaleidoscope.loop (); } diff --git a/src/Kaleidoscope/OneShot.cpp b/src/Kaleidoscope/OneShot.cpp index ad9ee20c..54edba2a 100644 --- a/src/Kaleidoscope/OneShot.cpp +++ b/src/Kaleidoscope/OneShot.cpp @@ -21,21 +21,21 @@ using namespace KaleidoscopePlugins::Ranges; namespace KaleidoscopePlugins { - // ---- state --------- - - uint32_t OneShot::startTime = 0; - uint16_t OneShot::timeOut = 2500; - uint16_t OneShot::holdTimeOut = 250; - OneShot::state_t OneShot::State; - OneShot::state_t OneShot::stickyState; - OneShot::state_t OneShot::pressedState; - uint32_t OneShot::leftMask; - uint32_t OneShot::rightMask; - Key OneShot::prevKey; - bool OneShot::shouldCancel = false; - bool OneShot::shouldCancelStickies = false; - - // --- helper macros ------ +// ---- state --------- + +uint32_t OneShot::startTime = 0; +uint16_t OneShot::timeOut = 2500; +uint16_t OneShot::holdTimeOut = 250; +OneShot::state_t OneShot::State; +OneShot::state_t OneShot::stickyState; +OneShot::state_t OneShot::pressedState; +uint32_t OneShot::leftMask; +uint32_t OneShot::rightMask; +Key OneShot::prevKey; +bool OneShot::shouldCancel = false; +bool OneShot::shouldCancelStickies = false; + +// --- helper macros ------ #define isOS(key) (key.raw >= OS_FIRST && key.raw <= OS_LAST) #define isModifier(key) (key.raw >= Key_LeftControl.raw && key.raw <= Key_RightGui.raw) @@ -58,221 +58,221 @@ namespace KaleidoscopePlugins { #define hasTimedOut() (millis () - startTime >= timeOut) - // ---- OneShot stuff ---- - void - OneShot::injectNormalKey (uint8_t idx, uint8_t keyState) { +// ---- OneShot stuff ---- +void +OneShot::injectNormalKey (uint8_t idx, uint8_t keyState) { Key key; if (idx < 8) { - key.flags = Key_LeftControl.flags; - key.keyCode = Key_LeftControl.keyCode + idx; + key.flags = Key_LeftControl.flags; + key.keyCode = Key_LeftControl.keyCode + idx; } else { - key.flags = KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP; - key.keyCode = MOMENTARY_OFFSET + idx - 8; + key.flags = KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP; + key.keyCode = MOMENTARY_OFFSET + idx - 8; } handle_keyswitch_event (key, UNKNOWN_KEYSWITCH_LOCATION, keyState | INJECTED); - } +} - void - OneShot::activateOneShot (uint8_t idx) { +void +OneShot::activateOneShot (uint8_t idx) { injectNormalKey (idx, IS_PRESSED); - } +} - void - OneShot::cancelOneShot (uint8_t idx) { +void +OneShot::cancelOneShot (uint8_t idx) { clearOneShot (idx); injectNormalKey (idx, WAS_PRESSED); - } +} - void - OneShot::mask (byte row, byte col) { +void +OneShot::mask (byte row, byte col) { if (row >= ROWS || col >= COLS) - return; + return; if (col >= 8) { - col = col - 8; - rightMask |= SCANBIT (row, col); + col = col - 8; + rightMask |= SCANBIT (row, col); } else - leftMask |= SCANBIT (row, col); - } + leftMask |= SCANBIT (row, col); +} - void - OneShot::unmask (byte row, byte col) { +void +OneShot::unmask (byte row, byte col) { if (row >= ROWS || col >= COLS) - return; + return; if (col >= 8) { - col = col - 8; - rightMask &= ~(SCANBIT (row, col)); + col = col - 8; + rightMask &= ~(SCANBIT (row, col)); } else - leftMask &= ~(SCANBIT (row, col)); - } + leftMask &= ~(SCANBIT (row, col)); +} - bool - OneShot::isMasked (byte row, byte col) { +bool +OneShot::isMasked (byte row, byte col) { if (row >= ROWS || col >= COLS) - return false; + return false; if (col >= 8) { - col = col - 8; - return rightMask & SCANBIT (row, col); + col = col - 8; + return rightMask & SCANBIT (row, col); } else - return leftMask & SCANBIT (row, col); - } + return leftMask & SCANBIT (row, col); +} - Key - OneShot::eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState) { +Key +OneShot::eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState) { uint8_t idx; if (keyState & INJECTED) - return mappedKey; + return mappedKey; if (!State.all) { - if (!isOS (mappedKey)) { - if (isMasked (row, col)) { - if (key_toggled_off (keyState)) - unmask (row, col); - return Key_NoKey; + if (!isOS (mappedKey)) { + if (isMasked (row, col)) { + if (key_toggled_off (keyState)) + unmask (row, col); + return Key_NoKey; + } + + return mappedKey; } - return mappedKey; - } - - idx = mappedKey.raw - OS_FIRST; - if (key_toggled_off (keyState)) { - clearPressed (idx); - } else if (key_toggled_on (keyState)) { - startTime = millis (); - setPressed (idx); - setOneShot (idx); - saveAsPrevious (mappedKey); + idx = mappedKey.raw - OS_FIRST; + if (key_toggled_off (keyState)) { + clearPressed (idx); + } else if (key_toggled_on (keyState)) { + startTime = millis (); + setPressed (idx); + setOneShot (idx); + saveAsPrevious (mappedKey); - activateOneShot (idx); - } + activateOneShot (idx); + } - return Key_NoKey; + return Key_NoKey; } if (!key_is_pressed (keyState) && !key_was_pressed (keyState)) - return mappedKey; + return mappedKey; if (isOS (mappedKey)) { - idx = mappedKey.raw - OS_FIRST; + idx = mappedKey.raw - OS_FIRST; - if (isSticky (idx)) { - if (key_toggled_on (keyState)) { // maybe on _off instead? - saveAsPrevious (mappedKey); - clearSticky (idx); - cancelOneShot (idx); - } - } else { - if (key_toggled_off (keyState)) { - clearPressed (idx); - if ((millis () - startTime) >= holdTimeOut) { - cancelOneShot (idx); - } - } + if (isSticky (idx)) { + if (key_toggled_on (keyState)) { // maybe on _off instead? + saveAsPrevious (mappedKey); + clearSticky (idx); + cancelOneShot (idx); + } + } else { + if (key_toggled_off (keyState)) { + clearPressed (idx); + if ((millis () - startTime) >= holdTimeOut) { + cancelOneShot (idx); + } + } - if (key_toggled_on (keyState)) { - setPressed (idx); - if (isSameAsPrevious (mappedKey)) { - setSticky (idx); + if (key_toggled_on (keyState)) { + setPressed (idx); + if (isSameAsPrevious (mappedKey)) { + setSticky (idx); - saveAsPrevious (mappedKey); - } else { - startTime = millis (); + saveAsPrevious (mappedKey); + } else { + startTime = millis (); - setOneShot (idx); - saveAsPrevious (mappedKey); + setOneShot (idx); + saveAsPrevious (mappedKey); - activateOneShot (idx); - } + activateOneShot (idx); + } + } } - } - return Key_NoKey; + return Key_NoKey; } // ordinary key here, with some event if (key_is_pressed (keyState)) { - mask (row, col); - saveAsPrevious (mappedKey); - shouldCancel = true; + mask (row, col); + saveAsPrevious (mappedKey); + shouldCancel = true; } return mappedKey; - } +} - void - OneShot::loopHook (bool postClear) { +void +OneShot::loopHook (bool postClear) { if (!State.all) - return; + return; if (postClear) { - if (hasTimedOut ()) - cancel (); - - for (uint8_t i = 0; i < 32; i++) { - if (shouldCancel) { - if (isSticky (i)) { - if (shouldCancelStickies) { - clearSticky (i); + if (hasTimedOut ()) + cancel (); + + for (uint8_t i = 0; i < 32; i++) { + if (shouldCancel) { + if (isSticky (i)) { + if (shouldCancelStickies) { + clearSticky (i); + } + } else if (isOneShot (i) && !isPressed (i)) { + cancelOneShot (i); + } } - } else if (isOneShot (i) && !isPressed (i)) { - cancelOneShot (i); - } } - } - if (shouldCancel) { - shouldCancel = false; - shouldCancelStickies = false; - } + if (shouldCancel) { + shouldCancel = false; + shouldCancelStickies = false; + } } else { - for (uint8_t i = 0; i < 8; i++) { - if (isOneShot (i)) { - activateOneShot (i); + for (uint8_t i = 0; i < 8; i++) { + if (isOneShot (i)) { + activateOneShot (i); + } } - } } - } +} - // --- glue code --- +// --- glue code --- - OneShot::OneShot (void) { - } +OneShot::OneShot (void) { +} - void - OneShot::begin (void) { +void +OneShot::begin (void) { event_handler_hook_use (eventHandlerHook); loop_hook_use (loopHook); - } +} - bool - OneShot::isActive (void) { +bool +OneShot::isActive (void) { return (State.all && !hasTimedOut ()); - } +} - bool - OneShot::isModifierActive (Key key) { +bool +OneShot::isModifierActive (Key key) { if (key.raw < Key_LeftControl.raw || key.raw > Key_RightGui.raw) - return false; + return false; return isOneShot (key.keyCode - Key_LeftControl.keyCode); - } +} - void - OneShot::cancel (bool withStickies) { +void +OneShot::cancel (bool withStickies) { shouldCancel = true; shouldCancelStickies = withStickies; - } +} - void - OneShot::inject (Key key, uint8_t keyState) { +void +OneShot::inject (Key key, uint8_t keyState) { eventHandlerHook (key, 255, 255, keyState); - } +} }; diff --git a/src/Kaleidoscope/OneShot.h b/src/Kaleidoscope/OneShot.h index 99096784..a7df2b90 100644 --- a/src/Kaleidoscope/OneShot.h +++ b/src/Kaleidoscope/OneShot.h @@ -25,7 +25,7 @@ #define OSL(n) (Key) {.raw = KaleidoscopePlugins::Ranges::OSL_FIRST + n} namespace KaleidoscopePlugins { - class OneShot : public KaleidoscopePlugin { +class OneShot : public KaleidoscopePlugin { public: OneShot (void); @@ -33,7 +33,9 @@ namespace KaleidoscopePlugins { static bool isActive (void); static void cancel (bool withStickies); - static void cancel (void) { cancel (false); }; + static void cancel (void) { + cancel (false); + }; static uint16_t timeOut; static uint16_t holdTimeOut; @@ -43,11 +45,11 @@ namespace KaleidoscopePlugins { private: typedef union { - struct { - uint8_t mods; - uint8_t layers; - }; - uint16_t all; + struct { + uint8_t mods; + uint8_t layers; + }; + uint16_t all; } state_t; static uint32_t startTime; static state_t State; @@ -69,7 +71,7 @@ namespace KaleidoscopePlugins { static Key eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState); static void loopHook (bool postClear); - }; +}; }; extern KaleidoscopePlugins::OneShot OneShot;