]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
71292db5118aecb850ab85585d39f2ee8f11f40f
[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];
55
56 static matrix_row_t matrix[MATRIX_ROWS];
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         raw_matrix[i] = 0;
124         matrix[i] = 0;
125     }
126     debounce_init(MATRIX_ROWS);
127
128     matrix_init_quantum();
129 }
130
131 uint8_t matrix_scan(void)
132 {
133   bool changed = false;
134
135 #if (DIODE_DIRECTION == COL2ROW)
136   // Set row, read cols
137   for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
138     changed |= read_cols_on_row(raw_matrix, current_row);
139   }
140 #elif (DIODE_DIRECTION == ROW2COL)
141   // Set col, read rows
142   for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
143     changed |= read_rows_on_col(raw_matrix, current_col);
144   }
145 #endif
146
147   debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
148
149   matrix_scan_quantum();
150   return 1;
151 }
152
153 //Deprecated.
154 bool matrix_is_modified(void)
155 {
156     if (debounce_active()) return false;
157     return true;
158 }
159
160 inline
161 bool matrix_is_on(uint8_t row, uint8_t col)
162 {
163     return (matrix[row] & ((matrix_row_t)1<col));
164 }
165
166 inline
167 matrix_row_t matrix_get_row(uint8_t row)
168 {
169     // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
170     // switch blocker installed and the switch is always pressed.
171 #ifdef MATRIX_MASKED
172     return matrix[row] & matrix_mask[row];
173 #else
174     return matrix[row];
175 #endif
176 }
177
178 void matrix_print(void)
179 {
180     print_matrix_header();
181
182     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
183         phex(row); print(": ");
184         print_matrix_row(row);
185         print("\n");
186     }
187 }
188
189 uint8_t matrix_key_count(void)
190 {
191     uint8_t count = 0;
192     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
193         count += matrix_bitpop(i);
194     }
195     return count;
196 }
197
198
199
200 #if (DIODE_DIRECTION == COL2ROW)
201
202 static void init_cols(void)
203 {
204     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
205         setPinInputHigh(col_pins[x]);
206     }
207 }
208
209 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
210 {
211     // Store last value of row prior to reading
212     matrix_row_t last_row_value = current_matrix[current_row];
213
214     // Clear data in matrix row
215     current_matrix[current_row] = 0;
216
217     // Select row and wait for row selecton to stabilize
218     select_row(current_row);
219     wait_us(30);
220
221     // For each col...
222     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
223
224         // Select the col pin to read (active low)
225         uint8_t pin_state = readPin(col_pins[col_index]);
226
227         // Populate the matrix row with the state of the col pin
228         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
229     }
230
231     // Unselect row
232     unselect_row(current_row);
233
234     return (last_row_value != current_matrix[current_row]);
235 }
236
237 static void select_row(uint8_t row)
238 {
239     setPinOutput(row_pins[row]);
240     writePinLow(row_pins[row]);
241 }
242
243 static void unselect_row(uint8_t row)
244 {
245     setPinInputHigh(row_pins[row]);
246 }
247
248 static void unselect_rows(void)
249 {
250     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
251         setPinInput(row_pins[x]);
252     }
253 }
254
255 #elif (DIODE_DIRECTION == ROW2COL)
256
257 static void init_rows(void)
258 {
259     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
260         setPinInputHigh(row_pins[x]);
261     }
262 }
263
264 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
265 {
266     bool matrix_changed = false;
267
268     // Select col and wait for col selecton to stabilize
269     select_col(current_col);
270     wait_us(30);
271
272     // For each row...
273     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
274     {
275
276         // Store last value of row prior to reading
277         matrix_row_t last_row_value = current_matrix[row_index];
278
279         // Check row pin state
280         if (readPin(row_pins[row_index]) == 0)
281         {
282             // Pin LO, set col bit
283             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
284         }
285         else
286         {
287             // Pin HI, clear col bit
288             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
289         }
290
291         // Determine if the matrix changed state
292         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
293         {
294             matrix_changed = true;
295         }
296     }
297
298     // Unselect col
299     unselect_col(current_col);
300
301     return matrix_changed;
302 }
303
304 static void select_col(uint8_t col)
305 {
306     setPinOutput(col_pins[col]);
307     writePinLow(col_pins[col]);
308 }
309
310 static void unselect_col(uint8_t col)
311 {
312     setPinInputHigh(col_pins[col]);
313 }
314
315 static void unselect_cols(void)
316 {
317     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
318         setPinInputHigh(col_pins[x]);
319     }
320 }
321
322 #endif