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 io_pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
36 static const io_pin_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) {
57 __attribute__ ((weak))
58 void matrix_scan_quantum(void) {
61 uint8_t matrix_rows(void) {
65 uint8_t matrix_cols(void) {
69 void matrix_power_up(void) {
70 #if DIODE_DIRECTION == COL2ROW
71 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
73 _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
76 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
78 _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
81 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
83 _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
86 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
88 _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
93 void matrix_init(void) {
94 /* frees PORTF by setting the JTD bit twice within four cycles */
95 #ifdef __AVR_ATmega32U4__
99 /* initializes the I/O pins */
100 #if DIODE_DIRECTION == COL2ROW
101 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
103 _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
106 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
108 _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
111 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
113 _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
116 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
118 _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
121 matrix_init_quantum();
124 #if DIODE_DIRECTION == COL2ROW
125 uint8_t matrix_scan(void) {
126 static matrix_row_t debouncing_matrix[MATRIX_ROWS];
127 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
129 matrix_row_t state = read_cols();
130 if (debouncing_matrix[r] != state) {
131 debouncing_matrix[r] = state;
132 debouncing_delay = DEBOUNCING_DELAY;
136 if (debouncing_delay >= 0) {
137 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
139 if (debouncing_delay >= 0) {
143 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
144 matrix[r] = debouncing_matrix[r];
148 matrix_scan_quantum();
152 static void toggle_row(uint8_t row) {
154 _SFR_IO8(row_pins[row].input_addr) = _BV(row_pins[row].bit);
157 static matrix_row_t read_cols(void) {
158 matrix_row_t state = 0;
159 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
161 if (!(_SFR_IO8(col_pins[c].input_addr) & _BV(col_pins[c].bit))) {
162 state |= (matrix_row_t)1 << c;
168 matrix_row_t matrix_get_row(uint8_t row) {
173 uint8_t matrix_scan(void) {
174 static matrix_col_t debouncing_matrix[MATRIX_COLS];
175 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
177 matrix_col_t state = read_rows();
178 if (debouncing_matrix[c] != state) {
179 debouncing_matrix[c] = state;
180 debouncing_delay = DEBOUNCING_DELAY;
184 if (debouncing_delay >= 0) {
185 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
187 if (debouncing_delay >= 0) {
191 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
192 matrix[c] = debouncing_matrix[c];
196 matrix_scan_quantum();
200 static void toggle_col(uint8_t col) {
202 _SFR_IO8(col_pins[col].input_addr) = _BV(col_pins[col].bit);
205 static matrix_col_t read_rows(void) {
206 matrix_col_t state = 0;
207 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
209 if (!(_SFR_IO8(row_pins[r].input_addr) & _BV(row_pins[r].bit))) {
210 state |= (matrix_col_t)1 << r;
216 matrix_row_t matrix_get_row(uint8_t row) {
217 matrix_row_t state = 0;
218 matrix_col_t mask = (matrix_col_t)1 << row;
219 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
220 if (matrix[c] & mask) {
221 state |= (matrix_row_t)1 << c;
229 bool matrix_is_modified(void) {
230 if (debouncing_delay >= 0) return false;
234 bool matrix_is_on(uint8_t row, uint8_t col) {
235 return matrix_get_row(row) & (matrix_row_t)1 << col;
238 void matrix_print(void) {
239 dprintln("Human-readable matrix state:");
240 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
241 dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
245 uint8_t matrix_key_count(void) {
247 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
248 count += bitpop16(matrix_get_row(r));