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 */
71 #ifdef __AVR_ATmega32U4__
75 /* initializes the I/O pins */
76 #if DIODE_DIRECTION == COL2ROW
77 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
79 _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
82 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
84 _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
87 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
89 _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
92 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
94 _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
97 matrix_init_quantum();
100 #if DIODE_DIRECTION == COL2ROW
101 uint8_t matrix_scan(void) {
102 static matrix_row_t debouncing_matrix[MATRIX_ROWS];
103 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
105 matrix_row_t state = read_cols();
106 if (debouncing_matrix[r] != state) {
107 debouncing_matrix[r] = state;
108 debouncing_delay = DEBOUNCING_DELAY;
112 if (debouncing_delay >= 0) {
113 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
115 if (debouncing_delay >= 0) {
119 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
120 matrix[r] = debouncing_matrix[r];
124 matrix_scan_quantum();
128 static void toggle_row(uint8_t row) {
130 _SFR_IO8(row_pins[row].input_addr) = _BV(row_pins[row].bit);
133 static matrix_row_t read_cols(void) {
134 matrix_row_t state = 0;
135 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
137 if (!(_SFR_IO8(col_pins[c].input_addr) & _BV(col_pins[c].bit))) {
138 state |= (matrix_row_t)1 << c;
144 matrix_row_t matrix_get_row(uint8_t row) {
149 uint8_t matrix_scan(void) {
150 static matrix_col_t debouncing_matrix[MATRIX_COLS];
151 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
153 matrix_col_t state = read_rows();
154 if (debouncing_matrix[c] != state) {
155 debouncing_matrix[c] = state;
156 debouncing_delay = DEBOUNCING_DELAY;
160 if (debouncing_delay >= 0) {
161 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
163 if (debouncing_delay >= 0) {
167 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
168 matrix[c] = debouncing_matrix[c];
172 matrix_scan_quantum();
176 static void toggle_col(uint8_t col) {
178 _SFR_IO8(col_pins[col].input_addr) = _BV(col_pins[col].bit);
181 static matrix_col_t read_rows(void) {
182 matrix_col_t state = 0;
183 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
185 if (!(_SFR_IO8(row_pins[r].input_addr) & _BV(row_pins[r].bit))) {
186 state |= (matrix_col_t)1 << r;
192 matrix_row_t matrix_get_row(uint8_t row) {
193 matrix_row_t state = 0;
194 matrix_col_t mask = (matrix_col_t)1 << row;
195 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
196 if (matrix[c] & mask) {
197 state |= (matrix_row_t)1 << c;
205 bool matrix_is_modified(void) {
206 if (debouncing_delay >= 0) return false;
210 bool matrix_is_on(uint8_t row, uint8_t col) {
211 return matrix_get_row(row) & (matrix_row_t)1 << col;
214 void matrix_print(void) {
215 dprintln("Human-readable matrix state:");
216 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
217 dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
221 uint8_t matrix_key_count(void) {
223 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
224 count += bitpop16(matrix_get_row(r));