From 8c8af7b97c3d0e6389b55e0f7a95ef592941e139 Mon Sep 17 00:00:00 2001 From: awalol Date: Sat, 21 Mar 2026 11:01:52 +0800 Subject: [PATCH] set buffer length --- src/audio.cpp | 19 ++++++++++--------- src/bt.cpp | 24 ++++++++++++------------ src/main.cpp | 3 --- src/usb.cpp | 2 +- src/usb.h | 13 ------------- src/usb_descriptors.c | 2 +- 6 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index 2b0b94e..d776851 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -14,6 +14,7 @@ #define REPORT_SIZE 142 #define REPORT_ID 0x32 #define VOLUME_GAIN 2 +#define BUFFER_LENGTH 55 static WDL_Resampler resampler; static uint8_t reportSeqCounter = 0; @@ -23,7 +24,7 @@ void audio_loop() { // 1. 读取 USB 音频数据 if (!tud_audio_available()) return; - int16_t raw[1024]; // 48000 / 3000 = 16, 64 * 16 = 1024 + int16_t raw[192]; // 48000 / 3000 = 16, 64 * 16 = 1024 uint32_t bytes_read = tud_audio_read(raw, sizeof(raw)); int frames = bytes_read / (INPUT_CHANNELS * sizeof(int16_t)); if (frames == 0){ @@ -32,16 +33,16 @@ void audio_loop() { // 2. 从4ch中提取ch3/ch4,转换为float输入重采样器 WDL_ResampleSample *in_buf; - int nsamples = resampler.ResamplePrepare(frames, OUTPUT_CHANNELS, &in_buf); + int nframes = resampler.ResamplePrepare(frames, OUTPUT_CHANNELS, &in_buf); - for (int i = 0; i < nsamples; i++) { + for (int i = 0; i < nframes; i++) { in_buf[i * 2] = (WDL_ResampleSample) raw[i * INPUT_CHANNELS + 2] / 32768.0f; in_buf[i * 2 + 1] = (WDL_ResampleSample) raw[i * INPUT_CHANNELS + 3] / 32768.0f; } // 3. 48kHz -> 3kHz 重采样 WDL_ResampleSample out_buf[SAMPLE_SIZE]; // 64 floats = 32帧 × 2ch - int out_frames = resampler.ResampleOut(out_buf, nsamples, SAMPLE_SIZE / OUTPUT_CHANNELS, OUTPUT_CHANNELS); + int out_frames = resampler.ResampleOut(out_buf, nframes, SAMPLE_SIZE / OUTPUT_CHANNELS, OUTPUT_CHANNELS); static int8_t haptic_buf[SAMPLE_SIZE]; static int haptic_buf_pos = 0; @@ -63,11 +64,11 @@ void audio_loop() { pkt[2] = 0x11 | (1 << 7); pkt[3] = 7; pkt[4] = 0b11111110; - pkt[5] = 0; - pkt[6] = 0; - pkt[7] = 0; - pkt[8] = 0; - pkt[9] = 0xFF; + pkt[5] = BUFFER_LENGTH; + pkt[6] = BUFFER_LENGTH; + pkt[7] = BUFFER_LENGTH; + pkt[8] = BUFFER_LENGTH; + pkt[9] = BUFFER_LENGTH; // buffer length pkt[10] = packetCounter++; pkt[11] = 0x12 | (1 << 7); pkt[12] = SAMPLE_SIZE; diff --git a/src/bt.cpp b/src/bt.cpp index 94f1468..095d62c 100644 --- a/src/bt.cpp +++ b/src/bt.cpp @@ -20,12 +20,12 @@ #include "pico/sync.h" #include "classic/sdp_server.h" -static btstack_packet_callback_registration_t hci_event_callback_registration, l2cap_event_callback_registration; +#define MTU 672 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); - static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); +static btstack_packet_callback_registration_t hci_event_callback_registration, l2cap_event_callback_registration; static bd_addr_t current_device_addr; static bool device_found = false; static bool new_pair = false; @@ -57,8 +57,8 @@ void bt_l2cap_init() { l2cap_add_event_handler(&l2cap_event_callback_registration); // 修复重连后自动断开的关键点 sdp_init(); - l2cap_register_service(l2cap_packet_handler, PSM_HID_CONTROL, 672, LEVEL_2); - l2cap_register_service(l2cap_packet_handler, PSM_HID_INTERRUPT, 672, LEVEL_2); + l2cap_register_service(l2cap_packet_handler, PSM_HID_CONTROL, MTU, LEVEL_2); + l2cap_register_service(l2cap_packet_handler, PSM_HID_INTERRUPT, MTU, LEVEL_2); l2cap_init(); } @@ -68,9 +68,9 @@ int bt_init() { printf("Failed to initialize CYW43\n"); return 1; } - + critical_section_init(&queue_lock); - + bt_l2cap_init(); // SSP (Secure Simple Pairing) @@ -255,10 +255,10 @@ static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *p printf("[L2CAP] Open HID channels\n"); if (new_pair) { if (hid_control_cid == 0) { - l2cap_create_channel(l2cap_packet_handler, current_device_addr, PSM_HID_CONTROL, 0xffff, + l2cap_create_channel(l2cap_packet_handler, current_device_addr, PSM_HID_CONTROL, MTU, &hid_control_cid); } else if (hid_interrupt_cid == 0) { - l2cap_create_channel(l2cap_packet_handler, current_device_addr, PSM_HID_INTERRUPT, 0xffff, + l2cap_create_channel(l2cap_packet_handler, current_device_addr, PSM_HID_INTERRUPT, MTU, &hid_interrupt_cid); } } @@ -314,7 +314,7 @@ static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t bt_data_callback(CONTROL, packet, size); } else { printf("[L2CAP] Data on unknown channel 0x%04X (Interrupt: 0x%04X, Control: 0x%04X)\n", - channel, hid_interrupt_cid, hid_control_cid); + channel, hid_interrupt_cid, hid_control_cid); } return; } @@ -412,7 +412,7 @@ static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t std::vector data = send_queue.front(); send_queue.pop(); critical_section_exit(&queue_lock); - + uint8_t status = l2cap_send(hid_interrupt_cid, data.data(), data.size()); if (status != 0) { printf("[L2CAP] Interrupt Error, Status: 0x%02X\n", status); @@ -429,11 +429,11 @@ void bt_write(uint8_t *data, uint16_t len) { packet[0] = 0xA2; memcpy(packet.data() + 1, data, len); fill_output_report_checksum(packet.data() + 1, len); - + critical_section_enter_blocking(&queue_lock); send_queue.push(std::move(packet)); // 使用 std::move 避免深拷贝 critical_section_exit(&queue_lock); - + if (hid_interrupt_cid == 0) { printf("[L2CAP bt_write] Warning: hid_interrupt_cid 0"); return; diff --git a/src/main.cpp b/src/main.cpp index d9c9e9a..9506161 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,6 @@ #include #include "bsp/board_api.h" -#include "usb.h" #include "bt.h" #include "utils.h" #include "resample.h" @@ -27,8 +26,6 @@ uint8_t interrupt_in_data[63] = { }; void interrupt_loop() { - if (board_millis() - lastTime < 4) return; - lastTime = board_millis(); if (!tud_hid_ready()) return; if (!tud_hid_report(0x01, interrupt_in_data, 63)) { printf("[USBHID] tud_hid_report error\n"); diff --git a/src/usb.cpp b/src/usb.cpp index 7a7862d..5b279b0 100644 --- a/src/usb.cpp +++ b/src/usb.cpp @@ -2,7 +2,7 @@ // Created by awalol on 2026/3/4. // -#include "usb.h" +#include "tusb.h" #include "bsp/board_api.h" uint8_t mute[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX + 1]; // +1 for master channel 0 diff --git a/src/usb.h b/src/usb.h index 92f7267..00b8726 100644 --- a/src/usb.h +++ b/src/usb.h @@ -5,17 +5,4 @@ #ifndef DS5_BRIDGE_USB_H #define DS5_BRIDGE_USB_H -#include "tusb.h" - -// typedef void (*usb_hid_get_report_callback_t)(uint16_t channel, uint8_t *data, uint16_t len); -// typedef void (*usb_hid_set_report_callback_t)(uint16_t channel, uint8_t *data, uint16_t len); - -/*uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, - uint16_t reqlen); -void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, - uint16_t bufsize);*/ -bool tud_audio_get_req_entity_cb(uint8_t rhport, tusb_control_request_t const *p_request); -bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const *p_request, uint8_t *buf); -void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, uint16_t len); - #endif //DS5_BRIDGE_USB_H \ No newline at end of file diff --git a/src/usb_descriptors.c b/src/usb_descriptors.c index 47aac31..756879b 100644 --- a/src/usb_descriptors.c +++ b/src/usb_descriptors.c @@ -313,7 +313,7 @@ uint8_t const descriptor_configuration[] = { 0x84, // bEndpointAddress: IN EP4 0x03, // bmAttributes: Interrupt 0x40, 0x00, // wMaxPacketSize: 64 - 0x06, // bInterval: 6 (polling every 8ms) + 0x06, // bInterval: 6 (polling every 4ms) // Endpoint Descriptor (HID OUT: EP3) 0x07, // bLength