Use proper timers instead of a loop counter

Loop counter depends on the speed of the loop, timers don't. As such, timers are
much more reliable, even at the cost of using more data space.

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

@ -124,23 +124,23 @@ modifiers and one-shot layer keys. It has the following methods:
### `.timeOut`
> The number of loop iterations to wait before timing out and cancelling the
> The number of milliseconds to wait before timing out and cancelling the
> one-shot effect, unless interrupted or cancelled before by any other means.
>
> Not strictly a method, it is a variable one can assign a new value to.
>
> Defaults to 40.
> Defaults to 2500.
### `.holdTimeOut`
> The number of loop iterations to wait before considering a held one-shot key
> as intentionally held. In this case, the one-shot effect will not trigger when
> The number of milliseconds to wait before considering a held one-shot key as
> intentionally held. In this case, the one-shot effect will not trigger when
> the key is released. In other words, holding a one-shot key at least this
> long, and then releasing it, will not trigger the one-shot effect.
>
> Not strictly a method, it is a variable one can assign a new value to.
>
> Defaults to 5.
> Defaults to 200.
## Further reading

@ -23,9 +23,9 @@ using namespace Akela::Ranges;
namespace Akela {
// ---- state ---------
uint8_t OneShot::Timer = 0;
uint8_t OneShot::timeOut = 40;
uint8_t OneShot::holdTimeOut = 5;
uint32_t OneShot::startTime = 0;
uint16_t OneShot::timeOut = 2500;
uint16_t OneShot::holdTimeOut = 250;
uint32_t OneShot::State = 0;
uint32_t OneShot::stickyState = 0;
uint32_t OneShot::pressedState = 0;
@ -58,7 +58,7 @@ namespace Akela {
#define toNormalMod(key, idx) {key.flags = 0; key.keyCode = Key_LCtrl.keyCode + idx;}
#define toNormalMT(key, idx) { key.raw = Key_NoKey.raw; Layer.on (idx - 8); }
#define hasTimedOut() (Timer >= timeOut)
#define hasTimedOut() (millis () - startTime >= timeOut)
// ----- passthrough ------
@ -187,8 +187,8 @@ namespace Akela {
idx = mappedKey.raw - OS_FIRST;
if (key_toggled_off (keyState)) {
clearPressed (idx);
} else if (key_toggled_on (keyState)){
Timer = 0;
} else if (key_toggled_on (keyState)) {
startTime = millis ();
setPressed (idx);
setOneShot (idx);
saveAsPrevious (mappedKey);
@ -214,7 +214,7 @@ namespace Akela {
} else {
if (key_toggled_off (keyState)) {
clearPressed (idx);
if (Timer >= holdTimeOut) {
if ((millis () - startTime) >= holdTimeOut) {
cancelOneShot (idx);
}
}
@ -226,7 +226,7 @@ namespace Akela {
saveAsPrevious (mappedKey);
} else {
Timer = 0;
startTime = millis ();
setOneShot (idx);
saveAsPrevious (mappedKey);
@ -256,9 +256,6 @@ namespace Akela {
return;
if (postClear) {
if (Timer < timeOut)
Timer++;
if (hasTimedOut ())
cancel ();
@ -266,11 +263,9 @@ namespace Akela {
if (shouldCancel) {
if (isSticky (i)) {
if (shouldCancelStickies) {
Timer = 0;
clearSticky (i);
}
} else if (isOneShot (i) && !isPressed (i)) {
Timer = 0;
cancelOneShot (i);
}
}

@ -36,15 +36,15 @@ namespace Akela {
static bool isActive (void);
static void cancel (bool withStickies);
static void cancel (void) { cancel (false); };
static uint8_t timeOut;
static uint8_t holdTimeOut;
static uint16_t timeOut;
static uint16_t holdTimeOut;
static bool isModifierActive (Key key);
void inject (Key key, uint8_t keyState);
private:
static uint8_t Timer;
static uint32_t startTime;
static uint32_t State;
static uint32_t stickyState;
static uint32_t pressedState;

Loading…
Cancel
Save