353afe87 |
1 | #include <avr/io.h> |
2 | #include <avr/pgmspace.h> |
3 | #include "matrix.h" |
4 | #include "led.h" |
5 | #include "wait.h" |
6 | #include "debug.h" |
7 | |
8 | #define CLK_HI() (PORTD |= (1<<0)) |
9 | #define CLK_LO() (PORTD &= ~(1<<0)) |
10 | #define STATE() (PIND & (1<<1)) |
11 | #define RST_HI() (PORTD |= (1<<3)) |
12 | #define RST_LO() (PORTD &= ~(1<<3)) |
13 | #define SENSE() (PIND & (1<<2)) |
14 | |
15 | static matrix_row_t matrix[8] = {}; |
16 | |
17 | |
18 | void matrix_init(void) |
19 | { |
20 | debug_enable = true; |
21 | debug_keyboard = true; |
22 | debug_matrix = true; |
23 | |
13fe8ac8 |
24 | // PD0: Clock. Counter couts up at falling edge. |
25 | // PD1: Key State. Hi if selected key is activated. |
26 | // PD2: Sense. Lo if any key is activated while Reset is Hi. |
27 | // PD3: Reset. Resets counters at riging edge. |
28 | DDRD |= (1<<3) | (1<<0); // output |
29 | DDRD &= ~((1<<2) | (1<<1)); // input |
30 | PORTD &= ~((1<<3) | (1<<0)); // low |
31 | PORTD |= (1<<2) | (1<<1); // pull-up |
353afe87 |
32 | |
33 | dprintf("init\n"); |
34 | } |
35 | |
36 | uint8_t matrix_scan(void) |
37 | { |
13fe8ac8 |
38 | // TODO: debouce & unplug detect |
39 | // Reset counters |
353afe87 |
40 | RST_HI(); |
41 | wait_us(10); |
353afe87 |
42 | RST_LO(); |
43 | wait_us(10); |
353afe87 |
44 | |
45 | // 8x8 matrix: row:sense, col:drive, key_on:hi |
46 | for (uint8_t col = 0; col < 8; col++) { |
47 | for (uint8_t row = 0; row < 8; row++) { |
48 | CLK_HI(); |
49 | wait_us(10); |
50 | |
51 | if (STATE()) { |
52 | matrix[row] |= (1<<col); |
53 | } else { |
54 | matrix[row] &= ~(1<<col); |
55 | } |
56 | |
13fe8ac8 |
57 | // proceed counter - next row |
353afe87 |
58 | CLK_LO(); |
59 | wait_us(10); |
60 | } |
61 | } |
62 | return 1; |
63 | } |
64 | |
65 | matrix_row_t matrix_get_row(uint8_t row) |
66 | { |
67 | return matrix[row]; |
68 | } |
69 | |
70 | void led_set(uint8_t usb_led) |
71 | { |
72 | } |