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>
31 static uint8_t debouncing
= DEBOUNCE
;
33 /* matrix state(1:on, 0:off) */
34 static matrix_row_t matrix
[MATRIX_ROWS
];
35 static matrix_row_t matrix_debouncing
[MATRIX_ROWS
];
37 static uint8_t read_rows(void);
38 static uint8_t read_fwkey(void);
39 static void init_rows(void);
40 static void unselect_cols(void);
41 static void select_col(uint8_t col
);
44 uint8_t matrix_rows(void)
50 uint8_t matrix_cols(void)
55 void matrix_init(void)
59 // initialize matrix state: all keys off
60 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
62 matrix_debouncing
[i
] = 0;
66 uint8_t matrix_scan(void)
68 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) { // 0-17
70 _delay_us(3); // TODO: Determine the correct value needed here.
71 uint8_t rows
= read_rows();
72 // Use the otherwise unused col: 12 row: 3 for firmware.
76 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) { // 0-5
77 bool prev_bit
= matrix_debouncing
[row
] & ((matrix_row_t
)1<<col
);
78 bool curr_bit
= rows
& (1<<row
);
79 if (prev_bit
!= curr_bit
) {
80 matrix_debouncing
[row
] ^= ((matrix_row_t
)1<<col
);
82 dprint("bounce!: "); dprintf("%02X", debouncing
); dprintln();
84 debouncing
= DEBOUNCE
;
94 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
95 matrix
[i
] = matrix_debouncing
[i
];
103 bool matrix_is_modified(void)
105 if (debouncing
) return false;
110 bool matrix_is_on(uint8_t row
, uint8_t col
)
112 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
116 matrix_row_t
matrix_get_row(uint8_t row
)
121 void matrix_print(void)
123 print("\nr/c 0123456789ABCDEF\n");
124 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
125 xprintf("%02X: %032lb\n", row
, bitrev32(matrix_get_row(row
)));
129 uint8_t matrix_key_count(void)
132 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
133 count
+= bitpop32(matrix
[i
]);
138 /* Row pin configuration
140 * pin: PD0 PD1 PD2 PD3 PD5 PB7
142 * Firmware uses its own pin PE2
144 static void init_rows(void)
146 // Input (DDR:0, PORT:0)
148 PORTD
&= ~0b00101111;
152 // Input with pull-up (DDR:0, PORT:1)
157 static uint8_t read_rows(void)
159 return (PIND
&(1<<0) ? (1<<0) : 0) |
160 (PIND
&(1<<1) ? (1<<1) : 0) |
161 (PIND
&(1<<2) ? (1<<2) : 0) |
162 (PIND
&(1<<3) ? (1<<3) : 0) |
163 (PIND
&(1<<5) ? (1<<4) : 0) |
164 (PINB
&(1<<7) ? (1<<5) : 0);
167 static uint8_t read_fwkey(void)
169 return PINE
&(1<<2) ? 0 : (1<<3);
173 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
174 * col / pin: PC6 PB6 PF0 PF1 PC7
198 static void unselect_cols(void)
200 DDRB
|= (1<<5) | (1<<6);
201 PORTB
&= ~((1<<5) | (1<<6));
203 DDRC
|= (1<<6) | (1<<7);
204 PORTC
&= ~((1<<6) | (1<<7));
209 DDRF
|= (1<<0) | (1<<1);
210 PORTF
&= ~((1<<0) | (1<<1));
213 static void select_col(uint8_t col
)
215 // Output high (DDR:1, PORT:1) to select
230 PORTF
|= (1<<0) | (1<<1);
248 PORTF
|= (1<<0) | (1<<1);
264 PORTF
|= (1<<0) | (1<<1);
282 PORTF
|= (1<<0) | (1<<1);