I'm working on building a keyboard, and some of the firmware runs on a wireless module programmed in C. I'm trying to roll my own debounce algorithm, and could use some help. Essentially, here's how the code is layed out:
#define DEBOUNCE 5
- this value could be anything from 1(inadvisable, not really a debounce) to infinity (also inadvisable, for obvious reasons). I'm debouncing 32 buttons with this, so I next create an array of 32-bit ints using:
static uint32_t key_integration[DEBOUNCE];
On each scan of the inputs (1KHz poll rate), I move the second-to-last item of the array to the last, the 3rd-to-last to the 2nd-to-last, etc, etc, until I finally input the raw scan of the keys into the 0th array item. (Side question: easier way to do this than just iterating a loop from 0 to DEBOUNCE-2? If I had a (32*DEBOUNCE)-bit integer, I would just use bitwise shifting, but... anyway.)
Now the tricky part with which I'm having difficulty: Comparing the array results to the current (debounced) key state. Essentially, if bit X of the key_integration int is 1 is all of the arrays, I want to XOR it with the current (debounced) key state to see if it's changed. If every bit X of the key_integration int is 0 over all the arrays, I want to the do the same. If bit X is scattered 1 and 0 accross the arrays, then it's still debouncing and doesn't need to be compared.
I'm currently confused on how to compare the key_integration array with the debounced key state int. I would really appreciate advice on how to either A) Compare them, or B) Refactor the code to accomplish the same result in a simpler way. I'm definitely not in a position to believe that what I've thought up is the best way to do it.