Use timers instead of loop counters

Loop counters are not reliable timer substitutes, use proper timers instead.

Fixes #5.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/389/head
Gergely Nagy 8 years ago
parent bae049583d
commit 74070edcf1

@ -22,8 +22,8 @@ using namespace Akela::Ranges;
namespace Akela { namespace Akela {
// --- state --- // --- state ---
uint16_t TapDance::timer; uint32_t TapDance::startTime;
uint8_t TapDance::timeOut = 5; uint16_t TapDance::timeOut = 200;
uint8_t TapDance::tapCount[32]; uint8_t TapDance::tapCount[32];
uint32_t TapDance::pressedState; uint32_t TapDance::pressedState;
uint32_t TapDance::triggeredState; uint32_t TapDance::triggeredState;
@ -47,7 +47,7 @@ namespace Akela {
tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Interrupt); tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Interrupt);
bitWrite (triggeredState, idx, 1); bitWrite (triggeredState, idx, 1);
timer = 0; startTime = millis ();
if (bitRead (pressedState, idx)) if (bitRead (pressedState, idx))
return; return;
@ -59,6 +59,8 @@ namespace Akela {
TapDance::timeout (void) { TapDance::timeout (void) {
uint8_t idx = lastTapDanceKey.raw - TD_FIRST; uint8_t idx = lastTapDanceKey.raw - TD_FIRST;
startTime = 0;
tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Timeout); tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Timeout);
bitWrite (triggeredState, idx, 1); bitWrite (triggeredState, idx, 1);
@ -74,7 +76,7 @@ namespace Akela {
TapDance::release (uint8_t tapDanceIndex) { TapDance::release (uint8_t tapDanceIndex) {
tapDanceAction (tapDanceIndex, lastTapDanceRow, lastTapDanceCol, tapCount[tapDanceIndex], Release); tapDanceAction (tapDanceIndex, lastTapDanceRow, lastTapDanceCol, tapCount[tapDanceIndex], Release);
timer = 0; startTime = 0;
tapCount[tapDanceIndex] = 0; tapCount[tapDanceIndex] = 0;
lastTapDanceKey.raw = Key_NoKey.raw; lastTapDanceKey.raw = Key_NoKey.raw;
@ -88,7 +90,7 @@ namespace Akela {
uint8_t idx = lastTapDanceKey.raw - TD_FIRST; uint8_t idx = lastTapDanceKey.raw - TD_FIRST;
tapCount[idx]++; tapCount[idx]++;
timer = 0; startTime = millis ();
tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Tap); tapDanceAction (idx, lastTapDanceRow, lastTapDanceCol, tapCount[idx], Tap);
@ -211,10 +213,7 @@ namespace Akela {
if (!isActive ()) if (!isActive ())
return; return;
if (timer < timeOut) if (startTime && (millis () - startTime) >= timeOut)
timer++;
if (timer >= timeOut)
timeout(); timeout();
} }
}; };

@ -42,12 +42,12 @@ namespace Akela {
TapDance (void); TapDance (void);
virtual void begin (void) final; virtual void begin (void) final;
static uint8_t timeOut; static uint16_t timeOut;
void actionKeys (uint8_t tapCount, ActionType tapDanceAction, uint8_t maxKeys, const Key tapKeys[]); void actionKeys (uint8_t tapCount, ActionType tapDanceAction, uint8_t maxKeys, const Key tapKeys[]);
private: private:
static uint16_t timer; static uint32_t startTime;
static uint8_t tapCount[32]; static uint8_t tapCount[32];
static uint32_t pressedState; static uint32_t pressedState;
static uint32_t triggeredState; static uint32_t triggeredState;

Loading…
Cancel
Save