2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
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.
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.
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/>.
27 #ifdef MATRIX_HAS_GHOST
28 # error "The universal matrix.c file cannot be used for this keyboard."
31 #ifndef DEBOUNCING_DELAY
32 # define DEBOUNCING_DELAY 5
35 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
36 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
38 #if DIODE_DIRECTION == COL2ROW
39 static matrix_row_t matrix[MATRIX_ROWS];
41 static matrix_col_t matrix[MATRIX_COLS];
43 static int8_t debouncing_delay = -1;
45 #if DIODE_DIRECTION == COL2ROW
46 static void toggle_row(uint8_t row);
47 static matrix_row_t read_cols(void);
49 static void toggle_col(uint8_t col);
50 static matrix_col_t read_rows(void);
53 __attribute__ ((weak))
54 void matrix_init_quantum(void) {
58 __attribute__ ((weak))
59 void matrix_scan_quantum(void) {
63 __attribute__ ((weak))
64 void matrix_init_kb(void) {
68 __attribute__ ((weak))
69 void matrix_scan_kb(void) {
73 __attribute__ ((weak))
74 void matrix_init_user(void) {
77 __attribute__ ((weak))
78 void matrix_scan_user(void) {
81 uint8_t matrix_rows(void) {
85 uint8_t matrix_cols(void) {
89 void matrix_power_up(void) {
90 #if DIODE_DIRECTION == COL2ROW
91 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
93 _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
96 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
98 _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
101 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
103 _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
106 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
108 _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
113 void matrix_init(void) {
114 /* frees PORTF by setting the JTD bit twice within four cycles */
115 #ifdef __AVR_ATmega32U4__
119 /* initializes the I/O pins */
120 #if DIODE_DIRECTION == COL2ROW
121 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
123 _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
126 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
128 _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
131 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
133 _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
136 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
138 _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
141 matrix_init_quantum();
144 #if DIODE_DIRECTION == COL2ROW
145 uint8_t matrix_scan(void) {
146 static matrix_row_t debouncing_matrix[MATRIX_ROWS];
147 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
149 matrix_row_t state = read_cols();
150 if (debouncing_matrix[r] != state) {
151 debouncing_matrix[r] = state;
152 debouncing_delay = DEBOUNCING_DELAY;
156 if (debouncing_delay >= 0) {
157 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
159 if (debouncing_delay >= 0) {
163 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
164 matrix[r] = debouncing_matrix[r];
168 matrix_scan_quantum();
172 static void toggle_row(uint8_t row) {
174 _SFR_IO8((row_pins[row] >> 4)) = _BV(row_pins[row] & 0xF);
177 static matrix_row_t read_cols(void) {
178 matrix_row_t state = 0;
179 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
181 if (!(_SFR_IO8((col_pins[c] >> 4)) & _BV(col_pins[c] & 0xF))) {
182 state |= (matrix_row_t)1 << c;
188 matrix_row_t matrix_get_row(uint8_t row) {
193 uint8_t matrix_scan(void) {
194 static matrix_col_t debouncing_matrix[MATRIX_COLS];
195 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
197 matrix_col_t state = read_rows();
198 if (debouncing_matrix[c] != state) {
199 debouncing_matrix[c] = state;
200 debouncing_delay = DEBOUNCING_DELAY;
204 if (debouncing_delay >= 0) {
205 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
207 if (debouncing_delay >= 0) {
211 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
212 matrix[c] = debouncing_matrix[c];
216 matrix_scan_quantum();
220 static void toggle_col(uint8_t col) {
222 _SFR_IO8((col_pins[col] >> 4)) = _BV(col_pins[col] & 0xF);
225 static matrix_col_t read_rows(void) {
226 matrix_col_t state = 0;
227 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
229 if (!(_SFR_IO8((row_pins[r] >> 4)) & _BV(row_pins[r] & 0xF))) {
230 state |= (matrix_col_t)1 << r;
236 matrix_row_t matrix_get_row(uint8_t row) {
237 matrix_row_t state = 0;
238 matrix_col_t mask = (matrix_col_t)1 << row;
239 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
240 if (matrix[c] & mask) {
241 state |= (matrix_row_t)1 << c;
249 bool matrix_is_modified(void) {
250 if (debouncing_delay >= 0) return false;
254 bool matrix_is_on(uint8_t row, uint8_t col) {
255 return matrix_get_row(row) & (matrix_row_t)1 << col;
258 void matrix_print(void) {
259 dprintln("Human-readable matrix state:");
260 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
261 dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
265 uint8_t matrix_key_count(void) {
267 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
268 count += bitpop16(matrix_get_row(r));