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: PC2 PB1 PB2 PB3 PC7 PB4 PB5 PB6
129 static void init_rows(void)
137 static uint8_t read_rows(void)
139 return (PINC
&(1<<2) ? 0 : (1<<0)) |
140 (PINB
&(1<<1) ? 0 : (1<<1)) |
141 (PINB
&(1<<2) ? 0 : (1<<2)) |
142 (PINB
&(1<<3) ? 0 : (1<<3)) |
143 (PINC
&(1<<7) ? 0 : (1<<4)) |
144 (PINB
&(1<<4) ? 0 : (1<<5)) |
145 (PINB
&(1<<5) ? 0 : (1<<6)) |
146 (PINB
&(1<<6) ? 0 : (1<<7));
149 /* These columns uses two 74HC42 4 to 10 bit demultiplexers (low active).
151 * COL PD1 PD0 PD2 PD6 PD5 PD4
163 * COL PD2 PD6 PD1 PD0 PD5 PD4
175 static void unselect_cols(void)
178 PORTD
&= ~0b01110111;
181 static void select_col(uint8_t col
)
185 PORTD
|= (1<<5) | (1<<6) | (1<<2);
188 PORTD
|= (1<<4) | (1<<6) | (1<<2);
191 PORTD
|= (1<<6) | (1<<2);
194 PORTD
|= (1<<4) | (1<<2) | (1<<0) | (1<<1);
197 PORTD
|= (1<<2) | (1<<0) | (1<<1);
200 PORTD
|= (1<<4) | (1<<5) | (1<<6) | (1<<0) | (1<<1);
203 PORTD
|= (1<<5) | (1<<6) | (1<<0) | (1<<1);
206 PORTD
|= (1<<4) | (1<<6) | (1<<0) | (1<<1);
209 PORTD
|= (1<<6) | (1<<0) | (1<<1);
212 PORTD
|= (1<<4) | (1<<5) | (1<<0) | (1<<1);
215 PORTD
|= (1<<5) | (1<<0) | (1<<1);
218 PORTD
|= (1<<4) | (1<<0) | (1<<1);
221 PORTD
|= (1<<0) | (1<<1);
224 PORTD
|= (1<<4) | (1<<1) | (1<<6) | (1<<2);
227 PORTD
|= (1<<1) | (1<<6) | (1<<2);
230 PORTD
|= (1<<4) | (1<<5) | (1<<0) | (1<<6) | (1<<2);
233 PORTD
|= (1<<0) | (1<<6) | (1<<2);
236 PORTD
|= (1<<4) | (1<<5) | (1<<6) | (1<<2);