Skip to content
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

Update HID Mouse example with scroll wheel status and middle mouse button click (IDFGH-11578) #12698

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
9 changes: 5 additions & 4 deletions examples/peripherals/usb/host/hid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ Hello, ESP32 USB HID Keyboard is here!
Mouse input data starts with the word "Mouse" and has the following structure.
```
Mouse
X: -00343 Y: 000183 | |o|
| | | |
| | | +- Right mouse button pressed status ("o" - pressed, " " - not pressed)
| | +--- Left mouse button pressed status ("o" - pressed, " " - not pressed)
X: -00343 Y: 000183 Wheel: 000004 | |o| |
| | | | | +- Right mouse button pressed status ("o" - pressed, " " - not pressed)
| | | | +- Middle mouse button pressed status ("o" - pressed, " " - not pressed)
| | | +--- Left mouse button pressed status ("o" - pressed, " " - not pressed)
| | +---------- Mouse wheel scroll status
| +---------- Y relative coordinate of the cursor
+----------------------- X relative coordinate of the cursor
```
7 changes: 5 additions & 2 deletions examples/peripherals/usb/host/hid/main/hid_host_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,19 @@ static void hid_host_mouse_report_callback(const uint8_t *const data, const int

static int x_pos = 0;
static int y_pos = 0;
static int wheel_pos = 0;

// Calculate absolute position from displacement
x_pos += mouse_report->x_displacement;
y_pos += mouse_report->y_displacement;
wheel_pos += mouse_report->scrollwheel;
Copy link
Collaborator

@roma-jam roma-jam Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current version of HID Driver was made for devices which have a HID boot interface. In that case the mouse report is simple and always has the default structure. Structure definition can be found in Appendix B2 of Device Class Definition for Human Interface Devices (HID) Version 1.11.

That is why the the structure in the hid_usage_mouse.h file for hid_mouse_input_report_boot_t satisfy the specification.
Sure, the scroll data in report for different devices pretty much always on the same place in buffer, but to be sure about the correct way is to handle the report device descriptor and handle the report data according to structure from descriptor.


hid_print_new_device_report_header(HID_PROTOCOL_MOUSE);

printf("X: %06d\tY: %06d\t|%c|%c|\r",
x_pos, y_pos,
printf("X: %06d\tY: %06d\tWheel: %06d\t|%c|%c|%c|\r",
x_pos, y_pos, wheel_pos,
(mouse_report->buttons.button1 ? 'o' : ' '),
(mouse_report->buttons.button3 ? 'o' : ' '),
(mouse_report->buttons.button2 ? 'o' : ' '));
fflush(stdout);
}
Expand Down