From 8a4a4e79c58c73f0bc245a7c3a9e8bbb1b2999c1 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 9 Feb 2017 23:13:43 +0100 Subject: [PATCH] Use a timer instead of a loop counter Loop counters are not a reliable way to track time, use a proper timer instead. Fixes #2. Signed-off-by: Gergely Nagy --- README.md | 8 ++++---- src/Akela/SpaceCadet.cpp | 20 ++++++-------------- src/Akela/SpaceCadet.h | 4 ++-- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e3b8d514..dbb97843 100644 --- a/README.md +++ b/README.md @@ -74,13 +74,13 @@ void setup () { ### `.timeOut` -> The number of loop iterations to wait before considering a held key in -> isolation as its secondary role. That is, we'd have to hold a `Shift` key -> this long, by itself, to trigger the `Shift` role in itself. +> The number of milliseconds to wait before considering a held key in isolation +> as its secondary role. That is, we'd have to hold a `Shift` key this long, by +> itself, to trigger the `Shift` role in itself. > > Not strictly a method, it is a variable one can assign a new value to. > -> Defaults to 40. +> Defaults to 1000. ## Further reading diff --git a/src/Akela/SpaceCadet.cpp b/src/Akela/SpaceCadet.cpp index 158265fb..bc57ece4 100644 --- a/src/Akela/SpaceCadet.cpp +++ b/src/Akela/SpaceCadet.cpp @@ -21,8 +21,8 @@ namespace Akela { uint8_t SpaceCadetShift::parenNeeded; - uint8_t SpaceCadetShift::timer; - uint8_t SpaceCadetShift::timeOut = 40; + uint32_t SpaceCadetShift::startTime; + uint16_t SpaceCadetShift::timeOut = 1000; Key SpaceCadetShift::leftParen, SpaceCadetShift::rightParen; SpaceCadetShift::SpaceCadetShift () { @@ -67,15 +67,13 @@ namespace Akela { if (key_toggled_on (keyState)) { if (mappedKey.raw == Key_LShift.raw) { // if it is LShift, remember it bitWrite (parenNeeded, 0, 1); - if (timer < timeOut) - timer++; + startTime = millis (); } else if (mappedKey.raw == Key_RShift.raw) { // if it is RShift, remember it bitWrite (parenNeeded, 1, 1); - if (timer < timeOut) - timer++; + startTime = millis (); } else { // if it is something else, we do not need a paren at the end. parenNeeded = 0; - timer = 0; + startTime = 0; } // this is all we need to do on keypress, let the next handler do its thing too. @@ -87,15 +85,10 @@ namespace Akela { if (!parenNeeded) return mappedKey; - // if we did not time out yet, up the timer - if (timer < timeOut) - timer++; - // if we timed out, that means we need to keep pressing shift, but won't // need the parens in the end. - if (timer >= timeOut) { + if ((millis () - startTime) >= timeOut) { parenNeeded = 0; - timer = 0; return mappedKey; } @@ -118,7 +111,6 @@ namespace Akela { Keyboard.sendReport (); parenNeeded = 0; - timer = 0; } return mappedKey; diff --git a/src/Akela/SpaceCadet.h b/src/Akela/SpaceCadet.h index e8baf643..5e5d987e 100644 --- a/src/Akela/SpaceCadet.h +++ b/src/Akela/SpaceCadet.h @@ -28,13 +28,13 @@ namespace Akela { virtual void begin (void) final; static void configure (Key left, Key right); - static uint8_t timeOut; + static uint16_t timeOut; void on (void); void off (void); private: static uint8_t parenNeeded; - static uint8_t timer; + static uint32_t startTime; static Key leftParen, rightParen; static Key eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState);