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