Skip to content

Commit d33fe38

Browse files
authored
Merge pull request #1363 from tobozo/master
HID Mouse with absolute positioning
2 parents b90eccc + 805ebb3 commit d33fe38

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/class/hid/hid.h

+13
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,19 @@ typedef struct TU_ATTR_PACKED
300300
int8_t pan; // using AC Pan
301301
} hid_mouse_report_t;
302302

303+
304+
// Absolute Mouse: same as the Standard (relative) Mouse Report but
305+
// with int16_t instead of int8_t for X and Y coordinates.
306+
typedef struct TU_ATTR_PACKED
307+
{
308+
uint8_t buttons; /**< buttons mask for currently pressed buttons in the mouse. */
309+
int16_t x; /**< Current x position of the mouse. */
310+
int16_t y; /**< Current y position of the mouse. */
311+
int8_t wheel; /**< Current delta wheel movement on the mouse. */
312+
int8_t pan; // using AC Pan
313+
} hid_abs_mouse_report_t;
314+
315+
303316
/// Standard Mouse Buttons Bitmap
304317
typedef enum
305318
{

src/class/hid/hid_device.c

+13
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id,
146146
return tud_hid_n_report(instance, report_id, &report, sizeof(report));
147147
}
148148

149+
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)
150+
{
151+
hid_abs_mouse_report_t report =
152+
{
153+
.buttons = buttons,
154+
.x = x,
155+
.y = y,
156+
.wheel = vertical,
157+
.pan = horizontal
158+
};
159+
return tud_hid_n_report(instance, report_id, &report, sizeof(report));
160+
}
161+
149162
bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id,
150163
int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) {
151164
hid_gamepad_report_t report =

src/class/hid/hid_device.h

+59
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modi
7272
// use template layout report as defined by hid_mouse_report_t
7373
bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
7474

75+
// ABSOLUTE MOUSE: convenient helper to send absolute mouse report if application
76+
// use template layout report as defined by hid_abs_mouse_report_t
77+
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);
78+
79+
80+
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)
81+
{
82+
return tud_hid_n_abs_mouse_report(0, report_id, buttons, x, y, vertical, horizontal);
83+
}
84+
7585
// Gamepad: convenient helper to send gamepad report if application
7686
// use template layout report TUD_HID_REPORT_DESC_GAMEPAD
7787
bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons);
@@ -266,6 +276,55 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y
266276
HID_COLLECTION_END , \
267277
HID_COLLECTION_END \
268278

279+
// Absolute Mouse Report Descriptor Template
280+
#define TUD_HID_REPORT_DESC_ABSMOUSE(...) \
281+
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
282+
HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\
283+
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
284+
/* Report ID if any */\
285+
__VA_ARGS__ \
286+
HID_USAGE ( HID_USAGE_DESKTOP_POINTER ) ,\
287+
HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) ,\
288+
HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\
289+
HID_USAGE_MIN ( 1 ) ,\
290+
HID_USAGE_MAX ( 5 ) ,\
291+
HID_LOGICAL_MIN ( 0 ) ,\
292+
HID_LOGICAL_MAX ( 1 ) ,\
293+
/* Left, Right, Middle, Backward, Forward buttons */ \
294+
HID_REPORT_COUNT( 5 ) ,\
295+
HID_REPORT_SIZE ( 1 ) ,\
296+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
297+
/* 3 bit padding */ \
298+
HID_REPORT_COUNT( 1 ) ,\
299+
HID_REPORT_SIZE ( 3 ) ,\
300+
HID_INPUT ( HID_CONSTANT ) ,\
301+
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
302+
/* X, Y absolute position [0, 32767] */ \
303+
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
304+
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
305+
HID_LOGICAL_MIN ( 0x00 ) ,\
306+
HID_LOGICAL_MAX_N( 0x7FFF, 2 ) ,\
307+
HID_REPORT_SIZE ( 16 ) ,\
308+
HID_REPORT_COUNT ( 2 ) ,\
309+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
310+
/* Vertical wheel scroll [-127, 127] */ \
311+
HID_USAGE ( HID_USAGE_DESKTOP_WHEEL ) ,\
312+
HID_LOGICAL_MIN ( 0x81 ) ,\
313+
HID_LOGICAL_MAX ( 0x7f ) ,\
314+
HID_REPORT_COUNT( 1 ) ,\
315+
HID_REPORT_SIZE ( 8 ) ,\
316+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\
317+
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ), \
318+
/* Horizontal wheel scroll [-127, 127] */ \
319+
HID_USAGE_N ( HID_USAGE_CONSUMER_AC_PAN, 2 ), \
320+
HID_LOGICAL_MIN ( 0x81 ), \
321+
HID_LOGICAL_MAX ( 0x7f ), \
322+
HID_REPORT_COUNT( 1 ), \
323+
HID_REPORT_SIZE ( 8 ), \
324+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ), \
325+
HID_COLLECTION_END , \
326+
HID_COLLECTION_END \
327+
269328
// Consumer Control Report Descriptor Template
270329
#define TUD_HID_REPORT_DESC_CONSUMER(...) \
271330
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\

0 commit comments

Comments
 (0)