From 3300e084bc4f7e5e6f4fe3e26bc830cfe6a7a858 Mon Sep 17 00:00:00 2001 From: awalol Date: Tue, 28 Apr 2026 13:17:55 +0800 Subject: [PATCH] clean std:: --- src/audio.cpp | 11 +++++++---- src/bt.cpp | 16 ++++++++++------ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index 33c9bc0..f1ff706 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -23,6 +23,9 @@ // #define VOLUME_GAIN 2 #define BUFFER_LENGTH 48 +using std::clamp; +using std::max; + static WDL_Resampler resampler; static uint8_t reportSeqCounter = 0; static uint8_t packetCounter = 0; @@ -86,10 +89,10 @@ void audio_loop() { // 4. 转换为int8并缓冲,满64字节即组包发送 for (int i = 0; i < out_frames; i++) { - int val_l = (int) (out_buf[i * 2] * 127.0f * volume[1]); - int val_r = (int) (out_buf[i * 2 + 1] * 127.0f * volume[1]); - haptic_buf[haptic_buf_pos++] = (int8_t) std::clamp(val_l, -128, 127); // 似乎clamp有点多余?还是以防万一吧 - haptic_buf[haptic_buf_pos++] = (int8_t) std::clamp(val_r, -128, 127); + int val_l = (int) (out_buf[i * 2] * 127.0f * max(volume[1],1.0f)); + int val_r = (int) (out_buf[i * 2 + 1] * 127.0f * max(volume[1],1.0f)); + haptic_buf[haptic_buf_pos++] = (int8_t) clamp(val_l, -128, 127); // 似乎clamp有点多余?还是以防万一吧 + haptic_buf[haptic_buf_pos++] = (int8_t) clamp(val_r, -128, 127); if (haptic_buf_pos != SAMPLE_SIZE) { continue; diff --git a/src/bt.cpp b/src/bt.cpp index 8a3f56f..a4f49d1 100644 --- a/src/bt.cpp +++ b/src/bt.cpp @@ -23,6 +23,10 @@ #define MTU 672 +using std::unordered_map; +using std::vector; +using std::queue; + 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); @@ -34,8 +38,8 @@ static hci_con_handle_t acl_handle = HCI_CON_HANDLE_INVALID; static uint16_t hid_control_cid; static uint16_t hid_interrupt_cid; static bt_data_callback_t bt_data_callback = nullptr; -std::unordered_map > feature_data; -static std::queue > send_queue; +unordered_map > feature_data; +static queue > send_queue; static critical_section_t queue_lock; uint32_t inactive_time = 0; // 手柄长时间静默 @@ -447,7 +451,7 @@ static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t critical_section_exit(&queue_lock); break; } - std::vector data = send_queue.front(); + vector data = send_queue.front(); send_queue.pop(); critical_section_exit(&queue_lock); @@ -463,13 +467,13 @@ static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t void bt_write(uint8_t *data, uint16_t len) { if (hid_interrupt_cid == 0) return; - std::vector packet(len + 1); + vector packet(len + 1); 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 避免深拷贝 + send_queue.push(move(packet)); // 使用 move 避免深拷贝 critical_section_exit(&queue_lock); if (hid_interrupt_cid == 0) { @@ -481,7 +485,7 @@ void bt_write(uint8_t *data, uint16_t len) { } } -std::vector get_feature_data(uint8_t reportId, uint16_t len) { +vector get_feature_data(uint8_t reportId, uint16_t len) { if (!feature_data.contains(reportId) || feature_data[reportId].empty()) { if (hid_control_cid != 0) { uint8_t get_feature[] = {0x43, reportId};