]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
ac81794e59eb57dd09416ae145f8b33bd2389462
[qmk_firmware.git] / quantum / matrix.c
1 /*
2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdint.h>
19 #include <stdbool.h>
20 #if defined(__AVR__)
21 #include <avr/io.h>
22 #endif
23 #include "wait.h"
24 #include "print.h"
25 #include "debug.h"
26 #include "util.h"
27 #include "matrix.h"
28
29 #ifdef MATRIX_MASKED
30 extern const matrix_row_t matrix_mask[];
31 #endif
32
33 /* Set 0 if debouncing isn't needed */
34
35 #ifndef DEBOUNCING_DELAY
36 #   define DEBOUNCING_DELAY 5
37 #endif
38 static uint8_t debouncing = DEBOUNCING_DELAY;
39
40 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
41 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
42
43 /* matrix state(1:on, 0:off) */
44 static matrix_row_t matrix[MATRIX_ROWS];
45 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
46
47 #if DIODE_DIRECTION == ROW2COL
48     static matrix_row_t matrix_reversed[MATRIX_COLS];
49     static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
50 #endif
51
52 #if MATRIX_COLS > 16
53     #define SHIFTER 1UL
54 #else
55     #define SHIFTER 1
56 #endif
57
58 static matrix_row_t read_cols(void);
59 static void init_cols(void);
60 static void unselect_rows(void);
61 static void select_row(uint8_t row);
62
63 __attribute__ ((weak))
64 void matrix_init_quantum(void) {
65     matrix_init_kb();
66 }
67
68 __attribute__ ((weak))
69 void matrix_scan_quantum(void) {
70     matrix_scan_kb();
71 }
72
73 __attribute__ ((weak))
74 void matrix_init_kb(void) {
75     matrix_init_user();
76 }
77
78 __attribute__ ((weak))
79 void matrix_scan_kb(void) {
80     matrix_scan_user();
81 }
82
83 __attribute__ ((weak))
84 void matrix_init_user(void) {
85 }
86
87 __attribute__ ((weak))
88 void matrix_scan_user(void) {
89 }
90
91 inline
92 uint8_t matrix_rows(void) {
93     return MATRIX_ROWS;
94 }
95
96 inline
97 uint8_t matrix_cols(void) {
98     return MATRIX_COLS;
99 }
100
101 // void matrix_power_up(void) {
102 // #if DIODE_DIRECTION == COL2ROW
103 //     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
104 //         /* DDRxn */
105 //         _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
106 //         toggle_row(r);
107 //     }
108 //     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
109 //         /* PORTxn */
110 //         _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
111 //     }
112 // #else
113 //     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
114 //         /* DDRxn */
115 //         _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
116 //         toggle_col(c);
117 //     }
118 //     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
119 //         /* PORTxn */
120 //         _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
121 //     }
122 // #endif
123 // }
124
125 void matrix_init(void) {
126     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
127     #ifdef __AVR_ATmega32U4__
128         MCUCR |= _BV(JTD);
129         MCUCR |= _BV(JTD);
130     #endif
131
132     // initialize row and col
133     unselect_rows();
134     init_cols();
135
136     // initialize matrix state: all keys off
137     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
138         matrix[i] = 0;
139         matrix_debouncing[i] = 0;
140     }
141
142     matrix_init_quantum();
143 }
144
145 uint8_t matrix_scan(void)
146 {
147
148 #if DIODE_DIRECTION == COL2ROW
149     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
150         select_row(i);
151         wait_us(30);  // without this wait read unstable value.
152         matrix_row_t cols = read_cols();
153         if (matrix_debouncing[i] != cols) {
154             matrix_debouncing[i] = cols;
155             if (debouncing) {
156                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
157             }
158             debouncing = DEBOUNCING_DELAY;
159         }
160         unselect_rows();
161     }
162
163     if (debouncing) {
164         if (--debouncing) {
165             wait_ms(1);
166         } else {
167             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
168                 matrix[i] = matrix_debouncing[i];
169             }
170         }
171     }
172 #else
173     for (uint8_t i = 0; i < MATRIX_COLS; i++) {
174         select_row(i);
175         wait_us(30);  // without this wait read unstable value.
176         matrix_row_t rows = read_cols();
177         if (matrix_reversed_debouncing[i] != rows) {
178             matrix_reversed_debouncing[i] = rows;
179             if (debouncing) {
180                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
181             }
182             debouncing = DEBOUNCING_DELAY;
183         }
184         unselect_rows();
185     }
186
187     if (debouncing) {
188         if (--debouncing) {
189             wait_ms(1);
190         } else {
191             for (uint8_t i = 0; i < MATRIX_COLS; i++) {
192                 matrix_reversed[i] = matrix_reversed_debouncing[i];
193             }
194         }
195     }
196     for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
197         matrix_row_t row = 0;
198         for (uint8_t x = 0; x < MATRIX_COLS; x++) {
199             row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
200         }
201         matrix[y] = row;
202     }
203 #endif
204
205     matrix_scan_quantum();
206
207     return 1;
208 }
209
210 bool matrix_is_modified(void)
211 {
212     if (debouncing) return false;
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 #if (MATRIX_COLS <= 8)
237     print("\nr/c 01234567\n");
238 #elif (MATRIX_COLS <= 16)
239     print("\nr/c 0123456789ABCDEF\n");
240 #elif (MATRIX_COLS <= 32)
241     print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n");
242 #endif
243
244     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
245         phex(row); print(": ");
246 #if (MATRIX_COLS <= 8)
247         print_bin_reverse8(matrix_get_row(row));
248 #elif (MATRIX_COLS <= 16)
249         print_bin_reverse16(matrix_get_row(row));
250 #elif (MATRIX_COLS <= 32)
251         print_bin_reverse32(matrix_get_row(row));
252 #endif
253         print("\n");
254     }
255 }
256
257 uint8_t matrix_key_count(void)
258 {
259     uint8_t count = 0;
260     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
261 #if (MATRIX_COLS <= 8)
262         count += bitpop(matrix[i]);
263 #elif (MATRIX_COLS <= 16)
264         count += bitpop16(matrix[i]);
265 #elif (MATRIX_COLS <= 32)
266         count += bitpop32(matrix[i]);
267 #endif
268     }
269     return count;
270 }
271
272 static void init_cols(void)
273 {
274 #if DIODE_DIRECTION == COL2ROW
275     for(int x = 0; x < MATRIX_COLS; x++) {
276         int pin = col_pins[x];
277 #else
278     for(int x = 0; x < MATRIX_ROWS; x++) {
279         int pin = row_pins[x];
280 #endif
281         _SFR_IO8((pin >> 4) + 1) &=  ~_BV(pin & 0xF);
282         _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
283     }
284 }
285
286 static matrix_row_t read_cols(void)
287 {
288     matrix_row_t result = 0;
289
290 #if DIODE_DIRECTION == COL2ROW
291     for(int x = 0; x < MATRIX_COLS; x++) {
292         int pin = col_pins[x];
293 #else
294     for(int x = 0; x < MATRIX_ROWS; x++) {
295         int pin = row_pins[x];
296 #endif
297         result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x);
298     }
299     return result;
300 }
301
302 static void unselect_rows(void)
303 {
304 #if DIODE_DIRECTION == COL2ROW
305     for(int x = 0; x < MATRIX_ROWS; x++) {
306         int pin = row_pins[x];
307 #else
308     for(int x = 0; x < MATRIX_COLS; x++) {
309         int pin = col_pins[x];
310 #endif
311         _SFR_IO8((pin >> 4) + 1) &=  ~_BV(pin & 0xF);
312         _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
313     }
314 }
315
316 static void select_row(uint8_t row)
317 {
318
319 #if DIODE_DIRECTION == COL2ROW
320     int pin = row_pins[row];
321 #else
322     int pin = col_pins[row];
323 #endif
324     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF);
325     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF);
326 }