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` ### `.timeOut`
> The number of loop iterations to wait before considering a held key in > The number of milliseconds to wait before considering a held key in isolation
> isolation as its secondary role. That is, we'd have to hold a `Shift` key > as its secondary role. That is, we'd have to hold a `Shift` key this long, by
> this long, by itself, to trigger the `Shift` role in itself. > itself, to trigger the `Shift` role in itself.
> >
> 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 1000.
## Further reading ## Further reading

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

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

Loading…
Cancel
Save