Use a timer instead of a loop counter

Loop counters are unreliable, use proper timers instead.

Fixes #2.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/365/head
Gergely Nagy 8 years ago
parent 263b12151c
commit 35918a91a0

@ -56,8 +56,10 @@ The extension provides a `MagicCombo` singleton object, with the following metho
### `.minInterval` ### `.minInterval`
> Restrict the magic action to fire at most once every `minInterval` cycles. > Restrict the magic action to fire at most once every `minInterval`
> Defaults to 10. > milliseconds.
>
> Defaults to 500.
> >
> Not strictly a method, it is a variable one can assign a new value to. > Not strictly a method, it is a variable one can assign a new value to.

@ -21,8 +21,8 @@
namespace Akela { namespace Akela {
const MagicCombo::dictionary_t *MagicCombo::dictionary; const MagicCombo::dictionary_t *MagicCombo::dictionary;
uint8_t MagicCombo::minInterval = 10; uint16_t MagicCombo::minInterval = 500;
uint8_t MagicCombo::timer; uint32_t MagicCombo::startTime;
MagicCombo::MagicCombo (void) { MagicCombo::MagicCombo (void) {
} }
@ -42,9 +42,6 @@ namespace Akela {
if (!dictionary || postClear) if (!dictionary || postClear)
return; return;
if (timer && timer < minInterval)
timer++;
for (byte i = 0;; i++) { for (byte i = 0;; i++) {
dictionary_t combo; dictionary_t combo;
@ -56,9 +53,9 @@ namespace Akela {
if (KeyboardHardware.leftHandState.all == combo.leftHand && if (KeyboardHardware.leftHandState.all == combo.leftHand &&
KeyboardHardware.rightHandState.all == combo.rightHand) { KeyboardHardware.rightHandState.all == combo.rightHand) {
if (timer == 0 || timer >= minInterval || minInterval == 0) { if (startTime == 0 || minInterval == 0 || ((millis () - startTime) >= minInterval)) {
magicComboActions (i, combo.leftHand, combo.rightHand); magicComboActions (i, combo.leftHand, combo.rightHand);
timer = 1; startTime = millis ();
} }
break; break;
} }

@ -32,11 +32,11 @@ namespace Akela {
virtual void begin (void) final; virtual void begin (void) final;
static void configure (const dictionary_t dictionary[]); static void configure (const dictionary_t dictionary[]);
static uint8_t minInterval; static uint16_t minInterval;
private: private:
static const dictionary_t *dictionary; static const dictionary_t *dictionary;
static uint8_t timer; static uint32_t startTime;
static void loopHook (bool postClear); static void loopHook (bool postClear);
}; };

Loading…
Cancel
Save