7 #ifndef DEBOUNCING_DELAY
8 # define DEBOUNCING_DELAY 5
12 typedef uint8_t debounce_counter_t;
13 #define DEBOUNCE_COUNTER_MODULO 250
14 #define DEBOUNCE_COUNTER_INACTIVE 251
16 static debounce_counter_t *debounce_counters;
18 void debounce_init(uint8_t num_rows)
20 debounce_counters = malloc(num_rows*MATRIX_COLS);
21 memset(debounce_counters, DEBOUNCE_COUNTER_INACTIVE, num_rows*MATRIX_COLS);
24 void update_debounce_counters(uint8_t num_rows, uint8_t current_time)
26 for (uint8_t row = 0; row < num_rows; row++)
28 for (uint8_t col = 0; col < MATRIX_COLS; col++)
30 if (debounce_counters[row*MATRIX_COLS + col] != DEBOUNCE_COUNTER_INACTIVE)
32 if (TIMER_DIFF(current_time, debounce_counters[row*MATRIX_COLS + col], DEBOUNCE_COUNTER_MODULO) >= DEBOUNCING_DELAY) {
33 debounce_counters[row*MATRIX_COLS + col] = DEBOUNCE_COUNTER_INACTIVE;
40 void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time)
42 for (uint8_t row = 0; row < num_rows; row++)
44 matrix_row_t delta = raw[row] ^ cooked[row];
46 for (uint8_t col = 0; col < MATRIX_COLS; col++)
48 if (debounce_counters[row*MATRIX_COLS + col] == DEBOUNCE_COUNTER_INACTIVE && (delta & (1<<col)))
50 debounce_counters[row*MATRIX_COLS + col] = current_time;
51 cooked[row] ^= (1 << col);
57 void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed)
59 uint8_t current_time = timer_read() % DEBOUNCE_COUNTER_MODULO;
61 update_debounce_counters(num_rows, current_time);
62 transfer_matrix_values(raw, cooked, num_rows, current_time);