aboutsummaryrefslogtreecommitdiff
path: root/quantum/debounce/sym_eager_pr.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/debounce/sym_eager_pr.c')
-rw-r--r--quantum/debounce/sym_eager_pr.c110
1 files changed, 110 insertions, 0 deletions
diff --git a/quantum/debounce/sym_eager_pr.c b/quantum/debounce/sym_eager_pr.c
new file mode 100644
index 000000000..d12931fdd
--- /dev/null
+++ b/quantum/debounce/sym_eager_pr.c
@@ -0,0 +1,110 @@
1/*
2Copyright 2019 Alex Ong<the.onga@gmail.com>
3This program is free software: you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation, either version 2 of the License, or
6(at your option) any later version.
7This program is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY; without even the implied warranty of
9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10GNU General Public License for more details.
11You should have received a copy of the GNU General Public License
12along with this program. If not, see <http://www.gnu.org/licenses/>.
13*/
14
15/*
16Basic per-row algorithm. Uses an 8-bit counter per row.
17After pressing a key, it immediately changes state, and sets a counter.
18No 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#define debounce_counter_t uint8_t
31static bool matrix_need_update;
32
33static debounce_counter_t *debounce_counters;
34static bool counters_need_update;
35
36#define DEBOUNCE_ELAPSED 251
37#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
38
39static uint8_t wrapping_timer_read(void) {
40 static uint16_t time = 0;
41 static uint8_t last_result = 0;
42 uint16_t new_time = timer_read();
43 uint16_t diff = new_time - time;
44 time = new_time;
45 last_result = (last_result + diff) % (MAX_DEBOUNCE + 1);
46 return last_result;
47}
48
49void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
50void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
51
52// we use num_rows rather than MATRIX_ROWS to support split keyboards
53void debounce_init(uint8_t num_rows) {
54 debounce_counters = (debounce_counter_t *)malloc(num_rows * sizeof(debounce_counter_t));
55 for (uint8_t r = 0; r < num_rows; r++) {
56 debounce_counters[r] = DEBOUNCE_ELAPSED;
57 }
58}
59
60void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
61 uint8_t current_time = wrapping_timer_read();
62 bool needed_update = counters_need_update;
63 if (counters_need_update) {
64 update_debounce_counters(num_rows, current_time);
65 }
66
67 if (changed || (needed_update && !counters_need_update) || matrix_need_update) {
68 transfer_matrix_values(raw, cooked, num_rows, current_time);
69 }
70}
71
72// If the current time is > debounce counter, set the counter to enable input.
73void update_debounce_counters(uint8_t num_rows, uint8_t current_time) {
74 counters_need_update = false;
75 debounce_counter_t *debounce_pointer = debounce_counters;
76 for (uint8_t row = 0; row < num_rows; row++) {
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// upload from raw_matrix to final matrix;
89void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
90 matrix_need_update = false;
91 debounce_counter_t *debounce_pointer = debounce_counters;
92 for (uint8_t row = 0; row < num_rows; row++) {
93 matrix_row_t existing_row = cooked[row];
94 matrix_row_t raw_row = raw[row];
95
96 // determine new value basd on debounce pointer + raw value
97 if (existing_row != raw_row) {
98 if (*debounce_pointer == DEBOUNCE_ELAPSED) {
99 *debounce_pointer = current_time;
100 cooked[row] = raw_row;
101 counters_need_update = true;
102 } else {
103 matrix_need_update = true;
104 }
105 }
106 debounce_pointer++;
107 }
108}
109
110bool debounce_active(void) { return true; }