2 Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
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/>.
21 #include <util/delay.h>
30 static uint8_t debouncing = DEBOUNCE;
32 static matrix_row_t matrix[MATRIX_ROWS];
33 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
35 static uint8_t read_rows(void);
36 static void init_rows(void);
37 static void unselect_cols(void);
38 static void select_col(uint8_t col);
40 inline uint8_t matrix_rows(void)
45 inline uint8_t matrix_cols(void)
50 void matrix_init(void)
54 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
56 matrix_debouncing[i] = 0;
60 uint8_t matrix_scan(void)
62 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
65 uint8_t rows = read_rows();
66 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
67 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
68 bool curr_bit = rows & (1<<row);
69 if (prev_bit != curr_bit) {
70 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
71 debouncing = DEBOUNCE;
81 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
82 matrix[i] = matrix_debouncing[i];
90 bool matrix_is_modified(void)
92 if (debouncing) return false;
96 inline bool matrix_is_on(uint8_t row, uint8_t col)
98 return (matrix[row] & ((matrix_row_t)1<<col));
101 inline matrix_row_t matrix_get_row(uint8_t row)
106 void matrix_print(void)
108 print("\nr/c 0123456789ABCDEF\n");
109 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
110 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
114 uint8_t matrix_key_count(void)
117 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
118 count += bitpop32(matrix[i]);
123 /* Row pin configuration
125 * row: 0 1 2 3 4 5 6 7
126 * pin: PB1 PC2 PB6 PB4 PB3 PB5 PB0 PB2
129 static void init_rows(void)
137 static uint8_t read_rows(void)
139 return (PINB&(1<<1) ? 0 : (1<<0)) |
140 (PINC&(1<<2) ? 0 : (1<<1)) |
141 (PINB&(1<<6) ? 0 : (1<<2)) |
142 (PINB&(1<<4) ? 0 : (1<<3)) |
143 (PINB&(1<<3) ? 0 : (1<<4)) |
144 (PINB&(1<<5) ? 0 : (1<<5)) |
145 (PINB&(1<<0) ? 0 : (1<<6)) |
146 (PINB&(1<<2) ? 0 : (1<<7));
149 /* These columns uses two 74HC42 4 to 10 bit demultiplexers (low active).
151 * COL PD6 PD5 PD4 PD3 PD2 PD1
163 * COL PD1 PD2 PD3 PD4 PD5 PD6
173 static void unselect_cols(void)
176 PORTD &= ~0b01111111;
179 static void select_col(uint8_t col)
183 PORTD |= (1<<6) | (1<<3) | (1<<2) | (1<<1);
186 PORTD |= (1<<5) | (1<<4) | (1<<3) | (1<<2) | (1<<1);
189 PORTD |= (1<<4) | (1<<3) | (1<<2) | (1<<1);
192 PORTD |= (1<<6) | (1<<2) | (1<<1);
195 PORTD |= (1<<5) | (1<<2) | (1<<1);
198 PORTD |= (1<<5) | (1<<3) | (1<<2) | (1<<1);
201 PORTD |= (1<<6) | (1<<5) | (1<<3) | (1<<1);
204 PORTD |= (1<<6) | (1<<5) | (1<<4) | (1<<3) | (1<<2);
207 PORTD |= (1<<6) | (1<<5) | (1<<2);
210 PORTD |= (1<<6) | (1<<5) | (1<<4) | (1<<2);
213 PORTD |= (1<<6) | (1<<5);
216 PORTD |= (1<<6) | (1<<5) | (1<<4) | (1<<3);
219 PORTD |= (1<<6) | (1<<5) | (1<<4);
222 PORTD |= (1<<6) | (1<<5) | (1<<3) | (1<<2);
225 PORTD |= (1<<6) | (1<<5) | (1<<3);
228 PORTD |= (1<<6) | (1<<5) | (1<<1);
231 PORTD |= (1<<5) | (1<<4) | (1<<2) | (1<<1);
234 PORTD |= (1<<4) | (1<<2) | (1<<1);