2 Copyright 2017 MechMerlin <mechmerlin@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(uint8_t col);
32 static void init_rows(void);
33 static void unselect_cols(void);
34 static void select_col(uint8_t col);
36 __attribute__ ((weak))
37 void matrix_init_quantum(void) {
41 __attribute__ ((weak))
42 void matrix_scan_quantum(void) {
46 __attribute__ ((weak))
47 void matrix_init_kb(void) {
51 __attribute__ ((weak))
52 void matrix_scan_kb(void) {
56 __attribute__ ((weak))
57 void matrix_init_user(void) {
60 __attribute__ ((weak))
61 void matrix_scan_user(void) {
64 void backlight_init_ports(void)
76 void matrix_init(void) {
77 backlight_init_ports();
81 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
83 matrix_debouncing[i] = 0;
86 matrix_init_quantum();
89 uint8_t matrix_scan(void) {
90 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
94 uint8_t rows = read_rows(col);
96 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
97 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
98 bool curr_bit = rows & (1<<row);
99 if (prev_bit != curr_bit) {
100 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
101 debouncing = DEBOUNCING_DELAY;
111 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
112 matrix[i] = matrix_debouncing[i];
117 matrix_scan_quantum();
121 inline matrix_row_t matrix_get_row(uint8_t row) {
125 void matrix_print(void) {
126 print("\nr/c 0123456789ABCDEF\n");
127 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
128 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
132 /* Row pin configuration
134 * pin: PB7 PD0 PD1 PD2 PD3 PD5
136 * Esc uses its own pin PE2
138 static void init_rows(void) {
140 PORTD &= ~0b00101111;
143 PORTB &= ~0b10000000;
149 static uint8_t read_rows(uint8_t col) {
151 return (PIND&(1<<0) ? (1<<0) : 0) |
152 (PIND&(1<<1) ? (1<<1) : 0) |
153 (PIND&(1<<2) ? (1<<2) : 0) |
154 (PIND&(1<<3) ? (1<<3) : 0) |
155 (PIND&(1<<5) ? (1<<4) : 0) |
156 (PINB&(1<<7) ? (1<<5) : 0) |
157 (col==0 ? ((PINE&(1<<2) ? 0 : (1<<2))) : 0);
161 uint8_t read_fwkey(void)
163 return PINE&(1<<2) ? 0 : (1<<2);
167 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
168 * col / pin: PC6 PB6 PF0 PF1 PC7
187 static void unselect_cols(void) {
189 PORTB &= ~0b01000000;
192 PORTC &= ~0b11000000;
195 PORTF &= ~0b00000011;
198 static void select_col(uint8_t col) {