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/>.
29 #ifdef MATRIX_HAS_GHOST
30 # error "The universal matrix.c file cannot be used for this keyboard."
33 #ifndef DEBOUNCING_DELAY
34 # define DEBOUNCING_DELAY 5
37 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
38 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
40 #if DIODE_DIRECTION == COL2ROW
41 static matrix_row_t matrix[MATRIX_ROWS];
43 static matrix_col_t matrix[MATRIX_COLS];
45 static int8_t debouncing_delay = -1;
47 #if DIODE_DIRECTION == COL2ROW
48 static void toggle_row(uint8_t row);
49 static matrix_row_t read_cols(void);
51 static void toggle_col(uint8_t col);
52 static matrix_col_t read_rows(void);
55 __attribute__ ((weak))
56 void matrix_init_quantum(void) {
60 __attribute__ ((weak))
61 void matrix_scan_quantum(void) {
65 __attribute__ ((weak))
66 void matrix_init_kb(void) {
70 __attribute__ ((weak))
71 void matrix_scan_kb(void) {
75 __attribute__ ((weak))
76 void matrix_init_user(void) {
79 __attribute__ ((weak))
80 void matrix_scan_user(void) {
83 uint8_t matrix_rows(void) {
87 uint8_t matrix_cols(void) {
91 // void matrix_power_up(void) {
92 // #if DIODE_DIRECTION == COL2ROW
93 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
95 // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
98 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
100 // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
103 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
105 // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
108 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
110 // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
115 void matrix_init(void) {
116 /* frees PORTF by setting the JTD bit twice within four cycles */
117 #ifdef __AVR_ATmega32U4__
121 /* initializes the I/O pins */
122 #if DIODE_DIRECTION == COL2ROW
123 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
125 _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
128 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
130 _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
133 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
135 _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
138 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
140 _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
143 matrix_init_quantum();
146 #if DIODE_DIRECTION == COL2ROW
147 uint8_t matrix_scan(void) {
148 static matrix_row_t debouncing_matrix[MATRIX_ROWS];
149 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
151 matrix_row_t state = read_cols();
152 if (debouncing_matrix[r] != state) {
153 debouncing_matrix[r] = state;
154 debouncing_delay = DEBOUNCING_DELAY;
158 if (debouncing_delay >= 0) {
159 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
161 if (debouncing_delay >= 0) {
165 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
166 matrix[r] = debouncing_matrix[r];
170 matrix_scan_quantum();
174 static void toggle_row(uint8_t row) {
176 _SFR_IO8((row_pins[row] >> 4)) = _BV(row_pins[row] & 0xF);
179 static matrix_row_t read_cols(void) {
180 matrix_row_t state = 0;
181 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
183 if (!(_SFR_IO8((col_pins[c] >> 4)) & _BV(col_pins[c] & 0xF))) {
184 state |= (matrix_row_t)1 << c;
190 matrix_row_t matrix_get_row(uint8_t row) {
195 uint8_t matrix_scan(void) {
196 static matrix_col_t debouncing_matrix[MATRIX_COLS];
197 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
199 matrix_col_t state = read_rows();
200 if (debouncing_matrix[c] != state) {
201 debouncing_matrix[c] = state;
202 debouncing_delay = DEBOUNCING_DELAY;
206 if (debouncing_delay >= 0) {
207 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
209 if (debouncing_delay >= 0) {
213 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
214 matrix[c] = debouncing_matrix[c];
218 matrix_scan_quantum();
222 static void toggle_col(uint8_t col) {
224 _SFR_IO8((col_pins[col] >> 4)) = _BV(col_pins[col] & 0xF);
227 static matrix_col_t read_rows(void) {
228 matrix_col_t state = 0;
229 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
231 if (!(_SFR_IO8((row_pins[r] >> 4)) & _BV(row_pins[r] & 0xF))) {
232 state |= (matrix_col_t)1 << r;
238 matrix_row_t matrix_get_row(uint8_t row) {
239 matrix_row_t state = 0;
240 matrix_col_t mask = (matrix_col_t)1 << row;
241 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
242 if (matrix[c] & mask) {
243 state |= (matrix_row_t)1 << c;
251 bool matrix_is_modified(void) {
252 if (debouncing_delay >= 0) return false;
256 bool matrix_is_on(uint8_t row, uint8_t col) {
257 return matrix_get_row(row) & (matrix_row_t)1 << col;
260 void matrix_print(void) {
261 dprintln("Human-readable matrix state:");
262 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
263 dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
267 uint8_t matrix_key_count(void) {
269 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
270 count += bitpop16(matrix_get_row(r));