From ff2ef71db18d25041568d0406dd517a4806e2689 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 9 Feb 2017 22:28:55 +0100 Subject: [PATCH] Use timers instead of loop counters As loop counters are not reliable when it comes to timing, use timers instead. Signed-off-by: Gergely Nagy --- src/Akela/GhostInTheFirmware.cpp | 17 ++++++++--------- src/Akela/GhostInTheFirmware.h | 6 +++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Akela/GhostInTheFirmware.cpp b/src/Akela/GhostInTheFirmware.cpp index 2455019e..35715558 100644 --- a/src/Akela/GhostInTheFirmware.cpp +++ b/src/Akela/GhostInTheFirmware.cpp @@ -22,8 +22,8 @@ namespace Akela { GhostInTheFirmware::GhostKey *GhostInTheFirmware::ghostKeys; bool GhostInTheFirmware::isActive; uint16_t GhostInTheFirmware::currentPos; - uint8_t GhostInTheFirmware::timer; - uint8_t GhostInTheFirmware::timeOut; + uint32_t GhostInTheFirmware::startTime; + uint16_t GhostInTheFirmware::timeOut; GhostInTheFirmware::GhostInTheFirmware (void) { } @@ -49,32 +49,31 @@ namespace Akela { return; if (timeOut == 0) { - timeOut = pgm_read_byte (&(ghostKeys[currentPos].delay)); + timeOut = pgm_read_word (&(ghostKeys[currentPos].delay)); if (timeOut == 0) { currentPos = 0; isActive = false; return; } - } else if (timer == timeOut / 3 && timer != 0) { + } else if ((millis () - startTime) == timeOut / 3) { byte row = pgm_read_byte (&(ghostKeys[currentPos].row)); byte col = pgm_read_byte (&(ghostKeys[currentPos].col)); handle_key_event (Key_NoKey, row, col, WAS_PRESSED); } - if (timer < timeOut / 3) { + if ((millis () - startTime) < timeOut / 3) { byte row = pgm_read_byte (&(ghostKeys[currentPos].row)); byte col = pgm_read_byte (&(ghostKeys[currentPos].col)); handle_key_event (Key_NoKey, row, col, IS_PRESSED); } - timer++; - - if (timer > timeOut) { + if ((millis () - startTime) > timeOut) { currentPos++; - timer = timeOut = 0; + timeOut = 0; + startTime = millis (); } } diff --git a/src/Akela/GhostInTheFirmware.h b/src/Akela/GhostInTheFirmware.h index 4c7cdf4f..a66bc82b 100644 --- a/src/Akela/GhostInTheFirmware.h +++ b/src/Akela/GhostInTheFirmware.h @@ -26,7 +26,7 @@ namespace Akela { typedef struct { byte row; byte col; - uint8_t delay; + uint16_t delay; } GhostKey; GhostInTheFirmware (void); @@ -39,8 +39,8 @@ namespace Akela { static GhostKey *ghostKeys; static bool isActive; static uint16_t currentPos; - static uint8_t timer; - static uint8_t timeOut; + static uint32_t startTime; + static uint16_t timeOut; static void loopHook (bool postClear); };