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 | |
24 | // PD0:Clock PD1:State PD2:Sense_All PD3:Reset(Scan_All) |
25 | DDRD = (1<<3) | (1<<0); |
26 | PORTD = (1<<2) | (1<<1); |
27 | |
28 | dprintf("init\n"); |
29 | } |
30 | |
31 | uint8_t matrix_scan(void) |
32 | { |
33 | |
34 | // Scan_all resets counter |
35 | RST_HI(); |
36 | wait_us(10); |
37 | // TODO: cannot get reliable value from SENSE() |
38 | //uint8_t s = SENSE() | STATE(); |
39 | //if (!SENSE()) return 0; // no activated key |
40 | RST_LO(); |
41 | wait_us(10); |
42 | //if (!s) return 0; |
43 | |
44 | // 8x8 matrix: row:sense, col:drive, key_on:hi |
45 | for (uint8_t col = 0; col < 8; col++) { |
46 | for (uint8_t row = 0; row < 8; row++) { |
47 | CLK_HI(); |
48 | wait_us(10); |
49 | |
50 | if (STATE()) { |
51 | matrix[row] |= (1<<col); |
52 | } else { |
53 | matrix[row] &= ~(1<<col); |
54 | } |
55 | |
56 | // clock lo - next row |
57 | CLK_LO(); |
58 | wait_us(10); |
59 | } |
60 | } |
61 | return 1; |
62 | } |
63 | |
64 | matrix_row_t matrix_get_row(uint8_t row) |
65 | { |
66 | return matrix[row]; |
67 | } |
68 | |
69 | void led_set(uint8_t usb_led) |
70 | { |
71 | } |