USB Absolute Mouse Mode

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
patrickryan
 
Posts: 2
Joined: Mon Dec 27, 2010 12:07 pm

USB Absolute Mouse Mode

Post by patrickryan »

The Arduino IDE 1.0 supports ardruino boards that have a built-in USB capability such as the atmega32u4. This solution has only been tested with adafruit's atmega32u4 breakout board on Ubuntu.

Relative Mode
One of the capabilities of these boards is to emulate a mouse. A mouse is typically a relative mode device. If you call Mouse.move(1,1), it will move 1 pixel right and 1 pixel down from where ever it currently is. Mouse.move(-5, 10) will move left 5 and down 10. Note that your board has no way of knowing where the mouse is at any point in time.

Absolute Mode
Fortunately, there is an absolute mode available to the mouse. Once enabled, performing a Mouse.move(1,1) will move the mouse to the very top left corner of the display, no matter where it currently is, or what resolution it is in. Likewise, Mouse.move(100,100) will move it to the bottom right corner of the display. Mouse.move(50,50) is the center (based on the LOGICAL_MAXIMUM in HID.cpp)

The arduino IDE can support absolute mouse mode. However it requires modifying one of the IDE files. This solution is for Arduino 1.0 and has not been tested on prior versions.

Procedure:
1. Kill your Arduino IDE if it is running.
2. Cd to (your arduino directory)/hardware/arduino/cores/arduino directory.
3. Copy your HID.cpp file somewhere safe that you can restore it later if you want to use relative mouse mode again.
4. Copy the attached HID.cpp replacement code below and paste it into your (arduino directory)/hardware/arduino/cores/arduino/ directory.
5. Copy the example sketch into the Arduino IDE and compile it.
6. Download the sketch to your atmega32u4 or similar board.
7. Within about 5 seconds, the mouse should start moving around the screen.

Now when you use the normal Mouse.move(x, y) it will use absolute coordinates (vice the default relative coordinates). Note that LOGICAL_MINIMUM(1) and LOGICAL_MAXIMUM(100) in HID.cpp map to the coordinate space of any display you connect to. So your sketches will always use coordinates between LOGICAL_MINIMUM and LOGICAL_MAXIMUM.

Here's the example sketch that simply moves the mouse left to right, and top to bottom:

Example Sketch

Code: Select all

void setup(){
}

void loop(){
    for(int y = 1; y <= 100; y++){
        for(int x = 1; x <= 100; x++){
            delay(100);
            Mouse.move(x, y);
        }
    }
}
Attachments
HID.cpp
(11.74 KiB) Downloaded 277 times

User avatar
cayenne
 
Posts: 9
Joined: Thu Mar 01, 2012 11:22 pm

Re: USB Absolute Mouse Mode

Post by cayenne »

Hi Patrick, I wounder if You can help Me? I'm kinda new to microcontrollers. One project I would like to do involves making hardware for Flight simulators and an easy way to get data into the sims is via mouse, and keyboard commands. I will explore direct connection methods later but for now I wan to use K/M emulation using Arduinos but documentation in this area is lacking unless I overlooked it. I read all of this http://www.ladyada.net/library/arduino/unofaq.html but its still a little vague. If You don't mind could You explain the hardware and software You used to get Your board to be recognized as a mouse? Or could You point Me towards some documentation on doing this with the various flavors of Arduinos. Knowing that it can be done is fine, I would just like to know How..Thanks, BANNED

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”