Skip to content

Commit 7b77d1e

Browse files
committed
added Mouse.moveAbs() as a new feature for absolute mouse positioning as requested in issue arduino#1417.
all parameters have the range of -32768 to 32767 and must be scaled to screen pixels some examples: x=0, y=0 is the middle of the screen x=-32768, y=-32768 is the top left corner x=32767, y=-32768 is the top right corner x=32767, y=32767 is the bottom right corner x=-32768, y=32767 is the bottom left corner
1 parent 674ab3b commit 7b77d1e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

hardware/arduino/cores/arduino/HID.cpp

+56-1
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ Keyboard_ Keyboard;
4949
#define HID_REPORTID_KEYBOARD (2)
5050
#define HID_REPORTID_RAWHID (3)
5151
#define HID_REPORTID_SYSTEMCONTROL (4)
52+
#define HID_REPORTID_MOUSE_ABS (5)
5253
extern const u8 _hidReportDescriptor[] PROGMEM;
5354
const u8 _hidReportDescriptor[] = {
5455

55-
// Mouse
56+
// Mouse relative
5657
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
5758
0x09, 0x02, // USAGE (Mouse)
5859
0xa1, 0x01, // COLLECTION (Application)
@@ -82,6 +83,38 @@ const u8 _hidReportDescriptor[] = {
8283
0xc0, // END_COLLECTION
8384
0xc0, // END_COLLECTION
8485

86+
#ifdef MOUSE_ABS_ENABLED
87+
// Mouse absolute
88+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
89+
0x09, 0x02, // USAGE (Mouse)
90+
0xa1, 0x01, // COLLECTION (Application)
91+
0x09, 0x01, // USAGE (Pointer)
92+
0xa1, 0x00, // COLLECTION (Physical)
93+
0x85, HID_REPORTID_MOUSE_ABS, // REPORT_ID (5)
94+
0x05, 0x09, // USAGE_PAGE (Button)
95+
0x19, 0x01, // USAGE_MINIMUM (Button 1)
96+
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
97+
0x15, 0x00, // LOGICAL_MINIMUM (0)
98+
0x25, 0x01, // LOGICAL_MAXIMUM (1)
99+
0x95, 0x03, // REPORT_COUNT (3)
100+
0x75, 0x01, // REPORT_SIZE (1)
101+
0x81, 0x02, // INPUT (Data,Var,Abs)
102+
0x95, 0x01, // REPORT_COUNT (1)
103+
0x75, 0x05, // REPORT_SIZE (5)
104+
0x81, 0x03, // INPUT (Cnst,Var,Abs)
105+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
106+
0x09, 0x30, // USAGE (X)
107+
0x09, 0x31, // USAGE (Y)
108+
0x09, 0x38, // USAGE (Wheel)
109+
0x16, 0x00, 0x80, // LOGICAL_MINIMUM (-32768)
110+
0x26, 0xff, 0x7f, // LOGICAL_MAXIMUM (32767)
111+
0x75, 0x10, // REPORT_SIZE (16)
112+
0x95, 0x03, // REPORT_COUNT (3)
113+
0x81, 0x02, // INPUT (Data,Var,Abs)
114+
0xc0, // END_COLLECTION
115+
0xc0, // END_COLLECTION
116+
#endif
117+
85118
// Keyboard
86119
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
87120
0x09, 0x06, // USAGE (Keyboard)
@@ -268,6 +301,28 @@ void Mouse_::move(signed char x, signed char y, signed char wheel)
268301
HID_SendReport(HID_REPORTID_MOUSE,m,sizeof(m));
269302
}
270303

304+
#ifdef MOUSE_ABS_ENABLED
305+
// all parameters have the range of -32768 to 32767 and must be scaled to screen pixels
306+
// some examples:
307+
// x=0,y=0 is the middle of the screen
308+
// x=-32768,y=-32768 is the top left corner
309+
// x=32767,y=-32768 is the top right corner
310+
// x=32767,y=32767 is the bottom right corner
311+
// x=-32768,y=32767 is the bottom left corner
312+
void Mouse_::moveAbs(int16_t x, int16_t y, int16_t wheel)
313+
{
314+
u8 m[7];
315+
m[0] = _buttons; // TODO: is it a good idea to take over the _buttons from relative report here or should it be left out?
316+
m[1] = LSB(x);
317+
m[2] = MSB(x);
318+
m[3] = LSB(y);
319+
m[4] = MSB(y);
320+
m[5] = LSB(wheel);
321+
m[6] = MSB(wheel);
322+
HID_SendReport(HID_REPORTID_MOUSE_ABS,m,sizeof(m));
323+
}
324+
#endif
325+
271326
void Mouse_::buttons(uint8_t b)
272327
{
273328
if (b != _buttons)

hardware/arduino/cores/arduino/USBAPI.h

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define __USBAPI__
55

66
#if defined(USBCON)
7+
#define MOUSE_ABS_ENABLED
78

89
//================================================================================
910
//================================================================================
@@ -65,6 +66,9 @@ class Mouse_
6566
void end(void);
6667
void click(uint8_t b = MOUSE_LEFT);
6768
void move(signed char x, signed char y, signed char wheel = 0);
69+
#ifdef MOUSE_ABS_ENABLED
70+
void moveAbs(int16_t x, int16_t y, int16_t wheel); // all parameters have the range of -32768 to 32767 and must be scaled to screen pixels
71+
#endif
6872
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
6973
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
7074
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default

0 commit comments

Comments
 (0)