sym_defer_g.c (1288B)
1 // Copyright 2017 Alex Ong<the.onga@gmail.com> 2 // Copyright 2021 Simon Arlott 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 // 5 // Basic global debounce algorithm. Used in 99% of keyboards at time of implementation 6 // When no state changes have occured for DEBOUNCE milliseconds, we push the state. 7 8 #include "debounce.h" 9 #include "timer.h" 10 #include <string.h> 11 #ifndef DEBOUNCE 12 # define DEBOUNCE 5 13 #endif 14 15 // Maximum debounce: 255ms 16 #if DEBOUNCE > UINT8_MAX 17 # undef DEBOUNCE 18 # define DEBOUNCE UINT8_MAX 19 #endif 20 21 #if DEBOUNCE > 0 22 23 void debounce_init(void) {} 24 25 bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) { 26 static fast_timer_t debouncing_time; 27 static bool debouncing = false; 28 bool cooked_changed = false; 29 30 if (changed) { 31 debouncing = true; 32 debouncing_time = timer_read_fast(); 33 } else if (debouncing && timer_elapsed_fast(debouncing_time) >= DEBOUNCE) { 34 size_t matrix_size = MATRIX_ROWS_PER_HAND * sizeof(matrix_row_t); 35 if (memcmp(cooked, raw, matrix_size) != 0) { 36 memcpy(cooked, raw, matrix_size); 37 cooked_changed = true; 38 } 39 debouncing = false; 40 } 41 42 return cooked_changed; 43 } 44 45 #else // no debouncing. 46 # include "none.c" 47 #endif