aboutsummaryrefslogtreecommitdiff
path: root/quantum/matrix.c
diff options
context:
space:
mode:
authorIBNobody <protospherex@gmail.com>2016-10-29 16:12:58 -0500
committerIBNobody <protospherex@gmail.com>2016-10-29 16:12:58 -0500
commitf4030289744fc6dc82dd85c955070c0845813cc5 (patch)
tree8e61c9ea3da9d583ef4f8ac053ab1c540dcaaa16 /quantum/matrix.c
parente40c33f754a86c4dd7bd3c7b5c7efe822f2893bc (diff)
downloadqmk_firmware-f4030289744fc6dc82dd85c955070c0845813cc5.tar.gz
qmk_firmware-f4030289744fc6dc82dd85c955070c0845813cc5.zip
added fixed debounce code
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r--quantum/matrix.c143
1 files changed, 70 insertions, 73 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c
index f45b251e4..3c488b417 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -25,6 +25,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
25#include "debug.h" 25#include "debug.h"
26#include "util.h" 26#include "util.h"
27#include "matrix.h" 27#include "matrix.h"
28#include "timer.h"
29
30
31/* Set 0 if debouncing isn't needed */
32
33#ifndef DEBOUNCING_DELAY
34# define DEBOUNCING_DELAY 5
35#endif
36
37#if (DEBOUNCING_DELAY > 0)
38 static uint16_t debouncing_time;
39 static bool debouncing = false;
40#endif
28 41
29#if (MATRIX_COLS <= 8) 42#if (MATRIX_COLS <= 8)
30# define print_matrix_header() print("\nr/c 01234567\n") 43# define print_matrix_header() print("\nr/c 01234567\n")
@@ -44,15 +57,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
44#endif 57#endif
45 58
46#ifdef MATRIX_MASKED 59#ifdef MATRIX_MASKED
47extern const matrix_row_t matrix_mask[]; 60 extern const matrix_row_t matrix_mask[];
48#endif
49
50/* Set 0 if debouncing isn't needed */
51
52#ifndef DEBOUNCING_DELAY
53# define DEBOUNCING_DELAY 5
54#endif 61#endif
55static uint8_t debouncing = DEBOUNCING_DELAY;
56 62
57static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 63static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
58static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 64static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
@@ -66,13 +72,13 @@ static matrix_row_t matrix_debouncing[MATRIX_ROWS];
66 72
67#if (DIODE_DIRECTION == COL2ROW) 73#if (DIODE_DIRECTION == COL2ROW)
68 static void init_cols(void); 74 static void init_cols(void);
69 static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); 75 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
70 static void unselect_rows(void); 76 static void unselect_rows(void);
71 static void select_row(uint8_t row); 77 static void select_row(uint8_t row);
72 static void unselect_row(uint8_t row); 78 static void unselect_row(uint8_t row);
73#else // ROW2COL 79#else // ROW2COL
74 static void init_rows(void); 80 static void init_rows(void);
75 static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); 81 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
76 static void unselect_cols(void); 82 static void unselect_cols(void);
77 static void unselect_col(uint8_t col); 83 static void unselect_col(uint8_t col);
78 static void select_col(uint8_t col); 84 static void select_col(uint8_t col);
@@ -174,83 +180,56 @@ uint8_t matrix_scan(void)
174 180
175 // Set row, read cols 181 // Set row, read cols
176 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 182 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
177 read_cols_on_row(matrix, current_row); 183# if (DEBOUNCING_DELAY > 0)
178 } 184 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
179 185
180 // select_row(i); 186 if (matrix_changed) {
181 // wait_us(30); // without this wait read unstable value. 187 debouncing = true;
182 // matrix_row_t current_row = read_cols(); 188 debouncing_time = timer_read();
183 // if (matrix_debouncing[i] != current_row) { 189 }
184 // matrix_debouncing[i] = current_row; 190
185 // if (debouncing) { 191# else
186 // debug("bounce!: "); debug_hex(debouncing); debug("\n"); 192 read_cols_on_row(matrix, current_row);
187 // } 193# endif
188 // debouncing = DEBOUNCING_DELAY; 194
189 // } 195 }
190 // unselect_row(i);
191 // }
192
193 // if (debouncing) {
194 // if (--debouncing) {
195 // wait_ms(1);
196 // } else {
197 // for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
198 // matrix[i] = matrix_debouncing[i];
199 // }
200 // }
201 // }
202 196
203#else // ROW2COL 197#else // ROW2COL
204 198
205 // Set col, read rows 199 // Set col, read rows
206 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 200 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
207 read_rows_on_col(matrix, current_col); 201# if (DEBOUNCING_DELAY > 0)
208 } 202 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
203 if (matrix_changed) {
204 debouncing = true;
205 debouncing_time = timer_read();
206 }
207# else
208 read_rows_on_col(matrix, current_col);
209# endif
209 210
210 211 }
211 // for (uint8_t i = 0; i < MATRIX_COLS; i++) {
212 // select_col(i);
213 // wait_us(30); // without this wait read unstable value.
214 // matrix_col_t current_col = read_rows();
215 // if (matrix_transposed_debouncing[i] != current_col) {
216 // matrix_transposed_debouncing[i] = current_col;
217 // if (debouncing) {
218 // debug("bounce!: "); debug_hex(debouncing); debug("\n");
219 // }
220 // debouncing = DEBOUNCING_DELAY;
221 // }
222 // unselect_col(i);
223 // }
224
225 // if (debouncing) {
226 // if (--debouncing) {
227 // wait_ms(1);
228 // } else {
229 // for (uint8_t i = 0; i < MATRIX_COLS; i++) {
230 // matrix_transposed[i] = matrix_transposed_debouncing[i];
231 // }
232 // }
233 // }
234
235 // // Untranspose matrix
236 // for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
237 // matrix_row_t row = 0;
238 // for (uint8_t x = 0; x < MATRIX_COLS; x++) {
239 // row |= ((matrix_transposed[x] & (1<<y)) >> y) << x;
240 // }
241 // matrix[y] = row;
242 // }
243 212
244#endif 213#endif
245 214
215# if (DEBOUNCING_DELAY > 0)
216 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
217 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
218 matrix[i] = matrix_debouncing[i];
219 }
220 debouncing = false;
221 }
222# endif
223
246 matrix_scan_quantum(); 224 matrix_scan_quantum();
247// matrix_print();
248 return 1; 225 return 1;
249} 226}
250 227
251bool matrix_is_modified(void) 228bool matrix_is_modified(void)
252{ 229{
230#if (DEBOUNCING_DELAY > 0)
253 if (debouncing) return false; 231 if (debouncing) return false;
232#endif
254 return true; 233 return true;
255} 234}
256 235
@@ -305,8 +284,11 @@ static void init_cols(void)
305 } 284 }
306} 285}
307 286
308static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 287static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
309{ 288{
289 // Store last value of row prior to reading
290 matrix_row_t last_row_value = current_matrix[current_row];
291
310 // Clear data in matrix row 292 // Clear data in matrix row
311 current_matrix[current_row] = 0; 293 current_matrix[current_row] = 0;
312 294
@@ -327,6 +309,8 @@ static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
327 309
328 // Unselect row 310 // Unselect row
329 unselect_row(current_row); 311 unselect_row(current_row);
312
313 return (last_row_value == current_matrix[current_row]);
330} 314}
331 315
332static void select_row(uint8_t row) 316static void select_row(uint8_t row)
@@ -363,15 +347,20 @@ static void init_rows(void)
363 } 347 }
364} 348}
365 349
366static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 350static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
367{ 351{
352 bool matrix_changed = false;
368 353
369 // Select col and wait for col selecton to stabilize 354 // Select col and wait for col selecton to stabilize
370 select_col(current_col); 355 select_col(current_col);
371 wait_us(30); 356 wait_us(30);
372 357
373 // For each row... 358 // For each row...
374 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { 359 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
360 {
361
362 // Store last value of row prior to reading
363 matrix_row_t last_row_value = current_matrix[row_index];
375 364
376 // Check row pin state 365 // Check row pin state
377 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) 366 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
@@ -384,10 +373,18 @@ static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
384 // Pin HI, clear col bit 373 // Pin HI, clear col bit
385 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 374 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
386 } 375 }
376
377 // Determine if the matrix changed state
378 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
379 {
380 matrix_changed = true;
381 }
387 } 382 }
388 383
389 // Unselect col 384 // Unselect col
390 unselect_col(current_col); 385 unselect_col(current_col);
386
387 return matrix_changed;
391} 388}
392 389
393static void select_col(uint8_t col) 390static void select_col(uint8_t col)