From 351d71dfe1599e61efd5b024c4ce3d9aec99aeae Mon Sep 17 00:00:00 2001 From: awalol Date: Tue, 5 May 2026 15:19:22 +0800 Subject: [PATCH] cmd: controller mode switch --- CMakeLists.txt | 6 ----- src/bt.cpp | 2 -- src/config.cpp | 4 +++ src/config.h | 1 + src/usb_descriptors.cpp | 60 +++++++++++++++++++++-------------------- 5 files changed, 36 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 62a4e0c..c4cd9a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,12 +71,6 @@ target_compile_definitions(ds5-bridge PRIVATE WDL_RESAMPLE_TYPE=float CYW43_PIO_CLOCK_DIV_INT=5 ) -option(ENABLE_DSE "Enable DSE" OFF) -if(ENABLE_DSE) - target_compile_definitions(ds5-bridge PRIVATE - ENABLE_DSE # 将PID和产品名称设置为DSE - ) -endif() pico_set_program_name(ds5-bridge "ds5-bridge") pico_set_program_version(ds5-bridge "0.5.3") diff --git a/src/bt.cpp b/src/bt.cpp index 06ea162..56a71bd 100644 --- a/src/bt.cpp +++ b/src/bt.cpp @@ -11,8 +11,6 @@ #include "btstack_event.h" #include "l2cap.h" #include "pico/cyw43_arch.h" -#include "pico/stdio.h" -#include "usb.h" #include "utils.h" #include "bsp/board_api.h" #include "pico/sync.h" diff --git a/src/config.cpp b/src/config.cpp index 8853480..d8b0254 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -69,6 +69,10 @@ void config_valid() { body->haptics_buffer_length = 48; printf("[Config] haptics_buffer_length is invalid\n"); } + if (body->controller_mode > 1) { + body->controller_mode = 0; + printf("[Config] controller_mode is invalid\n"); + } } void config_load() { diff --git a/src/config.h b/src/config.h index c3af2cd..782eb52 100644 --- a/src/config.h +++ b/src/config.h @@ -14,6 +14,7 @@ struct __attribute__((packed)) Config_body { uint8_t disable_pico_led; // bool uint8_t polling_rate_mode; // 0: 250Hz,1: 500Hz,2: instant uint8_t haptics_buffer_length; // [16,255] + uint8_t controller_mode; // 0: DS5, 1: DSE }; struct __attribute__((packed)) Config { diff --git a/src/usb_descriptors.cpp b/src/usb_descriptors.cpp index c0b16ea..cb3412b 100644 --- a/src/usb_descriptors.cpp +++ b/src/usb_descriptors.cpp @@ -27,10 +27,14 @@ #include "tusb.h" #include "config.h" +bool ds_mode() { + return get_config().controller_mode == 0; +} + //--------------------------------------------------------------------+ // Device Descriptors //--------------------------------------------------------------------+ -static tusb_desc_device_t const desc_device = +tusb_desc_device_t desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, @@ -47,11 +51,8 @@ static tusb_desc_device_t const desc_device = .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .idVendor = 0x054C, -#ifdef ENABLE_DSE - .idProduct = 0x0DF2, -#else - .idProduct = 0x0CE6, -#endif + // .idProduct = 0x0CE6, // DS + // .idProduct = 0x0DF2, // DSE .bcdDevice = 0x0100, .iManufacturer = 0x01, @@ -64,7 +65,8 @@ static tusb_desc_device_t const desc_device = // Invoked when received GET DEVICE DESCRIPTOR // Application return pointer to descriptor uint8_t const *tud_descriptor_device_cb(void) { - return (uint8_t const *) &desc_device; + desc_device.idProduct = ds_mode() ? 0x0CE6 : 0x0DF2; + return reinterpret_cast(&desc_device); } //--------------------------------------------------------------------+ @@ -303,11 +305,8 @@ uint8_t descriptor_configuration[] = { 0x00, // bCountryCode: Not localized 0x01, // bNumDescriptors: 1 report descriptor 0x22, // bDescriptorType: Report -#ifdef ENABLE_DSE - 0xA1, 0x01, // wDescriptorLength: 417 (0x01A1) -#else - 0x31, 0x01, // wDescriptorLength: 289 (0x0121) -#endif + 0x31, 0x01, // wDescriptorLength: 305 (0x0131) DS + // 0xA5, 0x01, // wDescriptorLength: 421 (0x01A5) DSE // Endpoint Descriptor (HID IN: EP4) 0x07, // bLength @@ -343,8 +342,14 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { bInterval = 0x01; break; } - descriptor_configuration[sizeof(descriptor_configuration) - 1] = bInterval; - descriptor_configuration[sizeof(descriptor_configuration) - 8] = bInterval; + constexpr auto offset = sizeof(descriptor_configuration); + descriptor_configuration[offset - 1] = bInterval; + descriptor_configuration[offset - 8] = bInterval; + if (ds_mode()) { + descriptor_configuration[offset - 16] = 0x31; + }else { + descriptor_configuration[offset - 16] = 0xA5; + } return descriptor_configuration; } @@ -352,7 +357,6 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { // HID Report Descriptor //--------------------------------------------------------------------+ -#ifndef ENABLE_DSE uint8_t const desc_hid_report_ds[] = { 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) 0x09, 0x05, // Usage (Game Pad) @@ -504,11 +508,9 @@ uint8_t const desc_hid_report_ds[] = { 0x95, 0x3F, 0xB1, 0x02, 0xC0, // End Collection - // 289 bytes + // 305 bytes }; -#endif -#ifdef ENABLE_DSE uint8_t const desc_hid_report_dse[] = { 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) 0x09, 0x05, // Usage (Game Pad) @@ -718,20 +720,18 @@ uint8_t const desc_hid_report_dse[] = { 0x95, 0x3F, 0xB1, 0x02, 0xC0, // End Collection - // 417 bytes + // 421 bytes }; -#endif // Invoked when received GET HID REPORT DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) { (void) itf; -#ifdef ENABLE_DSE + if (ds_mode()) { + return desc_hid_report_ds; + } return desc_hid_report_dse; -#else - return desc_hid_report_ds; -#endif } //--------------------------------------------------------------------+ @@ -751,11 +751,7 @@ static char const *string_desc_arr[] = { (const char[]){0x09, 0x04}, // 0: is supported language is English (0x0409) "Sony Interactive Entertainment", // 1: Manufacturer -#ifdef ENABLE_DSE - "DualSense Edge Wireless Controller", -#else - "DualSense Wireless Controller", // 2: Product -#endif + NULL, // 2: Product NULL, // 3: Serials will use unique ID if possible }; @@ -767,6 +763,12 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { (void) langid; size_t chr_count; + if (ds_mode()) { + string_desc_arr[2] = "DualSense Wireless Controller"; + }else { + string_desc_arr[2] = "DualSense Edge Wireless Controller"; + } + switch (index) { case STRID_LANGID: memcpy(&_desc_str[1], string_desc_arr[0], 2);