Lecture manette "Gamepad" en USB

This commit is contained in:
Samuel 2025-08-18 20:21:17 +02:00
parent 27951754ed
commit 2ab4f9a595
2 changed files with 82 additions and 2 deletions

View File

@ -49,6 +49,37 @@ static void process_kbd_report(hid_keyboard_report_t const *report);
static void process_mouse_report(hid_mouse_report_t const * report);
static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len);
// HID report from 8bitPro
typedef struct TU_ATTR_PACKED
{
uint8_t dummy;
uint8_t x, y, z, rz; // joystick
uint hat_switch:4;
uint b1:1;
uint b2:1;
uint b3:1;
uint b4:1; // 8 bits
uint b5:1;
uint b6:1;
uint b7:1;
uint b8:1;
uint b9:1;
uint b10:1;
uint b11:1;
uint b12:1; // 16 bits
uint b13:1;
uint b14:1;
uint data_1:6;
uint gachette_gauche:8;
uint gachette_droite:8;
} _8bitpro_report_t ;
void hid_app_task(void)
{
// nothing to do
@ -77,8 +108,20 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
// Therefore for this simple example, we only need to parse generic report descriptor (with built-in parser)
if ( itf_protocol == HID_ITF_PROTOCOL_NONE )
{
uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);
printf("VID = %04x, PID = %04x\r\n", vid, pid);
hid_info[instance].report_count = tuh_hid_parse_report_descriptor(hid_info[instance].report_info, MAX_REPORT, desc_report, desc_len);
printf("HID has %u reports \r\n", hid_info[instance].report_count);
if(hid_info[instance].report_count == 0){
printf("desc_len:%d\n", desc_len);
for(int i=0; i<10; i++){
printf("+ 0x%X 0x%X\n", desc_report[i*2], desc_report[i*2+1]);
}
printf("\n");
}
}
// request to receive report
@ -114,6 +157,8 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
default:
// Generic report requires matching ReportID and contents with previous parsed report info
printf("Generic report recieved\n");
process_generic_report(dev_addr, instance, report, len);
break;
}
@ -240,6 +285,41 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
tuh_hid_report_info_t* rpt_info_arr = hid_info[instance].report_info;
tuh_hid_report_info_t* rpt_info = NULL;
// Supposons que ce soit notre manette !
_8bitpro_report_t _8bitpro_report;
memcpy(&_8bitpro_report, report, sizeof(_8bitpro_report));
printf(">j1:%d\n", _8bitpro_report.x);
printf(">j2:%d\n", _8bitpro_report.y);
printf(">j3:%d\n", _8bitpro_report.z);
printf(">j4:%d\n", _8bitpro_report.rz);
printf(">hat:%d\n", _8bitpro_report.hat_switch);
printf(">b1:%d\n", _8bitpro_report.b1);
printf(">b2:%d\n", _8bitpro_report.b2);
printf(">b3:%d\n", _8bitpro_report.b3);
printf(">b4:%d\n", _8bitpro_report.b4);
printf(">b5:%d\n", _8bitpro_report.b5);
printf(">b6:%d\n", _8bitpro_report.b6);
printf(">b7:%d\n", _8bitpro_report.b7);
printf(">b8:%d\n", _8bitpro_report.b8);
printf(">b9:%d\n", _8bitpro_report.b9);
printf(">b10:%d\n", _8bitpro_report.b10);
printf(">b11:%d\n", _8bitpro_report.b11);
printf(">b12:%d\n", _8bitpro_report.b12);
printf(">b13:%d\n", _8bitpro_report.b13);
printf(">b14:%d\n", _8bitpro_report.b14);
printf(">data1:%d\n", _8bitpro_report.data_1);
printf(">g1:%d\n", _8bitpro_report.gachette_droite);
printf(">g2:%d\n", _8bitpro_report.gachette_gauche);
printf("HID Report brut\n");
for(int i=0; i< len; i++){
printf("0x%X ", report[i]);
}
printf("\n");
if ( rpt_count == 1 && rpt_info_arr[0].report_id == 0)
{
// Simple report without report ID as 1st byte

4
main.c
View File

@ -161,8 +161,8 @@ int main(void) {
led_blinking_task();
cdc_app_task();
hid_app_task();
log_to_sd(big_big_buffer);
log_to_usb(big_big_buffer); // à commenter pour obtenir un test juste sur la carte SD.(ou simplement ne pas brancher de clé USB)
//log_to_sd(big_big_buffer);
//log_to_usb(big_big_buffer); // à commenter pour obtenir un test juste sur la carte SD.(ou simplement ne pas brancher de clé USB)
}
}