Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

key repeat (retrigger button push when button is held down)

  • Loading branch information...
commit 2b04a29ea8f0712ce6453fc8ff56d1a3a757887f 1 parent eb962de
@beerriot authored
Showing with 23 additions and 6 deletions.
  1. +5 −0 include/nkbuttons.h
  2. +18 −6 src/nkbuttons.c
View
5 include/nkbuttons.h
@@ -14,6 +14,11 @@ struct nkbuttons {
uint8_t stable;
// the most recent read of the buttons
uint8_t last_read;
+ // number of times stable state has been read
+ uint8_t stable_count;
+ // whether or not key triggers are currently repeats
+ // (smaller delay between firings after first repeat)
+ uint8_t is_repeat;
};
void nkbuttons_init();
View
24 src/nkbuttons.c
@@ -27,13 +27,23 @@ uint8_t nkbuttons_read(struct nkbuttons* state) {
// factor out bounces by sampling twice before determining
// what is actually pressed
uint8_t pressed = state->last_read & fresh;
-
- // find out what buttons are pushed now that weren't before
- uint8_t newly = (state->stable ^ pressed) & pressed;
-
- // update state
state->last_read = fresh;
- state->stable = pressed;
+
+ uint8_t newly = 0;
+ if (state->stable == pressed) {
+ if (++state->stable_count > (state->is_repeat ? 14 : 30)) {
+ state->stable_count = 0;
+ state->is_repeat = 1;
+ // trigger a key repeat, if it's still held down
+ newly = pressed;
+ }
+ } else {
+ state->stable_count = 0;
+ state->is_repeat = 0;
+ // find out what buttons are pushed now that weren't before
+ newly = (state->stable ^ pressed) & pressed;
+ state->stable = pressed;
+ }
// reply with newly-pushed buttons
return newly;
@@ -43,6 +53,8 @@ uint8_t nkbuttons_read(struct nkbuttons* state) {
void nkbuttons_clear(struct nkbuttons* state) {
state->stable = 0;
state->last_read = 0;
+ state->stable_count = 0;
+ state->is_repeat = 0;
// throw away buttons already pressed
nkbuttons_read(state);
nkbuttons_read(state);
Please sign in to comment.
Something went wrong with that request. Please try again.