From 776e146404cce0e72f787f49cdfeb0f2ef574ea7 Mon Sep 17 00:00:00 2001 From: Steven Dan Date: Thu, 23 Apr 2026 17:05:35 +0800 Subject: [PATCH] update footstep and threshold --- .../src/extensions/audiohw.xc | 107 ++++++++++++------ .../src/extensions/dsp.c | 60 ++++++++-- .../src/extensions/eq.c | 14 +++ 3 files changed, 138 insertions(+), 43 deletions(-) diff --git a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/audiohw.xc b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/audiohw.xc index ef0ac32..a2d1004 100644 --- a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/audiohw.xc +++ b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/audiohw.xc @@ -101,6 +101,9 @@ unsigned g_led_enable = 1; // LED开关,默认开启 // HID 0xB0 CMD_EXPAND_GAIN到达时由eq.c设置,button_task读取后更新footstep LED // -1 (0xFFFFFFFF) 表示无待处理请求 unsigned g_hid_expand_gain_request = (unsigned)-1; +// HID 0xB0 CMD_LMT_THRESHOLD到达时由eq.c设置,button_task读取后保存到flash +// 存储值为-threshold (0~35);-1 (0xFFFFFFFF) 表示无待处理请求 +unsigned g_hid_lmt_threshold_request = (unsigned)-1; // HID 0x84 FACTORY_RESET命令到达时由eq.c设置,button_task轮询后执行重启 unsigned g_request_factory_reset = 0; uint32_t get_reference_time(); @@ -402,8 +405,9 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, unsigned push_button_music_mode_state_old = 1; // Active low unsigned push_button_ai71_onoff_state_old = 1; // Active low unsigned push_button_game_mode_state_old = 1; // Active low - unsigned push_button_footsteps_enhancement_state_old = 1; // Active low + unsigned push_button_footsteps_enhancement_state_old = 0; // Active low unsigned push_button_aidenoise_onoff_state_old = 1; // Active low + unsigned char saved_footstep = 0; @@ -470,14 +474,21 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, // 加载脚步增强状态(保存值为expand_gain: 0/6/12) { unsigned char footstep_path[] = "footstep"; - unsigned char saved_footstep = load_value(footstep_path); - if (saved_footstep == 6) flag_footsteps_enhancement = 1; - else if (saved_footstep == 12) flag_footsteps_enhancement = 2; - else if (saved_footstep == 255) { - // 未初始化:出厂默认脚步增强开启(12dB),写入flash + saved_footstep = load_value(footstep_path); + debug_printf("Loaded footstep gain from flash: %d\n", saved_footstep); + if (saved_footstep == 0) { + flag_footsteps_enhancement = 0; + } else if (saved_footstep <= 6) { + flag_footsteps_enhancement = 1; + } else if (saved_footstep <= 20) { + flag_footsteps_enhancement = 2; + } else { + // 未初始化(255)或异常值:出厂默认脚步增强开启(12dB),写入flash + saved_footstep = 12; flag_footsteps_enhancement = 2; save_value(footstep_path, (unsigned char)12); - } else flag_footsteps_enhancement = 0; + debug_printf("Saved footstep gain to flash: %d\n", saved_footstep); + } debug_printf("Loaded footstep gain from flash: %d, state=%d\n", saved_footstep, flag_footsteps_enhancement); } #endif @@ -726,12 +737,30 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, #if USE_EX3D == 1 // 发送初始脚步增强expand_gain到tile1 { - int init_expand = 0; - if (flag_footsteps_enhancement == 1) init_expand = 6; - else if (flag_footsteps_enhancement == 2) init_expand = 12; cc_mic_level <: 0xFD; - cc_mic_level <: (unsigned)init_expand; - debug_printf("Sent init expand_gain %d to hid_button_task\n", init_expand); + cc_mic_level <: (unsigned) saved_footstep; + debug_printf("set init expand_gain %d to hid_button_task\n", saved_footstep); + } + + // 加载枪声阈值并同步到tile1 + // 存储格式: -threshold (0~35, 0=threshold=0dB), 255=未初始化 + { + unsigned char lmt_thresh_path[] = "lmt_thresh"; + unsigned char raw = load_value(lmt_thresh_path); + int init_threshold; + if (raw == 255) { + // 未初始化,使用默认值-15 + init_threshold = -15; + save_value(lmt_thresh_path, (unsigned char)15); + } else if (raw <= 36) { + // raw=0~35 全部有效: 0=threshold=0dB(不限幅), 35=threshold=-35dB(最大限幅) + init_threshold = -(int)(raw - 1); + } else { + init_threshold = -15; + } + cc_mic_level <: 0xFA; + cc_mic_level <: (unsigned)init_threshold; + debug_printf("Loaded lmt_threshold=%d from flash (raw=%d), sent to tile1\n", init_threshold, raw); } // 恢复脚步增强LED初始状态 @@ -1137,7 +1166,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, factory_reset_hold_count = 0; if (!btn_combo_active) { if (btn_music_hold_ticks == BTN_COMBO_DELAY_TICKS) { - debug_printf("========= Music button pressed for %d ticks\n", BTN_COMBO_DELAY_TICKS); + debug_printf("Music button pressed for %d ticks\n", BTN_COMBO_DELAY_TICKS); #if (F3_F4_FPS_UAC2 == 1) active_mode = 1; need_reboot = 1; @@ -1169,7 +1198,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, factory_reset_hold_count = 0; if (!btn_combo_active) { if (btn_game_hold_ticks == BTN_COMBO_DELAY_TICKS) { - debug_printf("========= Game button pressed for %d ticks\n", BTN_COMBO_DELAY_TICKS); + debug_printf("Game button pressed for %d ticks\n", BTN_COMBO_DELAY_TICKS); #if F1_MUSIC_UAC2 == 1 active_mode = 2; need_reboot = 1; @@ -1402,35 +1431,35 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, // 3档循环: 0(关/0dB) → 1(中亮/6dB) → 2(高亮/12dB) → 0 flag_footsteps_enhancement = (flag_footsteps_enhancement + 1) % 3; mode_change = 1; + debug_printf("Footstep mode changed: %d -> %d\n", flag_footsteps_enhancement, flag_footsteps_enhancement + 1); } } push_button_footsteps_enhancement_state_old = button_footsteps_enhancement; if(mode_change) { - int new_expand_gain; if(flag_footsteps_enhancement == 0) { - new_expand_gain = 0; + saved_footstep = 0; led_off(&led_ctx, LED_FOOTSTEP_MODE); } else if(flag_footsteps_enhancement == 1) { - new_expand_gain = 6; + saved_footstep = 6; led_on(&led_ctx, LED_FOOTSTEP_MODE); } else { - new_expand_gain = 12; + saved_footstep = 12; led_set_brightness(&led_ctx, LED_FOOTSTEP_MODE, 128); } led_update_all(&led_ctx); // 发送expand_gain到tile1执行 cc_mic_level <: 0xFD; - cc_mic_level <: (unsigned)new_expand_gain; + cc_mic_level <: (unsigned)saved_footstep; // 掉电保存 unsigned char footstep_path[] = "footstep"; - save_value(footstep_path, (unsigned char)new_expand_gain); - debug_printf("Footstep state=%d, expand_gain=%d\n", flag_footsteps_enhancement, new_expand_gain); + save_value(footstep_path, (unsigned char)saved_footstep); + debug_printf("Footstep state=%d, expand_gain=%d\n", flag_footsteps_enhancement, saved_footstep); } // HID 0xB0 CMD_EXPAND_GAIN请求:同步footstep LED状态 @@ -1458,9 +1487,27 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, led_set_brightness(&led_ctx, LED_FOOTSTEP_MODE, 128); } led_update_all(&led_ctx); - unsigned char fp[] = "footstep"; - save_value(fp, (unsigned char)hid_gain_req); - debug_printf("HID set footstep gain=%d, state=%d\n", hid_gain_req, flag_footsteps_enhancement); + } + + unsigned char fp[] = "footstep"; + save_value(fp, (unsigned char)hid_gain_req); + saved_footstep = hid_gain_req; + debug_printf("HID set footstep gain=%d, state=%d\n", hid_gain_req, flag_footsteps_enhancement); + } + } + + // HID 0xB0 CMD_LMT_THRESHOLD请求:保存阈值到flash + // eq.c以(-threshold+1)编码存入shared global,范围1~36;此处解码后存flash (0~35) + { + unsigned hid_thresh_req; + GET_SHARED_GLOBAL(hid_thresh_req, g_hid_lmt_threshold_request); + if (hid_thresh_req != (unsigned)-1) { + SET_SHARED_GLOBAL(g_hid_lmt_threshold_request, (unsigned)-1); + if (hid_thresh_req >= 1 && hid_thresh_req <= 36) { + unsigned char lmt_thresh_path[] = "lmt_thresh"; + unsigned char flash_raw = (unsigned char)(hid_thresh_req - 1); + save_value(lmt_thresh_path, flash_raw); + debug_printf("HID set lmt_threshold=-%d, saved to flash\n", (int)(hid_thresh_req - 1)); } } } @@ -2032,14 +2079,10 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, #if USE_EX3D == 1 // HID脚步增强状态变化主动上报(按键切换或HID 0xB0命令导致的变化,含首次开机上报) { - unsigned current_footstep_expand; - if (flag_footsteps_enhancement == 0) current_footstep_expand = 0; - else if (flag_footsteps_enhancement == 1) current_footstep_expand = 6; - else current_footstep_expand = 12; #if HID_DFU_EN if (!g_in_fw_upgrade) #endif - if (last_footstep_expand != current_footstep_expand) { + if (last_footstep_expand != saved_footstep ) { unsafe { unsigned char * unsafe ptr = (unsigned char * unsafe)hidSendData; ptr[0] = 1; @@ -2048,15 +2091,15 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, // EX3D GET CMD_EXPAND_GAIN = 0x193 (little-endian) ptr[3] = 0x93; ptr[4] = 0x01; ptr[5] = 0x00; ptr[6] = 0x00; // 返回值: expand_gain (uint32, little-endian) - ptr[7] = (unsigned char)current_footstep_expand; + ptr[7] = (unsigned char)saved_footstep; ptr[8] = 0x00; ptr[9] = 0x00; ptr[10] = 0x00; for (int i = 11; i < HID_MAX_DATA_BYTES; i++) ptr[i] = 0x00; } hidSetChangePending(0x1); debug_printf("Footstep expand_gain changed: %d -> %d, HID 0xB1 report sent\n", - last_footstep_expand, current_footstep_expand); + last_footstep_expand, saved_footstep); } - last_footstep_expand = current_footstep_expand; + last_footstep_expand = saved_footstep; } #endif #endif diff --git a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/dsp.c b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/dsp.c index 7684ee7..cebc571 100644 --- a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/dsp.c +++ b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/dsp.c @@ -41,6 +41,12 @@ extern void device_reboot(void); unsigned g_ex3d_key_verified = 0; int32_t sys_vol = 0; unsigned g_mic_vol_cmd_pending = 0; +// 0xFA boot sync收到后置1;ex3d_task用此标志决定是否保留boot sync的阈值 +static volatile int g_boot_lmt_threshold_loaded = 0; +static volatile int g_boot_footstep_expand_gain_loaded = 0; +// audio_ex3d_init会覆盖Ex3dExpandGain为库默认值;用此变量保存0xFD boot sync收到的值 +static volatile int32_t g_boot_footstep_expand_gain_value = 12; +static volatile int32_t g_boot_lmt_threshold_value = -5; chanend_t uc_ex3d_to_ubm, uc_eq_data; static unsigned ubm_sample_freq = 0; @@ -665,10 +671,9 @@ void hid_receive_task_in_c(unsigned char * RcvData, unsigned * SendData) } threshold = Ex3dLimiterThreshold + (sys_vol + Ex3dOnGain + Ex3dExpandGain); - - if((-35 <= threshold) && (threshold <= 0)) { - EX3DAudio_SetLimiterThreshold(threshold); - } + if (threshold > 0) threshold = 0; + if (threshold < -35) threshold = -35; + EX3DAudio_SetLimiterThreshold(threshold); } else { // Get debug_printf("Get CMD_LMT_THRESHOLD : %d\r\n", Ex3dLimiterThreshold); @@ -934,8 +939,24 @@ void hid_button_task(chanend_t cc_mic_level, chanend_t c_hidRcvData, chanend_t c if ((0 <= gain) && (gain <= 20)) { Ex3dExpandGain = gain; EX3DAudio_SetExpandGain(Ex3dExpandGain); + g_boot_footstep_expand_gain_value = gain; + g_boot_footstep_expand_gain_loaded = 1; debug_printf("Button set CMD_EXPAND_GAIN: %d\n", gain); } +#endif + } else if (tmp == 0xFA) { + // 开机同步枪声阈值:从tile0 flash加载的Ex3dLimiterThreshold + // 必须先读取payload,无论是否启用EX3D + int32_t threshold = (int32_t)chan_in_word(cc_mic_level); +#if USE_EX3D == 1 + if ((-35 <= threshold) && (threshold <= 0)) { + Ex3dLimiterThreshold = threshold; + g_boot_lmt_threshold_loaded = 1; + g_boot_lmt_threshold_value = threshold; + EX3DAudio_SetLimiterThreshold(threshold); + // audio_ex3d_init尚未运行(T≈200ms),ex3d_task将在T=500ms后用正确的OnGain重新应用 + debug_printf("Boot sync lmt_threshold=%d stored, will apply after ex3d init\n", threshold); + } #endif } else if (tmp == 0xFE) { // 数字监听开关同步命令:来自 tile0(所有模式都需要处理) @@ -1007,11 +1028,10 @@ void hid_button_task(chanend_t cc_mic_level, chanend_t c_hidRcvData, chanend_t c #if USE_EX3D == 1 sys_vol = sys_vol * (-1); int32_t threshold = Ex3dLimiterThreshold + (sys_vol + Ex3dOnGain + Ex3dExpandGain); - - if((-35 <= threshold) && (threshold <= 0)) { - EX3DAudio_SetLimiterThreshold(threshold); - debug_printf("Set CMD_LMT_THRESHOLD : %d\r\n", threshold); - } + if (threshold > 0) threshold = 0; + if (threshold < -35) threshold = -35; + EX3DAudio_SetLimiterThreshold(threshold); + debug_printf("Set CMD_LMT_THRESHOLD : %d\r\n", threshold); debug_printf("sys_vol:%d\n", sys_vol); #endif } @@ -1084,11 +1104,29 @@ void ex3d_task(){ Ex3dOffGain = -5; Ex3dOnGain = -5; Ex3dLfeGain = -5; - Ex3dLimiterThreshold = -15; + // 若boot sync (0xFA) 已到达,保留flash加载的阈值;否则使用默认值-15 + if (!g_boot_lmt_threshold_loaded) { + Ex3dLimiterThreshold = -15; + } + + // audio_ex3d_init内部会将Ex3dExpandGain重置为库默认值(6),这里始终用保存值恢复 + Ex3dExpandGain = g_boot_footstep_expand_gain_loaded ? g_boot_footstep_expand_gain_value : 12; + Ex3dLimiterThreshold = g_boot_lmt_threshold_loaded ? g_boot_lmt_threshold_value : -15; EX3DAudio_SetOnGain(Ex3dOnGain); EX3DAudio_SetOffGain(Ex3dOffGain); - EX3DAudio_SetLimiterThreshold(Ex3dLimiterThreshold); + // audio_ex3d_init完成后用正确的增益链重新计算并应用阈值 + { + int32_t init_effective = Ex3dLimiterThreshold + (sys_vol + Ex3dOnGain + Ex3dExpandGain); + if (init_effective > 0) init_effective = 0; + if (init_effective < -35) init_effective = -35; + EX3DAudio_SetLimiterThreshold(init_effective); + debug_printf("ex3d_task init: lmt_threshold=%d effective=%d expand=%d\n", + Ex3dLimiterThreshold, init_effective, Ex3dExpandGain); + } + // 0xFD boot sync在init前调用了EX3DAudio_SetExpandGain,init后重新应用 + EX3DAudio_SetExpandGain(Ex3dExpandGain); + debug_printf("ex3d_task init: expand_gain=%d\n", Ex3dExpandGain); #endif diff --git a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/eq.c b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/eq.c index 45f8fa8..f6cb676 100644 --- a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/eq.c +++ b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/eq.c @@ -464,6 +464,7 @@ extern void hid_receive_task_in_c(unsigned char * RcvData, unsigned * SendData); static chanend_t g_ex3d_hid_chanend = 0; static unsigned ex3d_b0b1_result[HID_MAX_DATA_BYTES / 4] = {0}; extern unsigned g_hid_expand_gain_request; +extern unsigned g_hid_lmt_threshold_request; void SetEx3dHidChan(chanend_t c) { g_ex3d_hid_chanend = c; } #endif @@ -823,6 +824,19 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { SET_SHARED_GLOBAL(g_hid_expand_gain_request, gain_val); } + // 若是SET CMD_LMT_THRESHOLD (0x87),通知button_task保存阈值到flash + // 存储编码: -threshold (0~35),避免与哨兵值0xFFFFFFFF冲突 + if (data[1] == 0xB0 && ex3d_cmd == 0x87 && params_len >= 4) { + int32_t threshold_val = (int32_t)((uint32_t)data[6] | ((uint32_t)data[7] << 8) | + ((uint32_t)data[8] << 16) | ((uint32_t)data[9] << 24)); + if (threshold_val >= -35 && threshold_val <= 0) { + // Encode as (-threshold_val + 1) so range is 1~36. + // Avoids save_code=0 (flash "uninitialized" sentinel) when threshold_val=0. + uint32_t save_code = (uint32_t)(-threshold_val) + 1; + SET_SHARED_GLOBAL(g_hid_lmt_threshold_request, save_code); + } + } + // 发送命令到tile1并同步等待结果 // 协议: cmd(1 byte) + ex3d_cmd_code(4 bytes) + params_len(1 byte) + params(N bytes) chan_out_byte(g_ex3d_hid_chanend, data[1]);