Pages: [1]   Go Down
Author Topic: USB Absolute Mouse Mode  (Read 7100 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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 attached 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:
void setup(){
}

void loop(){
    for(int y = 1; y <= 100; y++){
        for(int x = 1; x <= 100; x++){
            delay(100);
            Mouse.move(x, y);
        }
    }
}


* HID.cpp (11.74 KB - downloaded 178 times.)
* AbsoluteMouseMode.ino (0.17 KB - downloaded 106 times.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the post! exactly what I was looking for smiley

I had to make some minor changes to make HID.cpp compatible with Ardino 1.0.1 IDE

* HID.cpp (14.5 KB - downloaded 92 times.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is an example sketch that uses the random fuction to set mouse coordinates.
It uses a "high" input to pin 2 to activate

Quote
void setup(){
  pinMode(2, INPUT);
}

void loop(){
  int x;
  int y;
  
  if(digitalRead(2) == HIGH){
    Mouse.begin();
    for(int z = 1; z <= 10; z++){
      x = random(0, 100);
      y = random(0, 100); 
      Mouse.move(x, y);
      delay(100);
    }
    Mouse.end();
  }
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you!!!
This source is from the computer works fine
But the absolute coordinates from an Android phone does not work.
Android relative coordinates in the source, it works well.
Is there a good way to
I'll give you favor.
I have tested Android phones Galaxy S2
Favor the development environment IDE 1.0.1.
I do not speak English well.I'm sorry.
« Last Edit: September 27, 2012, 12:59:03 am by yunowa » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was able to use this example to successfully get the mouse to move to a specific location.  It works exactly as expected, until  I follow this with

Mouse.click();

The mouse moves to the extreme upper left hand corner of the screen before clicking instead of remaining where I put it.
Am I overlooking something?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was able to get the cursor to stop moving to 0,0 upon Mouse.click();
In the HIP.CPP, I found

Code:
void Mouse_::click(uint8_t b)
{
_buttons = b;
  move(0,0,0);
_buttons = 0;
  move(0,0,0);
}
and changed it to
Code:
void Mouse_::click(uint8_t b)
{
_buttons = b;
// move(0,0,0);
_buttons = 0;
// move(0,0,0);
}

However, I am still not getting a click in absolute mode.  Any ideas?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i try to use it for a pro micro 5V from SparkFun but it wont work. Arduino gives me this error

D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp: In constructor 'Keyboard_::Keyboard_()':
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:267: error: class 'Keyboard_' does not have any field named '_keyMap'
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp: At global scope:
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:276: error: variable or field 'setKeyMap' declared void
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:276: error: 'KeyMap' was not declared in this scope
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:276: error: 'keyMap' was not declared in this scope
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp: In member function 'virtual size_t Keyboard_::write(uint8_t)':
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:424: error: '_keyMap' was not declared in this scope
D:\Dropbox\arduino-1.0.3\hardware\SF32u4_boards\cores\arduino\HID.cpp:439: error: 'KEY_MODIFIER_LEFT_SHIFT' was not declared in this scope


edit : same problem at 1.0

edit 2  : now it works with a disabled keyboard function. I still hope for solution so i can also use the keyboard.  When i move the mouse to (100,100) . I don't go to pixel 100,100 but somewhere in the right corner of my screen. Can i use the function that the 100 mean the pixel size?
« Last Edit: December 19, 2012, 12:12:30 pm by slechtvalk » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

would you help me how to increase the resolution above mouse.move(100,100); because am using an wide screen laptop it may require mouse.move(120.120);
 
Logged

Cadiz (Spain)
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi alls!!

Thanks for your work, it is awesome.

I am trying to move the mouse pointer in one 17´´ Laptop display.
it works but not fit all the display,if the diagonal line is drawed by (1,1) to (100,100) y should need (1,1) to (120,120)

i have triyed change HID.cpp logical_maximun from 0x64 to 0x78 but it didn´t work

Code:
#ifdef ABSOLUTE_MOUSE_MODE
    0x15, 0x01,                    //     LOGICAL_MINIMUM (1)       <<<< This allows us to talk to any display resolution
    0x25, 0x78,                    //     LOGICAL_MAXIMUM (120)     <<<<  as though it was 100x100 pixels
    0x75, 0x08,                    //     REPORT_SIZE (8)
    0x95, 0x03,                    //     REPORT_COUNT (3)
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)      <<<< This allows us to moveTo absolute positions
#else

and i have tryed change usbcore.cpp device descriptor(...,64,.....) to 78 too,.but it didn´t work
i think it was packetsize variable and maybe.... but not.
Code:
DEVICE DESCRIPTOR
const DeviceDescriptor USB_DeviceDescriptor =
D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);

const DeviceDescriptor USB_DeviceDescriptorA =
D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);


Sincerely i have no much idea about what i am doing...
i supose tath somewhere there is a variable that say how many distance can move ore some thing but i have no idea about where can i find it.


someone can help me

Thanks!!!!
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is great, however, mapping to 100 pixels gives a VERY staccato effect to the mouse movement.

What values would you change in the HID file to map to any resolution?
Logged

Pages: [1]   Go Up
Jump to: