From 7c9cfd11a2b39d1b79d20ace4359db09dcea063d Mon Sep 17 00:00:00 2001 From: Steven Dan Date: Tue, 24 Mar 2026 16:20:16 +0800 Subject: [PATCH] key buttons --- .../src/extensions/audiohw.xc | 185 ++++++++++++------ .../src/extensions/dsp.c | 66 +++---- 2 files changed, 155 insertions(+), 96 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 0c5e18c..84fadca 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 @@ -326,9 +326,9 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) - unsigned flag_music_mode = 0; - unsigned flag_ai71_onoff = 0; - unsigned flag_game_mode = 0; + // active_mode: 0=off, 1=music (LED_MUSIC, no algorithm), + // 2=game (LED_GAME_MODE, IR_GAME), 3=AI7.1 (LED_AI7_1, IR_7_1_GAME) + unsigned active_mode = 0; unsigned flag_footsteps_enhancement = 0; unsigned flag_aidenoise_onoff = 0; @@ -355,8 +355,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) led_control_t led_ctx; - // Windows模式:从 "game_mode" 加载(模式值 0-3) - // UAC1模式:从 "uac1_mode" 加载(等效模式值 0-2) + // saved_mode / active_mode: 0=off, 1=music, 2=game (IR_GAME), 3=AI7.1 (IR_7_1_GAME) unsigned /* char */ saved_mode; unsigned char saved_uac1_mode = 0; unsigned char path[] = "game_mode"; @@ -370,17 +369,22 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) #if defined(UAC1_MODE) delay_milliseconds(300); if (saved_mode == 255) { - saved_mode = 0; +#if defined(F5_MUSIC_UAC1) + saved_mode = 1; // f5: default music mode +#else + saved_mode = 0; // f6/f7: default off +#endif save_value(path, saved_mode); debug_printf("Saved game_mode to flash: %d\n", saved_mode); } #else if (saved_mode == 255) { - // 如果文件不存在,根据编译模式设置默认值 -#if defined(MODE_F1_MUSIC_UAC2) +#if defined(F1_MUSIC_UAC2) + saved_mode = 1; // f1: default music mode +#elif defined(F3_F4_FPS_UAC2) + saved_mode = 3; // f3/f4: default AI7.1 (IR_7_1_GAME) +#else saved_mode = 0; -#elif defined(MODE_F3_F4_FPS_UAC2) - saved_mode = 2; #endif save_value(path, saved_mode); debug_printf("Saved game_mode to flash: %d\n", saved_mode); @@ -392,14 +396,14 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) #if UAC1_MODE == 1 if (host_os == OS_WIN) { printf("Detected Windows OS (OS_WIN) saved_mode: %d\n", saved_mode); - unsigned flag = (saved_mode == 0) ? MODE_F1_MUSIC_UAC2 : MODE_F3_F4_FPS_UAC2; + unsigned flag = (saved_mode <= 1) ? MODE_F1_MUSIC_UAC2 : MODE_F3_F4_FPS_UAC2; SetRoleSwitchFlag(flag); device_reboot(); while (1); } else { - if (saved_mode != 0) { + if (saved_mode >= 2) { SetRoleSwitchFlag(MODE_F6_F7_FPS_UAC1); device_reboot(); while (1); @@ -410,8 +414,12 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) g_windows_detect_done = 1; - SET_SHARED_GLOBAL(g_game_mode, saved_mode); - debug_printf("Set new g_game_mode to %d\n", saved_mode); + active_mode = (saved_mode <= 3) ? saved_mode : 0; + { + unsigned hid_mode_init = (active_mode >= 2) ? (active_mode - 1) : 0; + SET_SHARED_GLOBAL(g_game_mode, hid_mode_init); + debug_printf("active_mode=%d hid_mode=%d\n", active_mode, hid_mode_init); + } @@ -477,6 +485,16 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) // 点亮D系列当前位置 led_on(&led_ctx, led_d_physical_map[i]); } + // Initialize mode button LEDs: at most one may be lit at a time + led_off(&led_ctx, LED_MUSIC); + led_off(&led_ctx, LED_GAME_MODE); + led_off(&led_ctx, LED_AI7_1); + switch (active_mode) { + case 1: led_on(&led_ctx, LED_MUSIC); break; + case 2: led_on(&led_ctx, LED_GAME_MODE); break; + case 3: led_on(&led_ctx, LED_AI7_1); break; + default: break; + } led_update_all(&led_ctx); @@ -487,11 +505,12 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) debug_printf("button task start\n"); - //chan_out_byte(cc_mic_level, 0xFC); // 音频模式传输命令 - //chan_out_byte(cc_mic_level, saved_mode); // 音频模式值(0=无音效,1=STEREO_2K,2=SPATIAL_GAME,3=SPATIAL_MOVIE) - cc_mic_level <: 0xFC; - cc_mic_level <: saved_mode; - debug_printf("Sent audio_mode %d to hid_button_task via cc_mic_level channel\n", saved_mode); + { + unsigned hid_mode_startup = (active_mode >= 2) ? (active_mode - 1) : 0; + cc_mic_level <: 0xFC; + cc_mic_level <: hid_mode_startup; + debug_printf("Sent hid_mode %d (active_mode=%d) to hid_button_task\n", hid_mode_startup, active_mode); + } // port_enable(p_ctrl_keys); port_ctrl_keys = 0;//port_in(p_ctrl_keys); @@ -660,65 +679,105 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) button_footsteps_enhancement = ((mic_encoder1 & 0b0010) != 0); // 4F1 button_aidenoise_onoff = ((mic_encoder2 & 0b1000) != 0); // 4E3 - if(button_music_mode == 0) - { - if(push_button_music_mode_state_old == 1) - { - flag_music_mode ^= 1; - mode_change = 1; + // --- Mode button handling --- + // active_mode: 0=off, 1=music, 2=game (IR_GAME), 3=AI7.1 (IR_7_1_GAME) + // + // Boot chain: power-on → F5 → OS detect → route to F1/F3/F4/F5/F6/F7 + // + // Music-only firmware (F1, F5): USE_EX3D not set + // Music btn: toggle LED locally (no reboot) + // Game/AI7.1 btn: save mode + reboot (F5 will route to FPS firmware) + // + // FPS firmware (F3/F4, F6/F7): USE_EX3D set + // Music btn: save mode=1 + reboot (F5 will route to music firmware) + // Game/AI7.1 btn: switch IR algorithm locally (no reboot) + + unsigned mode_btn_change = 0; + unsigned need_reboot = 0; + + if (button_music_mode == 0) { + if (push_button_music_mode_state_old == 1) { +#if USE_EX3D == 1 + // FPS firmware: switching to music needs different USB config + active_mode = 1; + need_reboot = 1; +#else + // Music-only firmware: toggle music LED locally + active_mode = (active_mode == 1) ? 0 : 1; + mode_btn_change = 1; +#endif } } push_button_music_mode_state_old = button_music_mode; - if(mode_change) - if(flag_music_mode) - { - led_on(&led_ctx, LED_MUSIC); - } - else - { - led_off(&led_ctx, LED_MUSIC); - } - mode_change = 0; - if(button_ai71_onoff == 0) - { - if(push_button_ai71_onoff_state_old == 1) - { - flag_ai71_onoff ^= 1; - mode_change = 1; + if (button_ai71_onoff == 0) { + if (push_button_ai71_onoff_state_old == 1) { +#if USE_EX3D == 1 + // FPS firmware: switch algorithm locally (toggle off or activate) + active_mode = (active_mode == 3) ? 0 : 3; + mode_btn_change = 1; +#else + // Music-only firmware: AI7.1 needs FPS firmware + active_mode = 3; + need_reboot = 1; +#endif } - } push_button_ai71_onoff_state_old = button_ai71_onoff; - if(mode_change) - if(flag_ai71_onoff) - { - led_on(&led_ctx, LED_AI7_1); - } - else - { - led_off(&led_ctx, LED_AI7_1); - } - mode_change = 0; - if(button_game_mode == 0) - { - if(push_button_game_mode_state_old == 1) - { - flag_game_mode ^= 1; - mode_change = 1; + if (button_game_mode == 0) { + if (push_button_game_mode_state_old == 1) { +#if USE_EX3D == 1 + // FPS firmware: switch algorithm locally (toggle off or activate) + active_mode = (active_mode == 2) ? 0 : 2; + mode_btn_change = 1; +#else + // Music-only firmware: game needs FPS firmware + active_mode = 2; + need_reboot = 1; +#endif } } push_button_game_mode_state_old = button_game_mode; - if(mode_change) - if(flag_game_mode) + + if (mode_btn_change || need_reboot) { + // Always persist mode to flash so boot chain restores it { - led_on(&led_ctx, LED_GAME_MODE); + unsigned char save_path[] = "game_mode"; + save_value(save_path, (unsigned char)active_mode); } - else + + if (need_reboot) { + // Reboot: F5 will re-detect OS and route to correct firmware + debug_printf("Mode %d requires firmware switch, rebooting\n", active_mode); + delay_milliseconds(20); + device_reboot(); + while (1); + } + + // Local update path (no reboot): update LEDs and DSP algorithm + led_off(&led_ctx, LED_MUSIC); + led_off(&led_ctx, LED_GAME_MODE); + led_off(&led_ctx, LED_AI7_1); + switch (active_mode) { + case 1: led_on(&led_ctx, LED_MUSIC); break; + case 2: led_on(&led_ctx, LED_GAME_MODE); break; + case 3: led_on(&led_ctx, LED_AI7_1); break; + default: break; + } + led_update_all(&led_ctx); + +#if USE_EX3D == 1 { - led_off(&led_ctx, LED_GAME_MODE); + // hid_mode: 0=IR_OFF, 1=IR_GAME, 2=IR_7_1_GAME + unsigned hid_mode = (active_mode >= 2) ? (active_mode - 1) : 0; + SET_SHARED_GLOBAL(g_game_mode, hid_mode); + cc_mic_level <: 0xFC; + cc_mic_level <: hid_mode; + debug_printf("Mode changed: active=%d hid=%d\n", active_mode, hid_mode); } +#endif + } mode_change = 0; if(button_footsteps_enhancement == 0) 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 6c94e93..5c70323 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 @@ -868,41 +868,34 @@ void hid_button_task(chanend_t cc_mic_level, chanend_t c_hidRcvData, chanend_t c debug_printf("hid_button_task received audio_mode: %d\n", current_mode_local); if (current_mode_local == 0) { - // 无音效模式:灯灭,算法关闭 - audio_ex3d_onoff(0); + // 关闭算法 + audio_ex3d_set_onoff(0); #if IR_SWITCHING_MODE is_3d_on = IR_OFF; #else is_3d_on = A3D_OFF; #endif - debug_printf("Mode 0 (NO_EFFECT): algorithm OFF\n"); + debug_printf("Mode 0: algorithm OFF\n"); } else if (current_mode_local == 1) { - // STEREO_2K模式:红灯,算法开启 - audio_ex3d_onoff(1); + // 游戏模式 (IR_GAME, stereo): SF index 0 #if IR_SWITCHING_MODE + audio_ex3d_set_sf(0); // SF 0 = STEREO GAME is_3d_on = IR_GAME; #else is_3d_on = A3D_ON; #endif - debug_printf("Mode 1 (STEREO_2K): algorithm ON\n"); + audio_ex3d_set_onoff(1); + debug_printf("Mode 1: GAME (IR_GAME) ON, sf=0\n"); } else if (current_mode_local == 2) { - // SPATIAL_GAME模式:蓝灯,算法开启 - audio_ex3d_onoff(1); + // AI7.1模式 (IR_7_1_GAME, 7.1): SF index 1 #if IR_SWITCHING_MODE + audio_ex3d_set_sf(1); // SF 1 = SPATIAL GAME (7.1) is_3d_on = IR_7_1_GAME; #else is_3d_on = A3D_ON; #endif - debug_printf("Mode 2 (SPATIAL_GAME): algorithm ON\n"); - } else if (current_mode_local == 3) { - // SPATIAL_MOVIE模式:蓝灯闪烁,算法开启 - audio_ex3d_onoff(1); -#if IR_SWITCHING_MODE - is_3d_on = IR_7_1_MOVIE; -#else - is_3d_on = A3D_ON; -#endif - debug_printf("Mode 3 (SPATIAL_MOVIE): algorithm ON\n"); + audio_ex3d_set_onoff(1); + debug_printf("Mode 2: AI7.1 (IR_7_1_GAME) ON, sf=1\n"); } SET_SHARED_GLOBAL(g_3d_on_off_t1, is_3d_on); } else { @@ -971,22 +964,29 @@ void hid_button_task(chanend_t cc_mic_level, chanend_t c_hidRcvData, chanend_t c GET_SHARED_GLOBAL(host_app_sf, g_3d_on_off_t1); if (host_app_sf != is_3d_on) { is_3d_on = host_app_sf; - audio_ex3d_onoff(is_3d_on != IR_OFF); - if (is_3d_on == IR_OFF) current_mode_local = 0; - else if (is_3d_on == IR_GAME) current_mode_local = 1; - else if (is_3d_on == IR_7_1_GAME) current_mode_local = 2; - else if (is_3d_on == IR_7_1_MOVIE) current_mode_local = 3; - - switch (is_3d_on) { - case IR_GAME: printf("Stereo Game on\n"); break; - case IR_MUSIC: printf("Stereo Music on\n"); break; - case IR_MOVIE: printf("Stereo Movie on\n"); break; - case IR_7_1_GAME: printf("3d Game on\n"); break; - case IR_7_1_MUSIC:printf("3d Music on\n"); break; - case IR_7_1_MOVIE:printf("3d Movie on\n"); break; - case IR_OFF: printf("3d off\n"); break; - default: break; + // Select SF and set on/off together + if (is_3d_on == IR_OFF) { + audio_ex3d_set_onoff(0); + current_mode_local = 0; + printf("3d off\n"); + } else if (is_3d_on == IR_GAME) { + audio_ex3d_set_sf(0); + audio_ex3d_set_onoff(1); + current_mode_local = 1; + printf("Stereo Game on (sf=0)\n"); + } else if (is_3d_on == IR_7_1_GAME) { + audio_ex3d_set_sf(1); + audio_ex3d_set_onoff(1); + current_mode_local = 2; + printf("3d Game on (sf=1)\n"); + } else if (is_3d_on == IR_7_1_MOVIE) { + audio_ex3d_set_sf(2); + audio_ex3d_set_onoff(1); + current_mode_local = 3; + printf("3d Movie on (sf=2)\n"); + } else { + audio_ex3d_set_onoff(is_3d_on != IR_OFF); } SET_SHARED_GLOBAL(g_3d_on_off_t1, is_3d_on); }