1 | #define F_CPU 16000000UL
|
---|
2 |
|
---|
3 | #include <avr/io.h>
|
---|
4 | #include <avr/pgmspace.h>
|
---|
5 | #include <util/delay.h>
|
---|
6 |
|
---|
7 | #include "../../libs/ps2_keyboard/ps2_keyboard.h"
|
---|
8 |
|
---|
9 | uint8_t keys[16];
|
---|
10 |
|
---|
11 | const uint8_t pcw[128] PROGMEM = {
|
---|
12 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
---|
13 | '?' ,0x20,'?' ,0xA0,0x00,0x02,'?' ,0x12,'?' ,0x03,0xA4,'?' ,'?' ,0x84,0x26,'?', // 0
|
---|
14 | '?' ,0xA7,0x26,'?' ,0xA1,0x83,0x80,'?' ,'?' ,'?' ,0x87,0x74,0x85,0x73,0x81,'?', // 1
|
---|
15 | '?' ,0x76,0x77,0x75,0x72,0x70,0x71,'?' ,'?' ,0x57,0x67,0x65,0x63,0x62,0x61,'?', // 2
|
---|
16 | '?' ,0x56,0x66,0x54,0x64,0x53,0x60,'?' ,'?' ,'?' ,0x46,0x55,0x52,0x51,0x50,'?', // 3
|
---|
17 | '?' ,',' ,0x45,0x43,0x42,0x40,0x41,'?' ,'?' ,0x37,0x23,0x44,0x35,0x33,0x31,'?', // 4
|
---|
18 | '?' ,'?' ,'?' ,0x11,0x32,0x30,'?' ,'?' ,0x86,0x25,0x22,0x21,0x36,0x34,'?' ,'?', // 5
|
---|
19 | '?' ,'?' ,'?' ,'?' ,'?' ,'?' ,0x97,'?' ,'?' ,0x17,'?' ,0x15,0x24,'?' ,'?' ,'?', // 6
|
---|
20 | 0x01,0xA6,0x07,0x16,0x05,0x14,0x82,0xA5,0x13,0x27,0x06,0xA3,0xA2,0x04,0x10,'?' // 7
|
---|
21 | };
|
---|
22 |
|
---|
23 | int main() {
|
---|
24 | init_keyboard();
|
---|
25 |
|
---|
26 | // PCW init - configure pins directions
|
---|
27 | PORTB = 0;
|
---|
28 | DDRB = 0x6; // PB1 and PB2 as outputs
|
---|
29 |
|
---|
30 | //debug LED - output
|
---|
31 | DDRD |= (1<<PD6);
|
---|
32 |
|
---|
33 | uint8_t key_code = 0;
|
---|
34 |
|
---|
35 | while(1) {
|
---|
36 | key_code = read_char(); // TODO this is blocking function
|
---|
37 |
|
---|
38 | uint8_t decode = pgm_read_byte(&(pcw[key_code]));
|
---|
39 | if(release)
|
---|
40 | keys[decode >> 4] &= ~(1 << (decode & 0xF));
|
---|
41 | else
|
---|
42 | keys[decode >> 4] |= 1 << (decode & 0xF);
|
---|
43 |
|
---|
44 | for(int idx = -1; idx < 16; idx++)
|
---|
45 | {
|
---|
46 | // send data to PCW
|
---|
47 |
|
---|
48 | // Start pulse
|
---|
49 | PORTB = 4;
|
---|
50 | _delay_us(6);
|
---|
51 | PORTB = 0;
|
---|
52 | _delay_us(6);
|
---|
53 | PORTB = 4;
|
---|
54 | _delay_us(6);
|
---|
55 | PORTB = 0;
|
---|
56 | _delay_us(6);
|
---|
57 |
|
---|
58 | // Address
|
---|
59 | for(int j = 4; --j >= 0;)
|
---|
60 | {
|
---|
61 | if(idx & (1<<j))
|
---|
62 | PORTB = 4;
|
---|
63 | else
|
---|
64 | PORTB = 0;
|
---|
65 |
|
---|
66 | // Clock
|
---|
67 | _delay_us(6);
|
---|
68 | PORTB |= 2;
|
---|
69 | _delay_us(12);
|
---|
70 | PORTB = 0;
|
---|
71 | _delay_us(21);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // One "empty" clock cycle
|
---|
75 | _delay_us(21 + 33);
|
---|
76 |
|
---|
77 | // Data
|
---|
78 | for(int j = 8; --j >= 0;)
|
---|
79 | {
|
---|
80 | if (keys[idx & 0xF] & (1 << j))
|
---|
81 | PORTB = 4;
|
---|
82 | else
|
---|
83 | PORTB = 0;
|
---|
84 |
|
---|
85 | // Clock
|
---|
86 | _delay_us(6);
|
---|
87 | PORTB |= 2;
|
---|
88 | _delay_us(12);
|
---|
89 | PORTB = 0;
|
---|
90 | _delay_us(21);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | return 0;
|
---|
96 | }
|
---|
97 |
|
---|