DO NOT USE - debounce successfully compiled.
[jackhill/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 "debounce.h"
25 #include "quantum.h"
26
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 #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
49 static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
50 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
51 #endif
52
53 /* matrix state(1:on, 0:off) */
54 static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
55 static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
56
57 #if (DIODE_DIRECTION == COL2ROW)
58 static void init_cols(void);
59 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
60 static void unselect_rows(void);
61 static void select_row(uint8_t row);
62 static void unselect_row(uint8_t row);
63 #elif (DIODE_DIRECTION == ROW2COL)
64 static void init_rows(void);
65 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
66 static void unselect_cols(void);
67 static void unselect_col(uint8_t col);
68 static void select_col(uint8_t col);
69 #endif
70
71 __attribute__ ((weak))
72 void matrix_init_quantum(void) {
73 matrix_init_kb();
74 }
75
76 __attribute__ ((weak))
77 void matrix_scan_quantum(void) {
78 matrix_scan_kb();
79 }
80
81 __attribute__ ((weak))
82 void matrix_init_kb(void) {
83 matrix_init_user();
84 }
85
86 __attribute__ ((weak))
87 void matrix_scan_kb(void) {
88 matrix_scan_user();
89 }
90
91 __attribute__ ((weak))
92 void matrix_init_user(void) {
93 }
94
95 __attribute__ ((weak))
96 void matrix_scan_user(void) {
97 }
98
99 inline
100 uint8_t matrix_rows(void) {
101 return MATRIX_ROWS;
102 }
103
104 inline
105 uint8_t matrix_cols(void) {
106 return MATRIX_COLS;
107 }
108
109 void matrix_init(void) {
110
111 // initialize row and col
112 #if (DIODE_DIRECTION == COL2ROW)
113 unselect_rows();
114 init_cols();
115 #elif (DIODE_DIRECTION == ROW2COL)
116 unselect_cols();
117 init_rows();
118 #endif
119
120 // initialize matrix state: all keys off
121 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
122 raw_matrix[i] = 0;
123 matrix[i] = 0;
124 }
125 debounce_init();
126
127 matrix_init_quantum();
128 }
129
130 uint8_t matrix_scan(void)
131 {
132 bool changed = false;
133
134 #if (DIODE_DIRECTION == COL2ROW)
135 // Set row, read cols
136 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
137 changed |= read_cols_on_row(raw_matrix, current_row);
138 }
139 #elif (DIODE_DIRECTION == ROW2COL)
140 // Set col, read rows
141 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
142 changed |= read_rows_on_col(raw_matrix, current_col);
143 }
144 #endif
145
146 debounce(raw_matrix, matrix, changed);
147
148 matrix_scan_quantum();
149 return 1;
150 }
151
152 //Deprecated.
153 bool matrix_is_modified(void)
154 {
155 if (debounce_active()) return false;
156 return true;
157 }
158
159 inline
160 bool matrix_is_on(uint8_t row, uint8_t col)
161 {
162 return (matrix[row] & ((matrix_row_t)1<col));
163 }
164
165 inline
166 matrix_row_t matrix_get_row(uint8_t row)
167 {
168 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
169 // switch blocker installed and the switch is always pressed.
170 #ifdef MATRIX_MASKED
171 return matrix[row] & matrix_mask[row];
172 #else
173 return matrix[row];
174 #endif
175 }
176
177 void matrix_print(void)
178 {
179 print_matrix_header();
180
181 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
182 phex(row); print(": ");
183 print_matrix_row(row);
184 print("\n");
185 }
186 }
187
188 uint8_t matrix_key_count(void)
189 {
190 uint8_t count = 0;
191 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
192 count += matrix_bitpop(i);
193 }
194 return count;
195 }
196
197
198
199 #if (DIODE_DIRECTION == COL2ROW)
200
201 static void init_cols(void)
202 {
203 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
204 setPinInputHigh(col_pins[x]);
205 }
206 }
207
208 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
209 {
210 // Store last value of row prior to reading
211 matrix_row_t last_row_value = current_matrix[current_row];
212
213 // Clear data in matrix row
214 current_matrix[current_row] = 0;
215
216 // Select row and wait for row selecton to stabilize
217 select_row(current_row);
218 wait_us(30);
219
220 // For each col...
221 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
222
223 // Select the col pin to read (active low)
224 uint8_t pin_state = readPin(col_pins[col_index]);
225
226 // Populate the matrix row with the state of the col pin
227 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
228 }
229
230 // Unselect row
231 unselect_row(current_row);
232
233 return (last_row_value != current_matrix[current_row]);
234 }
235
236 static void select_row(uint8_t row)
237 {
238 setPinOutput(row_pins[row]);
239 writePinLow(row_pins[row]);
240 }
241
242 static void unselect_row(uint8_t row)
243 {
244 setPinInputHigh(row_pins[row]);
245 }
246
247 static void unselect_rows(void)
248 {
249 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
250 setPinInput(row_pins[x]);
251 }
252 }
253
254 #elif (DIODE_DIRECTION == ROW2COL)
255
256 static void init_rows(void)
257 {
258 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
259 setPinInputHigh(row_pins[x]);
260 }
261 }
262
263 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
264 {
265 bool matrix_changed = false;
266
267 // Select col and wait for col selecton to stabilize
268 select_col(current_col);
269 wait_us(30);
270
271 // For each row...
272 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
273 {
274
275 // Store last value of row prior to reading
276 matrix_row_t last_row_value = current_matrix[row_index];
277
278 // Check row pin state
279 if (readPin(row_pins[row_index]) == 0)
280 {
281 // Pin LO, set col bit
282 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
283 }
284 else
285 {
286 // Pin HI, clear col bit
287 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
288 }
289
290 // Determine if the matrix changed state
291 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
292 {
293 matrix_changed = true;
294 }
295 }
296
297 // Unselect col
298 unselect_col(current_col);
299
300 return matrix_changed;
301 }
302
303 static void select_col(uint8_t col)
304 {
305 setPinOutput(col_pins[col]);
306 writePinLow(col_pins[col]);
307 }
308
309 static void unselect_col(uint8_t col)
310 {
311 setPinInputHigh(col_pins[col]);
312 }
313
314 static void unselect_cols(void)
315 {
316 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
317 setPinInputHigh(col_pins[x]);
318 }
319 }
320
321 #endif