Mouse hardware programming
There are basically three different ways in which a mouse can be connected
to a PC: Via a special interface card (a 'bus mouse'), via a PS/2 style
connection (a 'PS/2 mouse') and via the serial port (a 'serial mouse').
All operate in basically the same way: Data packets containing information
about mouse movement (measured in units of 'mickeys') and button press
states are sent to the PC whenever the mouse moves or the button press state
changes.
I have no info on programming PS/2 or bus mice. Anything that anyone has
would be appreciated.
Contents:
Introduction
Serial mice are connected to the serial port of a PC and use the RS-232-C
protocol (supported by the PC's serial hardware) to send information to the
PC.
Originally mice pretty much always used one of the two protocols, Microsoft
Mode (for 2 button mice) and Mouse Systems Mode (for 3 button mice). The names
come from the respective companies which (presumably) defined the standards
in the first place.
Now, three button mice generally support both protocols; Switching between
them is either done manually with a switch on the actual mouse hardware,
or by the use of a serial control line. The DTR and RTS lines are both set
active for normal operation; one or the other controls mode switching (the
other line is used as a power supply for the mouse). The details depends on
the model and manufacturer of the mouse (though they can be determined by
trial and error for any particular mouse).
Autodetecting the mouse type is possible because serial mice send an
identification byte immediately after they are powered on (by raising the
appropriate control line[s]). See the description of the protocol for the
identification bytes of each protocol. Identification should generally be
performed using a 7-data-bit transfer protocol (no parity, 1 stop bit).
Author's note: I have a mouse which uses the DTR line as the switch
(active = microsoft mode) and the RTS as power supply. If there are really
variations on this, i'd like to know.
Microsoft Mode
- On power on, the mouse sends an "M" to the PC
- Data transmission protocol is 7 data bits, 1 stop bit, no parity
- There are 3 bytes per information packet, which take this format:
- 0: 1/LB/RB/Y7/Y6/X7/X6 (MSB/...../LSB)
- 1: 0/X5/X4/X3/X2/X1/X0
- 2: 0/Y5/Y4/Y3/Y2/Y1/Y0
The MSB of first byte is used as synchronisation failure protection (so driver software
stays in sync with hardware). X7-0 are the X delta value (that is, the amount of
movement since the last packet was sent), Y7-0 are the Y delta, LB = left button
state (1=pushed), RB = right button state.
The Microsoft Ballpoint pointing device
I'm not exactly sure what this is (some kind of mouse or trackball presumably)
but the programming information is here. It appears to have four buttons
(switches) named "sw1" through "sw4".
- On power on, the mouse sends a "B" to the PC
- The Ballpoint protocol is the same as the Microsoft Mode protocol (above)
except there is an extra byte appended to the packet, in the following format:
- 3: 0/-/-/sw2/sw4/Y8/X8 (LB=sw1, RB=sw3)
The X and Y delta values are extended one bit each.
Mouse Systems Mode
- Unlike Microsoft Mode, Mouse Systems Mode uses 8 bit data transmission
(with no parity and one stop bit).
- On power up, the mouse sends "H" as a 7-bit transmission. After
recieving the "H", the line protocol should be set to 8 bit communication.
- The data packets have 5 bytes and are in the following format:
- 0: 1/0/0/0/0/LB/MB/RB ({L/M/R}B = left/right/middle button)
- 1: X7/X6/X5/X4/X3/X2/X1/X0
- 2: Y7/Y6/Y5/Y4/Y3/Y2/Y1/Y0
- 3: X7'/X6'/X5'/X4'/X3'/X2'/X1'/X0'
- 4: Y7'/Y6'/Y5'/Y4'/Y3'/Y2'/Y1'/Y0'
Unlike the Microsoft Mode, LB/MB/RB are 0 if pushed, 1 if not. The delta values
X' and Y' give the movement since the X and Y values (in the same packet) were
sent. Driver/hardware synchronisation is not as easy as for Microsoft Mode;
the only real test is that the first byte bits match those shown above. A far
better test is to measure the delay between bytes being recieved; there should
be no long delays in the middle of a packet. As long as the driver is guarenteed
to recive all bytes sent, however, there should be no problem as transmission
should be reliable.