diff --git a/CMakeLists.txt b/CMakeLists.txt index b348907..2d10478 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ target_link_libraries( host_cdc_msc_hid PUBLIC hardware_spi hardware_sync pico_aon_timer + pico_multicore pico_stdlib ${HWDEP_LIBS} ) diff --git a/hid_app.c b/hid_app.c index 2bc2827..295dfb3 100644 --- a/hid_app.c +++ b/hid_app.c @@ -25,6 +25,7 @@ #include "bsp/board_api.h" #include "gamepad.h" +#include "hid_app.h" #include "tusb.h" //--------------------------------------------------------------------+ @@ -48,35 +49,15 @@ static struct }hid_info[CFG_TUH_HID]; -struct gamepad_data_t{ - uint8_t joystick_x_gauche; - uint8_t joystick_y_gauche; - uint8_t joystick_x_droit; - uint8_t joystick_y_droit; - uint8_t gachette_gauche; - uint8_t gachette_droite; - uint8_t dpad; - bool bouton_A; - bool bouton_B; - bool bouton_X; - bool bouton_Y; - bool bouton_Start; - bool bouton_Select; - bool bouton_L; - bool bouton_L2; - bool bouton_R; - bool bouton_R2; - bool bouton_joystick_gauche; - bool bouton_joystick_droit; -}; 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); -void process_8bitpro_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len); -void process_SonyDualShock4_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len); -void process_BDA_NSW_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len); +struct gamepad_data_t process_8bitpro_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len); +struct gamepad_data_t process_SonyDualShock4_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len); +struct gamepad_data_t process_BDA_NSW_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len); void affiche_gamepad_data(struct gamepad_data_t _gamepad_report); +void refresh_gamepad_data(struct gamepad_data_t * gamepad_data); // HID report from 8bitPro @@ -163,6 +144,8 @@ typedef struct TU_ATTR_PACKED } BDA_NSW_report_t ; +struct gamepad_data_t gamepad_data; + void hid_app_task(void) { @@ -250,11 +233,11 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons // Generic report requires matching ReportID and contents with previous parsed report info // Report - 2dc8:6002 8BitDo 8BitDo SN30 Pro+ if(hid_info[instance].vid == 0x2dc8 && hid_info[instance].pid == 0x6002){ - process_8bitpro_report(dev_addr, instance, report, len); + gamepad_data = process_8bitpro_report(dev_addr, instance, report, len); }else if(hid_info[instance].vid == 0x054c && hid_info[instance].pid == 0x05c4){ - process_SonyDualShock4_report(dev_addr, instance, report, len); + gamepad_data = process_SonyDualShock4_report(dev_addr, instance, report, len); }else if(hid_info[instance].vid == 0x20d6 && hid_info[instance].pid == 0xa713){ - process_BDA_NSW_report(dev_addr, instance, report, len); + gamepad_data = process_BDA_NSW_report(dev_addr, instance, report, len); }else{ process_generic_report(dev_addr, instance, report, len); } @@ -375,10 +358,11 @@ static void process_mouse_report(hid_mouse_report_t const * report) //--------------------------------------------------------------------+ // Report - 2dc8:6002 8BitDo 8BitDo SN30 Pro+ //--------------------------------------------------------------------+ -void process_8bitpro_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ +struct gamepad_data_t process_8bitpro_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ struct gamepad_data_t gamepad_data; _8bitpro_report_t gamepad_report; memcpy(&gamepad_report, report, sizeof(gamepad_report)); + refresh_gamepad_data(&gamepad_data); // Copie des données du rapport vers la structure standard gamepad_data.joystick_x_gauche = gamepad_report.x; @@ -402,16 +386,18 @@ void process_8bitpro_report(uint8_t dev_addr, uint8_t instance, uint8_t const* r gamepad_data.gachette_gauche = gamepad_report.gachette_gauche; affiche_gamepad_data(gamepad_data); + return gamepad_data; } //--------------------------------------------------------------------+ // 054c:05c4 Sony Corp. DualShock 4 [CUH-ZCT1x] //--------------------------------------------------------------------+ -void process_SonyDualShock4_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ +struct gamepad_data_t process_SonyDualShock4_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ struct gamepad_data_t gamepad_data; SonyDualShock4_report_t gamepad_report; memcpy(&gamepad_report, report, sizeof(gamepad_report)); + refresh_gamepad_data(&gamepad_data); // Copie des données du rapport vers la structure standard gamepad_data.joystick_x_gauche = gamepad_report.x; @@ -430,21 +416,23 @@ void process_SonyDualShock4_report(uint8_t dev_addr, uint8_t instance, uint8_t c gamepad_data.bouton_L2 = gamepad_report.b7; gamepad_data.bouton_R = gamepad_report.b6; gamepad_data.bouton_L = gamepad_report.b5; - gamepad_data.dpad = gamepad_report.hat_switch; + gamepad_data.dpad = gamepad_report.hat_switch; gamepad_data.gachette_droite = gamepad_report.gachette_droite; gamepad_data.gachette_gauche = gamepad_report.gachette_gauche; affiche_gamepad_data(gamepad_data); - + return gamepad_data; + } //--------------------------------------------------------------------+ // Report - 20d6:a713 BDA NSW wired controller //--------------------------------------------------------------------+ -void process_BDA_NSW_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ +struct gamepad_data_t process_BDA_NSW_report(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ struct gamepad_data_t gamepad_data; BDA_NSW_report_t gamepad_report; memcpy(&gamepad_report, report, sizeof(gamepad_report)); + refresh_gamepad_data(&gamepad_data); // Copie des données du rapport vers la structure standard gamepad_data.joystick_x_gauche = gamepad_report.x; @@ -470,6 +458,7 @@ void process_BDA_NSW_report(uint8_t dev_addr, uint8_t instance, uint8_t const* r gamepad_data.dpad = gamepad_report.hat_switch; affiche_gamepad_data(gamepad_data); + return gamepad_data; } @@ -581,4 +570,16 @@ void affiche_gamepad_data(struct gamepad_data_t _gamepad_report){ printf(">gd:%d\n", _gamepad_report.gachette_droite); printf(">gg:%d\n", _gamepad_report.gachette_gauche); +} + +void refresh_gamepad_data(struct gamepad_data_t * gamepad_data){ + gamepad_data->temps_ms = (uint32_t) get_absolute_time() / 1000; +} + +bool gamepad_connecte(){ + return ((get_absolute_time() / 1000) - gamepad_data.temps_ms < 1000); +} + +struct gamepad_data_t get_gamepad_data(){ + return gamepad_data; } \ No newline at end of file diff --git a/hid_app.h b/hid_app.h new file mode 100644 index 0000000..38775ac --- /dev/null +++ b/hid_app.h @@ -0,0 +1,29 @@ +#include + + +struct gamepad_data_t{ + uint32_t temps_ms; + + uint8_t joystick_x_gauche; + uint8_t joystick_y_gauche; + uint8_t joystick_x_droit; + uint8_t joystick_y_droit; + uint8_t gachette_gauche; + uint8_t gachette_droite; + uint8_t dpad; + bool bouton_A; + bool bouton_B; + bool bouton_X; + bool bouton_Y; + bool bouton_Start; + bool bouton_Select; + bool bouton_L; + bool bouton_L2; + bool bouton_R; + bool bouton_R2; + bool bouton_joystick_gauche; + bool bouton_joystick_droit; +}; + +bool gamepad_connecte(); +struct gamepad_data_t get_gamepad_data(); diff --git a/main.c b/main.c index 4ddd245..e498ed2 100644 --- a/main.c +++ b/main.c @@ -28,7 +28,9 @@ #include #include "bsp/board_api.h" +#include "hid_app.h" #include "lib/sd_driver/sd_card.h" +#include "pico/multicore.h" #include "tusb.h" #include "ff.h" #include "f_util.h" @@ -101,6 +103,22 @@ sd_card_t* sd_get_by_num(size_t num) { } } +void usb_routine(){ + while (1) { + // tinyusb host task + tuh_task(); + + 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) + + } +} + /*------------- MAIN -------------*/ int main(void) { @@ -154,14 +172,22 @@ int main(void) { strcat(big_big_buffer, buffer); size += strlen(buffer); } + multicore_launch_core1(usb_routine); while (1) { // tinyusb host task - tuh_task(); + /*tuh_task(); led_blinking_task(); cdc_app_task(); - hid_app_task(); + hid_app_task();*/ + if(gamepad_connecte()){ + struct gamepad_data_t gamepad_data; + gamepad_data = get_gamepad_data(); + printf(">joystique_xd:%d\n", gamepad_data.joystick_x_gauche); + } + printf(">gc:%d\n", gamepad_connecte()); + sleep_ms(1); //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)