From 7c1ab90992554ebacb89afc6ea6f9db7bf320799 Mon Sep 17 00:00:00 2001 From: DriftKingTW Date: Mon, 20 Mar 2023 22:29:55 +0800 Subject: [PATCH] feat: momentary layer support --- src/main.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/main.hpp | 2 ++ 2 files changed, 41 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 0a14da0..cd555b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,6 +56,8 @@ byte layoutLength = 0; String currentLayout = ""; // For maximum 10 layers const short jsonDocSize = 16384; +size_t tapToggleOrginalLayerIndex = 0; +bool isTemporaryToggled = false; byte inputs[] = {23, 19, 18, 5, 32, 33, 25}; // declaring inputs and outputs const int inputCount = sizeof(inputs) / sizeof(inputs[0]); @@ -655,11 +657,25 @@ void loop() { .substring(6, sizeof(keyMap[r][c].keyInfo) - 1) .toInt(); macroPress(macroMap[index]); + } else if (keyMap[r][c].keyInfo.startsWith("TT_")) { + // Tap-Toggle press + if (!isTemporaryToggled) { + size_t index = + keyMap[r][c] + .keyInfo + .substring(3, sizeof(keyMap[r][c].keyInfo) - 1) + .toInt(); + tapToggleActive(index); + } } else { // Standard key press keyPress(keyMap[r][c]); } } else { + if (keyMap[r][c].keyInfo.startsWith("TT_") && + isTemporaryToggled) { + tapToggleRelease(tapToggleOrginalLayerIndex); + } keyRelease(keyMap[r][c]); } delayMicroseconds(10); @@ -884,6 +900,29 @@ void macroPress(Macro ¯o) { delay(100); } +/** + * Tap toggle layer + * + * @param {size_t} index of the layer to be toggled + */ +void tapToggleActive(size_t index) { + isTemporaryToggled = true; + tapToggleOrginalLayerIndex = currentLayoutIndex; + currentLayoutIndex = index; + initKeys(); +} + +/** + * Tap toggle layer release + * + * @param {size_t} original layer index of the layer to be restored + */ +void tapToggleRelease(size_t orginalLayerIndex) { + isTemporaryToggled = false; + currentLayoutIndex = orginalLayerIndex; + initKeys(); +} + /** * Switch keymap layout * diff --git a/src/main.hpp b/src/main.hpp index 12f2b15..3278484 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -91,6 +91,8 @@ bool keyPress(uint8_t keyStroke, String keyInfo, bool keyState); void keyRelease(Key &key); bool keyRelease(uint8_t keyStroke, String keyInfo, bool keyState); void macroPress(Macro ¯o); +void tapToggleActive(size_t index); +void tapToggleRelease(size_t orginalLayerIndex); void switchLayout(); void switchLayout(int layoutIndex); int findLayoutIndex(String layoutName);