Commit | Line | Data |
---|---|---|
e6e13bcc TM |
1 | //Source: http://dduino.blogspot.com/2011/11/arduino-ps2-emulator-computer-control.html |
2 | ||
3 | //KBD stuff | |
4 | #include "ps2dev.h" // to emulate a PS/2 device | |
5 | PS2dev keyboard(19,18); // PS2dev object (2:data, 3:clock) | |
6 | int enabled = 0; // pseudo variable for state of "keyboard" | |
7 | unsigned char c; //char stores data recieved from computer for KBD | |
8 | ||
9 | //Sonar stuff | |
10 | ||
11 | void ack() | |
12 | { | |
13 | //acknowledge commands | |
14 | while(keyboard.write(0xFA)); | |
15 | } | |
16 | ||
17 | int keyboardcommand(int command) | |
18 | { | |
19 | unsigned char val; | |
20 | switch (command) | |
21 | { | |
22 | case 0xFF: //reset | |
23 | ack(); | |
24 | //the while loop lets us wait for the host to be ready | |
25 | while(keyboard.write(0xAA)!=0); | |
26 | break; | |
27 | case 0xFE: //resend | |
28 | ack(); | |
29 | break; | |
30 | case 0xF6: //set defaults | |
31 | //enter stream mode | |
32 | ack(); | |
33 | break; | |
34 | case 0xF5: //disable data reporting | |
35 | //FM | |
36 | enabled = 0; | |
37 | ack(); | |
38 | break; | |
39 | case 0xF4: //enable data reporting | |
40 | //FM | |
41 | enabled = 1; | |
42 | ack(); | |
43 | break; | |
44 | case 0xF3: //set typematic rate | |
45 | ack(); | |
46 | keyboard.read(&val); //do nothing with the rate | |
47 | ack(); | |
48 | break; | |
49 | case 0xF2: //get device id | |
50 | ack(); | |
51 | keyboard.write(0xAB); | |
52 | keyboard.write(0x83); | |
53 | break; | |
54 | case 0xF0: //set scan code set | |
55 | ack(); | |
56 | keyboard.read(&val); //do nothing with the rate | |
57 | ack(); | |
58 | break; | |
59 | case 0xEE: //echo | |
60 | //ack(); | |
61 | keyboard.write(0xEE); | |
62 | break; | |
63 | case 0xED: //set/reset LEDs | |
64 | ack(); | |
65 | keyboard.read(&val); //do nothing with the rate | |
66 | Serial.print("LEDs: "); | |
67 | Serial.println(val, HEX); | |
68 | digitalWrite(LED_BUILTIN, val); | |
69 | ack(); | |
70 | break; | |
71 | } | |
72 | return 0; | |
73 | } | |
74 | ||
75 | void setup() | |
76 | { | |
77 | // send the keyboard start up | |
78 | while(keyboard.write(0xAA)!=0); | |
79 | delay(10); | |
80 | // initialize the serial port: | |
81 | Serial.begin(9600); | |
82 | pinMode(LED_BUILTIN, OUTPUT); | |
83 | } | |
84 | ||
85 | void loop() | |
86 | { | |
87 | //if host device wants to send a command: | |
88 | if(keyboard.available()) | |
89 | { | |
90 | while(keyboard.read(&c)); | |
91 | //delay(100); | |
92 | keyboardcommand(c); | |
93 | //delay(100); | |
94 | //c = 0; | |
95 | } | |
96 | ||
97 | ||
98 | delay(1000); | |
99 | ||
100 | keyboard.write(0x16); | |
101 | keyboard.write(0xF0); | |
102 | keyboard.write(0x16); | |
103 | Serial.print('.'); | |
104 | ||
105 | //delay(1000); | |
106 | ||
107 | ||
108 | } |