Use a timer instead of a loop counter

Loop counters are not a reliable way to track time, use a proper timer instead.

Fixes #2.

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

@ -74,13 +74,13 @@ void setup () {
### `.timeOut`
> The number of loop iterations to wait before considering a held key in
> isolation as its secondary role. That is, we'd have to hold a `Shift` key
> this long, by itself, to trigger the `Shift` role in itself.
> The number of milliseconds to wait before considering a held key in isolation
> as its secondary role. That is, we'd have to hold a `Shift` key this long, by
> itself, to trigger the `Shift` role in itself.
>
> Not strictly a method, it is a variable one can assign a new value to.
>
> Defaults to 40.
> Defaults to 1000.
## Further reading

@ -21,8 +21,8 @@
namespace Akela {
uint8_t SpaceCadetShift::parenNeeded;
uint8_t SpaceCadetShift::timer;
uint8_t SpaceCadetShift::timeOut = 40;
uint32_t SpaceCadetShift::startTime;
uint16_t SpaceCadetShift::timeOut = 1000;
Key SpaceCadetShift::leftParen, SpaceCadetShift::rightParen;
SpaceCadetShift::SpaceCadetShift () {
@ -67,15 +67,13 @@ namespace Akela {
if (key_toggled_on (keyState)) {
if (mappedKey.raw == Key_LShift.raw) { // if it is LShift, remember it
bitWrite (parenNeeded, 0, 1);
if (timer < timeOut)
timer++;
startTime = millis ();
} else if (mappedKey.raw == Key_RShift.raw) { // if it is RShift, remember it
bitWrite (parenNeeded, 1, 1);
if (timer < timeOut)
timer++;
startTime = millis ();
} else { // if it is something else, we do not need a paren at the end.
parenNeeded = 0;
timer = 0;
startTime = 0;
}
// this is all we need to do on keypress, let the next handler do its thing too.
@ -87,15 +85,10 @@ namespace Akela {
if (!parenNeeded)
return mappedKey;
// if we did not time out yet, up the timer
if (timer < timeOut)
timer++;
// if we timed out, that means we need to keep pressing shift, but won't
// need the parens in the end.
if (timer >= timeOut) {
if ((millis () - startTime) >= timeOut) {
parenNeeded = 0;
timer = 0;
return mappedKey;
}
@ -118,7 +111,6 @@ namespace Akela {
Keyboard.sendReport ();
parenNeeded = 0;
timer = 0;
}
return mappedKey;

@ -28,13 +28,13 @@ namespace Akela {
virtual void begin (void) final;
static void configure (Key left, Key right);
static uint8_t timeOut;
static uint16_t timeOut;
void on (void);
void off (void);
private:
static uint8_t parenNeeded;
static uint8_t timer;
static uint32_t startTime;
static Key leftParen, rightParen;
static Key eventHandlerHook (Key mappedKey, byte row, byte col, uint8_t keyState);

Loading…
Cancel
Save