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` ### `.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. > 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. > Not strictly a method, it is a variable one can assign a new value to.
> >
> Defaults to 40. > Defaults to 2500.
### `.holdTimeOut` ### `.holdTimeOut`
> The number of loop iterations to wait before considering a held one-shot key > The number of milliseconds to wait before considering a held one-shot key as
> as intentionally held. In this case, the one-shot effect will not trigger when > 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 > 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. > 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. > Not strictly a method, it is a variable one can assign a new value to.
> >
> Defaults to 5. > Defaults to 200.
## Further reading ## Further reading

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

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

Loading…
Cancel
Save