2 Copyright 2017 Gabriel Young <gabeplaysdrums@live.com>
3 Copyright 2018 Alexander Fougner <fougner89 at gmail.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <util/delay.h>
28 #ifndef DEBOUNCING_DELAY
29 # define DEBOUNCING_DELAY 5
31 static uint8_t debouncing = DEBOUNCING_DELAY;
33 static matrix_row_t matrix[MATRIX_ROWS];
34 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
36 static matrix_row_t scan_col(void) {
38 (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) |
39 (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<1)) |
40 (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<2)) |
41 (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<3)) |
42 (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<4)) |
43 (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<5)) |
44 (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<6)) |
45 (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<7))
49 static void select_col(uint8_t col) {
51 case 0: PORTD = (PORTD & ~0b00111111) | 0b00110011; break;
52 case 1: PORTD = (PORTD & ~0b00111111) | 0b00100011; break;
53 case 2: PORTD = (PORTD & ~0b00111111) | 0b00010111; break;
54 case 3: PORTD = (PORTD & ~0b00111111) | 0b00110011; break;
55 case 4: PORTD = (PORTD & ~0b00111111) | 0b00010011; break;
56 case 5: PORTD = (PORTD & ~0b00111111) | 0b00011011; break;
57 case 6: PORTD = (PORTD & ~0b00111111) | 0b00110100; break;
58 case 7: PORTD = (PORTD & ~0b00111111) | 0b00111010; break;
59 case 8: PORTD = (PORTD & ~0b00111111) | 0b00111000; break;
60 case 9: PORTD = (PORTD & ~0b00111111) | 0b00111100; break;
61 case 10: PORTD = (PORTD & ~0b00111111) | 0b00110010; break;
62 case 11: PORTD = (PORTD & ~0b00111111) | 0b00011111; break;
63 case 12: PORTD = (PORTD & ~0b00111111) | 0b00001111; break;
64 case 13: PORTD = (PORTD & ~0b00111111) | 0b00100111; break;
65 case 14: PORTD = (PORTD & ~0b00111111) | 0b00000111; break;
66 case 15: PORTD = (PORTD & ~0b00111111) | 0b00110110; break;
67 case 16: PORTD = (PORTD & ~0b00111111) | 0b00001011; break;
68 case 17: PORTD = (PORTD & ~0b00111111) | 0b00000011; break;
72 void matrix_init(void) {
75 /* Column input pins */
81 for (uint8_t i=0; i < MATRIX_ROWS; i++)
82 matrix[i] = matrix_debouncing[i] = 0;
84 matrix_init_quantum();
87 uint8_t matrix_scan(void) {
88 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
91 matrix_row_t col_scan = scan_col();
92 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
93 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
94 bool curr_bit = col_scan & (1<<row);
95 if (prev_bit != curr_bit) {
96 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
97 debouncing = DEBOUNCING_DELAY;
106 for (uint8_t i = 0; i < MATRIX_ROWS; i++)
107 matrix[i] = matrix_debouncing[i];
110 matrix_scan_quantum();
114 inline matrix_row_t matrix_get_row(uint8_t row) {
118 void matrix_print(void) {
120 print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
121 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
122 matrix_row_t matrix_row = matrix_get_row(row);
123 xprintf("%02X: ", row);
124 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
125 bool curr_bit = matrix_row & (1<<col);
126 xprintf("%c", curr_bit ? '*' : '.');
133 uint8_t matrix_key_count(void) {
135 for (uint8_t row = 0; row < MATRIX_ROWS; row++)
136 count += bitpop32(matrix[row]);