aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ong <alex.ong@unsw.edu.au>2018-08-29 10:19:36 +1000
committerAlex Ong <alex.ong@unsw.edu.au>2018-08-29 10:19:36 +1000
commit3cf7f7322c24e3cab21d402f1a859b60df857603 (patch)
tree08e5f119b2faa8c393dbbad7c085fe83728046c3
parent9bd6d6112d698ea5823b268983809fe3b8d98b26 (diff)
downloadqmk_firmware-3cf7f7322c24e3cab21d402f1a859b60df857603.tar.gz
qmk_firmware-3cf7f7322c24e3cab21d402f1a859b60df857603.zip
Removed "debounce_algo = manual" in all keyboards with CUSTOM_MATRIX = yes.
-rw-r--r--quantum/matrix.c79
-rw-r--r--tmk_core/common.mk17
-rw-r--r--tmk_core/common/debounce.c22
-rw-r--r--tmk_core/common/debounce.h21
-rw-r--r--tmk_core/common/debounce/debounce_eager_pk.c124
-rw-r--r--tmk_core/common/debounce/debounce_sym_g.c59
-rw-r--r--tmk_core/common/debounce/readme.md28
-rw-r--r--tmk_core/common/keyboard.c22
8 files changed, 279 insertions, 93 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 3600d4e7b..bc7eb6b58 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -27,17 +27,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27#include "timer.h" 27#include "timer.h"
28 28
29 29
30/* Set 0 if debouncing isn't needed */
31
32#ifndef DEBOUNCING_DELAY
33# define DEBOUNCING_DELAY 5
34#endif
35
36#if (DEBOUNCING_DELAY > 0)
37 static uint16_t debouncing_time;
38 static bool debouncing = false;
39#endif
40
41#if (MATRIX_COLS <= 8) 30#if (MATRIX_COLS <= 8)
42# define print_matrix_header() print("\nr/c 01234567\n") 31# define print_matrix_header() print("\nr/c 01234567\n")
43# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 32# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
@@ -67,8 +56,6 @@ static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
67/* matrix state(1:on, 0:off) */ 56/* matrix state(1:on, 0:off) */
68static matrix_row_t matrix[MATRIX_ROWS]; 57static matrix_row_t matrix[MATRIX_ROWS];
69 58
70static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71
72 59
73#if (DIODE_DIRECTION == COL2ROW) 60#if (DIODE_DIRECTION == COL2ROW)
74 static void init_cols(void); 61 static void init_cols(void);
@@ -122,30 +109,6 @@ uint8_t matrix_cols(void) {
122 return MATRIX_COLS; 109 return MATRIX_COLS;
123} 110}
124 111
125// void matrix_power_up(void) {
126// #if (DIODE_DIRECTION == COL2ROW)
127// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
128// /* DDRxn */
129// _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
130// toggle_row(r);
131// }
132// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
133// /* PORTxn */
134// _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
135// }
136// #elif (DIODE_DIRECTION == ROW2COL)
137// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
138// /* DDRxn */
139// _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
140// toggle_col(c);
141// }
142// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
143// /* PORTxn */
144// _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
145// }
146// #endif
147// }
148
149void matrix_init(void) { 112void matrix_init(void) {
150 113
151 // initialize row and col 114 // initialize row and col
@@ -160,7 +123,6 @@ void matrix_init(void) {
160 // initialize matrix state: all keys off 123 // initialize matrix state: all keys off
161 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 124 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
162 matrix[i] = 0; 125 matrix[i] = 0;
163 matrix_debouncing[i] = 0;
164 } 126 }
165 127
166 matrix_init_quantum(); 128 matrix_init_quantum();
@@ -170,59 +132,24 @@ uint8_t matrix_scan(void)
170{ 132{
171 133
172#if (DIODE_DIRECTION == COL2ROW) 134#if (DIODE_DIRECTION == COL2ROW)
173
174 // Set row, read cols 135 // Set row, read cols
175 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 136 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
176# if (DEBOUNCING_DELAY > 0) 137 read_cols_on_row(matrix, current_row);
177 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
178
179 if (matrix_changed) {
180 debouncing = true;
181 debouncing_time = timer_read();
182 }
183
184# else
185 read_cols_on_row(matrix, current_row);
186# endif
187
188 } 138 }
189
190#elif (DIODE_DIRECTION == ROW2COL) 139#elif (DIODE_DIRECTION == ROW2COL)
191
192 // Set col, read rows 140 // Set col, read rows
193 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 141 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
194# if (DEBOUNCING_DELAY > 0) 142 read_rows_on_col(matrix, current_col);
195 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
196 if (matrix_changed) {
197 debouncing = true;
198 debouncing_time = timer_read();
199 }
200# else
201 read_rows_on_col(matrix, current_col);
202# endif
203
204 } 143 }
205
206#endif 144#endif
207 145
208# if (DEBOUNCING_DELAY > 0)
209 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
210 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
211 matrix[i] = matrix_debouncing[i];
212 }
213 debouncing = false;
214 }
215# endif
216
217 matrix_scan_quantum(); 146 matrix_scan_quantum();
218 return 1; 147 return 1;
219} 148}
220 149
150//Deprecated.
221bool matrix_is_modified(void) 151bool matrix_is_modified(void)
222{ 152{
223#if (DEBOUNCING_DELAY > 0)
224 if (debouncing) return false;
225#endif
226 return true; 153 return true;
227} 154}
228 155
diff --git a/tmk_core/common.mk b/tmk_core/common.mk
index 3e407f157..72b2d3cc8 100644
--- a/tmk_core/common.mk
+++ b/tmk_core/common.mk
@@ -1,4 +1,5 @@
1COMMON_DIR = common 1COMMON_DIR = common
2DEBOUNCE = $(COMMON_DIR)/debounce
2ifeq ($(PLATFORM),AVR) 3ifeq ($(PLATFORM),AVR)
3 PLATFORM_COMMON_DIR = $(COMMON_DIR)/avr 4 PLATFORM_COMMON_DIR = $(COMMON_DIR)/avr
4else ifeq ($(PLATFORM),CHIBIOS) 5else ifeq ($(PLATFORM),CHIBIOS)
@@ -9,6 +10,7 @@ endif
9 10
10TMK_COMMON_SRC += $(COMMON_DIR)/host.c \ 11TMK_COMMON_SRC += $(COMMON_DIR)/host.c \
11 $(COMMON_DIR)/keyboard.c \ 12 $(COMMON_DIR)/keyboard.c \
13 $(COMMON_DIR)/debounce.c \
12 $(COMMON_DIR)/action.c \ 14 $(COMMON_DIR)/action.c \
13 $(COMMON_DIR)/action_tapping.c \ 15 $(COMMON_DIR)/action_tapping.c \
14 $(COMMON_DIR)/action_macro.c \ 16 $(COMMON_DIR)/action_macro.c \
@@ -39,7 +41,20 @@ ifeq ($(PLATFORM),TEST)
39 TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom.c 41 TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom.c
40endif 42endif
41 43
42 44# Debounce Modules. If implemented in matrix.c, don't use these.
45ifeq ($(strip $(CUSTOM_MATRIX)), yes)
46 # Do nothing. Custom matrix code.
47else ifeq ($(strip $(SPLIT_KEYBOARD)), yes)
48 # Do nothing, debouncing is inside matrix.c inside split_common
49else ifeq ($(strip $(DEBOUNCE_ALGO)), manual)
50 # Do nothing. do your debouncing in matrix.c
51else ifeq ($(strip $(DEBOUNCE_ALGO)), sym_g)
52 TMK_COMMON_SRC += $(DEBOUNCE)/debounce_sym_g.c
53else ifeq ($(strip $(DEBOUNCE_ALGO)), eager_pk)
54 TMK_COMMON_SRC += $(DEBOUNCE)/debounce_eager_pk.c
55else # default algorithm
56 TMK_COMMON_SRC += $(DEBOUNCE)/debounce_sym_g.c
57endif
43 58
44# Option modules 59# Option modules
45ifeq ($(strip $(BOOTMAGIC_ENABLE)), yes) 60ifeq ($(strip $(BOOTMAGIC_ENABLE)), yes)
diff --git a/tmk_core/common/debounce.c b/tmk_core/common/debounce.c
new file mode 100644
index 000000000..406d2d0f3
--- /dev/null
+++ b/tmk_core/common/debounce.c
@@ -0,0 +1,22 @@
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#include "debounce.h"
15#include "matrix.h"
16//Default implementation - no debouncing
17__attribute__((weak)) void matrix_debounce_init(void) {}
18__attribute__((weak)) void matrix_debounce(void) {}
19__attribute__((weak)) matrix_row_t matrix_debounce_get_row(uint8_t row)
20{
21 return matrix_get_row(row);
22}
diff --git a/tmk_core/common/debounce.h b/tmk_core/common/debounce.h
new file mode 100644
index 000000000..fe3effab3
--- /dev/null
+++ b/tmk_core/common/debounce.h
@@ -0,0 +1,21 @@
1#ifndef DEBOUNCE_H
2#define DEBOUNCE_H
3#include <stdbool.h>
4#include <stdint.h>
5#include "matrix.h"
6#ifdef __cplusplus
7extern "C" {
8#endif
9 /* called to initialize any data stores your implementation has*/
10 void matrix_debounce_init(void);
11 /* call this every keyboard_task to debounce the matrix*/
12 void matrix_debounce(void);
13 /* matrix state on row */
14 matrix_row_t matrix_debounce_get_row(uint8_t row);
15 /* whether a switch is on */
16 bool matrix_debounce_is_on(uint8_t row, uint8_t col);
17
18#ifdef __cplusplus
19}
20#endif
21#endif
diff --git a/tmk_core/common/debounce/debounce_eager_pk.c b/tmk_core/common/debounce/debounce_eager_pk.c
new file mode 100644
index 000000000..984da0570
--- /dev/null
+++ b/tmk_core/common/debounce/debounce_eager_pk.c
@@ -0,0 +1,124 @@
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 "debounce.h"
22#include "matrix.h"
23#include "timer.h"
24
25#ifndef DEBOUNCE
26 #define DEBOUNCE 5
27#endif
28
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
39
40#define debounce_counter_t uint8_t
41
42static matrix_row_t matrix_debounced[MATRIX_ROWS];
43static debounce_counter_t debounce_counters[MATRIX_ROWS*MATRIX_COLS];
44
45#define DEBOUNCE_ELAPSED 251
46#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
47
48void update_debounce_counters(uint8_t current_time);
49void transfer_matrix_values(uint8_t current_time);
50
51void matrix_debounce_init(void)
52{
53 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
54 {
55 matrix_debounced[r] = 0;
56 }
57
58 int i = 0;
59 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
60 {
61 for (uint8_t c = 0; c < MATRIX_COLS; c++)
62 {
63 debounce_counters[i++] = DEBOUNCE_ELAPSED;
64 }
65 }
66}
67
68void matrix_debounce(void)
69{
70 uint8_t current_time = timer_read() % MAX_DEBOUNCE;
71 update_debounce_counters(current_time);
72 transfer_matrix_values(current_time);
73}
74
75//If the current time is > debounce counter, set the counter to enable input.
76void update_debounce_counters(uint8_t current_time)
77{
78 debounce_counter_t *debounce_pointer = debounce_counters;
79 for (uint8_t row = 0; row < MATRIX_ROWS; row++)
80 {
81 for (uint8_t col = 0; col < MATRIX_COLS; col++)
82 {
83 if (*debounce_pointer != DEBOUNCE_ELAPSED)
84 {
85 if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >=
86 DEBOUNCING_DELAY) {
87 *debounce_pointer = DEBOUNCE_ELAPSED;
88 }
89 }
90 debounce_pointer++;
91 }
92 }
93}
94
95// upload from raw_matrix to final matrix;
96void transfer_matrix_values(uint8_t current_time)
97{
98 debounce_counter_t *debounce_pointer = debounce_counters;
99 for (uint8_t row = 0; row < MATRIX_ROWS; row++)
100 {
101 matrix_row_t existing_row = matrix_debounced[row];
102 matrix_row_t raw_row = matrix_get_row(row);
103
104 for (uint8_t col = 0; col < MATRIX_COLS; col++)
105 {
106 matrix_row_t col_mask = (ROW_SHIFTER << col);
107 bool final_value = raw_row & col_mask;
108 bool existing_value = existing_row & col_mask;
109 if (*debounce_pointer == DEBOUNCE_ELAPSED &&
110 (existing_value != final_value))
111 {
112 *debounce_pointer = current_time;
113 existing_row ^= col_mask; //flip the bit.
114 }
115 debounce_pointer++;
116 }
117 matrix_debounced[row] = existing_row;
118 }
119}
120
121matrix_row_t matrix_debounce_get_row(uint8_t row)
122{
123 return matrix_debounced[row];
124}
diff --git a/tmk_core/common/debounce/debounce_sym_g.c b/tmk_core/common/debounce/debounce_sym_g.c
new file mode 100644
index 000000000..2fb7a589d
--- /dev/null
+++ b/tmk_core/common/debounce/debounce_sym_g.c
@@ -0,0 +1,59 @@
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 "debounce.h"
20#include "matrix.h"
21#include "timer.h"
22#ifndef DEBOUNCE
23 #define DEBOUNCE 5
24#endif
25static matrix_row_t matrix_debounced[MATRIX_ROWS];
26static bool debouncing = false;
27static uint16_t debouncing_time;
28
29void matrix_debounce_init(void)
30{
31 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
32 {
33 matrix_debounced[r] = 0;
34 }
35}
36
37void matrix_debounce(void)
38{
39 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
40 {
41 matrix_row_t raw = matrix_get_row(r);
42 if (raw != matrix_debounced[r])
43 {
44 debouncing = true;
45 debouncing_time = timer_read();
46 }
47 }
48 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
49 for (int i = 0; i < MATRIX_ROWS; i++) {
50 matrix_debounced[i] = matrix_get_row(i);
51 }
52 debouncing = false;
53 }
54}
55
56matrix_row_t matrix_debounce_get_row(uint8_t row)
57{
58 return matrix_debounced[row];
59}
diff --git a/tmk_core/common/debounce/readme.md b/tmk_core/common/debounce/readme.md
new file mode 100644
index 000000000..1a77d44df
--- /dev/null
+++ b/tmk_core/common/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/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index 13b3cb4c0..fe626efb3 100644
--- a/tmk_core/common/keyboard.c
+++ b/tmk_core/common/keyboard.c
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18#include <stdint.h> 18#include <stdint.h>
19#include "keyboard.h" 19#include "keyboard.h"
20#include "matrix.h" 20#include "matrix.h"
21#include "debounce.h"
21#include "keymap.h" 22#include "keymap.h"
22#include "host.h" 23#include "host.h"
23#include "led.h" 24#include "led.h"
@@ -157,6 +158,7 @@ void keyboard_init(void) {
157 MCUCR |= _BV(JTD); 158 MCUCR |= _BV(JTD);
158#endif 159#endif
159 matrix_init(); 160 matrix_init();
161 matrix_debounce_init();
160#ifdef PS2_MOUSE_ENABLE 162#ifdef PS2_MOUSE_ENABLE
161 ps2_mouse_init(); 163 ps2_mouse_init();
162#endif 164#endif
@@ -206,9 +208,6 @@ void keyboard_init(void) {
206void keyboard_task(void) 208void keyboard_task(void)
207{ 209{
208 static matrix_row_t matrix_prev[MATRIX_ROWS]; 210 static matrix_row_t matrix_prev[MATRIX_ROWS];
209#ifdef MATRIX_HAS_GHOST
210 // static matrix_row_t matrix_ghost[MATRIX_ROWS];
211#endif
212 static uint8_t led_status = 0; 211 static uint8_t led_status = 0;
213 matrix_row_t matrix_row = 0; 212 matrix_row_t matrix_row = 0;
214 matrix_row_t matrix_change = 0; 213 matrix_row_t matrix_change = 0;
@@ -217,24 +216,15 @@ void keyboard_task(void)
217#endif 216#endif
218 217
219 matrix_scan(); 218 matrix_scan();
219 matrix_debounce();
220
220 if (is_keyboard_master()) { 221 if (is_keyboard_master()) {
221 for (uint8_t r = 0; r < MATRIX_ROWS; r++) { 222 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
222 matrix_row = matrix_get_row(r); 223 matrix_row = matrix_debounce_get_row(r);
223 matrix_change = matrix_row ^ matrix_prev[r]; 224 matrix_change = matrix_row ^ matrix_prev[r];
224 if (matrix_change) { 225 if (matrix_change) {
225#ifdef MATRIX_HAS_GHOST 226#ifdef MATRIX_HAS_GHOST
226 if (has_ghost_in_row(r, matrix_row)) { 227 if (has_ghost_in_row(r, matrix_row)) continue;
227 /* Keep track of whether ghosted status has changed for
228 * debugging. But don't update matrix_prev until un-ghosted, or
229 * the last key would be lost.
230 */
231 //if (debug_matrix && matrix_ghost[r] != matrix_row) {
232 // matrix_print();
233 //}
234 //matrix_ghost[r] = matrix_row;
235 continue;
236 }
237 //matrix_ghost[r] = matrix_row;
238#endif 228#endif
239 if (debug_matrix) matrix_print(); 229 if (debug_matrix) matrix_print();
240 for (uint8_t c = 0; c < MATRIX_COLS; c++) { 230 for (uint8_t c = 0; c < MATRIX_COLS; c++) {