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/>.
30 extern const matrix_row_t matrix_mask[];
33 /* Set 0 if debouncing isn't needed */
35 #ifndef DEBOUNCING_DELAY
36 # define DEBOUNCING_DELAY 5
38 static uint8_t debouncing = DEBOUNCING_DELAY;
40 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
41 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
43 /* matrix state(1:on, 0:off) */
44 static matrix_row_t matrix[MATRIX_ROWS];
45 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
47 #if DIODE_DIRECTION == ROW2COL
48 static matrix_row_t matrix_reversed[MATRIX_COLS];
49 static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
58 static matrix_row_t read_cols(void);
59 static void init_cols(void);
60 static void unselect_rows(void);
61 static void select_row(uint8_t row);
63 __attribute__ ((weak))
64 void matrix_init_quantum(void) {
68 __attribute__ ((weak))
69 void matrix_scan_quantum(void) {
73 __attribute__ ((weak))
74 void matrix_init_kb(void) {
78 __attribute__ ((weak))
79 void matrix_scan_kb(void) {
83 __attribute__ ((weak))
84 void matrix_init_user(void) {
87 __attribute__ ((weak))
88 void matrix_scan_user(void) {
92 uint8_t matrix_rows(void) {
97 uint8_t matrix_cols(void) {
101 // void matrix_power_up(void) {
102 // #if DIODE_DIRECTION == COL2ROW
103 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
105 // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
108 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
110 // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
113 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
115 // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
118 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
120 // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
125 void matrix_init(void) {
126 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
127 #ifdef __AVR_ATmega32U4__
132 // initialize row and col
136 // initialize matrix state: all keys off
137 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
139 matrix_debouncing[i] = 0;
142 matrix_init_quantum();
145 uint8_t matrix_scan(void)
148 #if DIODE_DIRECTION == COL2ROW
149 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
151 wait_us(30); // without this wait read unstable value.
152 matrix_row_t cols = read_cols();
153 if (matrix_debouncing[i] != cols) {
154 matrix_debouncing[i] = cols;
156 debug("bounce!: "); debug_hex(debouncing); debug("\n");
158 debouncing = DEBOUNCING_DELAY;
167 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
168 matrix[i] = matrix_debouncing[i];
173 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
175 wait_us(30); // without this wait read unstable value.
176 matrix_row_t rows = read_cols();
177 if (matrix_reversed_debouncing[i] != rows) {
178 matrix_reversed_debouncing[i] = rows;
180 debug("bounce!: "); debug_hex(debouncing); debug("\n");
182 debouncing = DEBOUNCING_DELAY;
191 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
192 matrix_reversed[i] = matrix_reversed_debouncing[i];
196 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
197 matrix_row_t row = 0;
198 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
199 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
205 matrix_scan_quantum();
210 bool matrix_is_modified(void)
212 if (debouncing) return false;
217 bool matrix_is_on(uint8_t row, uint8_t col)
219 return (matrix[row] & ((matrix_row_t)1<col));
223 matrix_row_t matrix_get_row(uint8_t row)
225 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
226 // switch blocker installed and the switch is always pressed.
228 return matrix[row] & matrix_mask[row];
234 void matrix_print(void)
236 #if (MATRIX_COLS <= 8)
237 print("\nr/c 01234567\n");
238 #elif (MATRIX_COLS <= 16)
239 print("\nr/c 0123456789ABCDEF\n");
240 #elif (MATRIX_COLS <= 32)
241 print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n");
244 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
245 phex(row); print(": ");
246 #if (MATRIX_COLS <= 8)
247 print_bin_reverse8(matrix_get_row(row));
248 #elif (MATRIX_COLS <= 16)
249 print_bin_reverse16(matrix_get_row(row));
250 #elif (MATRIX_COLS <= 32)
251 print_bin_reverse32(matrix_get_row(row));
257 uint8_t matrix_key_count(void)
260 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
261 #if (MATRIX_COLS <= 8)
262 count += bitpop(matrix[i]);
263 #elif (MATRIX_COLS <= 16)
264 count += bitpop16(matrix[i]);
265 #elif (MATRIX_COLS <= 32)
266 count += bitpop32(matrix[i]);
272 static void init_cols(void)
274 #if DIODE_DIRECTION == COL2ROW
275 for(int x = 0; x < MATRIX_COLS; x++) {
276 int pin = col_pins[x];
278 for(int x = 0; x < MATRIX_ROWS; x++) {
279 int pin = row_pins[x];
281 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
282 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
286 static matrix_row_t read_cols(void)
288 matrix_row_t result = 0;
290 #if DIODE_DIRECTION == COL2ROW
291 for(int x = 0; x < MATRIX_COLS; x++) {
292 int pin = col_pins[x];
294 for(int x = 0; x < MATRIX_ROWS; x++) {
295 int pin = row_pins[x];
297 result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x);
302 static void unselect_rows(void)
304 #if DIODE_DIRECTION == COL2ROW
305 for(int x = 0; x < MATRIX_ROWS; x++) {
306 int pin = row_pins[x];
308 for(int x = 0; x < MATRIX_COLS; x++) {
309 int pin = col_pins[x];
311 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
312 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
316 static void select_row(uint8_t row)
319 #if DIODE_DIRECTION == COL2ROW
320 int pin = row_pins[row];
322 int pin = col_pins[row];
324 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);
325 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF);