-
Notifications
You must be signed in to change notification settings - Fork 2
/
hid_app.cpp
720 lines (671 loc) · 30 KB
/
hid_app.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
* author : Shuichi TAKANO
* since : Thu Jul 29 2021 03:39:11
*/
#include <tusb.h>
#include <stdio.h>
#include "xinput_host.h"
#include "gamepad.h"
#ifdef __cplusplus
extern "C"
{
#endif
#define MAX_REPORT 4
namespace
{
uint8_t _report_count[CFG_TUH_HID];
tuh_hid_report_info_t _report_info_arr[CFG_TUH_HID][MAX_REPORT];
// Is dual shock 4 controller detected?
bool isDS4(uint16_t vid, uint16_t pid)
{
return vid == 0x054c && (pid == 0x09cc || pid == 0x05c4);
}
// Is dual sense controller detected?
bool isDS5(uint16_t vid, uint16_t pid)
{
return vid == 0x054c && pid == 0x0ce6;
}
// Is PSClassic controller detected?
bool isPSClassic(uint16_t vid, uint16_t pid)
{
return vid == 0x054c && pid == 0x0cda;
}
// Is Nintendo controller detected?
bool isNintendo(uint16_t vid, uint16_t pid)
{
return vid == 0x057e && (pid == 0x2009 || pid == 0x2017);
}
// Is Genesis Mini 1 controller or Genesis Mini 2 controller detected?
bool isGenesisMini(uint16_t vid, uint16_t pid)
{
return vid == 0x0ca3 && (pid == 0x0025 || pid == 0x0024);
}
// Is MantaPad detected? Cheap Aliexpress SNES/NES controller
bool isMantaPad(uint16_t vid, uint16_t pid)
{
return vid == 0x081f && pid == 0xe401;
}
struct DS4Report
{
// https://www.psdevwiki.com/ps4/DS4-USB
struct Button1
{
inline static constexpr int SQUARE = 1 << 4;
inline static constexpr int CROSS = 1 << 5;
inline static constexpr int CIRCLE = 1 << 6;
inline static constexpr int TRIANGLE = 1 << 7;
};
struct Button2
{
inline static constexpr int L1 = 1 << 0;
inline static constexpr int R1 = 1 << 1;
inline static constexpr int L2 = 1 << 2;
inline static constexpr int R2 = 1 << 3;
inline static constexpr int SHARE = 1 << 4;
inline static constexpr int OPTIONS = 1 << 5;
inline static constexpr int L3 = 1 << 6;
inline static constexpr int R3 = 1 << 7;
};
uint8_t reportID;
uint8_t stickL[2];
uint8_t stickR[2];
uint8_t buttons1;
uint8_t buttons2;
uint8_t ps : 1;
uint8_t tpad : 1;
uint8_t counter : 6;
uint8_t triggerL;
uint8_t triggerR;
// ...
int getHat() const { return buttons1 & 15; }
};
struct DS5Report
{
uint8_t reportID;
uint8_t stickL[2];
uint8_t stickR[2];
uint8_t triggerL;
uint8_t triggerR;
uint8_t counter;
uint8_t buttons[3];
// ...
struct Button
{
inline static constexpr int SQUARE = 1 << 4;
inline static constexpr int CROSS = 1 << 5;
inline static constexpr int CIRCLE = 1 << 6;
inline static constexpr int TRIANGLE = 1 << 7;
inline static constexpr int L1 = 1 << 8;
inline static constexpr int R1 = 1 << 9;
inline static constexpr int L2 = 1 << 10;
inline static constexpr int R2 = 1 << 11;
inline static constexpr int SHARE = 1 << 12;
inline static constexpr int OPTIONS = 1 << 13;
inline static constexpr int L3 = 1 << 14;
inline static constexpr int R3 = 1 << 15;
inline static constexpr int PS = 1 << 16;
inline static constexpr int TPAD = 1 << 17;
};
int getHat() const { return buttons[0] & 15; }
};
// Report for Genesis Mini controller
struct GenesisMiniReport
{
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
uint8_t byte4;
uint8_t byte5;
uint8_t byte6;
uint8_t byte7;
uint8_t byte8;
struct Button
{
inline static constexpr int A = 0b01000000;
inline static constexpr int B = 0b00100000;
inline static constexpr int C = 0b00000010;
inline static constexpr int START = 0b00100000;
inline static constexpr int UP = 0;
inline static constexpr int DOWN = 0b11111111;
inline static constexpr int LEFT = 0;
inline static constexpr int RIGHT = 0b11111111;
;
};
};
// Report for MantaPad, cheap AliExpress SNES controller
struct MantaPadReport
{
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
uint8_t byte4;
uint8_t byte5;
uint8_t byte6;
uint8_t byte7;
uint8_t byte8;
struct Button
{
inline static constexpr int A = 0b00100000;
inline static constexpr int SNESB = 0b01000000;
inline static constexpr int X = 0b00010000;
inline static constexpr int NESB = X; // B Button on NES controller is X on SNES controller
inline static constexpr int Y = 0b10000000;
inline static constexpr int SELECT = 0b00010000;
inline static constexpr int START = 0b00100000;
inline static constexpr int UP = 0b00000000;
inline static constexpr int DOWN = 0b11111111;
inline static constexpr int LEFT = 0b00000000;
inline static constexpr int RIGHT = 0b11111111;
inline static constexpr int SHOULDERLEFT = 0b00000001;
inline static constexpr int SHOULDERRIGHT = 0b00000010;
};
};
// Report for PSClassic controller
struct PSClassicReport
{
uint8_t buttons;
// Idle 00010100
// up 00000100
// upright 00001000
// right 00011000
// rightdown 00101000
// down 00100100
// downleft 00100000
// left 00010000
// leftup 00100000
// start 00010110
// select 00010101
// St + sel 00010111
// selectup 00000101
// selectdown 00100101
uint8_t hat;
struct Button
{
inline static constexpr int ButtonsIdle = 0x00;
inline static constexpr int HatIdle = 0b00010100;
inline static constexpr int Circle = 0x02;
inline static constexpr int Cross = 0x04;
inline static constexpr int SELECT = 0b00010101;
inline static constexpr int START = 0b00010110;
inline static constexpr int UP = 0b00000100;
inline static constexpr int UPRIGHT = 0b00001000;
inline static constexpr int RIGHT = 0b00011000;
inline static constexpr int RIGHTDOWN = 0b00101000;
inline static constexpr int DOWN = 0b00100100;
inline static constexpr int DOWNLEFT = 0b00100000;
inline static constexpr int LEFT = 0b00010000;
inline static constexpr int LEFTUP = 0b00000000;
inline static constexpr int SELECTUP = 0b00000101;
inline static constexpr int SELECTDOWN = 0b00100101;
inline static constexpr int SELECTSTART = 0b00010111;
};
};
}
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len)
{
uint16_t vid, pid;
auto &gp = io::getCurrentGamePadState(0);
tuh_vid_pid_get(dev_addr, &vid, &pid);
if (isDS4(vid, pid))
{
printf("Dual Shock 4 Controller detected - device address = %d, instance = %d is mounted - ", dev_addr, instance);
strcpy(gp.GamePadName, "Dual Shock 4");
}
else if (isDS5(vid, pid))
{
printf("Dual Sense Controller detected - device address = %d, instance = %d is mounted - ", dev_addr, instance);
strcpy(gp.GamePadName, "Dual Sense");
}
else if (isMantaPad(vid, pid))
{
printf("MantaPad detected - device address = %d, instance = %d is mounted - ", dev_addr, instance);
strcpy(gp.GamePadName, "Manta");
}
else if (isGenesisMini(vid, pid))
{
printf("Sega Mega Drive/Genesis Mini %d controller detected - device address = %d, instance = %d is mounted - ", (pid == 0x0025) ? 1 : 2, dev_addr, instance);
sprintf(gp.GamePadName, "Genesis Mini %d", (pid == 0x0025) ? 1 : 2);
}
else if (isPSClassic(vid, pid))
{
printf("PlayStation Classic controller detected - device address = %d, instance = %d is mounted - ", dev_addr, instance);
strcpy(gp.GamePadName, "PSClassic");
}
// else if (isNintendo(vid, pid))
// {
// printf("(Unsupported) Nintendo controller detected - device address = %d, instance = %d is mounted - ", dev_addr, instance);
// strcpy(gp.GamePadName, "Nintendo");
// }
else
{
printf("Unkown device detected - HID device address = %d, instance = %d is mounted - ", dev_addr, instance);
sprintf(gp.GamePadName, "%04x:%04x", vid, pid);
}
printf("VID = %04x, PID = %04x\r\n", vid, pid);
const char *protocol_str[] = {"None", "Keyboard", "Mouse"}; // hid_protocol_type_t
uint8_t const interface_protocol = tuh_hid_interface_protocol(dev_addr, instance);
// Parse report descriptor with built-in parser
_report_count[instance] = tuh_hid_parse_report_descriptor(_report_info_arr[instance], MAX_REPORT, desc_report, desc_len);
printf("HID has %u reports and interface protocol = %d:%s\n", _report_count[instance],
interface_protocol, protocol_str[interface_protocol]);
if (!tuh_hid_receive_report(dev_addr, instance))
{
printf("Error: cannot request to receive report\r\n");
}
}
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
{
printf("HID device address = %d, instance = %d is unmounted\n", dev_addr, instance);
// Assume the controller is disconnected
auto &gp = io::getCurrentGamePadState(0);
gp.flagConnected(false);
gp.GamePadName[0] = 0;
}
void tuh_hid_report_received_cb(uint8_t dev_addr,
uint8_t instance, uint8_t const *report, uint16_t len)
{
uint8_t const rpt_count = _report_count[instance];
tuh_hid_report_info_t *rpt_info_arr = _report_info_arr[instance];
tuh_hid_report_info_t *rpt_info = NULL;
uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);
if (isDS4(vid, pid))
{
if (sizeof(DS4Report) <= len)
{
auto r = reinterpret_cast<const DS4Report *>(report);
if (r->reportID != 1)
{
printf("Invalid reportID %d\n", r->reportID);
return;
}
auto &gp = io::getCurrentGamePadState(0);
gp.axis[0] = r->stickL[0];
gp.axis[1] = r->stickL[1];
gp.buttons =
(r->buttons1 & DS4Report::Button1::CROSS ? io::GamePadState::Button::B : 0) |
(r->buttons1 & DS4Report::Button1::CIRCLE ? io::GamePadState::Button::A : 0) |
(r->buttons1 & DS4Report::Button1::TRIANGLE ? io::GamePadState::Button::X : 0) |
(r->buttons1 & DS4Report::Button1::SQUARE ? io::GamePadState::Button::Y : 0) |
(r->buttons2 & DS4Report::Button2::SHARE ? io::GamePadState::Button::SELECT : 0) |
(r->tpad ? io::GamePadState::Button::SELECT : 0) |
(r->buttons2 & DS4Report::Button2::OPTIONS ? io::GamePadState::Button::START : 0);
gp.hat = static_cast<io::GamePadState::Hat>(r->getHat());
gp.convertButtonsFromAxis(0, 1);
gp.convertButtonsFromHat();
gp.flagConnected(true);
}
else
{
printf("Invalid DS4 report size %zd\n", len);
return;
}
}
else if (isDS5(vid, pid))
{
if (sizeof(DS5Report) <= len)
{
auto r = reinterpret_cast<const DS5Report *>(report);
if (r->reportID != 1)
{
printf("Invalid reportID %d\n", r->reportID);
return;
}
auto buttons = r->buttons[0] | (r->buttons[1] << 8) | (r->buttons[2] << 16);
auto &gp = io::getCurrentGamePadState(0);
gp.axis[0] = r->stickL[0];
gp.axis[1] = r->stickL[1];
gp.buttons =
(buttons & DS5Report::Button::CROSS ? io::GamePadState::Button::B : 0) |
(buttons & DS5Report::Button::CIRCLE ? io::GamePadState::Button::A : 0) |
(buttons & DS5Report::Button::TRIANGLE ? io::GamePadState::Button::X : 0) |
(buttons & DS5Report::Button::SQUARE ? io::GamePadState::Button::Y : 0) |
(buttons & (DS5Report::Button::SHARE | DS5Report::Button::TPAD) ? io::GamePadState::Button::SELECT : 0) |
(buttons & DS5Report::Button::OPTIONS ? io::GamePadState::Button::START : 0);
gp.hat = static_cast<io::GamePadState::Hat>(r->getHat());
gp.convertButtonsFromAxis(0, 1);
gp.convertButtonsFromHat();
gp.flagConnected(true);
}
else
{
printf("Invalid DS5 report size %zd\n", len);
return;
}
}
else if (isMantaPad(vid, pid))
{
if (sizeof(MantaPadReport) == len)
{
auto r = reinterpret_cast<const MantaPadReport *>(report);
auto &gp = io::getCurrentGamePadState(0);
gp.buttons =
(r->byte6 & MantaPadReport::Button::A ? io::GamePadState::Button::A : 0) |
(r->byte6 & MantaPadReport::Button::NESB ? io::GamePadState::Button::B : 0) |
(r->byte6 & MantaPadReport::Button::SNESB ? io::GamePadState::Button::B : 0) |
(r->byte7 & MantaPadReport::Button::START ? io::GamePadState::Button::START : 0) |
(r->byte7 & MantaPadReport::Button::SELECT ? io::GamePadState::Button::SELECT : 0) |
(r->byte2 == MantaPadReport::Button::UP ? io::GamePadState::Button::UP : 0) |
(r->byte2 == MantaPadReport::Button::DOWN ? io::GamePadState::Button::DOWN : 0) |
(r->byte1 == MantaPadReport::Button::LEFT ? io::GamePadState::Button::LEFT : 0) |
(r->byte1 == MantaPadReport::Button::RIGHT ? io::GamePadState::Button::RIGHT : 0);
gp.flagConnected(true);
}
else
{
printf("Invalid MantaPad report size %zd\n", len);
return;
}
}
else if (isGenesisMini(vid, pid))
{
if (sizeof(GenesisMiniReport) == len)
{
auto r = reinterpret_cast<const GenesisMiniReport *>(report);
auto &gp = io::getCurrentGamePadState(0);
// Swap A and B because the button layout is different from NES controller
gp.buttons =
(r->byte6 & GenesisMiniReport::Button::B ? io::GamePadState::Button::A : 0) |
(r->byte6 & GenesisMiniReport::Button::A ? io::GamePadState::Button::B : 0) |
(r->byte7 & GenesisMiniReport::Button::C ? io::GamePadState::Button::SELECT : 0) |
(r->byte7 & GenesisMiniReport::Button::START ? io::GamePadState::Button::START : 0) |
(r->byte5 == GenesisMiniReport::Button::UP ? io::GamePadState::Button::UP : 0) |
(r->byte5 == GenesisMiniReport::Button::DOWN ? io::GamePadState::Button::DOWN : 0) |
(r->byte4 == GenesisMiniReport::Button::LEFT ? io::GamePadState::Button::LEFT : 0) |
(r->byte4 == GenesisMiniReport::Button::RIGHT ? io::GamePadState::Button::RIGHT : 0);
gp.flagConnected(true);
}
else
{
printf("Invalid Genesis Mini report size %zd\n", len);
return;
}
}
else if (isPSClassic(vid, pid))
{
if (sizeof(PSClassicReport) == len)
{
auto r = reinterpret_cast<const PSClassicReport *>(report);
auto &gp = io::getCurrentGamePadState(0);
gp.buttons =
(r->buttons & PSClassicReport::Button::Cross ? io::GamePadState::Button::B : 0) |
(r->buttons & PSClassicReport::Button::Circle ? io::GamePadState::Button::A : 0);
switch (r->hat)
{
case PSClassicReport::Button::UP:
gp.buttons = gp.buttons | io::GamePadState::Button::UP;
break;
case PSClassicReport::Button::UPRIGHT:
gp.buttons = gp.buttons | io::GamePadState::Button::UP | io::GamePadState::Button::RIGHT;
break;
case PSClassicReport::Button::RIGHT:
gp.buttons = gp.buttons | io::GamePadState::Button::RIGHT;
break;
case PSClassicReport::Button::RIGHTDOWN:
gp.buttons = gp.buttons | io::GamePadState::Button::RIGHT | io::GamePadState::Button::DOWN;
break;
case PSClassicReport::Button::DOWN:
gp.buttons = gp.buttons | io::GamePadState::Button::DOWN;
break;
case PSClassicReport::Button::DOWNLEFT:
gp.buttons = gp.buttons | io::GamePadState::Button::DOWN | io::GamePadState::Button::LEFT;
break;
case PSClassicReport::Button::LEFT:
gp.buttons = gp.buttons | io::GamePadState::Button::LEFT;
break;
case PSClassicReport::Button::LEFTUP:
gp.buttons = gp.buttons | io::GamePadState::Button::LEFT | io::GamePadState::Button::UP;
break;
case PSClassicReport::Button::SELECT:
gp.buttons = gp.buttons | io::GamePadState::Button::SELECT;
break;
case PSClassicReport::Button::START:
gp.buttons = gp.buttons | io::GamePadState::Button::START;
break;
case PSClassicReport::Button::SELECTSTART:
gp.buttons = gp.buttons | io::GamePadState::Button::SELECT | io::GamePadState::Button::START;
break;
case PSClassicReport::Button::SELECTUP:
gp.buttons = gp.buttons | io::GamePadState::Button::SELECT | io::GamePadState::Button::UP;
break;
case PSClassicReport::Button::SELECTDOWN:
gp.buttons = gp.buttons | io::GamePadState::Button::SELECT | io::GamePadState::Button::DOWN;
break;
default:
break;
}
gp.flagConnected(true);
}
else
{
printf("Invalid PSClassic Mini report size %zd\n", len);
return;
}
}
else if (isNintendo(vid, pid))
{
printf("Nintendo: len = %d\n", len); // Nintendo controllers do not report back
}
else
{
if (rpt_count == 1 && rpt_info_arr[0].report_id == 0)
{
// Simple report without report ID as 1st byte
rpt_info = &rpt_info_arr[0];
}
else
{
// Composite report, 1st byte is report ID, data starts from 2nd byte
uint8_t const rpt_id = report[0];
// Find report id in the arrray
for (uint8_t i = 0; i < rpt_count; i++)
{
if (rpt_id == rpt_info_arr[i].report_id)
{
rpt_info = &rpt_info_arr[i];
break;
}
}
report++;
len--;
}
if (!rpt_info)
{
printf("Couldn't find the report info for this report !\n");
return;
}
// printf("usage %d, %d\n", rpt_info->usage_page, rpt_info->usage);
if (rpt_info->usage_page == HID_USAGE_PAGE_DESKTOP)
{
switch (rpt_info->usage)
{
case HID_USAGE_DESKTOP_KEYBOARD:
{
auto r = reinterpret_cast<const hid_keyboard_report_t *>(report);
auto &gp = io::getCurrentGamePadState(0);
strcpy(gp.GamePadName, "Keyboard");
gp.buttons = 0;
for (uint8_t i = 0; i < 6; i++)
{
if (r->keycode[i])
{
switch (r->keycode[i])
{
case HID_KEY_A:
gp.buttons |= io::GamePadState::Button::SELECT;
break;
case HID_KEY_S:
gp.buttons |= io::GamePadState::Button::START;
break;
case HID_KEY_Z:
gp.buttons |= io::GamePadState::Button::B;
break;
case HID_KEY_X:
gp.buttons |= io::GamePadState::Button::A;
break;
case HID_KEY_ARROW_UP:
gp.buttons |= io::GamePadState::Button::UP;
break;
case HID_KEY_ARROW_DOWN:
gp.buttons |= io::GamePadState::Button::DOWN;
break;
case HID_KEY_ARROW_LEFT:
gp.buttons |= io::GamePadState::Button::LEFT;
break;
case HID_KEY_ARROW_RIGHT:
gp.buttons |= io::GamePadState::Button::RIGHT;
break;
default:
break;
}
}
}
break;
}
case HID_USAGE_DESKTOP_MOUSE:
TU_LOG1("HID receive mouse report\n");
// Assume mouse follow boot report layout
// process_mouse_report((hid_mouse_report_t const *)report);
break;
case HID_USAGE_DESKTOP_JOYSTICK:
{
// TU_LOG1("HID receive joystick report\n");
struct JoyStickReport
{
uint8_t axis[3];
uint8_t buttons;
// 実際のところはしらん
};
auto *rep = reinterpret_cast<const JoyStickReport *>(report);
// printf("x %d y %d button %02x\n", rep->axis[0], rep->axis[1], rep->buttons);
auto &gp = io::getCurrentGamePadState(0);
gp.axis[0] = rep->axis[0];
gp.axis[1] = rep->axis[1];
gp.axis[2] = rep->axis[2];
gp.buttons = rep->buttons;
gp.convertButtonsFromAxis(0, 1);
// BUFFALO BGC-FC801
// VID = 0411, PID = 00c6
}
break;
case HID_USAGE_DESKTOP_GAMEPAD:
TU_LOG1("HID receive gamepad report\n");
break;
default:
break;
}
}
}
if (!tuh_hid_receive_report(dev_addr, instance))
{
printf("Error: cannot request to receive report\r\n");
}
}
#pragma region XINPUT
// Since https://github.com/hathach/tinyusb/pull/2222, we can add in custom vendor drivers easily
usbh_class_driver_t const *usbh_app_driver_get_cb(uint8_t *driver_count)
{
*driver_count = 1;
return &usbh_xinput_driver;
}
// XXInput type of controlles. (Xbox 360, Xbox One, Xbox Series X)
// This is somewhat flaky and might not work with all controllers.
// Tested devices:
// - xbox Series X controller : Works
// - xbox One controller : Works
// - xbox elite controller : Works
// - 8bitdo SN30 Pro+ V6.01: Works. Hold X + Start to switch to Xinput mode. (LED 1 and 2 will blink). Then connect to USB.
// - 8bitdo Pro 2 V3.04: Works. Hold X + Start to switch to Xinput mode. (LED 1 and 2 will blink). Then connect to USB.
// - 8bitdo SN30 PRO Wired : Not working, recognized but no report
// - 8bitdo SF30 v2.05 Pro : Works Hold X + Start to switch to Xinput mode. (LED 1 and 2 will blink). Then connect to USB.
// - 8bitdo SN30 v2.05 Pro : Not tested, should probably work
//
// Troubleshooting:
// After flashing some bigger games, the controller might become unresponsive:
// - XBOX Controller. Play always with batteries removed. When controller becomes unresponsive:
// - unplug and replug the controller.
// - If controller is still unresponsive, unplug the pico from power, wait a few seconds then plug it back in.
// - Press start on the controller to start the last flashed game.
// - 8bitdo controllers, when controller becomes unresponsive:
// - Disconnect the controller
// - Hold start to switch the controller off (if it has built-in battery).
// - reconnect the controller.
// - Press start to start the last flashed game.
void tuh_xinput_report_received_cb(uint8_t dev_addr, uint8_t instance, xinputh_interface_t const *xid_itf, uint16_t len)
{
const xinput_gamepad_t *p = &xid_itf->pad;
const char *type_str;
if (xid_itf->last_xfer_result == XFER_RESULT_SUCCESS)
{
switch (xid_itf->type)
{
case 1:
type_str = "Xbox One";
break;
case 2:
type_str = "Xbox 360 Wireless";
break;
case 3:
type_str = "Xbox 360 Wired";
break;
case 4:
type_str = "Xbox OG";
break;
default:
type_str = "Unknown";
}
if (xid_itf->connected && xid_itf->new_pad_data)
{
auto &gp = io::getCurrentGamePadState(0);
gp.buttons = 0;
if (p->wButtons & XINPUT_GAMEPAD_A)
gp.buttons |= io::GamePadState::Button::B;
if (p->wButtons & XINPUT_GAMEPAD_B)
gp.buttons |= io::GamePadState::Button::A;
if (p->wButtons & XINPUT_GAMEPAD_DPAD_UP)
gp.buttons |= io::GamePadState::Button::UP;
if (p->wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
gp.buttons |= io::GamePadState::Button::DOWN;
if (p->wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
gp.buttons |= io::GamePadState::Button::LEFT;
if (p->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
gp.buttons |= io::GamePadState::Button::RIGHT;
if (p->wButtons & XINPUT_GAMEPAD_START)
gp.buttons |= io::GamePadState::Button::START;
if (p->wButtons & XINPUT_GAMEPAD_BACK)
gp.buttons |= io::GamePadState::Button::SELECT;
if (p->wButtons & XINPUT_GAMEPAD_GUIDE)
gp.buttons |= (io::GamePadState::Button::START | io::GamePadState::Button::SELECT);
gp.flagConnected(true);
}
}
tuh_xinput_receive_report(dev_addr, instance);
}
void tuh_xinput_mount_cb(uint8_t dev_addr, uint8_t instance, const xinputh_interface_t *xinput_itf)
{
auto &gp = io::getCurrentGamePadState(0);
strcpy(gp.GamePadName, "XInput");
printf("XINPUT MOUNTED %02x %d\n", dev_addr, instance);
// If this is a Xbox 360 Wireless controller we need to wait for a connection packet
// on the in pipe before setting LEDs etc. So just start getting data until a controller is connected.
if (xinput_itf->type == XBOX360_WIRELESS && xinput_itf->connected == false)
{
tuh_xinput_receive_report(dev_addr, instance);
return;
}
tuh_xinput_set_led(dev_addr, instance, 0, true);
tuh_xinput_set_led(dev_addr, instance, 1, true);
tuh_xinput_set_rumble(dev_addr, instance, 0, 0, true);
tuh_xinput_receive_report(dev_addr, instance);
}
void tuh_xinput_umount_cb(uint8_t dev_addr, uint8_t instance)
{
auto &gp = io::getCurrentGamePadState(0);
gp.GamePadName[0] = 0;
gp.flagConnected(false);
printf("XINPUT UNMOUNTED %02x %d\n", dev_addr, instance);
}
#pragma endregion
#ifdef __cplusplus
}
#endif