@@ -30,33 +30,25 @@ When no state changes have occured for DEBOUNCE milliseconds, we push the state.
30
30
# define DEBOUNCE UINT8_MAX
31
31
#endif
32
32
33
- typedef uint8_t debounce_counter_t ;
34
-
35
33
#if DEBOUNCE > 0
36
-
37
- # define DEBOUNCE_ELAPSED 0
38
-
39
- static debounce_counter_t debounce_counter = DEBOUNCE_ELAPSED ;
40
-
41
- static fast_timer_t last_time ;
34
+ static bool debouncing = false;
35
+ static fast_timer_t debouncing_time ;
42
36
43
37
void debounce_init (uint8_t num_rows ) {}
44
38
45
39
bool debounce (matrix_row_t raw [], matrix_row_t cooked [], uint8_t num_rows , bool changed ) {
46
40
bool cooked_changed = false;
47
41
48
42
if (changed ) {
49
- debounce_counter = DEBOUNCE ;
50
- last_time = timer_read_fast ();
51
- } else if (debounce_counter != DEBOUNCE_ELAPSED ) {
52
- if (debounce_counter <= timer_elapsed_fast (last_time )) {
53
- debounce_counter = DEBOUNCE_ELAPSED ;
54
- size_t matrix_size = num_rows * sizeof (matrix_row_t );
55
- if (memcmp (cooked , raw , matrix_size ) != 0 ) {
56
- memcpy (cooked , raw , matrix_size );
57
- cooked_changed = true;
58
- }
43
+ debouncing = true;
44
+ debouncing_time = timer_read_fast ();
45
+ } else if (debouncing && timer_elapsed_fast (debouncing_time ) >= DEBOUNCE ) {
46
+ size_t matrix_size = num_rows * sizeof (matrix_row_t );
47
+ if (memcmp (cooked , raw , matrix_size ) != 0 ) {
48
+ memcpy (cooked , raw , matrix_size );
49
+ cooked_changed = true;
59
50
}
51
+ debouncing = false;
60
52
}
61
53
62
54
return cooked_changed ;
0 commit comments