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