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 <algernon@madhouse-project.org>
pull/389/head
Gergely Nagy 8 years ago
parent 71815cd0f3
commit ff2ef71db1

@ -22,8 +22,8 @@ namespace Akela {
GhostInTheFirmware::GhostKey *GhostInTheFirmware::ghostKeys; GhostInTheFirmware::GhostKey *GhostInTheFirmware::ghostKeys;
bool GhostInTheFirmware::isActive; bool GhostInTheFirmware::isActive;
uint16_t GhostInTheFirmware::currentPos; uint16_t GhostInTheFirmware::currentPos;
uint8_t GhostInTheFirmware::timer; uint32_t GhostInTheFirmware::startTime;
uint8_t GhostInTheFirmware::timeOut; uint16_t GhostInTheFirmware::timeOut;
GhostInTheFirmware::GhostInTheFirmware (void) { GhostInTheFirmware::GhostInTheFirmware (void) {
} }
@ -49,32 +49,31 @@ namespace Akela {
return; return;
if (timeOut == 0) { if (timeOut == 0) {
timeOut = pgm_read_byte (&(ghostKeys[currentPos].delay)); timeOut = pgm_read_word (&(ghostKeys[currentPos].delay));
if (timeOut == 0) { if (timeOut == 0) {
currentPos = 0; currentPos = 0;
isActive = false; isActive = false;
return; return;
} }
} else if (timer == timeOut / 3 && timer != 0) { } else if ((millis () - startTime) == timeOut / 3) {
byte row = pgm_read_byte (&(ghostKeys[currentPos].row)); byte row = pgm_read_byte (&(ghostKeys[currentPos].row));
byte col = pgm_read_byte (&(ghostKeys[currentPos].col)); byte col = pgm_read_byte (&(ghostKeys[currentPos].col));
handle_key_event (Key_NoKey, row, col, WAS_PRESSED); 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 row = pgm_read_byte (&(ghostKeys[currentPos].row));
byte col = pgm_read_byte (&(ghostKeys[currentPos].col)); byte col = pgm_read_byte (&(ghostKeys[currentPos].col));
handle_key_event (Key_NoKey, row, col, IS_PRESSED); handle_key_event (Key_NoKey, row, col, IS_PRESSED);
} }
timer++; if ((millis () - startTime) > timeOut) {
if (timer > timeOut) {
currentPos++; currentPos++;
timer = timeOut = 0; timeOut = 0;
startTime = millis ();
} }
} }

@ -26,7 +26,7 @@ namespace Akela {
typedef struct { typedef struct {
byte row; byte row;
byte col; byte col;
uint8_t delay; uint16_t delay;
} GhostKey; } GhostKey;
GhostInTheFirmware (void); GhostInTheFirmware (void);
@ -39,8 +39,8 @@ namespace Akela {
static GhostKey *ghostKeys; static GhostKey *ghostKeys;
static bool isActive; static bool isActive;
static uint16_t currentPos; static uint16_t currentPos;
static uint8_t timer; static uint32_t startTime;
static uint8_t timeOut; static uint16_t timeOut;
static void loopHook (bool postClear); static void loopHook (bool postClear);
}; };

Loading…
Cancel
Save