@@ -93,70 +93,6 @@ void hid_host_device_callback(hid_host_device_handle_t hid_device_handle, const
9393 */
9494static const char * hid_proto_name_str [] = {"NONE" , "KEYBOARD" , "MOUSE" };
9595
96- /**
97- * @brief Sign-extends a 12-bit value to a 16-bit signed integer.
98- *
99- * Many HID mice encode high-resolution X/Y deltas using 12-bit signed values.
100- * This function correctly extends them to usable 16-bit signed values.
101- *
102- * @param value A 12-bit unsigned value (lower 12 bits significant).
103- * @return int16_t Signed version of the value.
104- */
105- inline int16_t sign_extend_12bit (uint16_t value ) {
106- if (value & 0x800 ) {
107- // If the 12th bit is set (negative number in 12-bit signed)
108- return (int16_t )(value | 0xF000 ); // Fill top 4 bits with 1s
109- } else {
110- return (int16_t )(value & 0x0FFF ); // Mask to 12 bits
111- }
112- }
113-
114- /**
115- * @brief Parses a mouse input report into a structured format.
116- *
117- * Supports both boot protocol reports (4 bytes) and extended HID reports.
118- *
119- * @param data Raw pointer to HID report data.
120- * @param length Length of the report in bytes.
121- * @return mouse_report_t Parsed report with movement and button states.
122- */
123- // static mouse_report_t parse_mouse_event(const uint8_t* const data, const int length) {
124- // mouse_report_t mouse_report = {0};
125-
126- // if (length <= 4) {
127- // hid_mouse_input_report_boot_t* boot_mouse_report = (hid_mouse_input_report_boot_t*)data;
128- // mouse_report.x_displacement = boot_mouse_report->x_displacement;
129- // mouse_report.y_displacement = boot_mouse_report->y_displacement;
130- // mouse_report.buttons.val = boot_mouse_report->buttons.val;
131- // if (length == 3) {
132- // mouse_report.scroll = data[4];
133- // }
134- // } else if (length == 5) {
135- // // Modern Logitech
136- // mouse_report.buttons.val = data[0];
137- // mouse_report.x_displacement = (int8_t)data[1];
138- // mouse_report.y_displacement = (int8_t)data[2];
139- // mouse_report.scroll = (int8_t)data[3];
140- // mouse_report.tilt = (int8_t)data[4];
141- // } else if (length < 9) {
142- // mouse_report.buttons.val = data[1];
143- // mouse_report.x_displacement = sign_extend_12bit((data[4] & 0x0F) << 8) | data[3];
144- // mouse_report.y_displacement = sign_extend_12bit(data[5] << 4) | (data[4] >> 4);
145- // mouse_report.scroll = (int8_t)data[6];
146- // if (length == 8) {
147- // mouse_report.tilt = (int8_t)data[7];
148- // }
149- // } else {
150- // mouse_report.buttons.val = data[1];
151- // mouse_report.x_displacement = (int16_t)((data[4] << 8) | data[3]);
152- // mouse_report.y_displacement = (int16_t)((data[6] << 8) | data[5]);
153- // mouse_report.scroll = (int8_t)data[7];
154- // mouse_report.tilt = (int8_t)data[8];
155- // }
156-
157- // return mouse_report;
158- // }
159-
16096/**
16197 * @brief USB HID Host Mouse Interface report callback handler
16298 *
@@ -169,7 +105,7 @@ static void hid_host_mouse_report_callback(const uint8_t* const data, const int
169105 return ;
170106 }
171107
172- mouse_report_t mouse_report = parse_mouse_event (data , length );
108+ mouse_report_t mouse_report = handle_mouse_event (data , length );
173109
174110 static int x_pos = 0 ;
175111 static int y_pos = 0 ;
@@ -213,67 +149,6 @@ static void hid_host_mouse_report_callback(const uint8_t* const data, const int
213149 (mouse_report .buttons .button2 ? 'o' : ' ' ), x_scroll , y_scroll );
214150}
215151
216- /**
217- * @brief Parses a gamepad HID report into the standard format.
218- *
219- * This function should be implemented per controller type (e.g., PS4, Xbox).
220- * It fills out the gamepad_report_t with button and axis values.
221- *
222- * @param data Raw HID report data.
223- * @param length Report length in bytes.
224- * @return gamepad_report_t
225- */
226- // gamepad_report_t parse_gamepad_report(const uint8_t* data, int length) {
227- // gamepad_report_t rpt = {0};
228-
229- // if (length < 10) return rpt;
230-
231- // rpt.report_id = data[0];
232-
233- // uint8_t hat = data[1];
234- // uint8_t b1 = data[2];
235- // uint8_t b2 = data[3];
236-
237- // rpt.buttons.val = 0;
238-
239- // rpt.buttons.up = (hat == 0x00 || hat == 0x01 || hat == 0x07);
240- // rpt.buttons.right = (hat == 0x01 || hat == 0x02 || hat == 0x03);
241- // rpt.buttons.down = (hat == 0x03 || hat == 0x04 || hat == 0x05);
242- // rpt.buttons.left = (hat == 0x05 || hat == 0x06 || hat == 0x07);
243-
244- // // Face buttons
245- // rpt.buttons.a = (b2 >> 6) & 1;
246- // rpt.buttons.b = (b2 >> 5) & 1;
247- // rpt.buttons.x = (b2 >> 4) & 1;
248- // rpt.buttons.y = (b2 >> 3) & 1;
249-
250- // // Thumbsticks
251- // rpt.buttons.l1 = (b2 >> 0) & 1;
252- // rpt.buttons.r1 = (b1 >> 7) & 1;
253-
254- // // Shoulders and triggers
255- // rpt.buttons.l2 = (b2 >> 2) & 1;
256- // rpt.buttons.r2 = (b2 >> 1) & 1;
257- // rpt.buttons.l3 = (b1 >> 2) & 1;
258- // rpt.buttons.r3 = (b1 >> 3) & 1;
259-
260- // // Extra buttons
261- // rpt.buttons.l4 = (b1 >> 1) & 1;
262- // rpt.buttons.r4 = (b1 >> 0) & 1;
263- // rpt.buttons.select = (b1 >> 6) & 1;
264- // rpt.buttons.start = (b1 >> 5) & 1;
265- // rpt.buttons.home = (b1 >> 4) & 1;
266-
267- // rpt.lx = data[4];
268- // rpt.ly = data[5];
269- // rpt.rx = data[6];
270- // rpt.ry = data[7];
271- // rpt.lt = data[8];
272- // rpt.rt = data[9];
273-
274- // return rpt;
275- // }
276-
277152static void print_gamepad_report (const gamepad_report_t * rpt , int length ) {
278153 char line1 [64 ], line2 [64 ], button_line [128 ];
279154
@@ -314,7 +189,7 @@ static void print_gamepad_report(const gamepad_report_t* rpt, int length) {
314189static void hid_host_generic_report_callback (const uint8_t * const data , const int length ) {
315190 ESP_LOGI (TAG , "Received generic report (%d bytes)" , length );
316191 if (length >= 10 ) {
317- gamepad_report_t rpt = parse_gamepad_report (data , length );
192+ gamepad_report_t rpt = handle_gamepad_event (data , length );
318193
319194 int lx = ((int )rpt .lx - 128 );
320195 int ly = ((int )rpt .ly - 128 );
0 commit comments