Return to the Home Page...
Features on BBC BASIC programming...
Your questions and answers...
Buy or Sell an NC...
The NC200 manual and data sheets for BBC BASIC...
Information about BBC BASIC on the NC...
Free software for you to download...

Tim's Amstrad NC Users' Site

Mouse Drivers for Your BBC BASIC Programs

My cat Ginger!By Tim Surtell

Since some mice have a serial interface like the Notepad and Notebook computers, I brought a cheap mouse (the "Ice Mouse" from Dixons) to see if I could program a mouse driver in BASIC.

After doing some research on the internet (see foot of page) I found out that there are three main types of mice, of which only the 'serial' type can be used on the NCs:

PS/2 mice have a serial interface, but the logic levels used a 0V and +5V.

Bus mice have no microprocessor inside them since all decoding is done by a circuit inside the computer. The only components inside these mice are the switches, LED's, and photo-diodes.

Serial mice can be used with the NC since they use the same RS232 serial transfer protocol as the NC, which has logic levels of -12V and +12V. These mice even have the correct 9-pin connector supplied, so there is no wiring.

Making your mouse work on the NC

There are two systems in use for sending data from a serial mouse.
The first system is the 'Microsoft Mouse' system which sends three bytes of information for every movement of the mouse. The other system, 'Mouse Systems Mouse' sends five bytes for every movement.

To get your mouse working on the NC, plug it into the serial port and then adjust the settings in the Serial Terminal Program (FUNCTION-S) to those below :

Baud rate = 1200
Data bits = 7
Stop bits = 1

You can now go to BASIC and try this short program which reads in the bytes sent from the mouse and displays them on the screen.

10 CLS
20 K=OPENUP("COM:")

30 ON ERROR CLOSE#K:END
40 B=BGET#K:PRINT ;B;" ";
50 GOTO 40

If you don't know what system your mouse uses, this program can tell you, since pressing the left button will make the mouse send three bytes if it's using 'Microsoft Mouse' or five bytes if it's using 'Mouse Systems Mouse'.

Microsoft Mouse

The mouse drivers I have written have been for the 'Microsoft Mouse' system, so I'll only explain how this works.

The three bytes sent are decoded as follows:

Byte D7 D6 D5 D4 D3 D2 D1 D0
1 1/0 1 LB RB Y7 Y6 X7 X6
2 1/0 0 X5 X4 X3 X2 X1 X0
3 1/0 0 Y5 Y4 Y3 Y2 Y1 Y0
  • Bit D7 is either 1 or 0 depending on if you are using 7 or 8 data bits.
  • Bit D6 is 1 only in the first byte as a means of checking where the start of the data is.
  • LB and RB give the states of the two buttons, 1 being pressed down.

The X and Y information is in two signed bytes. This means if bit 7 is 0 the increment is negative, and if it 1, the increment is positive. When the increment is negative, the value in bits 0-6 needs to be subtracted from 128 in order to find the actual distance to be moved.

Mouse Drivers

The first method I used to translate the X,Y data into movement of a graphics cursor was to simply add or subtract the value from the current X,Y position of the cursor -- a linear relationship. This works OK if the mouse is moved only small amounts, but because BASIC programs run fairly slowly, moving the mouse a large amount meant there was a delay before the cursor actually stopped at a new position. This problem makes it very hard to co-ordinate the mouse movement, since the user thinks the mouse is not moving fast enough, so he moves the mouse faster, and the cursor ends up in completly the wrong place!

This problem is solved by the program below, which collects all the data from the mouse until it stops, before the cursor is actually moved on the screen.

Mouse Driver version 5 ... 1.6kb

Program analysis

Line 90 Sets the variables. Y should be changed to 32 if you are using a NC100.

Lines 100 Traps the 'Time out' error that could occur if the mouse is not moved for a while (this situation would not actually occur in this version) and turns off the serial port if any other error occurs.

Line 120 Draws the cursor arrow. Note it uses PLOT 6 so any lines under the cursor are not rubbed out.

Lines 130 Opens the file COM:, which is the serial port.

Lines 140 to 170 Continuously check to see if a key has been pressed. If one has, program goes to PROCevent. In the case of the COM port, the command EOF tells you if there is any input waiting. If there is, EOF is FALSE so the REPEAT...UNTIL loops ends so the waiting bytes can be input.

Line 180 to 200 Read in the three bytes sent from the mouse. Line 190 checks bit 7 to see if it 1, indicating the first byte. If it is not, the port is read again until a first byte is encountered.

Line 210 Takes the X and Y increments from the bytes B2 and B3. In this application the extra bit 6 is ignored, so the increment can be a maximum of 63.

Lines 220 and 230 Check bit 7 of the X,Y increment to determine the direction. If negative, the increment is subtracted from 64. The counters XC and YC add up all the increments.

Line 240 Checks to see if there are more bytes waiting for input, and if there is, they are read in, otherwise GOTO 260.

Lines 260 and 270 Check the states of the buttons. If there is a change of state then PROCevent is called.

Lines 280 and 300 Remove and re-draw the cursor.

Line 290 Adds the counters to the current values for X and Y.

If you are using an NC100, Y in line 90 needs to be changed to 32.

To produce a text cursor, a new method was used. This time, any increment over a certain threshold makes the mouse move one character/line.

Mouse Driver version 6 ... 1.7kb

The program works in a similar way to the first one.

Line 100 Sets the variables. This time a record of both the text (X,Y) and graphic (MX,MY) positions is required since the cursor, which may look like a normal text cursor, is actually a graphic. S is the threshold that has to be reached before the cursor moves.

Lines 250 to 280 Check to see if the threshold is reached before drawing the cursor.

Line 290 and 300 Make sure the cursor does not go off the screen.

If you are using an NC100 MY in line 100 needs to be changed to 56, and MY in line 300 needs to be changed to 56.

PC Mouse Information by Tomi Engdahl on ePanorama.net: www.epanorama.net/documents/pc/mouse.html 

Programs that use the mouse driver:

  • The new version of PCB CAD, a program for designing Printed Circuit Boards.  Download it from the Applications page.
  • A numerical Slide Puzzle.  Download it from the Games page.
 
© 1997 – 2024 Tim Surtell

Tim's Amstrad NC Users' Site
www.ncus.org.uk

Return to top of page...

| Home | Features | Data Sheets | Questions | BBC BASIC | Software |