]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
Some updates to my minivan keymap (#3294)
[qmk_firmware.git] / quantum / matrix.c
1 /*
2 Copyright 2012-2017 Jun Wako, Jack Humbert
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 #if defined(__AVR__)
20 #include <avr/io.h>
21 #endif
22 #include "wait.h"
23 #include "print.h"
24 #include "debug.h"
25 #include "util.h"
26 #include "matrix.h"
27 #include "timer.h"
28
29
30 /* Set 0 if debouncing isn't needed */
31
32 #ifndef DEBOUNCING_DELAY
33 #   define DEBOUNCING_DELAY 5
34 #endif
35
36 #if (DEBOUNCING_DELAY > 0)
37     static uint16_t debouncing_time;
38     static bool debouncing = false;
39 #endif
40
41 #if (MATRIX_COLS <= 8)
42 #    define print_matrix_header()  print("\nr/c 01234567\n")
43 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
44 #    define matrix_bitpop(i)       bitpop(matrix[i])
45 #    define ROW_SHIFTER ((uint8_t)1)
46 #elif (MATRIX_COLS <= 16)
47 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
48 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
49 #    define matrix_bitpop(i)       bitpop16(matrix[i])
50 #    define ROW_SHIFTER ((uint16_t)1)
51 #elif (MATRIX_COLS <= 32)
52 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
53 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
54 #    define matrix_bitpop(i)       bitpop32(matrix[i])
55 #    define ROW_SHIFTER  ((uint32_t)1)
56 #endif
57
58 #ifdef MATRIX_MASKED
59     extern const matrix_row_t matrix_mask[];
60 #endif
61
62 #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
63 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
64 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
65 #endif
66
67 /* matrix state(1:on, 0:off) */
68 static matrix_row_t matrix[MATRIX_ROWS];
69
70 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71
72
73 #if (DIODE_DIRECTION == COL2ROW)
74     static void init_cols(void);
75     static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
76     static void unselect_rows(void);
77     static void select_row(uint8_t row);
78     static void unselect_row(uint8_t row);
79 #elif (DIODE_DIRECTION == ROW2COL)
80     static void init_rows(void);
81     static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
82     static void unselect_cols(void);
83     static void unselect_col(uint8_t col);
84     static void select_col(uint8_t col);
85 #endif
86
87 __attribute__ ((weak))
88 void matrix_init_quantum(void) {
89     matrix_init_kb();
90 }
91
92 __attribute__ ((weak))
93 void matrix_scan_quantum(void) {
94     matrix_scan_kb();
95 }
96
97 __attribute__ ((weak))
98 void matrix_init_kb(void) {
99     matrix_init_user();
100 }
101
102 __attribute__ ((weak))
103 void matrix_scan_kb(void) {
104     matrix_scan_user();
105 }
106
107 __attribute__ ((weak))
108 void matrix_init_user(void) {
109 }
110
111 __attribute__ ((weak))
112 void matrix_scan_user(void) {
113 }
114
115 inline
116 uint8_t matrix_rows(void) {
117     return MATRIX_ROWS;
118 }
119
120 inline
121 uint8_t matrix_cols(void) {
122     return MATRIX_COLS;
123 }
124
125 // void matrix_power_up(void) {
126 // #if (DIODE_DIRECTION == COL2ROW)
127 //     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
128 //         /* DDRxn */
129 //         _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
130 //         toggle_row(r);
131 //     }
132 //     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
133 //         /* PORTxn */
134 //         _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
135 //     }
136 // #elif (DIODE_DIRECTION == ROW2COL)
137 //     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
138 //         /* DDRxn */
139 //         _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
140 //         toggle_col(c);
141 //     }
142 //     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
143 //         /* PORTxn */
144 //         _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
145 //     }
146 // #endif
147 // }
148
149 void matrix_init(void) {
150
151     // initialize row and col
152 #if (DIODE_DIRECTION == COL2ROW)
153     unselect_rows();
154     init_cols();
155 #elif (DIODE_DIRECTION == 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_debouncing[i] = 0;
164     }
165
166     matrix_init_quantum();
167 }
168
169 uint8_t matrix_scan(void)
170 {
171
172 #if (DIODE_DIRECTION == COL2ROW)
173
174     // Set row, read cols
175     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
176 #       if (DEBOUNCING_DELAY > 0)
177             bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
178
179             if (matrix_changed) {
180                 debouncing = true;
181                 debouncing_time = timer_read();
182             }
183
184 #       else
185             read_cols_on_row(matrix, current_row);
186 #       endif
187
188     }
189
190 #elif (DIODE_DIRECTION == ROW2COL)
191
192     // Set col, read rows
193     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
194 #       if (DEBOUNCING_DELAY > 0)
195             bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
196             if (matrix_changed) {
197                 debouncing = true;
198                 debouncing_time = timer_read();
199             }
200 #       else
201              read_rows_on_col(matrix, current_col);
202 #       endif
203
204     }
205
206 #endif
207
208 #   if (DEBOUNCING_DELAY > 0)
209         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
210             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
211                 matrix[i] = matrix_debouncing[i];
212             }
213             debouncing = false;
214         }
215 #   endif
216
217     matrix_scan_quantum();
218     return 1;
219 }
220
221 bool matrix_is_modified(void)
222 {
223 #if (DEBOUNCING_DELAY > 0)
224     if (debouncing) return false;
225 #endif
226     return true;
227 }
228
229 inline
230 bool matrix_is_on(uint8_t row, uint8_t col)
231 {
232     return (matrix[row] & ((matrix_row_t)1<col));
233 }
234
235 inline
236 matrix_row_t matrix_get_row(uint8_t row)
237 {
238     // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
239     // switch blocker installed and the switch is always pressed.
240 #ifdef MATRIX_MASKED
241     return matrix[row] & matrix_mask[row];
242 #else
243     return matrix[row];
244 #endif
245 }
246
247 void matrix_print(void)
248 {
249     print_matrix_header();
250
251     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
252         phex(row); print(": ");
253         print_matrix_row(row);
254         print("\n");
255     }
256 }
257
258 uint8_t matrix_key_count(void)
259 {
260     uint8_t count = 0;
261     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
262         count += matrix_bitpop(i);
263     }
264     return count;
265 }
266
267
268
269 #if (DIODE_DIRECTION == COL2ROW)
270
271 static void init_cols(void)
272 {
273     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
274         uint8_t pin = col_pins[x];
275         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
276         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
277     }
278 }
279
280 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
281 {
282     // Store last value of row prior to reading
283     matrix_row_t last_row_value = current_matrix[current_row];
284
285     // Clear data in matrix row
286     current_matrix[current_row] = 0;
287
288     // Select row and wait for row selecton to stabilize
289     select_row(current_row);
290     wait_us(30);
291
292     // For each col...
293     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
294
295         // Select the col pin to read (active low)
296         uint8_t pin = col_pins[col_index];
297         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
298
299         // Populate the matrix row with the state of the col pin
300         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
301     }
302
303     // Unselect row
304     unselect_row(current_row);
305
306     return (last_row_value != current_matrix[current_row]);
307 }
308
309 static void select_row(uint8_t row)
310 {
311     uint8_t pin = row_pins[row];
312     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
313     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
314 }
315
316 static void unselect_row(uint8_t row)
317 {
318     uint8_t pin = row_pins[row];
319     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
320     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
321 }
322
323 static void unselect_rows(void)
324 {
325     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
326         uint8_t pin = row_pins[x];
327         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
328         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
329     }
330 }
331
332 #elif (DIODE_DIRECTION == ROW2COL)
333
334 static void init_rows(void)
335 {
336     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
337         uint8_t pin = row_pins[x];
338         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
339         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
340     }
341 }
342
343 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
344 {
345     bool matrix_changed = false;
346
347     // Select col and wait for col selecton to stabilize
348     select_col(current_col);
349     wait_us(30);
350
351     // For each row...
352     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
353     {
354
355         // Store last value of row prior to reading
356         matrix_row_t last_row_value = current_matrix[row_index];
357
358         // Check row pin state
359         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
360         {
361             // Pin LO, set col bit
362             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
363         }
364         else
365         {
366             // Pin HI, clear col bit
367             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
368         }
369
370         // Determine if the matrix changed state
371         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
372         {
373             matrix_changed = true;
374         }
375     }
376
377     // Unselect col
378     unselect_col(current_col);
379
380     return matrix_changed;
381 }
382
383 static void select_col(uint8_t col)
384 {
385     uint8_t pin = col_pins[col];
386     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
387     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
388 }
389
390 static void unselect_col(uint8_t col)
391 {
392     uint8_t pin = col_pins[col];
393     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
394     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
395 }
396
397 static void unselect_cols(void)
398 {
399     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
400         uint8_t pin = col_pins[x];
401         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
402         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
403     }
404 }
405
406 #endif