he USB Mouse Software allows you to connect a USB Mouse to the USB Host Board and read the mouse movements and button presses via I2C and serial TTL
It translates the USB MIDI data packets to/from Serial data packets at the MIDI baud rate of 31250.
This is a free software download for our USB Host Board and USB Host IC’s (SOIC, DIP, SSOP).
Please note: This software is free to download but only works on one of our USB boards or chips which must be bought separately.
To order, simply select which of our boards or IC products you wish to have the software loaded onto. See the USB Host Board and IC product pages for details onĀ pricingĀ and installing this software.
The USB Mouse software for our USB Host board allows you to easily read the raw X and Y mouse movement values, the scroll wheel and all the button inputs from a USB Mouse. Not only that but we have calculated the X and Y coordinates for a virtual mouse cursor position on a configurable “virtual screen“. All these values can be easily read into a microcontroller using the serial UART or via I2C.
Virtual Screen
One of the main features of our USB Host Mouse Software is our built-in ‘Virtual Screen‘.
Perhaps you have ideas on using a mouse to position the cursor on an LCD or TFT display. This would be useful if you want to implement a menu system say. Or perhaps you want to control some RC servo’s by scrolling the mouse around. Our calculated X and Y positions on a defined virtual screen is exactly what you need for both these options.
The Virtual Screen saves you from having to process the raw mouse movement data yourself, and also crucially saves you from having to constantly talk to the mouse to check to see if it has moved.
The Virtual Screen is setup using the following parameters, either via the serial RX/TX connection or via I2C. These parameters define the size of the virtual screen, the starting X and Y positions and the sensitivity of the mouse.
SCALE X | Width of the virtual screen (1 – 5000) |
SCALE Y | Height of the Virtual Screen (1 – 5000) |
START X | Starting X Position (0 to < SCALE X) |
START Y | Starting Y Position (0 to < SCALE Y) |
SPEED | Mouse sensitivity scale factor (1-10) |
Example 1:
We are using a 20×4 character LCD display. We would define the virtual screen using
SCALE X 20
SCALE Y 4
START X 0
START Y 0
SPEED 1
When the mouse is moved around you will get values for X that are between 0 and 19 and for Y that are between 0 and 3. Just like the mouse on your computer, the X and Y values will not move outside of the virtual screen. This can be used to position a mouse cursor on the LCD screen (see example Arduino program at the bottom of the page).
The SPEED setting defaults to 10 which is the maximum setting and uses the full sensitivity of the mouse for changes to the X and Y position. For small displays like the 20×4 LCD in our example, this sensitivity is too fast. Set the SPEED to any value from 1 – 10 to effectively scale the changes. For this LCD, we would recommend it is set to it’s minimum value of 1. This requires the mouse to be moved further before the X and Y values are changed.
Example 2:
Another example would be controlling servos. Using the Arduino software as an example, servos are controlled by a value between 0 and 180 with 90 being the centre position. If we therefore setup our virtual screen to be 180 x 180, we can use the values directly to control 2 servos.
Configuration Settings
Configuration can be done by sending commands via the serial port. Either by microcontroller or via a terminal program and a suitable serial TTL connection. Commands take effect immediately and are stored in Eeprom on the board. They are automatically loaded at bootup. The Virtual screen configuration can also be set using I2C communication.
The following Commands are available to be set via Serial TTL connection.
BAUD <value> | Set Serial Port Baud Rate (default 9600) [2400|4800|9600|14400|19200|38400|57600|115200] |
SCALE X <value> | Set X Scale width for virtual screen [1-5000] |
SCALE Y <value> | Set Y Scale height for virtual screen [1-5000] |
START X <value> | Set X starting position on virtual screen [0 – <SCALE X] |
START Y <value> | Set Y starting position on virtual screen [0 – <SCALE Y] |
SPEED <value> | Set Mouse sensitivity for virtual screen – default 10 [1 – 10] |
I2CSAVE <value> | Change whether settings set using I2C are stored in eeprom – default OFF [ON|OFF] |
I2C <value> | Set I2C Address [1 – 127] |
BAUD Rate
The USB Host board will communicate over serial TTL at the following baud rates
- 2400
- 4800
- 9600 (default)
- 14400
- 19200
- 28800
- 57600
- 115200
The default baud rate is 9600
I2C Address
The I2C default address is 41. This can be changed to any value between 1 and 127 using the I2C command.
I2CSAVE
With this option set to its default OFF state, any Virtual Screen setting made using I2C are temporary only and will not be saved and reloaded at bootup.
Getting Help and Checking Configuration
When using a serial connection, Help can be displayed at any time by typing ? or HELP
The current settings are displayed.
?
Hobbytronics USB Host Mouse v1.00
BAUD <value> - Set Serial Port Baud Rate
[2400|4800|9600|14400|19200|38400|57600|115200]
SCALE X|Y <value> - Set Mouse X and Y Scale values [1 - 5000]
(X=255 Y=255)
START X|Y <value> - Set Mouse X and Y Start values
(X=0 Y=0)
SPEED <value> - Set Mouse speed value [1 - 10]
(=10)
I2CSAVE <value> - Are config options saved when set using I2C ON|OFF
OFF
I2C <value> - Set I2C Address
(41 ) [1 - 127]
HELP or ? - display help
Reading the Mouse Data
Serial Connection (TX)
The best way to see what data the mouse is generating, is to monitor the serial output. Data is output in a comma delimited format. Move the mouse around and click each button and note which value changes.
The comma delimited output looks like the following
X,Y,LB,RB,SB,XS,YS,SS
Where
X - X Virtual Screen Pos
Y - Y Virtual Screen Pos
LB - Left Button
RB - Right Button
SB - Scroll Wheel Button
XS - X mouse scroll (move) value
YS - Y Mouse Scroll (move) value
SS - Scroll Wheel scroll value
Everytime the mouse is moved, or a button is pressed or released a text string is sent via the serial output
This serial transmission is useful for testing and for transmission via a wireless connection. For most uses however we would recommend using the I2C interface.
I2C
I2C is the recommended method of accessing the data if a hard-wired connection is used, as the comma delimited text string doesn’t need to be processed into individual elements.
You can request all of the data items or individual items or groups of items.
Data is stored in bytes in registers numbered from 0 through 9
Data Storage Format
0 – X Pos High Byte
1 – X Pos Low Byte
2 – Y Pos High Byte
3 – Y Pos Low Byte
4 – Left Button
5 – Right Button
6 – Scroll Wheel Button
7 – X mouse scroll value
8 – Y Mouse Scroll value
9 – Scroll Wheel scroll value
As per normal I2C practice, issue a write command and send the starting register address, then issue a read for the data that you require. Normally this would entail setting the starting address to 0, then reading the full 10 bytes.
Setting Configuration within I2C
The configuration settings for the ‘Virtual Screen’ can also be setup using I2C. This makes it easy to setup options at the beginning of your program. If the option I2CSAVE is set to OFF, then any configuration settings changed with I2C commands are not saved to eeprom.
Change the settings by setting the following registers
20 Register for setting scale X (16 bit)
22 Register for setting scale Y (16 bit)
24 Register for setting start X (16 bit)
26 Register for setting start Y (16 bit)
28 Register for setting SPEED (8 bit)
An example Arduino sketch is shown below and should be easily modifyable for other languages.
Software Updates
To upload the Software to the USB Host Board you will need to use the ds30Loader program. See the USB Host Board product page for more information on this.
Supported Mice
Not all mice are the same, and some are very complex indeed. For instance, mice designed for gaming have many extra buttons and features and come with driver disks. They can have complex USB descriptors and may not work with our software. Standard wired and wireless mice have been tested with this product and should work OK.
Example use of the Mouse Application
The following Arduino sketch has the mouse connected to an Arduino UNO and a 20×4 LCD display. Moving the mouse around moves the cursor block.
/*
I2C LCD and USB HOST MOUSE
Example Arduino sketch to communicate with HobbyTronics I2CLCD backpack and
USB HOST board with mouse attached
Simply moves a cursor block around the screen
Requires the Hobbytronics HTI2CLCD library
Copyright (c) 2012 www.hobbytronics.co.uk
This example code is in the public domain.
*/
#include <Wire.h>
#include <HTI2CLCD.h> // Include the LCD Library
HTI2CLCD lcd; // Create an instance of the HTI2CLCD
const int lcd_address=58; // I2C Address of LCD backpack
const int host_address=41; // I2C Address of USB Host (Mouse)
unsigned int mouse_data[10]; // Array to store Mouse values
unsigned int mousex, mousey; // Mouse X and Y screen position
unsigned long currentTime;
unsigned long cloopTime;
void setup()
{
delay(200); // Give LCD time to configure itself
Wire.begin(); // join i2c bus
lcd.setType(lcd_address,4,20); // Define rows and columns for LCD
lcd.clear(lcd_address); // Clear LCD
lcd.backLight(lcd_address,250); // Set backlight
lcd.write(lcd_address,0xFF); // Display block character
Wire.beginTransmission(host_address); // transmit to device
Wire.write(20); // Scale X Register
Wire.write(0x00); // Scale X
Wire.write(0x14); // Scale X (20)
Wire.endTransmission(); // end transmission
Wire.beginTransmission(host_address); // transmit to device
Wire.write(22); // Scale Y Register
Wire.write(0x00); // Scale Y
Wire.write(0x04); // Scale Y (4)
Wire.endTransmission(); // end transmission
Wire.beginTransmission(host_address); // transmit to device
Wire.write(24); // Start X Register
Wire.write(0x00); // Start X
Wire.write(0x00); // Start X (0)
Wire.endTransmission(); // end transmission
Wire.beginTransmission(host_address); // transmit to device
Wire.write(26); // Start Y Register
Wire.write(0x00); // Start Y (500)
Wire.write(0x00); // Start Y (0)
Wire.endTransmission(); // end transmission
Wire.beginTransmission(host_address); // transmit to device
Wire.write(28); // Speed Register
Wire.write(0x01); // Speed
Wire.endTransmission(); // end transmission
currentTime = millis();
cloopTime = currentTime;
}
void get_mouse()
{
// Get 8-bit Mouse data
// We only want single byte values (0 to 255)
unsigned char data_values_rcvd=0; // keep track of characters received
unsigned char value, i;
Wire.beginTransmission(host_address); // transmit to device
Wire.write(0); // Start receiving data from register 0
Wire.endTransmission(); // end transmission
Wire.requestFrom(host_address, 10); // request 10 bytes from slave device
// Data Format
// 0 - X Pos High Byte
// 1 - X Pos Low Byte
// 2 - Y Pos High Byte
// 3 - Y Pos Low Byte
// 4 - Left Button
// 5 - Right Button
// 6 - Scroll Wheel Button
// 7 - X mouse scroll value
// 8 - Y Mouse Scroll value
// 9 - Scroll Wheel scroll value
data_values_rcvd=0;
i=0;
while(Wire.available())
{
if(data_values_rcvd < 10)
{
value = Wire.read();
switch(data_values_rcvd)
{
case 0:
case 2:
mouse_data[i]=value<<8; // Don't increment i
break;
case 1:
case 3:
mouse_data[i++]+=value;
break;
default:
mouse_data[i++]=value;
}
}
data_values_rcvd++;
}
}
void loop()
{
currentTime = millis();
if(currentTime >= (cloopTime + 50)){
get_mouse(); // get mouse data
if((mousex != (mouse_data[0])) or (mousey != (mouse_data[1])))
{
// Mouse has moved - clear cursor block and move to new pos
lcd.setCursor(lcd_address,mousey+1,mousex+1);
lcd.write(lcd_address,32);
mousex=mouse_data[0];
mousey=mouse_data[1];
lcd.setCursor(lcd_address,mousey+1,mousex+1);
lcd.write(lcd_address,0xFF); // Display our block character
}
cloopTime = currentTime; // Updates cloopTime
}
}