From f2c3c778a752532cf973e052d5660a30da88b16e Mon Sep 17 00:00:00 2001 From: Fabian Felix Selbach Date: Fri, 1 Mar 2024 20:29:54 +0100 Subject: [PATCH] Added proper debouncing --- Readme.md | 3 ++- kbdrv-main.c | 17 +++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index 200ef5f..857e64d 100644 --- a/Readme.md +++ b/Readme.md @@ -7,6 +7,7 @@ A keyboard software meant to run on a Milk-V duo. - [x] (partial) LED support - [x] N-Key rollover (The currently included hid descriptor "only" supports 80 key rollover but adapting to more keys would be fairly trivial) - [x] Multiple bindings per key +- [x] Debouncing (immediate on, 5ms until key turns off) ### Todo - [ ] RGB - [ ] Layer support @@ -17,7 +18,7 @@ This file contains all the program code required to scan the keyboard matrix as ### keyboard\_mapping.h This file contains all the configuration and mapping of the device you are working with. This includes the GPIO pin mapping to rows/columns, matrix to binding -identifier and binding identifier to hid-modifiers/hid-usage codes. Currently there is no support for binding multiple actions to one key. +identifier and binding identifier to hid-modifiers/hid-usage codes. ## Important notes diff --git a/kbdrv-main.c b/kbdrv-main.c index a4dbd99..c2f37d0 100644 --- a/kbdrv-main.c +++ b/kbdrv-main.c @@ -10,7 +10,7 @@ #include -bool state[sizeof(columns)/sizeof(int)][sizeof(rows)/sizeof(int)]; +bool state[sizeof(columns)/sizeof(int)][sizeof(rows)/sizeof(int)][5]; #define BUF_LEN 512 int main() { @@ -56,7 +56,8 @@ int main() { perror(filename); return 3; } - + + int dbIndex = 4; //db = debounce! -> debounceIndex while(true){ FD_ZERO(&rfds); FD_SET(fd, &rfds); @@ -85,9 +86,11 @@ int main() { for(int c = 0; c < sizeof(columns)/sizeof(int); c++){ digitalWrite(columns[c], LOW); for(int r = 0; r < sizeof(rows)/sizeof(int); r++){ - char curState = state[c][r]; - state[c][r] = (digitalRead(rows[r]) == LOW); - if((state[c][r] == true || curState == true)){ + bool curState = state[c][r][(dbIndex+1)%5]; + state[c][r][dbIndex] = (digitalRead(rows[r]) == LOW); + if((state[c][r][dbIndex] == true || state[c][r][(dbIndex+1)%5] == true || state[c][r][(dbIndex+2)%5] == true + || state[c][r][(dbIndex+3)%5] == true || state[c][r][(dbIndex+4)%5] == true)){ + char bind[sizeof(mapping[c][r])/sizeof(char)]; strcpy(bind, mapping[c][r]); char *binding = strtok(bind, " "); @@ -106,10 +109,12 @@ int main() { } } } - //if(state[c][r] != curState) printf("statechange to %s column %d, row %d\n", (state[c][r] ? "HIGH" : "LOW"), c, r); + //if(state[c][r][dbIndex] != curState) printf("statechange to %s column %d, row %d\n", (state[c][r] ? "HIGH" : "LOW"), c, r); } digitalWrite(columns[c], HIGH); } + dbIndex--; + if(dbIndex > 0) dbIndex = 4; write(fd, report, 82);//Send Report over USB usleep(700); }