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`
> Restrict the magic action to fire at most once every `minInterval` cycles.
> Defaults to 10.
> Restrict the magic action to fire at most once every `minInterval`
> milliseconds.
>
> Defaults to 500.
>
> Not strictly a method, it is a variable one can assign a new value to.

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

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

Loading…
Cancel
Save