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 = DEBOUNCING_DELAY;
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);
90 debouncing = DEBOUNCING_DELAY;
100 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101 matrix[i] = matrix_debouncing[i];
106 matrix_scan_quantum();
110 inline matrix_row_t matrix_get_row(uint8_t row) {
114 void matrix_print(void) {
115 print("\nr/c 0123456789ABCDEF\n");
116 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
117 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
121 /* Row pin configuration
123 * pin: PB7 PD0 PD1 PD2 PD3 PD5
125 * Esc uses its own pin PE2
127 static void init_rows(void) {
129 PORTB &= ~0b10000000;
132 PORTD &= ~0b00101111;
138 static uint8_t read_rows() {
139 return (PINB&(1<<7) ? (1<<0) : 0) |
140 (PIND&(1<<0) ? (1<<1) : 0) |
141 (PIND&(1<<1) ? (1<<2) : 0) |
142 (PIND&(1<<2) ? (1<<3) : 0) |
143 (PIND&(1<<3) ? (1<<4) : 0) |
144 (PIND&(1<<5) ? (1<<5) : 0);
147 static uint8_t read_fwkey(void)
149 return PINE&(1<<2) ? 0 : (1<<0);
153 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
154 * col / pin: PC6 PB6 PF0 PF1 PC7
181 static void unselect_cols(void) {
183 PORTB &= ~0b01110000;
185 DDRC |= (1<<6) | (1<<7);
186 PORTC &= ~((1<<6) | (1<<7));
188 DDRF |= (1<<0) | (1<<1);
189 PORTF &= ~((1<<0) | (1<<1));
195 static void select_col(uint8_t col) {
210 PORTF |= (1<<0) | (1<<1);
228 PORTF |= (1<<0) | (1<<1);
244 PORTF |= (1<<0) | (1<<1);
262 PORTF |= (1<<0) | (1<<1);