Skip to content

Adding function to limit the input allowed for the Mouse.move() command #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
//================================================================================
// Mouse

/* This function is for limiting the input value for x and y
* axis to -127 <= x/y <= 127 since this is the allowed value
* range for a USB HID device.
*/
signed char limit_xy(int const xy)
{
if (xy < -127) return -127;
else if(xy > 127) return 127;
else return xy;
}

Mouse_::Mouse_(void) : _buttons(0)
{
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
Expand All @@ -82,12 +93,12 @@ void Mouse_::click(uint8_t b)
move(0,0,0);
}

void Mouse_::move(signed char x, signed char y, signed char wheel)
void Mouse_::move(int x, int y, signed char wheel)
{
uint8_t m[4];
m[0] = _buttons;
m[1] = x;
m[2] = y;
m[1] = limit_xy(x);
m[2] = limit_xy(y);
m[3] = wheel;
HID().SendReport(1,m,4);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Mouse_
void begin(void);
void end(void);
void click(uint8_t b = MOUSE_LEFT);
void move(signed char x, signed char y, signed char wheel = 0);
void move(int x, int y, signed char wheel = 0);
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
Expand Down