diff options
Diffstat (limited to 'quantum/debounce')
-rw-r--r-- | quantum/debounce/debounce_eager_pk.c | 121 | ||||
-rw-r--r-- | quantum/debounce/debounce_sym_g.c | 57 | ||||
-rw-r--r-- | quantum/debounce/readme.md | 28 |
3 files changed, 206 insertions, 0 deletions
diff --git a/quantum/debounce/debounce_eager_pk.c b/quantum/debounce/debounce_eager_pk.c new file mode 100644 index 000000000..b8ad09cee --- /dev/null +++ b/quantum/debounce/debounce_eager_pk.c | |||
@@ -0,0 +1,121 @@ | |||
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 | |||
31 | #if (MATRIX_COLS <= 8) | ||
32 | # define ROW_SHIFTER ((uint8_t)1) | ||
33 | #elif (MATRIX_COLS <= 16) | ||
34 | # define ROW_SHIFTER ((uint16_t)1) | ||
35 | #elif (MATRIX_COLS <= 32) | ||
36 | # define ROW_SHIFTER ((uint32_t)1) | ||
37 | #endif | ||
38 | |||
39 | |||
40 | |||
41 | #define debounce_counter_t uint8_t | ||
42 | |||
43 | static debounce_counter_t *debounce_counters; | ||
44 | |||
45 | #define DEBOUNCE_ELAPSED 251 | ||
46 | #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) | ||
47 | |||
48 | void update_debounce_counters(uint8_t num_rows, uint8_t current_time); | ||
49 | void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); | ||
50 | |||
51 | //we use num_rows rather than MATRIX_ROWS to support split keyboards | ||
52 | void debounce_init(uint8_t num_rows) | ||
53 | { | ||
54 | debounce_counters = (debounce_counter_t*)malloc(num_rows*MATRIX_COLS * sizeof(debounce_counter_t)); | ||
55 | int i = 0; | ||
56 | for (uint8_t r = 0; r < num_rows; r++) | ||
57 | { | ||
58 | for (uint8_t c = 0; c < MATRIX_COLS; c++) | ||
59 | { | ||
60 | debounce_counters[i++] = DEBOUNCE_ELAPSED; | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) | ||
66 | { | ||
67 | uint8_t current_time = timer_read() % MAX_DEBOUNCE; | ||
68 | update_debounce_counters(num_rows, current_time); | ||
69 | transfer_matrix_values(raw, cooked, num_rows, current_time); | ||
70 | } | ||
71 | |||
72 | //If the current time is > debounce counter, set the counter to enable input. | ||
73 | void update_debounce_counters(uint8_t num_rows, uint8_t current_time) | ||
74 | { | ||
75 | debounce_counter_t *debounce_pointer = debounce_counters; | ||
76 | for (uint8_t row = 0; row < num_rows; row++) | ||
77 | { | ||
78 | for (uint8_t col = 0; col < MATRIX_COLS; col++) | ||
79 | { | ||
80 | if (*debounce_pointer != DEBOUNCE_ELAPSED) | ||
81 | { | ||
82 | if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) { | ||
83 | *debounce_pointer = DEBOUNCE_ELAPSED; | ||
84 | } | ||
85 | } | ||
86 | debounce_pointer++; | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | // upload from raw_matrix to final matrix; | ||
92 | void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) | ||
93 | { | ||
94 | debounce_counter_t *debounce_pointer = debounce_counters; | ||
95 | for (uint8_t row = 0; row < num_rows; row++) | ||
96 | { | ||
97 | matrix_row_t existing_row = cooked[row]; | ||
98 | matrix_row_t raw_row = raw[row]; | ||
99 | |||
100 | for (uint8_t col = 0; col < MATRIX_COLS; col++) | ||
101 | { | ||
102 | matrix_row_t col_mask = (ROW_SHIFTER << col); | ||
103 | bool final_value = raw_row & col_mask; | ||
104 | bool existing_value = existing_row & col_mask; | ||
105 | if (*debounce_pointer == DEBOUNCE_ELAPSED && | ||
106 | (existing_value != final_value)) | ||
107 | { | ||
108 | *debounce_pointer = current_time; | ||
109 | existing_row ^= col_mask; //flip the bit. | ||
110 | } | ||
111 | debounce_pointer++; | ||
112 | } | ||
113 | cooked[row] = existing_row; | ||
114 | } | ||
115 | } | ||
116 | |||
117 | bool debounce_active(void) | ||
118 | { | ||
119 | return true; | ||
120 | } | ||
121 | |||
diff --git a/quantum/debounce/debounce_sym_g.c b/quantum/debounce/debounce_sym_g.c new file mode 100644 index 000000000..c8ab34e1a --- /dev/null +++ b/quantum/debounce/debounce_sym_g.c | |||
@@ -0,0 +1,57 @@ | |||
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 global debounce algorithm. Used in 99% of keyboards at time of implementation | ||
17 | When no state changes have occured for DEBOUNCE milliseconds, we push the state. | ||
18 | */ | ||
19 | #include "matrix.h" | ||
20 | #include "timer.h" | ||
21 | #include "quantum.h" | ||
22 | #ifndef DEBOUNCE | ||
23 | #define DEBOUNCE 5 | ||
24 | #endif | ||
25 | |||
26 | void debounce_init(uint8_t num_rows) {} | ||
27 | static bool debouncing = false; | ||
28 | |||
29 | #if DEBOUNCE > 0 | ||
30 | static uint16_t debouncing_time; | ||
31 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) | ||
32 | { | ||
33 | if (changed) { | ||
34 | debouncing = true; | ||
35 | debouncing_time = timer_read(); | ||
36 | } | ||
37 | |||
38 | if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) { | ||
39 | for (int i = 0; i < num_rows; i++) { | ||
40 | cooked[i] = raw[i]; | ||
41 | } | ||
42 | debouncing = false; | ||
43 | } | ||
44 | } | ||
45 | #else //no debouncing. | ||
46 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) | ||
47 | { | ||
48 | for (int i = 0; i < num_rows; i++) { | ||
49 | cooked[i] = raw[i]; | ||
50 | } | ||
51 | } | ||
52 | #endif | ||
53 | |||
54 | bool debounce_active(void) { | ||
55 | return debouncing; | ||
56 | } | ||
57 | |||
diff --git a/quantum/debounce/readme.md b/quantum/debounce/readme.md new file mode 100644 index 000000000..1a77d44df --- /dev/null +++ b/quantum/debounce/readme.md | |||
@@ -0,0 +1,28 @@ | |||
1 | Debounce algorithms belong in this folder. | ||
2 | Here are a few ideas | ||
3 | |||
4 | 1) Global vs Per-Key vs Per-Row | ||
5 | * Global - one timer for all keys. Any key change state affects global timer | ||
6 | * Per key - one timer per key | ||
7 | * Per row - one timer per row | ||
8 | |||
9 | 2) Eager vs symmetric vs assymetric | ||
10 | * Eager - any key change is reported immediately. All further inputs for DEBOUNCE ms are ignored. | ||
11 | * Symmetric - wait for no changes for DEBOUNCE ms before reporting change | ||
12 | * Assymetric - wait for different times depending on key-down/key-up. E.g. Eager key-down, DEBOUNCE ms key up. | ||
13 | |||
14 | 3) Timestamp vs cycles | ||
15 | * old old old code waits n cycles, decreasing count by one each matrix_scan | ||
16 | * newer code stores the millisecond the change occurred, and does subraction to figure out time elapsed. | ||
17 | * Timestamps are superior, i don't think cycles will ever be used again once upgraded. | ||
18 | |||
19 | The default algorithm is symmetric and global. | ||
20 | Here are a few that could be implemented: | ||
21 | |||
22 | debounce_sym_g.c | ||
23 | debounce_sym_pk.c | ||
24 | debounce_sym_pr.c | ||
25 | debounce_sym_pr_cycles.c //currently used in ergo-dox | ||
26 | debounce_eager_g.c | ||
27 | debounce_eager_pk.c | ||
28 | debounce_eager_pr.c //could be used in ergo-dox! | ||