From 4396a9f4050af2d1d0a56e81bec6acd510b4ceca Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 9 Feb 2017 22:16:54 +0100 Subject: [PATCH] Use proper timers instead of a loop counter Loop counter depends on the speed of the loop, timers don't. As such, timers are much more reliable, even at the cost of using more data space. Signed-off-by: Gergely Nagy --- README.md | 10 +++++----- src/Akela/OneShot.cpp | 21 ++++++++------------- src/Akela/OneShot.h | 6 +++--- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 944b82a8..cf62c376 100644 --- a/README.md +++ b/README.md @@ -124,23 +124,23 @@ modifiers and one-shot layer keys. It has the following methods: ### `.timeOut` -> The number of loop iterations to wait before timing out and cancelling the +> The number of milliseconds to wait before timing out and cancelling the > one-shot effect, unless interrupted or cancelled before by any other means. > > Not strictly a method, it is a variable one can assign a new value to. > -> Defaults to 40. +> Defaults to 2500. ### `.holdTimeOut` -> The number of loop iterations to wait before considering a held one-shot key -> as intentionally held. In this case, the one-shot effect will not trigger when +> The number of milliseconds to wait before considering a held one-shot key as +> intentionally held. In this case, the one-shot effect will not trigger when > the key is released. In other words, holding a one-shot key at least this > long, and then releasing it, will not trigger the one-shot effect. > > Not strictly a method, it is a variable one can assign a new value to. > -> Defaults to 5. +> Defaults to 200. ## Further reading diff --git a/src/Akela/OneShot.cpp b/src/Akela/OneShot.cpp index b1335309..6577f2ba 100644 --- a/src/Akela/OneShot.cpp +++ b/src/Akela/OneShot.cpp @@ -23,9 +23,9 @@ using namespace Akela::Ranges; namespace Akela { // ---- state --------- - uint8_t OneShot::Timer = 0; - uint8_t OneShot::timeOut = 40; - uint8_t OneShot::holdTimeOut = 5; + uint32_t OneShot::startTime = 0; + uint16_t OneShot::timeOut = 2500; + uint16_t OneShot::holdTimeOut = 250; uint32_t OneShot::State = 0; uint32_t OneShot::stickyState = 0; uint32_t OneShot::pressedState = 0; @@ -58,7 +58,7 @@ namespace Akela { #define toNormalMod(key, idx) {key.flags = 0; key.keyCode = Key_LCtrl.keyCode + idx;} #define toNormalMT(key, idx) { key.raw = Key_NoKey.raw; Layer.on (idx - 8); } -#define hasTimedOut() (Timer >= timeOut) +#define hasTimedOut() (millis () - startTime >= timeOut) // ----- passthrough ------ @@ -187,8 +187,8 @@ namespace Akela { idx = mappedKey.raw - OS_FIRST; if (key_toggled_off (keyState)) { clearPressed (idx); - } else if (key_toggled_on (keyState)){ - Timer = 0; + } else if (key_toggled_on (keyState)) { + startTime = millis (); setPressed (idx); setOneShot (idx); saveAsPrevious (mappedKey); @@ -214,7 +214,7 @@ namespace Akela { } else { if (key_toggled_off (keyState)) { clearPressed (idx); - if (Timer >= holdTimeOut) { + if ((millis () - startTime) >= holdTimeOut) { cancelOneShot (idx); } } @@ -226,7 +226,7 @@ namespace Akela { saveAsPrevious (mappedKey); } else { - Timer = 0; + startTime = millis (); setOneShot (idx); saveAsPrevious (mappedKey); @@ -256,9 +256,6 @@ namespace Akela { return; if (postClear) { - if (Timer < timeOut) - Timer++; - if (hasTimedOut ()) cancel (); @@ -266,11 +263,9 @@ namespace Akela { if (shouldCancel) { if (isSticky (i)) { if (shouldCancelStickies) { - Timer = 0; clearSticky (i); } } else if (isOneShot (i) && !isPressed (i)) { - Timer = 0; cancelOneShot (i); } } diff --git a/src/Akela/OneShot.h b/src/Akela/OneShot.h index 68f9ff98..8efe4e4d 100644 --- a/src/Akela/OneShot.h +++ b/src/Akela/OneShot.h @@ -36,15 +36,15 @@ namespace Akela { static bool isActive (void); static void cancel (bool withStickies); static void cancel (void) { cancel (false); }; - static uint8_t timeOut; - static uint8_t holdTimeOut; + static uint16_t timeOut; + static uint16_t holdTimeOut; static bool isModifierActive (Key key); void inject (Key key, uint8_t keyState); private: - static uint8_t Timer; + static uint32_t startTime; static uint32_t State; static uint32_t stickyState; static uint32_t pressedState;