The Microsoft Mouse protocol consists of 3 bytes sent individually as a package of 7 bits per byte (which is not a full byte) in a specific order each time an event occurs on one the mouse's motion detectors (movement, button press or button release).
Byte 1 | 1 LB RB Y7 Y6 X7 X6
Byte 2 | 0 X5 X4 X3 X2 X1 X0
Byte 3 | 0 Y5 Y4 Y3 Y2 Y1 Y0
- MSB at Byte 1 is always set to indicate the start of a package.
- LB and RB represents Left and Right button, active high.
- Y0-Y7 represents movement in Y position in 8 bit 2's compliment
- X0-X7 represents movement in X position in 8 bit 2's compliment
To get the full 8 bit value of Y-position i logically AND'ed byte 1 with the binary representation of the Y7 and Y6 bit, then bit-shifted it 4 times to the left, and OR'ed it with the rest of the Y values in Byte 3.
so
((Byte 1 & 12)<<4) | Byte 3 = Y position, likewise
((Byte 1 & 3)<<6) | Byte 2 = X position
if (Byte 1 & 32) == 32 {Left button press}To check which direction the mouse is moving, I could check if the signed (MSB) bit is high (negative value) or low (positive value). All though, it could be that the signed bit is low when the whole direction "register" is 0, so you would have to test both conditions to get both directions and eliminate the states when there is no movement at all.
if (Byte 1 & 16) == 16 {Right button press}
Here is the finished front panel
And here is the messy block diagram
And here is a video, hopefully the "LEDs" and animation will be less choppy on a faster computer