Skip to content

Commit 0c078ab

Browse files
authored
Merge pull request #10 from arduino-libraries/limit-mouse-input
Adding function to limit the input allowed for the Mouse.move() command
2 parents 97f606b + 431193a commit 0c078ab

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Mouse.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
6060
//================================================================================
6161
// Mouse
6262

63+
/* This function is for limiting the input value for x and y
64+
* axis to -127 <= x/y <= 127 since this is the allowed value
65+
* range for a USB HID device.
66+
*/
67+
signed char limit_xy(int const xy)
68+
{
69+
if (xy < -127) return -127;
70+
else if(xy > 127) return 127;
71+
else return xy;
72+
}
73+
6374
Mouse_::Mouse_(void) : _buttons(0)
6475
{
6576
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
@@ -82,12 +93,12 @@ void Mouse_::click(uint8_t b)
8293
move(0,0,0);
8394
}
8495

85-
void Mouse_::move(signed char x, signed char y, signed char wheel)
96+
void Mouse_::move(int x, int y, signed char wheel)
8697
{
8798
uint8_t m[4];
8899
m[0] = _buttons;
89-
m[1] = x;
90-
m[2] = y;
100+
m[1] = limit_xy(x);
101+
m[2] = limit_xy(y);
91102
m[3] = wheel;
92103
HID().SendReport(1,m,4);
93104
}

src/Mouse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Mouse_
4949
void begin(void);
5050
void end(void);
5151
void click(uint8_t b = MOUSE_LEFT);
52-
void move(signed char x, signed char y, signed char wheel = 0);
52+
void move(int x, int y, signed char wheel = 0);
5353
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
5454
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
5555
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default

0 commit comments

Comments
 (0)