aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfauxpark <fauxpark@gmail.com>2020-01-18 15:01:17 +1100
committerDrashna Jaelre <drashna@live.com>2020-01-17 20:01:17 -0800
commit055e940f0653ab057f3b95053f1be44c44799664 (patch)
tree99e9c89a075cf3c709bc7a4e5cb973afbaf90965
parent3dd43d9cabce8f2b749cf439f9dae5123800d37f (diff)
downloadqmk_firmware-055e940f0653ab057f3b95053f1be44c44799664.tar.gz
qmk_firmware-055e940f0653ab057f3b95053f1be44c44799664.zip
[Keyboard] Wasdat matrix cleanup (#7909)
* Wasdat matrix cleanup * Use _custom functions * More deduping, and signature fix
-rw-r--r--keyboards/maartenwut/wasdat/matrix.c246
-rw-r--r--keyboards/maartenwut/wasdat/rules.mk2
2 files changed, 54 insertions, 194 deletions
diff --git a/keyboards/maartenwut/wasdat/matrix.c b/keyboards/maartenwut/wasdat/matrix.c
index 04d221971..6dd79b533 100644
--- a/keyboards/maartenwut/wasdat/matrix.c
+++ b/keyboards/maartenwut/wasdat/matrix.c
@@ -17,34 +17,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
17#include <stdint.h> 17#include <stdint.h>
18#include <stdbool.h> 18#include <stdbool.h>
19#include "wait.h" 19#include "wait.h"
20#include "print.h"
21#include "debug.h"
22#include "util.h" 20#include "util.h"
23#include "matrix.h" 21#include "matrix.h"
24#include "debounce.h" 22#include "debounce.h"
25#include "quantum.h" 23#include "quantum.h"
26 24
27#if (MATRIX_COLS <= 8)
28# define print_matrix_header() print("\nr/c 01234567\n")
29# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
30# define matrix_bitpop(i) bitpop(matrix[i])
31# define ROW_SHIFTER ((uint8_t)1)
32#elif (MATRIX_COLS <= 16)
33# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
34# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
35# define matrix_bitpop(i) bitpop16(matrix[i])
36# define ROW_SHIFTER ((uint16_t)1)
37#elif (MATRIX_COLS <= 32)
38# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
39# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
40# define matrix_bitpop(i) bitpop32(matrix[i])
41# define ROW_SHIFTER ((uint32_t)1)
42#endif
43
44#ifdef MATRIX_MASKED
45 extern const matrix_row_t matrix_mask[];
46#endif
47
48#ifdef DIRECT_PINS 25#ifdef DIRECT_PINS
49static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; 26static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
50#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) 27#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
@@ -52,150 +29,58 @@ static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
52//static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 29//static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
53#endif 30#endif
54 31
55/* matrix state(1:on, 0:off) */ 32// matrix code
56static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
57static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
58
59__attribute__ ((weak))
60void matrix_init_quantum(void) {
61 matrix_init_kb();
62}
63
64__attribute__ ((weak))
65void matrix_scan_quantum(void) {
66 matrix_scan_kb();
67}
68
69__attribute__ ((weak))
70void matrix_init_kb(void) {
71 matrix_init_user();
72}
73
74__attribute__ ((weak))
75void matrix_scan_kb(void) {
76 matrix_scan_user();
77}
78
79__attribute__ ((weak))
80void matrix_init_user(void) {
81}
82
83__attribute__ ((weak))
84void matrix_scan_user(void) {
85}
86
87inline
88uint8_t matrix_rows(void) {
89 return MATRIX_ROWS;
90}
91
92inline
93uint8_t matrix_cols(void) {
94 return MATRIX_COLS;
95}
96
97//Deprecated.
98bool matrix_is_modified(void)
99{
100 if (debounce_active()) return false;
101 return true;
102}
103
104inline
105bool matrix_is_on(uint8_t row, uint8_t col)
106{
107 return (matrix[row] & ((matrix_row_t)1<<col));
108}
109
110inline
111matrix_row_t matrix_get_row(uint8_t row)
112{
113 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
114 // switch blocker installed and the switch is always pressed.
115#ifdef MATRIX_MASKED
116 return matrix[row] & matrix_mask[row];
117#else
118 return matrix[row];
119#endif
120}
121
122void matrix_print(void)
123{
124 print_matrix_header();
125
126 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
127 phex(row); print(": ");
128 print_matrix_row(row);
129 print("\n");
130 }
131}
132
133uint8_t matrix_key_count(void)
134{
135 uint8_t count = 0;
136 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137 count += matrix_bitpop(i);
138 }
139 return count;
140}
141
142 33
143#ifdef DIRECT_PINS 34#ifdef DIRECT_PINS
144 35
145static void init_pins(void) { 36static void init_pins(void) {
146 for (int row = 0; row < MATRIX_ROWS; row++) { 37 for (int row = 0; row < MATRIX_ROWS; row++) {
147 for (int col = 0; col < MATRIX_COLS; col++) { 38 for (int col = 0; col < MATRIX_COLS; col++) {
148 pin_t pin = direct_pins[row][col]; 39 pin_t pin = direct_pins[row][col];
149 if (pin != NO_PIN) { 40 if (pin != NO_PIN) {
150 setPinInputHigh(pin); 41 setPinInputHigh(pin);
151 } 42 }
43 }
152 } 44 }
153 }
154} 45}
155 46
156static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 47static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
157 matrix_row_t last_row_value = current_matrix[current_row]; 48 matrix_row_t last_row_value = current_matrix[current_row];
158 current_matrix[current_row] = 0; 49 current_matrix[current_row] = 0;
159 50
160 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 51 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
161 pin_t pin = direct_pins[current_row][col_index]; 52 pin_t pin = direct_pins[current_row][col_index];
162 if (pin != NO_PIN) { 53 if (pin != NO_PIN) {
163 current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index); 54 current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
55 }
164 } 56 }
165 }
166 57
167 return (last_row_value != current_matrix[current_row]); 58 return (last_row_value != current_matrix[current_row]);
168} 59}
169 60
170#elif (DIODE_DIRECTION == COL2ROW) 61#elif (DIODE_DIRECTION == COL2ROW)
171 62
172static void select_row(uint8_t row) 63static void select_row(uint8_t row) {
173{
174 setPinOutput(row_pins[row]); 64 setPinOutput(row_pins[row]);
175 writePinLow(row_pins[row]); 65 writePinLow(row_pins[row]);
176} 66}
177 67
178static void unselect_row(uint8_t row) 68static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
179{
180 setPinInputHigh(row_pins[row]);
181}
182 69
183static void unselect_rows(void) 70static void unselect_rows(void) {
184{ 71 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
185 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
186 setPinInputHigh(row_pins[x]); 72 setPinInputHigh(row_pins[x]);
187 } 73 }
188} 74}
189 75
190static void init_pins(void) { 76static void init_pins(void) {
191 unselect_rows(); 77 unselect_rows();
192 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 78 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
193 setPinInputHigh(col_pins[x]); 79 setPinInputHigh(col_pins[x]);
194 } 80 }
195} 81}
196 82
197static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 83static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
198{
199 // Store last value of row prior to reading 84 // Store last value of row prior to reading
200 matrix_row_t last_row_value = current_matrix[current_row]; 85 matrix_row_t last_row_value = current_matrix[current_row];
201 86
@@ -207,13 +92,13 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
207 wait_us(30); 92 wait_us(30);
208 93
209 // For each col... 94 // For each col...
210 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 95 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
211 96
212 // Select the col pin to read (active low) 97 // Select the col pin to read (active low)
213 uint8_t pin_state = readPin(col_pins[col_index]); 98 uint8_t pin_state = readPin(col_pins[col_index]);
214 99
215 // Populate the matrix row with the state of the col pin 100 // Populate the matrix row with the state of the col pin
216 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 101 current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
217 } 102 }
218 103
219 // Unselect row 104 // Unselect row
@@ -245,8 +130,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
245 * col 14: B7 130 * col 14: B7
246 * col 15: B3 131 * col 15: B3
247 */ 132 */
248static void select_col(uint8_t col) 133static void select_col(uint8_t col) {
249{
250 switch (col) { 134 switch (col) {
251 case 0: 135 case 0:
252 writePinLow(C7); 136 writePinLow(C7);
@@ -304,8 +188,7 @@ static void select_col(uint8_t col)
304 } 188 }
305} 189}
306 190
307static void unselect_col(uint8_t col) 191static void unselect_col(uint8_t col) {
308{
309 switch (col) { 192 switch (col) {
310 case 0: 193 case 0:
311 writePinHigh(C7); 194 writePinHigh(C7);
@@ -363,8 +246,7 @@ static void unselect_col(uint8_t col)
363 } 246 }
364} 247}
365 248
366static void unselect_cols(void) 249static void unselect_cols(void) {
367{
368 //Native 250 //Native
369 setPinOutput(D3); 251 setPinOutput(D3);
370 setPinOutput(D7); 252 setPinOutput(D7);
@@ -397,14 +279,13 @@ static void unselect_cols(void)
397} 279}
398 280
399static void init_pins(void) { 281static void init_pins(void) {
400 unselect_cols(); 282 unselect_cols();
401 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 283 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
402 setPinInputHigh(row_pins[x]); 284 setPinInputHigh(row_pins[x]);
403 } 285 }
404} 286}
405 287
406static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 288static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
407{
408 bool matrix_changed = false; 289 bool matrix_changed = false;
409 290
410 // Select col and wait for col selecton to stabilize 291 // Select col and wait for col selecton to stabilize
@@ -412,27 +293,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
412 wait_us(30); 293 wait_us(30);
413 294
414 // For each row... 295 // For each row...
415 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) 296 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
416 {
417
418 // Store last value of row prior to reading 297 // Store last value of row prior to reading
419 matrix_row_t last_row_value = current_matrix[row_index]; 298 matrix_row_t last_row_value = current_matrix[row_index];
420 299
421 // Check row pin state 300 // Check row pin state
422 if (readPin(row_pins[row_index]) == 0) 301 if (readPin(row_pins[row_index]) == 0) {
423 {
424 // Pin LO, set col bit 302 // Pin LO, set col bit
425 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 303 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
426 } 304 } else {
427 else
428 {
429 // Pin HI, clear col bit 305 // Pin HI, clear col bit
430 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 306 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
431 } 307 }
432 308
433 // Determine if the matrix changed state 309 // Determine if the matrix changed state
434 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) 310 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
435 {
436 matrix_changed = true; 311 matrix_changed = true;
437 } 312 }
438 } 313 }
@@ -445,40 +320,25 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
445 320
446#endif 321#endif
447 322
448void matrix_init(void) { 323void matrix_init_custom(void) {
449
450 // initialize key pins 324 // initialize key pins
451 init_pins(); 325 init_pins();
452
453 // initialize matrix state: all keys off
454 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
455 raw_matrix[i] = 0;
456 matrix[i] = 0;
457 }
458
459 debounce_init(MATRIX_ROWS);
460
461 matrix_init_quantum();
462} 326}
463 327
464uint8_t matrix_scan(void) 328bool matrix_scan_custom(matrix_row_t current_matrix[]) {
465{ 329 bool changed = false;
466 bool changed = false;
467 330
468#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) 331#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
469 // Set row, read cols 332 // Set row, read cols
470 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 333 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
471 changed |= read_cols_on_row(raw_matrix, current_row); 334 changed |= read_cols_on_row(current_matrix, current_row);
472 } 335 }
473#elif (DIODE_DIRECTION == ROW2COL) 336#elif (DIODE_DIRECTION == ROW2COL)
474 // Set col, read rows 337 // Set col, read rows
475 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 338 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
476 changed |= read_rows_on_col(raw_matrix, current_col); 339 changed |= read_rows_on_col(current_matrix, current_col);
477 } 340 }
478#endif 341#endif
479 342
480 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 343 return changed;
481
482 matrix_scan_quantum();
483 return (uint8_t)changed;
484} 344}
diff --git a/keyboards/maartenwut/wasdat/rules.mk b/keyboards/maartenwut/wasdat/rules.mk
index 136c7bce2..db9728623 100644
--- a/keyboards/maartenwut/wasdat/rules.mk
+++ b/keyboards/maartenwut/wasdat/rules.mk
@@ -31,7 +31,7 @@ AUDIO_ENABLE = no # Audio output on port C6
31FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches 31FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches
32HD44780_ENABLE = no # Enable support for HD44780 based LCDs 32HD44780_ENABLE = no # Enable support for HD44780 based LCDs
33 33
34CUSTOM_MATRIX = yes 34CUSTOM_MATRIX = lite
35SRC += matrix.c 35SRC += matrix.c
36 36
37LAYOUTS = fullsize_ansi fullsize_iso tkl_ansi tkl_iso 37LAYOUTS = fullsize_ansi fullsize_iso tkl_ansi tkl_iso