aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authoralex-ong <the.onga@gmail.com>2019-01-26 17:03:46 +1100
committeralex-ong <the.onga@gmail.com>2019-01-26 17:03:46 +1100
commitd0b691df0ee74863ca54ca697aa4d4212cf401a7 (patch)
treef584ae6d23347b7256a679fd3eecd653744b6187 /quantum
parent3949ab322dcce75f470ddfbe701c8763add69e63 (diff)
downloadqmk_firmware-d0b691df0ee74863ca54ca697aa4d4212cf401a7.tar.gz
qmk_firmware-d0b691df0ee74863ca54ca697aa4d4212cf401a7.zip
DO NOT USE - debounce successfully compiled.
Diffstat (limited to 'quantum')
-rw-r--r--quantum/debounce.c52
-rw-r--r--quantum/debounce.h25
-rw-r--r--quantum/debounce/debounce_eager_pk.c123
-rw-r--r--quantum/debounce/debounce_sym_g.c58
-rw-r--r--quantum/debounce/readme.md28
-rw-r--r--quantum/matrix.c9
6 files changed, 233 insertions, 62 deletions
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..7fe2d693d 100644
--- a/quantum/debounce.h
+++ b/quantum/debounce.h
@@ -1,11 +1,26 @@
1#pragma once 1#pragma once
2/*
3Copyright 2017 Alex Ong<the.onga@gmail.com>
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12You should have received a copy of the GNU General Public License
13along with this program. If not, see <http://www.gnu.org/licenses/>.
14*/
15
16
17#include "matrix.h"
18
19void debounce_init(void); //every debounce algorithm will have unique storage needs.
2 20
3// raw is the current key state 21// raw is the current key state
4// on entry cooked is the previous debounced state 22// cooked is the debounced input/output key state
5// on exit cooked is the current debounced state
6// changed is true if raw has changed since the last call 23// changed is true if raw has changed since the last call
7void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed); 24void debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed);
8 25
9bool debounce_active(void); 26bool debounce_active(void);
10
11void debounce_init(uint8_t num_rows); \ No newline at end of file
diff --git a/quantum/debounce/debounce_eager_pk.c b/quantum/debounce/debounce_eager_pk.c
new file mode 100644
index 000000000..e6e8bfd31
--- /dev/null
+++ b/quantum/debounce/debounce_eager_pk.c
@@ -0,0 +1,123 @@
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
121
122
123//Implementation of no debounce.
diff --git a/quantum/debounce/debounce_sym_g.c b/quantum/debounce/debounce_sym_g.c
new file mode 100644
index 000000000..c206f2864
--- /dev/null
+++ b/quantum/debounce/debounce_sym_g.c
@@ -0,0 +1,58 @@
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
25
26static bool debouncing = false;
27static uint16_t debouncing_time;
28
29void debounce_init(void) {}
30
31#if DEBOUNCE > 0
32void debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed)
33{
34 if (changed) {
35 debouncing = true;
36 debouncing_time = timer_read();
37 }
38
39 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
40 for (int i = 0; i < MATRIX_ROWS; i++) {
41 cooked[i] = raw[i];
42 }
43 debouncing = false;
44 }
45}
46#else //no debouncing.
47void debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed)
48{
49 for (int i = 0; i < MATRIX_ROWS; i++) {
50 cooked[i] = raw[i];
51 }
52}
53#endif
54
55bool debounce_active() {
56 return debouncing;
57}
58
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 71292db51..8fc4175bd 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -51,9 +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 56
58#if (DIODE_DIRECTION == COL2ROW) 57#if (DIODE_DIRECTION == COL2ROW)
59 static void init_cols(void); 58 static void init_cols(void);
@@ -123,7 +122,7 @@ void matrix_init(void) {
123 raw_matrix[i] = 0; 122 raw_matrix[i] = 0;
124 matrix[i] = 0; 123 matrix[i] = 0;
125 } 124 }
126 debounce_init(MATRIX_ROWS); 125 debounce_init();
127 126
128 matrix_init_quantum(); 127 matrix_init_quantum();
129} 128}
@@ -144,7 +143,7 @@ uint8_t matrix_scan(void)
144 } 143 }
145#endif 144#endif
146 145
147 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 146 debounce(raw_matrix, matrix, changed);
148 147
149 matrix_scan_quantum(); 148 matrix_scan_quantum();
150 return 1; 149 return 1;