set buffer length

This commit is contained in:
awalol
2026-03-21 11:01:52 +08:00
parent 3600499a4a
commit 8c8af7b97c
6 changed files with 24 additions and 39 deletions
+10 -9
View File
@@ -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;
+7 -7
View File
@@ -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();
}
@@ -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;
}
-3
View File
@@ -4,7 +4,6 @@
#include <cstdio>
#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");
+1 -1
View File
@@ -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
-13
View File
@@ -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
+1 -1
View File
@@ -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