aa0f63a9d4e812ce7d841d71c84ad33303dd2f8b
[jackhill/qmk/firmware.git] / quantum / debounce / eager_pk.c
1 /*
2 Copyright 2017 Alex Ong<the.onga@gmail.com>
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 2 of the License, or
6 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11 You should have received a copy of the GNU General Public License
12 along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14
15 /*
16 Basic per-key algorithm. Uses an 8-bit counter per key.
17 After pressing a key, it immediately changes state, and sets a counter.
18 No further inputs are accepted until DEBOUNCE milliseconds have occurred.
19 */
20
21 #include "matrix.h"
22 #include "timer.h"
23 #include "quantum.h"
24 #include <stdlib.h>
25
26 #ifndef DEBOUNCE
27 # define DEBOUNCE 5
28 #endif
29
30 #if (MATRIX_COLS <= 8)
31 # define ROW_SHIFTER ((uint8_t)1)
32 #elif (MATRIX_COLS <= 16)
33 # define ROW_SHIFTER ((uint16_t)1)
34 #elif (MATRIX_COLS <= 32)
35 # define ROW_SHIFTER ((uint32_t)1)
36 #endif
37
38 #define debounce_counter_t uint8_t
39
40 static debounce_counter_t *debounce_counters;
41 static bool counters_need_update;
42
43 #define DEBOUNCE_ELAPSED 251
44 #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
45
46 void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
47 void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
48
49 // we use num_rows rather than MATRIX_ROWS to support split keyboards
50 void debounce_init(uint8_t num_rows) {
51 debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
52 int i = 0;
53 for (uint8_t r = 0; r < num_rows; r++) {
54 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
55 debounce_counters[i++] = DEBOUNCE_ELAPSED;
56 }
57 }
58 }
59
60 void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
61 uint8_t current_time = timer_read() % MAX_DEBOUNCE;
62 if (counters_need_update) {
63 update_debounce_counters(num_rows, current_time);
64 }
65
66 if (changed) {
67 transfer_matrix_values(raw, cooked, num_rows, current_time);
68 }
69 }
70
71 // If the current time is > debounce counter, set the counter to enable input.
72 void update_debounce_counters(uint8_t num_rows, uint8_t current_time) {
73 counters_need_update = false;
74 debounce_counter_t *debounce_pointer = debounce_counters;
75 for (uint8_t row = 0; row < num_rows; row++) {
76 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
77 if (*debounce_pointer != DEBOUNCE_ELAPSED) {
78 if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) {
79 *debounce_pointer = DEBOUNCE_ELAPSED;
80 } else {
81 counters_need_update = true;
82 }
83 }
84 debounce_pointer++;
85 }
86 }
87 }
88
89 // upload from raw_matrix to final matrix;
90 void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
91 debounce_counter_t *debounce_pointer = debounce_counters;
92 for (uint8_t row = 0; row < num_rows; row++) {
93 matrix_row_t delta = raw[row] ^ cooked[row];
94 matrix_row_t existing_row = cooked[row];
95 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
96 matrix_row_t col_mask = (ROW_SHIFTER << col);
97 if ((delta & col_mask) && *debounce_pointer == DEBOUNCE_ELAPSED) {
98 *debounce_pointer = current_time;
99 counters_need_update = true;
100 existing_row ^= col_mask; // flip the bit.
101 }
102 debounce_pointer++;
103 }
104 cooked[row] = existing_row;
105 }
106 }
107
108 bool debounce_active(void) { return true; }