2 Copyright 2017 Luiz Ribeiro <luizribeiro@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 static uint8_t debouncing = DEBOUNCE;
29 static matrix_row_t matrix[MATRIX_ROWS];
30 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
32 void matrix_init(void) {
33 // all outputs for rows high
36 // all inputs for columns
38 DDRC &= ~(0x111111<<2);
40 // all columns are pulled-up
42 PORTC |= (0b111111<<2);
45 // initialize matrix state: all keys off
46 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
48 matrix_debouncing[row] = 0x00;
53 //_SFR_IO8(0x09) |= (1 << 4); //_BV(0x94 & 0xF);
55 // this is the code that *should* be executed in quantum.c
56 _SFR_IO8(0x12) |= _BV(0x4);
59 void matrix_set_row_status(uint8_t row) {
64 uint8_t bit_reverse(uint8_t x) {
65 x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
66 x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
67 x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
71 uint8_t matrix_scan(void) {
72 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
73 matrix_set_row_status(row);
77 // cols 0..7, PORTA 0 -> 7
80 // cols 8..13, PORTC 7 -> 0
81 bit_reverse((~PINC) & 0xFF) << 8
84 ((~PIND) & (1 << PIND7)) << 7
87 if (matrix_debouncing[row] != cols) {
88 matrix_debouncing[row] = cols;
89 debouncing = DEBOUNCE;
97 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
98 matrix[i] = matrix_debouncing[i];
108 inline matrix_row_t matrix_get_row(uint8_t row) {
112 void matrix_print(void) {