/* * an arduino sketch to interface with a ps/2 mouse. * Also uses serial protocol to talk back to the host * and report what it finds. colors on my mouse data white clck black gnd green vcc red with ic : "emc" em84510fp 9952w bc9cn3 data transmission frame Bit Function 1 Start bit ( always 0 ) 2-9 Data bits ( D0 - D7 ) 10 Parity bit ( odd parity ) 11 Stop bit ( always 1 ) */ #define verbose // for verbose output unsigned char msInitSequence[6]; /* * Pin 5 is the mouse data pin, pin 6 is the clock pin * Feel free to use whatever pins are convenient. */ #define MDATA 5 #define MCLK 6 /* * according to some code I saw, these functions will * correctly set the mouse clock and data pins for * various conditions. */ void gohi(int pin) { pinMode(pin, INPUT); digitalWrite(pin, HIGH); } void golo(int pin) { pinMode(pin, OUTPUT); digitalWrite(pin, LOW); } /* PRECONDITION : clock is HIGH */ /* sends a single bit and returns with clock high */ void sendBit(char bit) { if (bit) { gohi(MDATA); } else { golo(MDATA); } /* wait for clock cycle */ while (digitalRead(MCLK) == LOW) ; while (digitalRead(MCLK) == HIGH) ; } /* reads a single bit and returns with clock low */ char readBit() { char tmp = 0x00; while (digitalRead(MCLK) == HIGH) ; if (digitalRead(MDATA) == HIGH) tmp = 0x01; while (digitalRead(MCLK) == LOW) ; return tmp; } void mouse_write(char data) { char i; char parity = 1; /* If EM84510 is not transmitting or if the system choose to */ /* override the output, the system force CLK to an inactive */ /* level for a period of not less than 100μ sec while */ /* preparing for output. When the system is ready to output */ /* start bit (0), it allows CLK go to active level. */ /* put pins in output mode */ gohi(MDATA); gohi(MCLK); delayMicroseconds(200); /* start bit */ gohi(MCLK); // get the clock going sendBit(0); for (i=0; i < 8; i++) { char bit = (data>>i) & 0x01 ; sendBit(bit); parity = parity ^ (bit); } /* parity */ sendBit(parity); /* stop bit */ sendBit(1); /* wait for mouse to switch modes */ while ((digitalRead(MCLK) == LOW) || (digitalRead(MDATA) == LOW)) ; /* put a hold on the incoming data. */ golo(MCLK); } /* * Get a byte of data from the mouse */ char mouse_read(void) { char data = 0x00; int i; /* start the clock */ gohi(MCLK); gohi(MDATA); delayMicroseconds(50); readBit(); // startbit for (i=0; i < 8; i++) { data = data | (readBit()<