2 Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
3 Copyright 2016 Daniel Svensson <dsvensson@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>
29 static uint8_t debouncing = DEBOUNCING_DELAY;
30 static matrix_row_t matrix[MATRIX_ROWS];
31 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
33 static matrix_row_t read_cols(void);
34 static void select_row(uint8_t col);
36 inline uint8_t matrix_rows(void)
41 inline uint8_t matrix_cols(void)
46 void matrix_init(void)
48 /* Column output pins */
56 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
58 matrix_debouncing[i] = 0;
61 matrix_init_quantum();
64 uint8_t matrix_scan(void)
66 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
69 matrix_row_t rows = read_cols();
70 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
71 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
72 bool curr_bit = rows & (1<<row);
73 if (prev_bit != curr_bit) {
74 matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
75 debouncing = DEBOUNCING_DELAY;
84 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
85 matrix[i] = matrix_debouncing[i];
90 matrix_scan_quantum();
95 bool matrix_is_modified(void)
103 bool matrix_is_on(uint8_t row, uint8_t col)
105 return matrix[row] & 1 << col;
109 matrix_row_t matrix_get_row(uint8_t row)
114 void matrix_print(void)
116 print("\nr/c 0123456789ABCDEF\n");
117 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
118 phex(row); print(": ");
119 pbin_reverse16(matrix_get_row(row));
124 uint8_t matrix_key_count(void)
127 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
128 count += bitpop16(matrix[i]);
133 static matrix_row_t read_cols(void)
136 (PINB & (1 << 5) ? 0 : 1 << 0) |
137 (PINC & (1 << 7) ? 0 : 1 << 1) |
138 (PINB & (1 << 4) ? 0 : 1 << 2) |
139 (PINB & (1 << 6) ? 0 : 1 << 3) |
140 (PINB & (1 << 1) ? 0 : 1 << 4) |
141 (PINB & (1 << 0) ? 0 : 1 << 5) |
142 (PINB & (1 << 3) ? 0 : 1 << 6) |
143 (PINB & (1 << 2) ? 0 : 1 << 7);
146 static void select_row(uint8_t col)
150 PORTD = (PORTD & ~0b01111011) | 0b00110011;
153 PORTD = (PORTD & ~0b01111011) | 0b01110000;
156 PORTD = (PORTD & ~0b01111011) | 0b00010011;
159 PORTD = (PORTD & ~0b01111011) | 0b01101000;
162 PORTD = (PORTD & ~0b01111011) | 0b00001011;
165 PORTD = (PORTD & ~0b01111011) | 0b00111011;
168 PORTD = (PORTD & ~0b01111011) | 0b01111000;
171 PORTD = (PORTD & ~0b01111011) | 0b01100001;
174 PORTD = (PORTD & ~0b01111011) | 0b01101001;
177 PORTD = (PORTD & ~0b01111011) | 0b01110001;
180 PORTD = (PORTD & ~0b01111011) | 0b01101010;
183 PORTD = (PORTD & ~0b01111011) | 0b01100010;
186 PORTD = (PORTD & ~0b01111011) | 0b01111001;
189 PORTD = (PORTD & ~0b01111011) | 0b01100000;
192 PORTD = (PORTD & ~0b01111011) | 0b01000011;
195 PORTD = (PORTD & ~0b01111011) | 0b00011011;
198 PORTD = (PORTD & ~0b01111011) | 0b00100011;
201 PORTD = (PORTD & ~0b01111011) | 0b00101011;