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