Skip to content

Commit eb227d5

Browse files
committed
Cleanup working drivers
1 parent 911db9a commit eb227d5

File tree

4 files changed

+36
-145
lines changed

4 files changed

+36
-145
lines changed

main/badge_hid_drivers.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ esp_err_t analyze_mouse_layout(const uint8_t* desc, int desc_len, mouse_field_la
9292
report_count = data;
9393
else if (tag == HID_TAG_REPORT_ID) {
9494
layout_out->report_id = data;
95-
bit_offset += 8;
95+
bit_offset += 8;
9696
}
97-
break;;
97+
break;
98+
;
9899

99100
case HID_TYPE_LOCAL:
100101
if (tag == HID_TAG_USAGE && usage_index < 32)
@@ -308,11 +309,15 @@ esp_err_t analyze_gamepad_layout(const uint8_t* desc, const int desc_len, gamepa
308309
return ESP_OK;
309310
}
310311

311-
mouse_report_t parse_mouse_event(const uint8_t* data, const int length, mouse_field_layout_t* layout) {
312+
mouse_report_t handle_mouse_event(const uint8_t* data, const int length) {
313+
return parse_mouse_report(data, length, &mouse_layout);
314+
}
315+
316+
mouse_report_t parse_mouse_report(const uint8_t* data, const int length, mouse_field_layout_t* layout) {
312317

313318
// ESP_LOG_BUFFER_HEX(TAG, data, length);
314319

315-
mouse_report_t report = {0};
320+
mouse_report_t report = {0};
316321
if (!layout) {
317322
ESP_LOGW(TAG, "No layout for mouse!");
318323
return report;
@@ -341,11 +346,15 @@ mouse_report_t parse_mouse_event(const uint8_t* data, const int length, mouse_fi
341346
return report;
342347
}
343348

349+
gamepad_report_t handle_gamepad_event(const uint8_t* data, const int length) {
350+
return parse_gamepad_report(data, length, &gamepad_layout);
351+
}
352+
344353
gamepad_report_t parse_gamepad_report(const uint8_t* data, int length, gamepad_field_layout_t* layout) {
345354

346355
ESP_LOG_BUFFER_HEX(TAG, data, length);
347356

348-
gamepad_report_t report = {0};
357+
gamepad_report_t report = {0};
349358
if (!layout) {
350359
ESP_LOGW(TAG, "No layout for gamepad!");
351360
return report;
@@ -400,11 +409,15 @@ esp_err_t decode_descriptor_register_driver(const uint8_t* const desc, const int
400409
ESP_LOG_BUFFER_HEX(TAG, desc, desc_len);
401410
if (ESP_OK == analyze_mouse_layout(desc, desc_len, &mouse_layout)) {
402411
ESP_LOGI(TAG, "Parsed mouse layout:");
403-
ESP_LOGI(TAG, " Buttons: offset %u bits, count %u", mouse_layout.buttons.offset, mouse_layout.buttons.size);
404-
if (mouse_layout.x.present) ESP_LOGI(TAG, " X: offset %u bits, size %u bits", mouse_layout.x.offset, mouse_layout.x.size);
405-
if (mouse_layout.y.offset) ESP_LOGI(TAG, " Y: offset %u bits, size %u bits", mouse_layout.y.offset, mouse_layout.y.size);
412+
ESP_LOGI(TAG, " Buttons: offset %u bits, count %u", mouse_layout.buttons.offset,
413+
mouse_layout.buttons.size);
414+
if (mouse_layout.x.present)
415+
ESP_LOGI(TAG, " X: offset %u bits, size %u bits", mouse_layout.x.offset, mouse_layout.x.size);
416+
if (mouse_layout.y.offset)
417+
ESP_LOGI(TAG, " Y: offset %u bits, size %u bits", mouse_layout.y.offset, mouse_layout.y.size);
406418
if (mouse_layout.scroll.present)
407-
ESP_LOGI(TAG, " Scroll: offset %u bits, size %u bits", mouse_layout.scroll.offset, mouse_layout.scroll.size);
419+
ESP_LOGI(TAG, " Scroll: offset %u bits, size %u bits", mouse_layout.scroll.offset,
420+
mouse_layout.scroll.size);
408421
if (mouse_layout.tilt.present)
409422
ESP_LOGI(TAG, " Tilt: offset %u bits, size %u bits", mouse_layout.tilt.offset, mouse_layout.tilt.size);
410423
} else {

main/badge_hid_drivers.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ typedef struct {
180180
int8_t tilt;
181181
} mouse_report_t;
182182

183-
mouse_report_t parse_mouse_event(const uint8_t* const data, const int length, mouse_field_layout_t* layout);
183+
mouse_report_t handle_mouse_event(const uint8_t* const data, const int length);
184+
gamepad_report_t handle_gamepad_event(const uint8_t* const data, const int length);
185+
186+
mouse_report_t parse_mouse_report(const uint8_t* const data, const int length, mouse_field_layout_t* layout);
184187
gamepad_report_t parse_gamepad_report(const uint8_t* const data, const int length, gamepad_field_layout_t* layout);
185188

186189
esp_err_t analyze_mouse_layout(const uint8_t* desc, const int desc_len, mouse_field_layout_t* layout_out);

main/badge_hid_host.c

Lines changed: 2 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -93,70 +93,6 @@ void hid_host_device_callback(hid_host_device_handle_t hid_device_handle, const
9393
*/
9494
static 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-
277152
static 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) {
314189
static 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);

test_native/test_hid.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ int test_layouts(void) {
4343

4444
printf("Logitech layout M705 passed\n");
4545

46-
mouse_report = parse_mouse_event(mouse1_reports[0], 8, &mouse_layout);
46+
mouse_report = parse_mouse_report(mouse1_reports[0], 8, &mouse_layout);
4747
assert(0 == mouse_report.buttons.button1);
4848
assert(0 == mouse_report.buttons.button2);
4949
assert(0 == mouse_report.buttons.button3);
5050
assert(0 == mouse_report.x_displacement);
5151
assert(0 == mouse_report.y_displacement);
5252
assert(-2 == mouse_report.scroll);
5353
assert(0 == mouse_report.tilt);
54-
mouse_report = parse_mouse_event(mouse1_reports[1], 8, &mouse_layout);
54+
mouse_report = parse_mouse_report(mouse1_reports[1], 8, &mouse_layout);
5555
assert(0 == mouse_report.buttons.button1);
5656
assert(0 == mouse_report.buttons.button2);
5757
assert(0 == mouse_report.buttons.button3);
5858
assert(-1 == mouse_report.x_displacement);
5959
assert(4 == mouse_report.y_displacement);
6060
assert(0 == mouse_report.scroll);
6161
assert(1 == mouse_report.tilt);
62-
mouse_report = parse_mouse_event(mouse1_reports[2], 8, &mouse_layout);
62+
mouse_report = parse_mouse_report(mouse1_reports[2], 8, &mouse_layout);
6363
assert(0 == mouse_report.buttons.button1);
6464
assert(0 == mouse_report.buttons.button2);
6565
assert(0 == mouse_report.buttons.button3);
@@ -92,23 +92,23 @@ int test_layouts(void) {
9292

9393
printf("Trust Kuza layout passed\n");
9494

95-
mouse_report = parse_mouse_event(mouse2_reports[0], 5, &mouse_layout);
95+
mouse_report = parse_mouse_report(mouse2_reports[0], 5, &mouse_layout);
9696
assert(0 == mouse_report.buttons.button1);
9797
assert(0 == mouse_report.buttons.button2);
9898
assert(0 == mouse_report.buttons.button3);
9999
assert(48 == mouse_report.x_displacement);
100100
assert(-5 == mouse_report.y_displacement);
101101
assert(-2 == mouse_report.scroll);
102102
assert(0 == mouse_report.tilt);
103-
mouse_report = parse_mouse_event(mouse2_reports[1], 5, &mouse_layout);
103+
mouse_report = parse_mouse_report(mouse2_reports[1], 5, &mouse_layout);
104104
assert(1 == mouse_report.buttons.button1);
105105
assert(0 == mouse_report.buttons.button2);
106106
assert(0 == mouse_report.buttons.button3);
107107
assert(-3 == mouse_report.x_displacement);
108108
assert(11 == mouse_report.y_displacement);
109109
assert(2 == mouse_report.scroll);
110110
assert(0 == mouse_report.tilt);
111-
mouse_report = parse_mouse_event(mouse2_reports[2], 5, &mouse_layout);
111+
mouse_report = parse_mouse_report(mouse2_reports[2], 5, &mouse_layout);
112112
assert(1 == mouse_report.buttons.button1);
113113
assert(1 == mouse_report.buttons.button2);
114114
assert(0 == mouse_report.buttons.button3);
@@ -141,15 +141,15 @@ int test_layouts(void) {
141141

142142
printf("Fujitsu M520 layout passed\n");
143143

144-
mouse_report = parse_mouse_event(mouse3_reports[0], 4, &mouse_layout);
144+
mouse_report = parse_mouse_report(mouse3_reports[0], 4, &mouse_layout);
145145
assert(1 == mouse_report.buttons.button1);
146146
assert(0 == mouse_report.buttons.button2);
147147
assert(1 == mouse_report.buttons.button3);
148148
assert(16 == mouse_report.x_displacement);
149149
assert(-16 == mouse_report.y_displacement);
150150
assert(1 == mouse_report.scroll);
151151
assert(0 == mouse_report.tilt);
152-
mouse_report = parse_mouse_event(mouse3_reports[1], 4, &mouse_layout);
152+
mouse_report = parse_mouse_report(mouse3_reports[1], 4, &mouse_layout);
153153
assert(0 == mouse_report.buttons.button1);
154154
assert(0 == mouse_report.buttons.button2);
155155
assert(0 == mouse_report.buttons.button3);

0 commit comments

Comments
 (0)