]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
Merge branch 'master' into debounce_refactor
[qmk_firmware.git] / quantum / matrix.c
1 /*
2 Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include "wait.h"
20 #include "print.h"
21 #include "debug.h"
22 #include "util.h"
23 #include "matrix.h"
24 #include "timer.h"
25 #include "quantum.h"
26
27
28 #if (MATRIX_COLS <= 8)
29 #    define print_matrix_header()  print("\nr/c 01234567\n")
30 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
31 #    define matrix_bitpop(i)       bitpop(matrix[i])
32 #    define ROW_SHIFTER ((uint8_t)1)
33 #elif (MATRIX_COLS <= 16)
34 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
35 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
36 #    define matrix_bitpop(i)       bitpop16(matrix[i])
37 #    define ROW_SHIFTER ((uint16_t)1)
38 #elif (MATRIX_COLS <= 32)
39 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
40 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
41 #    define matrix_bitpop(i)       bitpop32(matrix[i])
42 #    define ROW_SHIFTER  ((uint32_t)1)
43 #endif
44
45 #ifdef MATRIX_MASKED
46     extern const matrix_row_t matrix_mask[];
47 #endif
48
49 #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
50 static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
51 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
52 #endif
53
54 /* matrix state(1:on, 0:off) */
55 static matrix_row_t matrix[MATRIX_ROWS];
56
57
58 #if (DIODE_DIRECTION == COL2ROW)
59     static void init_cols(void);
60     static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
61     static void unselect_rows(void);
62     static void select_row(uint8_t row);
63     static void unselect_row(uint8_t row);
64 #elif (DIODE_DIRECTION == ROW2COL)
65     static void init_rows(void);
66     static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
67     static void unselect_cols(void);
68     static void unselect_col(uint8_t col);
69     static void select_col(uint8_t col);
70 #endif
71
72 __attribute__ ((weak))
73 void matrix_init_quantum(void) {
74     matrix_init_kb();
75 }
76
77 __attribute__ ((weak))
78 void matrix_scan_quantum(void) {
79     matrix_scan_kb();
80 }
81
82 __attribute__ ((weak))
83 void matrix_init_kb(void) {
84     matrix_init_user();
85 }
86
87 __attribute__ ((weak))
88 void matrix_scan_kb(void) {
89     matrix_scan_user();
90 }
91
92 __attribute__ ((weak))
93 void matrix_init_user(void) {
94 }
95
96 __attribute__ ((weak))
97 void matrix_scan_user(void) {
98 }
99
100 inline
101 uint8_t matrix_rows(void) {
102     return MATRIX_ROWS;
103 }
104
105 inline
106 uint8_t matrix_cols(void) {
107     return MATRIX_COLS;
108 }
109
110 void matrix_init(void) {
111
112     // initialize row and col
113 #if (DIODE_DIRECTION == COL2ROW)
114     unselect_rows();
115     init_cols();
116 #elif (DIODE_DIRECTION == ROW2COL)
117     unselect_cols();
118     init_rows();
119 #endif
120
121     // initialize matrix state: all keys off
122     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
123         matrix[i] = 0;
124     }
125
126     matrix_init_quantum();
127 }
128
129 uint8_t matrix_scan(void)
130 {
131
132 #if (DIODE_DIRECTION == COL2ROW)
133     // Set row, read cols
134     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
135         read_cols_on_row(matrix, current_row);
136     }
137 #elif (DIODE_DIRECTION == ROW2COL)
138     // Set col, read rows
139     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
140         read_rows_on_col(matrix, current_col);
141     }
142 #endif
143
144     matrix_scan_quantum();
145     return 1;
146 }
147
148 //Deprecated.
149 bool matrix_is_modified(void)
150 {
151     return true;
152 }
153
154 inline
155 bool matrix_is_on(uint8_t row, uint8_t col)
156 {
157     return (matrix[row] & ((matrix_row_t)1<col));
158 }
159
160 inline
161 matrix_row_t matrix_get_row(uint8_t row)
162 {
163     // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
164     // switch blocker installed and the switch is always pressed.
165 #ifdef MATRIX_MASKED
166     return matrix[row] & matrix_mask[row];
167 #else
168     return matrix[row];
169 #endif
170 }
171
172 void matrix_print(void)
173 {
174     print_matrix_header();
175
176     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
177         phex(row); print(": ");
178         print_matrix_row(row);
179         print("\n");
180     }
181 }
182
183 uint8_t matrix_key_count(void)
184 {
185     uint8_t count = 0;
186     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
187         count += matrix_bitpop(i);
188     }
189     return count;
190 }
191
192
193
194 #if (DIODE_DIRECTION == COL2ROW)
195
196 static void init_cols(void)
197 {
198     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
199         setPinInputHigh(col_pins[x]);
200     }
201 }
202
203 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
204 {
205     // Store last value of row prior to reading
206     matrix_row_t last_row_value = current_matrix[current_row];
207
208     // Clear data in matrix row
209     current_matrix[current_row] = 0;
210
211     // Select row and wait for row selecton to stabilize
212     select_row(current_row);
213     wait_us(30);
214
215     // For each col...
216     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
217
218         // Select the col pin to read (active low)
219         uint8_t pin_state = readPin(col_pins[col_index]);
220
221         // Populate the matrix row with the state of the col pin
222         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
223     }
224
225     // Unselect row
226     unselect_row(current_row);
227
228     return (last_row_value != current_matrix[current_row]);
229 }
230
231 static void select_row(uint8_t row)
232 {
233     setPinOutput(row_pins[row]);
234     writePinLow(row_pins[row]);
235 }
236
237 static void unselect_row(uint8_t row)
238 {
239     setPinInputHigh(row_pins[row]);
240 }
241
242 static void unselect_rows(void)
243 {
244     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
245         setPinInput(row_pins[x]);
246     }
247 }
248
249 #elif (DIODE_DIRECTION == ROW2COL)
250
251 static void init_rows(void)
252 {
253     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
254         setPinInputHigh(row_pins[x]);
255     }
256 }
257
258 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
259 {
260     bool matrix_changed = false;
261
262     // Select col and wait for col selecton to stabilize
263     select_col(current_col);
264     wait_us(30);
265
266     // For each row...
267     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
268     {
269
270         // Store last value of row prior to reading
271         matrix_row_t last_row_value = current_matrix[row_index];
272
273         // Check row pin state
274         if (readPin(row_pins[row_index]) == 0)
275         {
276             // Pin LO, set col bit
277             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
278         }
279         else
280         {
281             // Pin HI, clear col bit
282             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
283         }
284
285         // Determine if the matrix changed state
286         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
287         {
288             matrix_changed = true;
289         }
290     }
291
292     // Unselect col
293     unselect_col(current_col);
294
295     return matrix_changed;
296 }
297
298 static void select_col(uint8_t col)
299 {
300     setPinOutput(col_pins[col]);
301     writePinLow(col_pins[col]);
302 }
303
304 static void unselect_col(uint8_t col)
305 {
306     setPinInputHigh(col_pins[col]);
307 }
308
309 static void unselect_cols(void)
310 {
311     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
312         setPinInputHigh(col_pins[x]);
313     }
314 }
315
316 #endif