]>
Commit | Line | Data |
---|---|---|
1 | #include <avr/io.h> | |
2 | #include <avr/pgmspace.h> | |
3 | #include "matrix.h" | |
4 | #include "led.h" | |
5 | #include "wait.h" | |
6 | #include "timer.h" | |
7 | #include "debug.h" | |
8 | ||
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)) | |
15 | ||
16 | static matrix_row_t matrix[8] = {}; | |
17 | static matrix_row_t matrix_debouncing[8] = {}; | |
18 | static uint16_t debouncing_time = 0; | |
19 | ||
20 | ||
21 | void matrix_init(void) | |
22 | { | |
23 | debug_enable = true; | |
24 | debug_keyboard = true; | |
25 | debug_matrix = true; | |
26 | ||
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 | |
35 | ||
36 | dprintf("init\n"); | |
37 | } | |
38 | ||
39 | uint8_t matrix_scan(void) | |
40 | { | |
41 | // TODO: debouce & unplug detect | |
42 | // Reset counters | |
43 | RST_HI(); | |
44 | wait_us(10); | |
45 | RST_LO(); | |
46 | wait_us(10); | |
47 | ||
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++) { | |
51 | CLK_HI(); | |
52 | wait_us(10); | |
53 | ||
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; | |
58 | } | |
59 | ||
60 | // proceed counter - next row | |
61 | CLK_LO(); | |
62 | wait_us(10); | |
63 | } | |
64 | } | |
65 | ||
66 | // debounced | |
67 | if (debouncing_time && timer_elapsed(debouncing_time) > DEBOUNCE) { | |
68 | for (int row = 0; row < MATRIX_ROWS; row++) { | |
69 | matrix[row] = matrix_debouncing[row]; | |
70 | } | |
71 | debouncing_time = 0; | |
72 | } | |
73 | return 1; | |
74 | } | |
75 | ||
76 | matrix_row_t matrix_get_row(uint8_t row) | |
77 | { | |
78 | return matrix[row]; | |
79 | } | |
80 | ||
81 | void led_set(uint8_t usb_led) | |
82 | { | |
83 | } |