2 Copyright 2016-2017 Ralf Schmitt <ralf@bunkertor.net> Rasmus Schults <rasmusx@gmail.com>
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <util/delay.h>
25 static uint8_t debouncing = DEBOUNCE;
27 /* matrix state(1:on, 0:off) */
28 static matrix_row_t matrix[MATRIX_ROWS];
29 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
31 static uint8_t read_rows(void);
32 static uint8_t read_fwkey(void);
33 static void init_rows(void);
34 static void unselect_cols(void);
35 static void select_col(uint8_t col);
38 __attribute__ ((weak))
39 void matrix_init_kb(void) {
43 __attribute__ ((weak))
44 void matrix_scan_kb(void) {
48 __attribute__ ((weak))
49 void matrix_init_user(void) {
52 __attribute__ ((weak))
53 void matrix_scan_user(void) {
56 void matrix_init(void) {
68 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
70 matrix_debouncing[i] = 0;
73 matrix_init_quantum();
76 uint8_t matrix_scan(void) {
77 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
81 uint8_t rows = read_rows();
85 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
86 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
87 bool curr_bit = rows & (1<<row);
88 if (prev_bit != curr_bit) {
89 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
91 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
93 debouncing = DEBOUNCE;
103 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
104 matrix[i] = matrix_debouncing[i];
109 matrix_scan_quantum();
113 inline matrix_row_t matrix_get_row(uint8_t row) {
117 void matrix_print(void) {
118 print("\nr/c 0123456789ABCDEF\n");
119 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
120 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
124 /* Row pin configuration
126 * pin: PB7 PD0 PD1 PD2 PD3 PD5
128 * Esc uses its own pin PE2
130 static void init_rows(void) {
132 PORTB &= ~0b10000000;
135 PORTD &= ~0b00101111;
141 static uint8_t read_rows() {
142 return (PINB&(1<<7) ? (1<<0) : 0) |
143 (PIND&(1<<0) ? (1<<1) : 0) |
144 (PIND&(1<<1) ? (1<<2) : 0) |
145 (PIND&(1<<2) ? (1<<3) : 0) |
146 (PIND&(1<<3) ? (1<<4) : 0) |
147 (PIND&(1<<5) ? (1<<5) : 0);
150 static uint8_t read_fwkey(void)
152 return PINE&(1<<2) ? 0 : (1<<0);
156 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
157 * col / pin: PC6 PB6 PF0 PF1 PC7
184 static void unselect_cols(void) {
186 PORTB &= ~0b01110000;
188 DDRC |= (1<<6) | (1<<7);
189 PORTC &= ~((1<<6) | (1<<7));
191 DDRF |= (1<<0) | (1<<1);
192 PORTF &= ~((1<<0) | (1<<1));
198 static void select_col(uint8_t col) {
213 PORTF |= (1<<0) | (1<<1);
231 PORTF |= (1<<0) | (1<<1);
247 PORTF |= (1<<0) | (1<<1);
265 PORTF |= (1<<0) | (1<<1);