From ffb8e89885c66d8d7dee104773c29e322265daa0 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 6 Aug 2024 14:00:02 +0200 Subject: add logic for dynamically handling device id --- src/CMakeLists.txt | 13 ++++++------- src/gamepad.c | 22 +++++++++++++++++++++- src/gamepad.h | 3 +++ src/xinput.c | 16 ++++++++++++++-- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4782941..a24869b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/deps/tusb_xinput xinput_host) -add_executable(btinput +add_executable(wirecutter main.c xinput.c gamepad.c @@ -8,19 +8,18 @@ add_executable(btinput btstack_config.h ) -target_include_directories(btinput PUBLIC +target_include_directories(wirecutter PUBLIC ${CMAKE_CURRENT_LIST_DIR} ) -pico_enable_stdio_usb(btinput 0) -pico_enable_stdio_uart(btinput 1) +pico_enable_stdio_usb(wirecutter 0) +pico_enable_stdio_uart(wirecutter 1) -pico_add_extra_outputs(btinput) +pico_add_extra_outputs(wirecutter) -target_link_libraries(btinput PUBLIC +target_link_libraries(wirecutter PUBLIC pico_stdlib pico_cyw43_arch_none - pico_btstack_cyw43 pico_btstack_classic tinyusb_host tinyusb_board diff --git a/src/gamepad.c b/src/gamepad.c index 93af18f..5e4a426 100644 --- a/src/gamepad.c +++ b/src/gamepad.c @@ -14,6 +14,7 @@ static const char hid_device_name[] = "Wirecutter Adapter"; static const char service_name[] = "Wireless Gamepad"; static uint8_t hid_service_buffer[4090]; static uint8_t device_id_sdp_service_buffer[100]; +static uint8_t device_id_sdp_service_record_handle = 0; static btstack_packet_callback_registration_t hci_event_callback_registration; uint16_t hid_cid; @@ -199,6 +200,25 @@ static void setup_sync_button(void) irq_set_enabled(IO_IRQ_BANK0, true); } +void change_device_id(uint16_t vendor_id, uint16_t product_id) +{ + if (0 == device_id_sdp_service_record_handle) + device_id_sdp_service_record_handle = sdp_create_service_record_handle(); + else + sdp_unregister_service(device_id_sdp_service_record_handle); + + + device_id_create_sdp_record( + device_id_sdp_service_buffer, + device_id_sdp_service_record_handle, + DEVICE_ID_VENDOR_ID_SOURCE_USB, + vendor_id, product_id, + 1 + ); + btstack_assert(de_get_len(device_id_sdp_service_buffer) <= sizeof(device_id_sdp_service_buffer)); + sdp_register_service(device_id_sdp_service_buffer); +} + int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]){ (void)argc; @@ -244,7 +264,7 @@ int btstack_main(int argc, const char * argv[]){ ); hid_create_sdp_record(hid_service_buffer, sdp_create_service_record_handle(), &hid_params); - btstack_assert(de_get_len( hid_service_buffer) <= sizeof(hid_service_buffer)); + btstack_assert(de_get_len(hid_service_buffer) <= sizeof(hid_service_buffer)); sdp_register_service(hid_service_buffer); // HID Device diff --git a/src/gamepad.h b/src/gamepad.h index 295619c..04a0ca8 100644 --- a/src/gamepad.h +++ b/src/gamepad.h @@ -1,6 +1,8 @@ #ifndef GAMEPAD_H #define GAMEPAD_H +#include + typedef struct { int16_t x; @@ -16,5 +18,6 @@ typedef struct extern gamepad_t gamepad; void request_can_send_now_event(void); +void change_device_id(uint16_t vendor_id, uint16_t product_id); #endif \ No newline at end of file diff --git a/src/xinput.c b/src/xinput.c index 07eb01a..18efacd 100644 --- a/src/xinput.c +++ b/src/xinput.c @@ -6,6 +6,7 @@ #define XINPUT_GAMEPAD_DPAD (XINPUT_GAMEPAD_DPAD_UP | XINPUT_GAMEPAD_DPAD_DOWN | XINPUT_GAMEPAD_DPAD_LEFT | XINPUT_GAMEPAD_DPAD_RIGHT) #define XINPUT_GAMEPAD_THUMB (XINPUT_GAMEPAD_LEFT_THUMB | XINPUT_GAMEPAD_RIGHT_THUMB) +tusb_desc_device_t dev_desc; //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){ @@ -25,8 +26,6 @@ void tuh_xinput_report_received_cb(uint8_t dev_addr, uint8_t instance, xinputh_i gamepad.y = p->sThumbLY; gamepad.z = p->sThumbRX; gamepad.rz = p->sThumbRY; - //gamepad.rx = ((float)p->bLeftTrigger * 256) - 32767; - //gamepad.ry = ((float)p->bRightTrigger * 256) - 32767; gamepad.gas = p->bRightTrigger; gamepad.brake = p->bLeftTrigger; @@ -73,6 +72,18 @@ void tuh_xinput_report_received_cb(uint8_t dev_addr, uint8_t instance, xinputh_i tuh_xinput_receive_report(dev_addr, instance); } +void handle_device_descriptor(tuh_xfer_t* xfer) +{ + if (XFER_RESULT_SUCCESS != xfer->result) + { + TU_LOG1("Failed to get usb descriptor\n"); + return; + } + + change_device_id(dev_desc.idVendor, dev_desc.idProduct); + TU_LOG1("Updated device id to %d:%d\n", dev_desc.idVendor, dev_desc.idProduct); +} + void tuh_xinput_mount_cb(uint8_t dev_addr, uint8_t instance, const xinputh_interface_t *xinput_itf) { TU_LOG1("Controller attached\n"); @@ -83,6 +94,7 @@ void tuh_xinput_mount_cb(uint8_t dev_addr, uint8_t instance, const xinputh_inter tuh_xinput_receive_report(dev_addr, instance); return; } + tuh_descriptor_get_device(dev_addr, &dev_desc, sizeof(dev_desc), handle_device_descriptor, 0); 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); -- cgit v1.2.3