]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
Optimize matrix scanning (#343)
[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 #include <avr/io.h>
21 #include "wait.h"
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26
27 #ifdef MATRIX_HAS_GHOST
28 #   error "The universal matrix.c file cannot be used for this keyboard."
29 #endif
30
31 #ifndef DEBOUNCING_DELAY
32 #   define DEBOUNCING_DELAY 5
33 #endif
34
35 static const io_pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
36 static const io_pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
37 /* matrix state */
38 #if DIODE_DIRECTION == COL2ROW
39 static matrix_row_t matrix[MATRIX_ROWS];
40 static matrix_row_t debouncing_matrix[MATRIX_ROWS];
41 #else
42 static matrix_col_t matrix[MATRIX_COLS];
43 static matrix_col_t debouncing_matrix[MATRIX_COLS];
44 #endif
45 static int8_t debouncing_delay = -1;
46
47 #if DIODE_DIRECTION == COL2ROW
48 static void toggle_row(uint8_t row);
49 static matrix_row_t read_cols(void);
50 #else
51 static void toggle_col(uint8_t col);
52 static matrix_col_t read_rows(void);
53 #endif
54
55 __attribute__ ((weak))
56 void matrix_init_quantum(void) {
57 }
58
59 __attribute__ ((weak))
60 void matrix_scan_quantum(void) {
61 }
62
63 uint8_t matrix_rows(void) {
64     return MATRIX_ROWS;
65 }
66
67 uint8_t matrix_cols(void) {
68     return MATRIX_COLS;
69 }
70
71 void matrix_init(void) {
72     /* frees PORTF by setting the JTD bit twice within four cycles */
73     MCUCR |= _BV(JTD);
74     MCUCR |= _BV(JTD);
75     /* initializes the I/O pins */
76 #if DIODE_DIRECTION == COL2ROW
77     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
78         /* DDRxn */
79         _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
80         toggle_row(r);
81     }
82     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
83         /* PORTxn */
84         _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
85     }
86 #else
87     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
88         /* DDRxn */
89         _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
90         toggle_col(c);
91     }
92     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
93         /* PORTxn */
94         _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
95     }
96 #endif
97     matrix_init_quantum();
98 }
99
100 #if DIODE_DIRECTION == COL2ROW
101 uint8_t matrix_scan(void) {
102     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
103         toggle_row(r);
104         matrix_row_t state = read_cols();
105         if (debouncing_matrix[r] != state) {
106             debouncing_matrix[r] = state;
107             debouncing_delay = DEBOUNCING_DELAY;
108         }
109         toggle_row(r);
110     }
111     if (debouncing_delay >= 0) {
112         dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
113         --debouncing_delay;
114         if (debouncing_delay >= 0) {
115             wait_ms(1);
116         }
117         else {
118             for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
119                 matrix[r] = debouncing_matrix[r];
120             }
121         }
122     }
123     matrix_scan_quantum();
124     return 1;
125 }
126
127 static void toggle_row(uint8_t row) {
128     /* PINxn */
129     _SFR_IO8(row_pins[row].input_addr) = _BV(row_pins[row].bit);
130 }
131
132 static matrix_row_t read_cols(void) {
133     matrix_row_t state = 0;
134     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
135         /* PINxn */
136         if (!(_SFR_IO8(col_pins[c].input_addr) & _BV(col_pins[c].bit))) {
137             state |= (matrix_row_t)1 << c;
138         }
139     }
140     return state;
141 }
142
143 matrix_row_t matrix_get_row(uint8_t row) {
144     return matrix[row];
145 }
146
147 #else
148 uint8_t matrix_scan(void) {
149     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
150         toggle_col(c);
151         matrix_col_t state = read_rows();
152         if (debouncing_matrix[c] != state) {
153             debouncing_matrix[c] = state;
154             debouncing_delay = DEBOUNCING_DELAY;
155         }
156         toggle_col(c);
157     }
158     if (debouncing_delay >= 0) {
159         dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
160         --debouncing_delay;
161         if (debouncing_delay >= 0) {
162             wait_ms(1);
163         }
164         else {
165             for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
166                 matrix[c] = debouncing_matrix[c];
167             }
168         }
169     }
170     matrix_scan_quantum();
171     return 1;
172 }
173
174 static void toggle_col(uint8_t col) {
175     /* PINxn */
176     _SFR_IO8(col_pins[col].input_addr) = _BV(col_pins[col].bit);
177 }
178
179 static matrix_col_t read_rows(void) {
180     matrix_col_t state = 0;
181     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
182         /* PINxn */
183         if (!(_SFR_IO8(row_pins[r].input_addr) & _BV(row_pins[r].bit))) {
184             state |= (matrix_col_t)1 << r;
185         }
186     }
187     return state;
188 }
189
190 matrix_row_t matrix_get_row(uint8_t row) {
191     matrix_row_t state = 0;
192     matrix_col_t mask = (matrix_col_t)1 << row;
193     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
194         if (matrix[c] & mask) {
195             state |= (matrix_row_t)1 << c;
196         }
197     }
198     return state;
199 }
200
201 #endif
202
203 bool matrix_is_modified(void) {
204     if (debouncing_delay >= 0) return false;
205     return true;
206 }
207
208 bool matrix_is_on(uint8_t row, uint8_t col) {
209     return matrix_get_row(row) & (matrix_row_t)1 << col;
210 }
211
212 void matrix_print(void) {
213     dprintln("Human-readable matrix state:");
214     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
215         dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
216     }
217 }
218
219 uint8_t matrix_key_count(void) {
220     uint8_t count = 0;
221     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
222         count += bitpop16(matrix_get_row(r));
223     }
224     return count;
225 }