http://playground.arduino.cc/ComponentLib/Ps2mouse
[Arduino] / libraries / ps2 / examples / ps2_mouse / ps2_mouse.pde
1 #include <ps2.h>\r
2 \r
3 /*\r
4  * an arduino sketch to interface with a ps/2 mouse.\r
5  * Also uses serial protocol to talk back to the host\r
6  * and report what it finds.\r
7  */\r
8 \r
9 /*\r
10  * Pin 5 is the mouse data pin, pin 6 is the clock pin\r
11  * Feel free to use whatever pins are convenient.\r
12  */\r
13 PS2 mouse(6, 5);\r
14 \r
15 /*\r
16  * initialize the mouse. Reset it, and place it into remote\r
17  * mode, so we can get the encoder data on demand.\r
18  */\r
19 void mouse_init()\r
20 {\r
21   mouse.write(0xff);  // reset\r
22   mouse.read();  // ack byte\r
23   mouse.read();  // blank */\r
24   mouse.read();  // blank */\r
25   mouse.write(0xf0);  // remote mode\r
26   mouse.read();  // ack\r
27   delayMicroseconds(100);\r
28 }\r
29 \r
30 void setup()\r
31 {\r
32   Serial.begin(9600);\r
33   mouse_init();\r
34 }\r
35 \r
36 /*\r
37  * get a reading from the mouse and report it back to the\r
38  * host via the serial line.\r
39  */\r
40 void loop()\r
41 {\r
42   char mstat;\r
43   char mx;\r
44   char my;\r
45 \r
46   /* get a reading from the mouse */\r
47   mouse.write(0xeb);  // give me data!\r
48   mouse.read();      // ignore ack\r
49   mstat = mouse.read();\r
50   mx = mouse.read();\r
51   my = mouse.read();\r
52 \r
53   /* send the data back up */\r
54   Serial.print(mstat, BIN);\r
55   Serial.print("\tX=");\r
56   Serial.print(mx, DEC);\r
57   Serial.print("\tY=");\r
58   Serial.print(my, DEC);\r
59   Serial.println();\r
60 //  delay(20);  /* twiddle */\r
61 }\r