From a887c6fb393f02e4caaebcee39703f50d5e20766 Mon Sep 17 00:00:00 2001 From: awalol Date: Tue, 5 May 2026 18:47:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20absolute=5Ftime=5Ft=20?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3=20time=5Fus=5F32=20=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bt.cpp | 18 +++++++++--------- src/config.cpp | 4 ++++ src/config.h | 3 ++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/bt.cpp b/src/bt.cpp index 8664bfd..3c4e180 100644 --- a/src/bt.cpp +++ b/src/bt.cpp @@ -37,7 +37,7 @@ static bt_data_callback_t bt_data_callback = nullptr; unordered_map > feature_data; static queue > send_queue; static critical_section_t queue_lock; -uint32_t inactive_time = 0; // 手柄长时间静默 +absolute_time_t inactive_time = 0; // 手柄长时间静默 void bt_register_data_callback(bt_data_callback_t callback) { bt_data_callback = callback; @@ -321,10 +321,10 @@ static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t return; } if (packet[3] < 120 || packet[3] > 140) { - inactive_time = time_us_32(); - }else if (time_us_32() - inactive_time > 1800 * 1000 * 1000){ + inactive_time = get_absolute_time(); + } else if (absolute_time_diff_us(inactive_time, get_absolute_time()) > get_config().inactive_time * 60 * 1000 * 1000) { printf("disconnect when inactive\n"); - inactive_time = time_us_32(); + inactive_time = get_absolute_time(); bt_disconnect(); } } else if (channel == hid_control_cid) { @@ -360,7 +360,7 @@ static void l2cap_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t if (!get_config().disable_pico_led) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true); } - inactive_time = time_us_32(); + inactive_time = get_absolute_time(); printf("Init DualSense\n"); @@ -490,16 +490,16 @@ vector get_feature_data(uint8_t reportId, uint16_t len) { return ret; } -void set_feature_data(uint8_t reportId, uint8_t* data,uint16_t len) { +void set_feature_data(uint8_t reportId, uint8_t *data, uint16_t len) { if (hid_control_cid != 0) { uint8_t get_feature[len + 2]; get_feature[0] = 0x53; get_feature[1] = reportId; - memcpy(get_feature + 2,data,len); - fill_feature_report_checksum(get_feature + 1,len + 1); + memcpy(get_feature + 2, data, len); + fill_feature_report_checksum(get_feature + 1, len + 1); l2cap_send(hid_control_cid, get_feature, len + 2); printf("[L2CAP] Requesting Set Feature Report 0x%02X\n", reportId); - printf_hexdump(get_feature,len + 2); + printf_hexdump(get_feature, len + 2); } } diff --git a/src/config.cpp b/src/config.cpp index d8b0254..553a518 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -53,6 +53,10 @@ void config_valid() { body->speaker_volume = 2.0f; printf("[Config] Speaker Volume is invalid\n"); } + if (body->inactive_time < 10 || body->inactive_time > 60) { + body->inactive_time = 30; + printf("[Config] Inactive time is invalid\n"); + } if (body->disable_inactive_disconnect > 1) { body->disable_inactive_disconnect = 0; printf("[Config] disable_auto_disconnect is invalid\n"); diff --git a/src/config.h b/src/config.h index 782eb52..76445df 100644 --- a/src/config.h +++ b/src/config.h @@ -10,11 +10,12 @@ struct __attribute__((packed)) Config_body { float haptics_gain; // [1.0,2.0] float speaker_volume; // [1.0,2.0] + uint8_t inactive_time; // [10,60] min uint8_t disable_inactive_disconnect; // bool: 0 disable,1 enable 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 + uint8_t controller_mode; // 0: DS5, 1: DSE 自动切换识别太麻烦了,懒的做了,期待pr }; struct __attribute__((packed)) Config {