Skip to content

Commit 0e264a7

Browse files
authored
added support for absolute mouse
1 parent c6ec4e7 commit 0e264a7

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

libraries/USB/src/tusb_hid_mouse.h

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#pragma once
2+
3+
#include "class/hid/hid_device.h"
4+
5+
#if !defined TUD_HID_REPORT_DESC_ABSMOUSE
6+
// This version of arduino-esp32 does not handle absolute mouse natively.
7+
// Let's throw a minimalistic implementation of absmouse driver.
8+
// See: https://github.com/hathach/tinyusb/pull/1363
9+
// Also see: https://github.com/espressif/arduino-esp32/pull/6331
10+
11+
extern "C" {
12+
13+
// Absolute Mouse data struct is a copy of the relative mouse struct
14+
// with int16_t instead of int8_t for X and Y coordinates.
15+
typedef struct TU_ATTR_PACKED
16+
{
17+
uint8_t buttons = 0;
18+
int16_t x = 0;
19+
int16_t y = 0;
20+
int8_t wheel = 0;
21+
int8_t pan = 0;
22+
} hid_abs_mouse_report_t;
23+
24+
25+
// Absolute Mouse Report Descriptor Template applies those datatype changes too
26+
#define TUD_HID_REPORT_DESC_ABSMOUSE(...) \
27+
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
28+
HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\
29+
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
30+
/* Report ID if any */\
31+
__VA_ARGS__ \
32+
HID_USAGE ( HID_USAGE_DESKTOP_POINTER ) ,\
33+
HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) ,\
34+
HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\
35+
HID_USAGE_MIN ( 1 ) ,\
36+
HID_USAGE_MAX ( 5 ) ,\
37+
HID_LOGICAL_MIN ( 0 ) ,\
38+
HID_LOGICAL_MAX ( 1 ) ,\
39+
/* Left, Right, Middle, Backward, Forward buttons */ \
40+
HID_REPORT_COUNT( 5 ) ,\
41+
HID_REPORT_SIZE ( 1 ) ,\
42+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
43+
/* 3 bit padding */ \
44+
HID_REPORT_COUNT( 1 ) ,\
45+
HID_REPORT_SIZE ( 3 ) ,\
46+
HID_INPUT ( HID_CONSTANT ) ,\
47+
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
48+
/* X, Y absolute position [0, 32767] */ \
49+
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
50+
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
51+
HID_LOGICAL_MIN ( 0x00 ) ,\
52+
HID_LOGICAL_MAX_N( 0x7FFF, 2 ) ,\
53+
HID_REPORT_SIZE ( 16 ) ,\
54+
HID_REPORT_COUNT ( 2 ) ,\
55+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
56+
/* Vertical wheel scroll [-127, 127] */ \
57+
HID_USAGE ( HID_USAGE_DESKTOP_WHEEL ) ,\
58+
HID_LOGICAL_MIN ( 0x81 ) ,\
59+
HID_LOGICAL_MAX ( 0x7f ) ,\
60+
HID_REPORT_COUNT( 1 ) ,\
61+
HID_REPORT_SIZE ( 8 ) ,\
62+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\
63+
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ), \
64+
/* Horizontal wheel scroll [-127, 127] */ \
65+
HID_USAGE_N ( HID_USAGE_CONSUMER_AC_PAN, 2 ), \
66+
HID_LOGICAL_MIN ( 0x81 ), \
67+
HID_LOGICAL_MAX ( 0x7f ), \
68+
HID_REPORT_COUNT( 1 ), \
69+
HID_REPORT_SIZE ( 8 ), \
70+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ), \
71+
HID_COLLECTION_END , \
72+
HID_COLLECTION_END \
73+
74+
static inline bool tud_hid_n_abs_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal)
75+
{
76+
hid_abs_mouse_report_t report =
77+
{
78+
.buttons = buttons,
79+
.x = x,
80+
.y = y,
81+
.wheel = vertical,
82+
.pan = horizontal
83+
};
84+
return tud_hid_n_report(instance, report_id, &report, sizeof(report));
85+
}
86+
87+
static inline bool tud_hid_abs_mouse_report(uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal)
88+
{
89+
return tud_hid_n_abs_mouse_report(0, report_id, buttons, x, y, vertical, horizontal);
90+
}
91+
92+
93+
} // end extern "C"
94+
95+
#else
96+
#pragma message "This file is now safe to delete along with its include from USBHIDMouse.h"
97+
#endif

0 commit comments

Comments
 (0)