]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
bc7eb6b585d125781dec55b7bce2330a392dfa6d
[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 #if (MATRIX_COLS <= 8)
31 #    define print_matrix_header()  print("\nr/c 01234567\n")
32 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
33 #    define matrix_bitpop(i)       bitpop(matrix[i])
34 #    define ROW_SHIFTER ((uint8_t)1)
35 #elif (MATRIX_COLS <= 16)
36 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
37 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
38 #    define matrix_bitpop(i)       bitpop16(matrix[i])
39 #    define ROW_SHIFTER ((uint16_t)1)
40 #elif (MATRIX_COLS <= 32)
41 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
42 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
43 #    define matrix_bitpop(i)       bitpop32(matrix[i])
44 #    define ROW_SHIFTER  ((uint32_t)1)
45 #endif
46
47 #ifdef MATRIX_MASKED
48     extern const matrix_row_t matrix_mask[];
49 #endif
50
51 #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
52 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
53 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
54 #endif
55
56 /* matrix state(1:on, 0:off) */
57 static matrix_row_t matrix[MATRIX_ROWS];
58
59
60 #if (DIODE_DIRECTION == COL2ROW)
61     static void init_cols(void);
62     static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
63     static void unselect_rows(void);
64     static void select_row(uint8_t row);
65     static void unselect_row(uint8_t row);
66 #elif (DIODE_DIRECTION == ROW2COL)
67     static void init_rows(void);
68     static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
69     static void unselect_cols(void);
70     static void unselect_col(uint8_t col);
71     static void select_col(uint8_t col);
72 #endif
73
74 __attribute__ ((weak))
75 void matrix_init_quantum(void) {
76     matrix_init_kb();
77 }
78
79 __attribute__ ((weak))
80 void matrix_scan_quantum(void) {
81     matrix_scan_kb();
82 }
83
84 __attribute__ ((weak))
85 void matrix_init_kb(void) {
86     matrix_init_user();
87 }
88
89 __attribute__ ((weak))
90 void matrix_scan_kb(void) {
91     matrix_scan_user();
92 }
93
94 __attribute__ ((weak))
95 void matrix_init_user(void) {
96 }
97
98 __attribute__ ((weak))
99 void matrix_scan_user(void) {
100 }
101
102 inline
103 uint8_t matrix_rows(void) {
104     return MATRIX_ROWS;
105 }
106
107 inline
108 uint8_t matrix_cols(void) {
109     return MATRIX_COLS;
110 }
111
112 void matrix_init(void) {
113
114     // initialize row and col
115 #if (DIODE_DIRECTION == COL2ROW)
116     unselect_rows();
117     init_cols();
118 #elif (DIODE_DIRECTION == ROW2COL)
119     unselect_cols();
120     init_rows();
121 #endif
122
123     // initialize matrix state: all keys off
124     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
125         matrix[i] = 0;
126     }
127
128     matrix_init_quantum();
129 }
130
131 uint8_t matrix_scan(void)
132 {
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         read_cols_on_row(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         read_rows_on_col(matrix, current_col);
143     }
144 #endif
145
146     matrix_scan_quantum();
147     return 1;
148 }
149
150 //Deprecated.
151 bool matrix_is_modified(void)
152 {
153     return true;
154 }
155
156 inline
157 bool matrix_is_on(uint8_t row, uint8_t col)
158 {
159     return (matrix[row] & ((matrix_row_t)1<col));
160 }
161
162 inline
163 matrix_row_t matrix_get_row(uint8_t row)
164 {
165     // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
166     // switch blocker installed and the switch is always pressed.
167 #ifdef MATRIX_MASKED
168     return matrix[row] & matrix_mask[row];
169 #else
170     return matrix[row];
171 #endif
172 }
173
174 void matrix_print(void)
175 {
176     print_matrix_header();
177
178     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
179         phex(row); print(": ");
180         print_matrix_row(row);
181         print("\n");
182     }
183 }
184
185 uint8_t matrix_key_count(void)
186 {
187     uint8_t count = 0;
188     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
189         count += matrix_bitpop(i);
190     }
191     return count;
192 }
193
194
195
196 #if (DIODE_DIRECTION == COL2ROW)
197
198 static void init_cols(void)
199 {
200     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
201         uint8_t pin = col_pins[x];
202         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
203         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
204     }
205 }
206
207 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
208 {
209     // Store last value of row prior to reading
210     matrix_row_t last_row_value = current_matrix[current_row];
211
212     // Clear data in matrix row
213     current_matrix[current_row] = 0;
214
215     // Select row and wait for row selecton to stabilize
216     select_row(current_row);
217     wait_us(30);
218
219     // For each col...
220     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
221
222         // Select the col pin to read (active low)
223         uint8_t pin = col_pins[col_index];
224         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
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     uint8_t pin = row_pins[row];
239     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
240     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
241 }
242
243 static void unselect_row(uint8_t row)
244 {
245     uint8_t pin = row_pins[row];
246     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
247     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
248 }
249
250 static void unselect_rows(void)
251 {
252     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
253         uint8_t pin = row_pins[x];
254         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
255         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
256     }
257 }
258
259 #elif (DIODE_DIRECTION == ROW2COL)
260
261 static void init_rows(void)
262 {
263     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
264         uint8_t pin = row_pins[x];
265         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
266         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
267     }
268 }
269
270 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
271 {
272     bool matrix_changed = false;
273
274     // Select col and wait for col selecton to stabilize
275     select_col(current_col);
276     wait_us(30);
277
278     // For each row...
279     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
280     {
281
282         // Store last value of row prior to reading
283         matrix_row_t last_row_value = current_matrix[row_index];
284
285         // Check row pin state
286         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
287         {
288             // Pin LO, set col bit
289             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
290         }
291         else
292         {
293             // Pin HI, clear col bit
294             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
295         }
296
297         // Determine if the matrix changed state
298         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
299         {
300             matrix_changed = true;
301         }
302     }
303
304     // Unselect col
305     unselect_col(current_col);
306
307     return matrix_changed;
308 }
309
310 static void select_col(uint8_t col)
311 {
312     uint8_t pin = col_pins[col];
313     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
314     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
315 }
316
317 static void unselect_col(uint8_t col)
318 {
319     uint8_t pin = col_pins[col];
320     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
321     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
322 }
323
324 static void unselect_cols(void)
325 {
326     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
327         uint8_t pin = col_pins[x];
328         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
329         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
330     }
331 }
332
333 #endif