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_init(void) {
70 /* frees PORTF by setting the JTD bit twice within four cycles */
73 /* initializes the I/O pins */
74 #if DIODE_DIRECTION == COL2ROW
75 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
77 _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
80 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
82 _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
85 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
87 _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
90 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
92 _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
95 matrix_init_quantum();
98 #if DIODE_DIRECTION == COL2ROW
99 uint8_t matrix_scan(void) {
100 static matrix_row_t debouncing_matrix[MATRIX_ROWS];
101 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
103 matrix_row_t state = read_cols();
104 if (debouncing_matrix[r] != state) {
105 debouncing_matrix[r] = state;
106 debouncing_delay = DEBOUNCING_DELAY;
110 if (debouncing_delay >= 0) {
111 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
113 if (debouncing_delay >= 0) {
117 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
118 matrix[r] = debouncing_matrix[r];
122 matrix_scan_quantum();
126 static void toggle_row(uint8_t row) {
128 _SFR_IO8(row_pins[row].input_addr) = _BV(row_pins[row].bit);
131 static matrix_row_t read_cols(void) {
132 matrix_row_t state = 0;
133 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
135 if (!(_SFR_IO8(col_pins[c].input_addr) & _BV(col_pins[c].bit))) {
136 state |= (matrix_row_t)1 << c;
142 matrix_row_t matrix_get_row(uint8_t row) {
147 uint8_t matrix_scan(void) {
148 static matrix_col_t debouncing_matrix[MATRIX_COLS];
149 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
151 matrix_col_t state = read_rows();
152 if (debouncing_matrix[c] != state) {
153 debouncing_matrix[c] = 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 c = MATRIX_COLS - 1; c >= 0; --c) {
166 matrix[c] = debouncing_matrix[c];
170 matrix_scan_quantum();
174 static void toggle_col(uint8_t col) {
176 _SFR_IO8(col_pins[col].input_addr) = _BV(col_pins[col].bit);
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) {
183 if (!(_SFR_IO8(row_pins[r].input_addr) & _BV(row_pins[r].bit))) {
184 state |= (matrix_col_t)1 << r;
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;
203 bool matrix_is_modified(void) {
204 if (debouncing_delay >= 0) return false;
208 bool matrix_is_on(uint8_t row, uint8_t col) {
209 return matrix_get_row(row) & (matrix_row_t)1 << col;
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)));
219 uint8_t matrix_key_count(void) {
221 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
222 count += bitpop16(matrix_get_row(r));