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 = DEBOUNCE;
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 __attribute__ ((weak))
37 void matrix_init_kb(void) {
41 __attribute__ ((weak))
42 void matrix_scan_kb(void) {
46 __attribute__ ((weak))
47 void matrix_init_user(void) {
50 __attribute__ ((weak))
51 void matrix_scan_user(void) {
54 inline uint8_t matrix_rows(void)
59 inline uint8_t matrix_cols(void)
64 void matrix_init(void)
66 /* Column output pins */
74 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
76 matrix_debouncing[i] = 0;
79 matrix_init_quantum();
82 uint8_t matrix_scan(void)
84 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
87 matrix_row_t rows = read_cols();
88 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
89 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
90 bool curr_bit = rows & (1<<row);
91 if (prev_bit != curr_bit) {
92 matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
93 debouncing = DEBOUNCE;
102 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103 matrix[i] = matrix_debouncing[i];
108 matrix_scan_quantum();
113 bool matrix_is_modified(void)
121 bool matrix_is_on(uint8_t row, uint8_t col)
123 return matrix[row] & 1 << col;
127 matrix_row_t matrix_get_row(uint8_t row)
132 void matrix_print(void)
134 print("\nr/c 0123456789ABCDEF\n");
135 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
136 phex(row); print(": ");
137 pbin_reverse16(matrix_get_row(row));
142 uint8_t matrix_key_count(void)
145 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
146 count += bitpop16(matrix[i]);
151 static matrix_row_t read_cols(void)
154 (PINB & (1 << 5) ? 0 : 1 << 0) |
155 (PINC & (1 << 7) ? 0 : 1 << 1) |
156 (PINB & (1 << 4) ? 0 : 1 << 2) |
157 (PINB & (1 << 6) ? 0 : 1 << 3) |
158 (PINB & (1 << 1) ? 0 : 1 << 4) |
159 (PINB & (1 << 0) ? 0 : 1 << 5) |
160 (PINB & (1 << 3) ? 0 : 1 << 6) |
161 (PINB & (1 << 2) ? 0 : 1 << 7);
164 static void select_row(uint8_t col)
168 PORTD = (PORTD & ~0b01111011) | 0b00110011;
171 PORTD = (PORTD & ~0b01111011) | 0b01110000;
174 PORTD = (PORTD & ~0b01111011) | 0b00010011;
177 PORTD = (PORTD & ~0b01111011) | 0b01101000;
180 PORTD = (PORTD & ~0b01111011) | 0b00001011;
183 PORTD = (PORTD & ~0b01111011) | 0b00111011;
186 PORTD = (PORTD & ~0b01111011) | 0b01111000;
189 PORTD = (PORTD & ~0b01111011) | 0b01100001;
192 PORTD = (PORTD & ~0b01111011) | 0b01101001;
195 PORTD = (PORTD & ~0b01111011) | 0b01110001;
198 PORTD = (PORTD & ~0b01111011) | 0b01101010;
201 PORTD = (PORTD & ~0b01111011) | 0b01100010;
204 PORTD = (PORTD & ~0b01111011) | 0b01111001;
207 PORTD = (PORTD & ~0b01111011) | 0b01100000;
210 PORTD = (PORTD & ~0b01111011) | 0b01000011;
213 PORTD = (PORTD & ~0b01111011) | 0b00011011;
216 PORTD = (PORTD & ~0b01111011) | 0b00100011;
219 PORTD = (PORTD & ~0b01111011) | 0b00101011;