From 0d32db4d9fd6c3cc56295993b3b1d0e28a99783f Mon Sep 17 00:00:00 2001 From: awalol Date: Tue, 5 May 2026 13:00:28 +0800 Subject: [PATCH] cmd: poll rate switch --- CMakeLists.txt | 2 +- src/main.cpp | 13 +++++++++++++ src/{usb_descriptors.c => usb_descriptors.cpp} | 17 ++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) rename src/{usb_descriptors.c => usb_descriptors.cpp} (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d39245d..62a4e0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ pico_sdk_init() # Add executable. Default name is the project name, version 0.1 add_executable(ds5-bridge - src/usb_descriptors.c + src/usb_descriptors.cpp src/main.cpp src/usb.cpp src/audio.cpp diff --git a/src/main.cpp b/src/main.cpp index 086bcf1..7ad248b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,14 @@ volatile bool report_dirty = false; void interrupt_loop() { if (!tud_hid_ready()) return; + // TODO: Refactor for better code reuse + if (get_config().polling_rate_mode != 2) { + if (!tud_hid_report(0x01, interrupt_in_data, 63)) { + printf("[USBHID] tud_hid_report error\n"); + } + return; + } + bool should_send = false; // Local buffer to hold the report data while we prepare it to send. uint8_t safe_report[63]; @@ -72,6 +80,11 @@ void on_bt_data(CHANNEL_TYPE channel, uint8_t *data, uint16_t len) { set_headset(data[56] & 1); } + if (get_config().polling_rate_mode != 2) { + memcpy(interrupt_in_data, data + 3, 63); + return; + } + // We add the critical section here to avoid any race conditions when writing to the interrupt_in_data buffer, // which is shared between the main loop and this callback. // The critical section ensures that only one thread can access the buffer at a time, diff --git a/src/usb_descriptors.c b/src/usb_descriptors.cpp similarity index 98% rename from src/usb_descriptors.c rename to src/usb_descriptors.cpp index f2ac8c6..c0b16ea 100644 --- a/src/usb_descriptors.c +++ b/src/usb_descriptors.cpp @@ -25,6 +25,7 @@ #include "bsp/board_api.h" #include "tusb.h" +#include "config.h" //--------------------------------------------------------------------+ // Device Descriptors @@ -69,7 +70,7 @@ uint8_t const *tud_descriptor_device_cb(void) { //--------------------------------------------------------------------+ // Configuration Descriptor //--------------------------------------------------------------------+ -uint8_t const descriptor_configuration[] = { +uint8_t descriptor_configuration[] = { // --- CONFIGURATION DESCRIPTOR --- 0x09, // bLength 0x02, // bDescriptorType (CONFIGURATION) @@ -330,6 +331,20 @@ uint8_t const descriptor_configuration[] = { // Descriptor contents must exist long enough for transfer to complete uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { (void) index; // for multiple configurations + auto bInterval = 0x01; + switch (get_config().polling_rate_mode) { + case 0: + bInterval = 0x04; + break; + case 1: + bInterval = 0x02; + break; + case 2: + bInterval = 0x01; + break; + } + descriptor_configuration[sizeof(descriptor_configuration) - 1] = bInterval; + descriptor_configuration[sizeof(descriptor_configuration) - 8] = bInterval; return descriptor_configuration; }