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;
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 ();
}
}

@ -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);
};

Loading…
Cancel
Save