2 Copyright 2018 Jack Humbert <jack.humb@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <util/delay.h>
27 __attribute__ ((weak))
28 void matrix_init_kb(void) {
32 __attribute__ ((weak))
33 void matrix_scan_kb(void) {
37 __attribute__ ((weak))
38 void matrix_init_user(void) { }
40 __attribute__ ((weak))
41 void matrix_scan_user(void) { }
43 // #define MATRIX_ROW_PINS { B3, B4, B5, B6, B7 }
44 // #define MATRIX_COL_PINS { A0, A1, A2, A3, A4, A5, A6, A7, C7, C6, C5, C4, C3, C2, D7 }
46 static uint8_t debouncing = DEBOUNCE;
48 static matrix_row_t matrix[MATRIX_ROWS];
49 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
51 void matrix_init(void) {
53 // disables JTAG so we can use them as columns
58 DDRB |= ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
59 PORTB |= ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
62 DDRA &= ~((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
63 DDRC &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2));
67 PORTA |= ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
68 PORTC |= ((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2));
71 // initialize matrix state: all keys off
72 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
74 matrix_debouncing[row] = 0x00;
77 matrix_init_quantum();
80 uint8_t matrix_scan(void) {
83 for (uint8_t c = 0; c < MATRIX_ROWS; c++) {
85 case 0: PORTB &= ~(1 << 3); break;
86 case 1: PORTB &= ~(1 << 4); break;
87 case 2: PORTB &= ~(1 << 5); break;
88 case 3: PORTB &= ~(1 << 6); break;
89 case 4: PORTB &= ~(1 << 7); break;
93 matrix_row_t current_row = (
94 (((PINA & (1 << 0)) ? 0 : 1 ) << 0) |
95 (((PINA & (1 << 1)) ? 0 : 1 ) << 1) |
96 (((PINA & (1 << 2)) ? 0 : 1 ) << 2) |
97 (((PINA & (1 << 3)) ? 0 : 1 ) << 3) |
98 (((PINA & (1 << 4)) ? 0 : 1 ) << 4) |
99 (((PINA & (1 << 5)) ? 0 : 1 ) << 5) |
100 (((PINA & (1 << 6)) ? 0 : 1 ) << 6) |
101 (((PINA & (1 << 7)) ? 0 : 1 ) << 7) |
102 (((PINC & (1 << 7)) ? 0 : 1 ) << 8) |
103 (((PINC & (1 << 6)) ? 0 : 1 ) << 9) |
104 (((PINC & (1 << 5)) ? 0 : 1 ) << 10) |
105 (((PINC & (1 << 4)) ? 0 : 1 ) << 11) |
106 (((PINC & (1 << 3)) ? 0 : 1 ) << 12) |
107 (((PINC & (1 << 2)) ? 0 : 1 ) << 13) |
108 (((PIND & (1 << 7)) ? 0 : 1 ) << 14)
112 case 0: PORTB |= (1 << 3); break;
113 case 1: PORTB |= (1 << 4); break;
114 case 2: PORTB |= (1 << 5); break;
115 case 3: PORTB |= (1 << 6); break;
116 case 4: PORTB |= (1 << 7); break;
119 if (matrix_debouncing[c] != current_row) {
120 matrix_debouncing[c] = current_row;
121 debouncing = DEBOUNCE;
129 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
130 matrix[i] = matrix_debouncing[i];
135 matrix_scan_quantum();
140 inline matrix_row_t matrix_get_row(uint8_t row) {
144 void matrix_print(void) {