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);
37 __attribute__ ((weak))
38 void matrix_init_kb(void) {
42 __attribute__ ((weak))
43 void matrix_scan_kb(void) {
47 __attribute__ ((weak))
48 void matrix_init_user(void) {
51 __attribute__ ((weak))
52 void matrix_scan_user(void) {
55 void backlight_init_ports(void)
67 void matrix_init(void) {
68 backlight_init_ports();
72 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
74 matrix_debouncing[i] = 0;
77 matrix_init_quantum();
80 uint8_t matrix_scan(void) {
81 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
85 uint8_t rows = read_rows(col);
87 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
88 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
89 bool curr_bit = rows & (1<<row);
90 if (prev_bit != curr_bit) {
91 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
92 debouncing = DEBOUNCING_DELAY;
102 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103 matrix[i] = matrix_debouncing[i];
108 matrix_scan_quantum();
112 inline matrix_row_t matrix_get_row(uint8_t row) {
116 void matrix_print(void) {
117 print("\nr/c 0123456789ABCDEF\n");
118 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
119 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
123 /* Row pin configuration
125 * pin: PB7 PD0 PD1 PD2 PD3 PD5
127 * Esc uses its own pin PE2
129 static void init_rows(void) {
131 PORTD &= ~0b00101111;
134 PORTB &= ~0b10000000;
140 static uint8_t read_rows(uint8_t col) {
142 return (PIND&(1<<0) ? (1<<0) : 0) |
143 (PIND&(1<<1) ? (1<<1) : 0) |
144 (PIND&(1<<2) ? (1<<2) : 0) |
145 (PIND&(1<<3) ? (1<<3) : 0) |
146 (PIND&(1<<5) ? (1<<4) : 0) |
147 (PINB&(1<<7) ? (1<<5) : 0) |
148 (col==0 ? ((PINE&(1<<2) ? 0 : (1<<2))) : 0);
152 uint8_t read_fwkey(void)
154 return PINE&(1<<2) ? 0 : (1<<2);
158 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
159 * col / pin: PC6 PB6 PF0 PF1 PC7
178 static void unselect_cols(void) {
180 PORTB &= ~0b01000000;
183 PORTC &= ~0b11000000;
186 PORTF &= ~0b00000011;
189 static void select_col(uint8_t col) {