aboutsummaryrefslogtreecommitdiff
path: root/keyboards/ergodox_ez/matrix.c
diff options
context:
space:
mode:
authorErez Zukerman <bulk@ezuk.org>2018-11-22 10:51:20 -0500
committerGitHub <noreply@github.com>2018-11-22 10:51:20 -0500
commit48262bdce0666442850125d7f0298fb5e0044b0e (patch)
tree220a0f200bfe2586c6653721f96ae6c92b7168d9 /keyboards/ergodox_ez/matrix.c
parentb7dd415c9787c4300db1c31740fa94b93c68c2ac (diff)
parentad91454574ad1f0fad73923d04d6d1e037e45fed (diff)
downloadqmk_firmware-48262bdce0666442850125d7f0298fb5e0044b0e.tar.gz
qmk_firmware-48262bdce0666442850125d7f0298fb5e0044b0e.zip
Merge pull request #4454 from trunneml/improveddebounce
Adaptive debounce logic
Diffstat (limited to 'keyboards/ergodox_ez/matrix.c')
-rw-r--r--keyboards/ergodox_ez/matrix.c49
1 files changed, 28 insertions, 21 deletions
diff --git a/keyboards/ergodox_ez/matrix.c b/keyboards/ergodox_ez/matrix.c
index 3ff1c7ab4..22837d312 100644
--- a/keyboards/ergodox_ez/matrix.c
+++ b/keyboards/ergodox_ez/matrix.c
@@ -57,6 +57,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
57 57
58/* matrix state(1:on, 0:off) */ 58/* matrix state(1:on, 0:off) */
59static matrix_row_t matrix[MATRIX_ROWS]; 59static matrix_row_t matrix[MATRIX_ROWS];
60/*
61 * matrix state(1:on, 0:off)
62 * contains the raw values without debounce filtering of the last read cycle.
63 */
64static matrix_row_t raw_matrix[MATRIX_ROWS];
60 65
61// Debouncing: store for each key the number of scans until it's eligible to 66// Debouncing: store for each key the number of scans until it's eligible to
62// change. When scanning the matrix, ignore any changes in keys that have 67// change. When scanning the matrix, ignore any changes in keys that have
@@ -118,6 +123,7 @@ void matrix_init(void)
118 // initialize matrix state: all keys off 123 // initialize matrix state: all keys off
119 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 124 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
120 matrix[i] = 0; 125 matrix[i] = 0;
126 raw_matrix[i] = 0;
121 for (uint8_t j=0; j < MATRIX_COLS; ++j) { 127 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
122 debounce_matrix[i * MATRIX_COLS + j] = 0; 128 debounce_matrix[i * MATRIX_COLS + j] = 0;
123 } 129 }
@@ -151,26 +157,30 @@ void matrix_power_up(void) {
151 157
152// Returns a matrix_row_t whose bits are set if the corresponding key should be 158// Returns a matrix_row_t whose bits are set if the corresponding key should be
153// eligible to change in this scan. 159// eligible to change in this scan.
154matrix_row_t debounce_mask(uint8_t row) { 160matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
155 matrix_row_t result = 0; 161 matrix_row_t result = 0;
156 for (uint8_t j=0; j < MATRIX_COLS; ++j) { 162 matrix_row_t change = rawcols ^ raw_matrix[row];
157 if (debounce_matrix[row * MATRIX_COLS + j]) { 163 raw_matrix[row] = rawcols;
158 --debounce_matrix[row * MATRIX_COLS + j]; 164 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
165 if (debounce_matrix[row * MATRIX_COLS + i]) {
166 --debounce_matrix[row * MATRIX_COLS + i];
159 } else { 167 } else {
160 result |= (1 << j); 168 result |= (1 << i);
161 } 169 }
162 }
163 return result;
164}
165
166// Report changed keys in the given row. Resets the debounce countdowns
167// corresponding to each set bit in 'change' to DEBOUNCE.
168void debounce_report(matrix_row_t change, uint8_t row) {
169 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
170 if (change & (1 << i)) { 170 if (change & (1 << i)) {
171 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE; 171 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
172 } 172 }
173 } 173 }
174 return result;
175}
176
177matrix_row_t debounce_read_cols(uint8_t row) {
178 // Read the row without debouncing filtering and store it for later usage.
179 matrix_row_t cols = read_cols(row);
180 // Get the Debounce mask.
181 matrix_row_t mask = debounce_mask(cols, row);
182 // debounce the row and return the result.
183 return (cols & mask) | (matrix[row] & ~mask);;
174} 184}
175 185
176uint8_t matrix_scan(void) 186uint8_t matrix_scan(void)
@@ -214,15 +224,12 @@ uint8_t matrix_scan(void)
214 select_row(i + MATRIX_ROWS_PER_SIDE); 224 select_row(i + MATRIX_ROWS_PER_SIDE);
215 // we don't need a 30us delay anymore, because selecting a 225 // we don't need a 30us delay anymore, because selecting a
216 // left-hand row requires more than 30us for i2c. 226 // left-hand row requires more than 30us for i2c.
217 matrix_row_t mask = debounce_mask(i); 227
218 matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask); 228 // grab cols from left hand
219 debounce_report(cols ^ matrix[i], i); 229 matrix[i] = debounce_read_cols(i);
220 matrix[i] = cols;
221 // grab cols from right hand 230 // grab cols from right hand
222 mask = debounce_mask(i + MATRIX_ROWS_PER_SIDE); 231 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
223 cols = (read_cols(i + MATRIX_ROWS_PER_SIDE) & mask) | (matrix[i + MATRIX_ROWS_PER_SIDE] & ~mask); 232
224 debounce_report(cols ^ matrix[i + MATRIX_ROWS_PER_SIDE], i + MATRIX_ROWS_PER_SIDE);
225 matrix[i + MATRIX_ROWS_PER_SIDE] = cols;
226 unselect_rows(); 233 unselect_rows();
227 } 234 }
228 235