aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDrashna Jaelre <drashna@live.com>2019-02-14 20:36:20 -0800
committerGitHub <noreply@github.com>2019-02-14 20:36:20 -0800
commitc22f3ba3a2605280b81829a764c29c9e01ae674f (patch)
treef7aba084d7aa8d1f60058cd409eefb5a0d7ebb34
parentcc5c6b449a4a36fc56fa5896b2b8f120e4bb0b31 (diff)
parent7d8c62993921383a35f9cd172fe0a1d2e893b2f3 (diff)
downloadqmk_firmware-c22f3ba3a2605280b81829a764c29c9e01ae674f.tar.gz
qmk_firmware-c22f3ba3a2605280b81829a764c29c9e01ae674f.zip
Debounce refactor / API (#3720)
* Added xeal60 via clone of lets split * Delete removed other keymaps * Basic keymap (no FN). Compiles. * Removed NP_STAR and NP_SLSH. * Removed "debounce_algo = manual" in all keyboards with CUSTOM_MATRIX = yes. * Changed order of rules in TMK. Documented feature. * Fixed missing whitespace in debounce documentation Table wasn't working due to missing newline. * Added bold in a few areas. * DO NOT USE - Removed debounce from TMK. * Remove accidental xeal60 commit * DO NOT USE - debounce successfully compiled. * DO NOT USE Revert back to original API to support split_keyboards. * Working eager_pk * Whitespace cleanup. * Restored debounce.h since there wasnt any real change. * Moved debouncing_time variable to inside #if debounce * Removed check for custom_matrix. We can safely include the debounce file for compilation when custom_matrix is used. * Removed #include "matrix.h" from debounce.h * Bug fix - was using MATRIX_ROWS instead of num_rows * Fixed compilation error with debounce_sym_g * Renamed DEBOUNCE_ALGO to DEBOUNCE_TYPE * Malloc array in debounce_eager_pk, since split keyboards only use MATRIX_ROWS/2. * Fix compile error in debounce_eager_pk * Stricter, leaner DEBOUNCE_TYPE section in common_features.mk. Cleanup debounce_type.mk
-rw-r--r--common_features.mk16
-rw-r--r--docs/feature_debounce_type.md46
-rw-r--r--keyboards/handwired/xealous/rules.mk2
-rw-r--r--quantum/debounce.c52
-rw-r--r--quantum/debounce.h2
-rw-r--r--quantum/debounce/debounce_eager_pk.c121
-rw-r--r--quantum/debounce/debounce_sym_g.c57
-rw-r--r--quantum/debounce/readme.md28
-rw-r--r--quantum/matrix.c31
-rw-r--r--tmk_core/common/keyboard.c17
10 files changed, 272 insertions, 100 deletions
diff --git a/common_features.mk b/common_features.mk
index b5fdb404b..6f02dbac6 100644
--- a/common_features.mk
+++ b/common_features.mk
@@ -282,10 +282,20 @@ ifneq ($(strip $(CUSTOM_MATRIX)), yes)
282 endif 282 endif
283endif 283endif
284 284
285# Include the standard debounce code if needed 285DEBOUNCE_DIR:= $(QUANTUM_DIR)/debounce
286ifneq ($(strip $(CUSTOM_DEBOUNCE)), yes) 286# Debounce Modules. If implemented in matrix.c, don't use these.
287 QUANTUM_SRC += $(QUANTUM_DIR)/debounce.c 287DEBOUNCE_TYPE?= sym_g
288VALID_DEBOUNCE_TYPES := sym_g eager_pk custom
289ifeq ($(filter $(DEBOUNCE_TYPE),$(VALID_DEBOUNCE_TYPES)),)
290 $(error DEBOUNCE_TYPE="$(DEBOUNCE_TYPE)" is not a valid debounce algorithm)
288endif 291endif
292ifeq ($(strip $(DEBOUNCE_TYPE)), sym_g)
293 QUANTUM_SRC += $(DEBOUNCE_DIR)/debounce_sym_g.c
294else ifeq ($(strip $(DEBOUNCE_TYPE)), eager_pk)
295 QUANTUM_SRC += $(DEBOUNCE_DIR)/debounce_eager_pk.c
296endif
297
298
289 299
290ifeq ($(strip $(SPLIT_KEYBOARD)), yes) 300ifeq ($(strip $(SPLIT_KEYBOARD)), yes)
291 OPT_DEFS += -DSPLIT_KEYBOARD 301 OPT_DEFS += -DSPLIT_KEYBOARD
diff --git a/docs/feature_debounce_type.md b/docs/feature_debounce_type.md
new file mode 100644
index 000000000..82b3d7de1
--- /dev/null
+++ b/docs/feature_debounce_type.md
@@ -0,0 +1,46 @@
1# Debounce algorithm
2
3QMK supports multiple debounce algorithms through its debounce API.
4
5The underlying debounce algorithm is determined by which matrix.c file you are using.
6
7The logic for which debounce method called is below. It checks various defines that you have set in rules.mk
8
9```
10DEBOUNCE_TYPE?= sym_g
11VALID_DEBOUNCE_TYPES := sym_g eager_pk custom
12ifeq ($(filter $(DEBOUNCE_TYPE),$(VALID_DEBOUNCE_TYPES)),)
13 $(error DEBOUNCE_TYPE="$(DEBOUNCE_TYPE)" is not a valid debounce algorithm)
14endif
15ifeq ($(strip $(DEBOUNCE_TYPE)), sym_g)
16 QUANTUM_SRC += $(DEBOUNCE_DIR)/debounce_sym_g.c
17else ifeq ($(strip $(DEBOUNCE_TYPE)), eager_pk)
18 QUANTUM_SRC += $(DEBOUNCE_DIR)/debounce_eager_pk.c
19endif
20```
21
22# Debounce selection
23
24| DEBOUNCE_ALGO | Description | What to do |
25| ------------- | --------------------------------------------------- | ----------------------------- |
26| Not defined | You are using the included matrix.c and debounce.c | Nothing. Debounce_sym_g will be compiled, and used if necessary |
27| custom | Use your own debounce.c | ```SRC += debounce.c``` add your own debounce.c and implement necessary functions |
28| sym_g / eager_pk | You are using the included matrix.c and debounce.c | Use an alternative debounce algorithm |
29
30**Regarding split keyboards**:
31The debounce code is compatible with split keyboards.
32
33# Use your own debouncing code
34* Set ```DEBOUNCE_TYPE = custom ```.
35* Add ```SRC += debounce.c```
36* Add your own ```debounce.c```. Look at included ```debounce_sym_g.c```s for sample implementations.
37* Debouncing occurs after every raw matrix scan.
38* Use num_rows rather than MATRIX_ROWS, so that split keyboards are supported correctly.
39
40# Changing between included debouncing methods
41You can either use your own code, by including your own debounce.c, or switch to another included one.
42Included debounce methods are:
43* debounce_eager_pk - debouncing per key. On any state change, response is immediate, followed by ```DEBOUNCE_DELAY``` millseconds of no further input for that key
44* debounce_sym_g - debouncing per keyboard. On any state change, a global timer is set. When ```DEBOUNCE_DELAY``` milliseconds of no changes has occured, all input changes are pushed.
45
46
diff --git a/keyboards/handwired/xealous/rules.mk b/keyboards/handwired/xealous/rules.mk
index 07e1c875e..7d07c9aa5 100644
--- a/keyboards/handwired/xealous/rules.mk
+++ b/keyboards/handwired/xealous/rules.mk
@@ -68,7 +68,7 @@ SUBPROJECT_rev1 = yes
68SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend 68SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
69 69
70CUSTOM_MATRIX = no 70CUSTOM_MATRIX = no
71CUSTOM_DEBOUNCE = yes 71DEBOUNCE_TYPE = custom
72 72
73LAYOUTS = split60 73LAYOUTS = split60
74 74
diff --git a/quantum/debounce.c b/quantum/debounce.c
deleted file mode 100644
index 929023ab2..000000000
--- a/quantum/debounce.c
+++ /dev/null
@@ -1,52 +0,0 @@
1
2#include "matrix.h"
3#include "timer.h"
4#include "quantum.h"
5
6#ifndef DEBOUNCING_DELAY
7# define DEBOUNCING_DELAY 5
8#endif
9
10void debounce_init(uint8_t num_rows) {
11}
12
13#if DEBOUNCING_DELAY > 0
14
15static bool debouncing = false;
16
17void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
18 static uint16_t debouncing_time;
19
20 if (changed) {
21 debouncing = true;
22 debouncing_time = timer_read();
23 }
24
25 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
26 for (uint8_t i = 0; i < num_rows; i++) {
27 cooked[i] = raw[i];
28 }
29 debouncing = false;
30 }
31}
32
33bool debounce_active(void) {
34 return debouncing;
35}
36
37#else
38
39// no debounce
40void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
41 if (changed)
42 {
43 for (uint8_t i = 0; i < num_rows; i++) {
44 cooked[i] = raw[i];
45 }
46 }
47}
48
49bool debounce_active(void) {
50 return false;
51}
52#endif
diff --git a/quantum/debounce.h b/quantum/debounce.h
index 360af77e7..9ca05c682 100644
--- a/quantum/debounce.h
+++ b/quantum/debounce.h
@@ -8,4 +8,4 @@ void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
8 8
9bool debounce_active(void); 9bool debounce_active(void);
10 10
11void debounce_init(uint8_t num_rows); \ No newline at end of file 11void debounce_init(uint8_t num_rows);
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/*
2Copyright 2017 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-key algorithm. Uses an 8-bit counter per key.
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
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
43static debounce_counter_t *debounce_counters;
44
45#define DEBOUNCE_ELAPSED 251
46#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
47
48void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
49void 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
52void 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
65void 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.
73void 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;
92void 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
117bool 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/*
2Copyright 2017 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 global debounce algorithm. Used in 99% of keyboards at time of implementation
17When 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
26void debounce_init(uint8_t num_rows) {}
27static bool debouncing = false;
28
29#if DEBOUNCE > 0
30static uint16_t debouncing_time;
31void 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.
46void 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
54bool 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 @@
1Debounce algorithms belong in this folder.
2Here are a few ideas
3
41) 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
92) 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
143) 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
19The default algorithm is symmetric and global.
20Here are a few that could be implemented:
21
22debounce_sym_g.c
23debounce_sym_pk.c
24debounce_sym_pr.c
25debounce_sym_pr_cycles.c //currently used in ergo-dox
26debounce_eager_g.c
27debounce_eager_pk.c
28debounce_eager_pr.c //could be used in ergo-dox!
diff --git a/quantum/matrix.c b/quantum/matrix.c
index d2b805d7f..f7cad1a0f 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -51,10 +51,8 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
51#endif 51#endif
52 52
53/* matrix state(1:on, 0:off) */ 53/* matrix state(1:on, 0:off) */
54static matrix_row_t raw_matrix[MATRIX_ROWS]; 54static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
55 55static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
56static matrix_row_t matrix[MATRIX_ROWS];
57
58 56
59#if (DIODE_DIRECTION == COL2ROW) 57#if (DIODE_DIRECTION == COL2ROW)
60 static void init_cols(void); 58 static void init_cols(void);
@@ -108,30 +106,6 @@ uint8_t matrix_cols(void) {
108 return MATRIX_COLS; 106 return MATRIX_COLS;
109} 107}
110 108
111// void matrix_power_up(void) {
112// #if (DIODE_DIRECTION == COL2ROW)
113// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
114// /* DDRxn */
115// _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
116// toggle_row(r);
117// }
118// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
119// /* PORTxn */
120// _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
121// }
122// #elif (DIODE_DIRECTION == ROW2COL)
123// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
124// /* DDRxn */
125// _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
126// toggle_col(c);
127// }
128// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
129// /* PORTxn */
130// _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
131// }
132// #endif
133// }
134
135void matrix_init(void) { 109void matrix_init(void) {
136 110
137 // initialize row and col 111 // initialize row and col
@@ -175,6 +149,7 @@ uint8_t matrix_scan(void)
175 return 1; 149 return 1;
176} 150}
177 151
152//Deprecated.
178bool matrix_is_modified(void) 153bool matrix_is_modified(void)
179{ 154{
180 if (debounce_active()) return false; 155 if (debounce_active()) return false;
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index 15652276b..849d74aa1 100644
--- a/tmk_core/common/keyboard.c
+++ b/tmk_core/common/keyboard.c
@@ -252,9 +252,6 @@ void keyboard_init(void) {
252void keyboard_task(void) 252void keyboard_task(void)
253{ 253{
254 static matrix_row_t matrix_prev[MATRIX_ROWS]; 254 static matrix_row_t matrix_prev[MATRIX_ROWS];
255#ifdef MATRIX_HAS_GHOST
256 // static matrix_row_t matrix_ghost[MATRIX_ROWS];
257#endif
258 static uint8_t led_status = 0; 255 static uint8_t led_status = 0;
259 matrix_row_t matrix_row = 0; 256 matrix_row_t matrix_row = 0;
260 matrix_row_t matrix_change = 0; 257 matrix_row_t matrix_change = 0;
@@ -263,24 +260,14 @@ void keyboard_task(void)
263#endif 260#endif
264 261
265 matrix_scan(); 262 matrix_scan();
263
266 if (is_keyboard_master()) { 264 if (is_keyboard_master()) {
267 for (uint8_t r = 0; r < MATRIX_ROWS; r++) { 265 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
268 matrix_row = matrix_get_row(r); 266 matrix_row = matrix_get_row(r);
269 matrix_change = matrix_row ^ matrix_prev[r]; 267 matrix_change = matrix_row ^ matrix_prev[r];
270 if (matrix_change) { 268 if (matrix_change) {
271#ifdef MATRIX_HAS_GHOST 269#ifdef MATRIX_HAS_GHOST
272 if (has_ghost_in_row(r, matrix_row)) { 270 if (has_ghost_in_row(r, matrix_row)) { continue; }
273 /* Keep track of whether ghosted status has changed for
274 * debugging. But don't update matrix_prev until un-ghosted, or
275 * the last key would be lost.
276 */
277 //if (debug_matrix && matrix_ghost[r] != matrix_row) {
278 // matrix_print();
279 //}
280 //matrix_ghost[r] = matrix_row;
281 continue;
282 }
283 //matrix_ghost[r] = matrix_row;
284#endif 271#endif
285 if (debug_matrix) matrix_print(); 272 if (debug_matrix) matrix_print();
286 for (uint8_t c = 0; c < MATRIX_COLS; c++) { 273 for (uint8_t c = 0; c < MATRIX_COLS; c++) {