Made scanning more efficient
[jackhill/qmk/firmware.git] / quantum / matrix.c
1 /*
2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdint.h>
19 #include <stdbool.h>
20 #if defined(__AVR__)
21 #include <avr/io.h>
22 #endif
23 #include "wait.h"
24 #include "print.h"
25 #include "debug.h"
26 #include "util.h"
27 #include "matrix.h"
28
29 #if (MATRIX_COLS <= 8)
30 # define print_matrix_header() print("\nr/c 01234567\n")
31 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
32 # define matrix_bitpop(i) bitpop(matrix[i])
33 # define ROW_SHIFTER ((uint8_t)1)
34 #elif (MATRIX_COLS <= 16)
35 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
36 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
37 # define matrix_bitpop(i) bitpop16(matrix[i])
38 # define ROW_SHIFTER ((uint16_t)1)
39 #elif (MATRIX_COLS <= 32)
40 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
41 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
42 # define matrix_bitpop(i) bitpop32(matrix[i])
43 # define ROW_SHIFTER ((uint32_t)1)
44 #endif
45
46 #ifdef MATRIX_MASKED
47 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
55 static uint8_t debouncing = DEBOUNCING_DELAY;
56
57 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
58 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
59
60 /* matrix state(1:on, 0:off) */
61 static matrix_row_t matrix[MATRIX_ROWS];
62
63 static matrix_row_t matrix_raw[MATRIX_ROWS];
64 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
65
66
67 #if (DIODE_DIRECTION == COL2ROW)
68 static void init_cols(void);
69 static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
70 static void unselect_rows(void);
71 static void select_row(uint8_t row);
72 static void unselect_row(uint8_t row);
73 #else // ROW2COL
74 static void init_rows(void);
75 static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
76 static void unselect_cols(void);
77 static void unselect_col(uint8_t col);
78 static void select_col(uint8_t col);
79 #endif
80
81 __attribute__ ((weak))
82 void matrix_init_quantum(void) {
83 matrix_init_kb();
84 }
85
86 __attribute__ ((weak))
87 void matrix_scan_quantum(void) {
88 matrix_scan_kb();
89 }
90
91 __attribute__ ((weak))
92 void matrix_init_kb(void) {
93 matrix_init_user();
94 }
95
96 __attribute__ ((weak))
97 void matrix_scan_kb(void) {
98 matrix_scan_user();
99 }
100
101 __attribute__ ((weak))
102 void matrix_init_user(void) {
103 }
104
105 __attribute__ ((weak))
106 void matrix_scan_user(void) {
107 }
108
109 inline
110 uint8_t matrix_rows(void) {
111 return MATRIX_ROWS;
112 }
113
114 inline
115 uint8_t matrix_cols(void) {
116 return MATRIX_COLS;
117 }
118
119 // void matrix_power_up(void) {
120 // #if (DIODE_DIRECTION == COL2ROW)
121 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
122 // /* DDRxn */
123 // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
124 // toggle_row(r);
125 // }
126 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
127 // /* PORTxn */
128 // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
129 // }
130 // #else
131 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
132 // /* DDRxn */
133 // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
134 // toggle_col(c);
135 // }
136 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
137 // /* PORTxn */
138 // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
139 // }
140 // #endif
141 // }
142
143 void matrix_init(void) {
144
145 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
146 #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
147 MCUCR |= _BV(JTD);
148 MCUCR |= _BV(JTD);
149 #endif
150
151 // initialize row and col
152 #if (DIODE_DIRECTION == COL2ROW)
153 unselect_rows();
154 init_cols();
155 #else // ROW2COL
156 unselect_cols();
157 init_rows();
158 #endif
159
160 // initialize matrix state: all keys off
161 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
162 matrix[i] = 0;
163 matrix_raw[i] = 0;
164 matrix_debouncing[i] = 0;
165 }
166
167 matrix_init_quantum();
168 }
169
170 uint8_t matrix_scan(void)
171 {
172
173 #if (DIODE_DIRECTION == COL2ROW)
174
175 // Set row, read cols
176 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
177 read_cols_on_row(matrix, current_row);
178 }
179
180 // select_row(i);
181 // wait_us(30); // without this wait read unstable value.
182 // matrix_row_t current_row = read_cols();
183 // if (matrix_debouncing[i] != current_row) {
184 // matrix_debouncing[i] = current_row;
185 // if (debouncing) {
186 // debug("bounce!: "); debug_hex(debouncing); debug("\n");
187 // }
188 // debouncing = DEBOUNCING_DELAY;
189 // }
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
203 #else // ROW2COL
204
205 // Set col, read rows
206 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
207 read_rows_on_col(matrix, current_col);
208 }
209
210
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
244 #endif
245
246 matrix_scan_quantum();
247 // matrix_print();
248 return 1;
249 }
250
251 bool matrix_is_modified(void)
252 {
253 if (debouncing) return false;
254 return true;
255 }
256
257 inline
258 bool matrix_is_on(uint8_t row, uint8_t col)
259 {
260 return (matrix[row] & ((matrix_row_t)1<col));
261 }
262
263 inline
264 matrix_row_t matrix_get_row(uint8_t row)
265 {
266 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
267 // switch blocker installed and the switch is always pressed.
268 #ifdef MATRIX_MASKED
269 return matrix[row] & matrix_mask[row];
270 #else
271 return matrix[row];
272 #endif
273 }
274
275 void matrix_print(void)
276 {
277 print_matrix_header();
278
279 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
280 phex(row); print(": ");
281 print_matrix_row(row);
282 print("\n");
283 }
284 }
285
286 uint8_t matrix_key_count(void)
287 {
288 uint8_t count = 0;
289 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
290 count += matrix_bitpop(i);
291 }
292 return count;
293 }
294
295
296
297 #if (DIODE_DIRECTION == COL2ROW)
298
299 static void init_cols(void)
300 {
301 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
302 uint8_t pin = col_pins[x];
303 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
304 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
305 }
306 }
307
308 static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
309 {
310 // Clear data in matrix row
311 current_matrix[current_row] = 0;
312
313 // Select row and wait for row selecton to stabilize
314 select_row(current_row);
315 wait_us(30);
316
317 // For each col...
318 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
319
320 // Select the col pin to read (active low)
321 uint8_t pin = col_pins[col_index];
322 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
323
324 // Populate the matrix row with the state of the col pin
325 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
326 }
327
328 // Unselect row
329 unselect_row(current_row);
330 }
331
332 static void select_row(uint8_t row)
333 {
334 uint8_t pin = row_pins[row];
335 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
336 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
337 }
338
339 static void unselect_row(uint8_t row)
340 {
341 uint8_t pin = row_pins[row];
342 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
343 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
344 }
345
346 static void unselect_rows(void)
347 {
348 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
349 uint8_t pin = row_pins[x];
350 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
351 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
352 }
353 }
354
355 #else // ROW2COL
356
357 static void init_rows(void)
358 {
359 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
360 uint8_t pin = row_pins[x];
361 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
362 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
363 }
364 }
365
366 static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
367 {
368
369 // Select col and wait for col selecton to stabilize
370 select_col(current_col);
371 wait_us(30);
372
373 // For each row...
374 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
375
376 // Check row pin state
377 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
378 {
379 // Pin LO, set col bit
380 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
381 }
382 else
383 {
384 // Pin HI, clear col bit
385 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
386 }
387 }
388
389 // Unselect col
390 unselect_col(current_col);
391 }
392
393 static void select_col(uint8_t col)
394 {
395 uint8_t pin = col_pins[col];
396 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
397 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
398 }
399
400 static void unselect_col(uint8_t col)
401 {
402 uint8_t pin = col_pins[col];
403 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
404 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
405 }
406
407 static void unselect_cols(void)
408 {
409 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
410 uint8_t pin = col_pins[x];
411 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
412 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
413 }
414 }
415
416 #endif