2 #include <avr/pgmspace.h>
9 #define CLK_HI() (PORTD |= (1<<0))
10 #define CLK_LO() (PORTD &= ~(1<<0))
11 #define STATE() (!!(PIND & (1<<1)))
12 #define RST_HI() (PORTD |= (1<<3))
13 #define RST_LO() (PORTD &= ~(1<<3))
14 #define SENSE() (PIND & (1<<2))
16 static matrix_row_t matrix[8] = {};
17 static matrix_row_t matrix_debouncing[8] = {};
18 static uint16_t debouncing_time = 0;
21 void matrix_init(void)
24 debug_keyboard = true;
27 // PD0: Clock. Counter couts up at falling edge.
28 // PD1: Key State. Hi if selected key is activated.
29 // PD2: Sense. Lo if any key is activated while Reset is Hi.
30 // PD3: Reset. Resets counters at riging edge.
31 DDRD |= (1<<3) | (1<<0); // output
32 DDRD &= ~((1<<2) | (1<<1)); // input
33 PORTD &= ~((1<<3) | (1<<0)); // low
34 PORTD |= (1<<2) | (1<<1); // pull-up
39 uint8_t matrix_scan(void)
41 // TODO: debouce & unplug detect
48 // 8x8 matrix: row:sense, col:drive, key_on:hi
49 for (uint8_t col = 0; col < 8; col++) {
50 for (uint8_t row = 0; row < 8; row++) {
54 // detect state change and start debounce
55 if ((matrix_debouncing[row] & (1<<col)) ^ (STATE()<<col)) {
56 matrix_debouncing[row] ^= (1<<col);
57 debouncing_time = timer_read() || 1;
60 // proceed counter - next row
67 if (debouncing_time && timer_elapsed(debouncing_time) > DEBOUNCE) {
68 for (int row = 0; row < MATRIX_ROWS; row++) {
69 matrix[row] = matrix_debouncing[row];
76 matrix_row_t matrix_get_row(uint8_t row)
81 void led_set(uint8_t usb_led)