diff --git a/sw_usb_audio/app_usb_aud_phaten_golden/src/extensions/eq.c b/sw_usb_audio/app_usb_aud_phaten_golden/src/extensions/eq.c index e530bb3..c44ac9f 100644 --- a/sw_usb_audio/app_usb_aud_phaten_golden/src/extensions/eq.c +++ b/sw_usb_audio/app_usb_aud_phaten_golden/src/extensions/eq.c @@ -5,18 +5,9 @@ #endif #include "debug_print.h" #include "xc_ptr.h" -#include // 添加offsetof宏支持 #include // 添加数学函数支持 -#include - -#if 0 // DEBUG_PRINT_ENABLE -#define DPRINTF(...) printf(__VA_ARGS__) -#else -#define DPRINTF(...) -#endif extern void device_reboot(void); -//extern void delay_milliseconds(unsigned int); // 常量定义 #define EQ_DISABLED_MODE 10 // 禁用EQ的模式编号 @@ -28,48 +19,27 @@ extern void device_reboot(void); #include #include "eq_params_44100.h" -// 改动原因:进一步优化EQ参数存储策略,所有采样率共用44100Hz数组 -// 参数在所有采样率下是相同的,只有系数需要根据采样率动态计算 -// 切换采样率时,直接使用44100Hz数组的参数,重新计算系数即可 -/** - * @brief 获取指定采样率的EQ数组指针(统一使用44100Hz数组) - * @param sample_rate 采样率 - * @return EQ数组指针,始终返回44100Hz数组 - * - * 改动原因:所有采样率共用44100Hz数组,参数相同,系数根据采样率动态计算 - */ -static inline eq_mode_data_t (*get_eq_array_ptr_common(uint32_t sample_rate))[NUM_EQ_MODES][NUM_EQ_CHANS] { - // 所有采样率都使用44100Hz数组(参数源) - // 注意:切换采样率时需要重新计算系数 - return &sEQ_data_44100HZ; -} +#define RSP_HDR(r, cmd) do { (r)[0] = 0x77; (r)[1] = (uint8_t)(cmd); } while(0) + #include "eq_coefficient_calculator.h" #include "biquad_standalone.h" #include "eq_flash_storage.h" -#include "user_func.h" // 改动原因:包含check_sum函数声明,用于uart_checksum宏定义 +#include "user_func.h" + static struct { - bool is_read_device_params_request; // 设备参数读取请求标志 -} device_params_request = {0}; -static struct { - bool is_get_led_info_request; // GET_LED_INFO请求标志 - bool is_get_led_switch_request; // GET_LED_SWITCH请求标志 - bool is_set_led_switch_request; // SET_LED_SWITCH请求标志(用于响应) - bool is_get_led_status_request; // GET_LED_STATUS请求标志 - bool is_get_led_count_request; // GET_LED_COUNT请求标志 - uint8_t led_index; // LED索引 + uint8_t pending_cmd; // 0x95/0x96/0x97/0x98/0x99 or 0 + uint8_t led_index; } led_request = {0}; + static struct { - bool is_get_uac_mode_info_request; // GET_UAC_MODE_INFO请求标志 - bool is_get_current_uac_mode_request; // GET_CURRENT_UAC_MODE请求标志 + uint8_t pending_cmd; // 0x9A/0x9C or 0 } uac_request = {0}; + static struct { - bool is_set_ex3d_cmd_request; // SET_EX3D_CMD请求标志 - bool is_get_ex3d_cmd_request; // GET_EX3D_CMD请求标志 - uint32_t ex3d_command_code; // EX3D命令码 - uint8_t ex3d_params[56]; // EX3D参数数据(最多14个uint32,56字节) - uint8_t ex3d_params_len; // EX3D参数长度 - uint8_t ex3d_response[56]; // EX3D响应数据 - uint8_t ex3d_response_len; // EX3D响应长度 + uint8_t pending_cmd; // 0xB0/0xB1 or 0 + uint8_t ex3d_params_len; + uint32_t ex3d_command_code; + uint8_t ex3d_params[56]; } ex3d_request = {0}; #if EQ_EN @@ -80,306 +50,78 @@ unsigned int g_new_eq_mode = 9; unsigned int g_eq_sample_rate = 0; unsigned int g_force_eq_mode_change = 0; unsigned int g_force_request_eq_mode_change = 0; -unsigned int g_eq_enable = 0; // EQ使能开关,1=启用,0=禁用,默认启用 -unsigned int g_saved_eq_mode = 9; // 保存之前的EQ模式,用于恢复,默认bypass模式 - -eq_mode_data_t eq_hid_params; +unsigned int g_eq_enable = 0; +unsigned int g_saved_eq_mode = 9; // 全局模式信息存储 - 只存储NUM_EQ_MODES条信息 eq_mode_info_t g_mode_info = {0}; static struct { - uint32_t sample_rate; + uint8_t pending_cmd; // HID command code (0 = none pending) uint8_t mode; uint8_t eq_index; - bool is_read_request; - bool is_read_mode_request; - uint8_t read_mode_request_mode; // 改动原因:添加0x8B命令的mode参数存储,0xFF表示获取当前模式,0-9表示获取指定模式 - bool is_read_device_info_request; - bool is_reset_eq_params_request; - bool is_read_mode_count_request; // 新增:获取模式总数请求标志 - bool is_set_and_save_mode_request; // 新增:设置并保存模式请求标志 - bool is_read_volume_request; // 改动原因:添加读取音量请求标志,用于EQ命令中的GET_VOLUME (0x94) - bool is_set_eq_enable_request; // 改动原因:添加设置EQ使能请求标志,用于SET_EQ_ENABLE (0x9D) - bool is_get_eq_enable_request; // 改动原因:添加获取EQ使能请求标志,用于GET_EQ_ENABLE (0x9E) - uint8_t eq_enable_value; // 改动原因:添加EQ使能值存储,用于SET_EQ_ENABLE命令 - bool is_read_sample_format_request; // 改动原因:添加读取采样率和格式请求标志,用于GET_SAMPLE_FORMAT (0x9F) - bool is_get_gain_mode_request; // 改动原因:添加获取增益模式请求标志,用于GET_GAIN_MODE (0xA1) - bool is_get_filter_mode_request; // 改动原因:添加获取滤波器模式请求标志,用于GET_FILTER_MODE (0xA3) - bool is_get_game_mode_request; // 改动原因:添加获取游戏模式请求标志,用于GET_GAME_MODE (0xA5) - bool is_get_firmware_version_request; // 改动原因:添加获取固件版本请求标志,用于GET_FIRMWARE_VERSION (0xA6) - int32_t post_gain_db; + uint8_t read_mode_request_mode; } read_request = {0}; -static const int32_t attenuation_lookup_table[75] = { -0x46CB697E, // 0 37.0 -0x42D59AB2, // 1 36.5 -0x3F18820D, // 2 36.0 -0x3B90F36C, // 3 35.5 -0x383BF01B, // 4 35.0 -0x3516A44B, // 5 34.5 -0x321E64A7, // 6 34.0 -0x2F50AC17, // 7 33.5 -0x2CAB1996, // 8 33.0 -0x2A2B6E34, // 9 32.5 -0x27CF8B27, // 10 32.0 -0x25957003, // 11 31.5 -0x237B3907, // 12 31.0 -0x217F1D80, // 13 30.5 -0x1F9F6E49, // 14 30.0 -0x1DDA945A, // 15 29.5 -0x1C2F0F70, // 16 29.0 -0x1A9B74C6, // 17 28.5 -0x191E6DE4, // 18 28.0 -0x17B6B778, // 19 27.5 -0x16632049, // 20 27.0408 -0x1522882D, // 21 26.5 -0x13F3DF1C, // 22 26.0 -0x12D62444, // 23 25.5 -0x11C86531, // 24 25.0 -0x10C9BD00, // 25 24.5 -0x0FD9539A, // 26 24.0 -0x0EF65CFF, // 27 23.5 -0x0E20189A, // 28 23.0 -0x0D55D09B, // 29 22.5 -0x0C96D95B, // 30 22.0 -0x0BE290D1, // 31 21.5 -0x0B385E03, // 32 21.0 -0x0A97B085, // 33 20.5 -0x0A000000, // 34 20.0 -0x0970CBBC, // 35 19.5 -0x08E99A36, // 36 19.0 -0x0869F8B8, // 37 18.5 -0x07F17AF3, // 38 18.0 -0x077FBAAB, // 39 17.5 -0x07145759, // 40 17.0 -0x06AEF5DE, // 41 16.5 -0x064F4034, // 42 16.0 -0x05F4E524, // 43 15.5 -0x059F9802, // 44 15.0 -0x054F106D, // 45 14.5 -0x05030A10, // 46 14.0 -0x04BB4468, // 47 13.5 -0x0477828F, // 48 13.0 -0x04378B05, // 49 12.5 -0x03FB2783, // 50 12.0 -0x03C224CD, // 51 11.5 -0x038C5280, // 52 11.0 -0x035982F3, // 53 10.5 -0x03298B07, // 54 10.0 -0x02FC4209, // 55 9.5 -0x02D1818B, // 56 9.0 -0x02A92547, // 57 8.5 -0x02830AFD, // 58 8.0 -0x025F1258, // 59 7.5 -0x023D1CD4, // 60 7.0 -0x021D0D9E, // 61 6.5 -0x01FEC982, // 62 6.0 -0x01E236D3, // 63 5.5 -0x01C73D51, // 64 5.0 -0x01ADC619, // 65 4.5 -0x0195BB8F, // 66 4.0 -0x017F094C, // 67 3.5 -0x01699C0F, // 68 3.0 -0x015561A9, // 69 2.5 -0x014248EF, // 70 2.0 -0x013041AE, // 71 1.5 -0x011F3C99, // 72 1.0 -0x010F2B40, // 73 0.5 -0x01000000, // 74 0.0 -}; - -static const int32_t attenuation_lookup_table_minus[183] = { -0x01000000, // 74 0.0 -0x00F1ADF9, // 75 -0.5 -0x00E42905, // 76 -1.0 -0x00D765AC, // 77 -1.5 -0x00CB5918, // 78 -2.0 -0x00BFF911, // 79 -2.5 -0x00B53BEF, // 80 -3.0 -0x00AB1896, // 81 -3.5 -0x00A1866B, // 82 -4.0 -0x00987D50, // 83 -4.5 -0x008FF599, // 84 -5.0 -0x0087E80A, // 85 -5.5 -0x00804DCE, // 86 -6.0 -0x00792070, // 87 -6.5 -0x007259DB, // 88 -7.0 -0x006BF44D, // 89 -7.5 -0x0065EA59, // 90 -8.0 -0x006036E1, // 91 -8.5 -0x005AD50C, // 92 -9.0 -0x0055C04B, // 93 -9.5 -0x0050F44D, // 94 -10.0 -0x004C6D00, // 95 -10.5 -0x0048268D, // 96 -11.0 -0x00441D53, // 97 -11.5 -0x00404DE6, // 98 -12.0 -0x003CB508, // 99 -12.5 -0x00394FAE, // 100 -13.0 -0x00361AF6, // 101 -13.5 -0x00331426, // 102 -14.0 -0x003038AE, // 103 -14.5 -0x002D8621, // 104 -15.0 -0x002AFA35, // 105 -15.5 -0x002892C1, // 106 -16.0 -0x00264DBA, // 107 -16.5 -0x00242934, // 108 -17.0 -0x0022235D, // 109 -17.5 -0x00203A7E, // 110 -18.0 -0x001E6CF7, // 111 -18.5 -0x001CB942, // 112 -19.0 -0x001B1DEC, // 113 -19.5 -0x00199999, // 114 -20.0 -0x00182AFF, // 115 -20.5 -0x0016D0E6, // 116 -21.0 -0x00158A2A, // 117 -21.5 -0x001455B5, // 118 -22.0 -0x00133281, // 119 -22.5 -0x00121F97, // 120 -23.0 -0x00111C0F, // 121 -23.5 -0x0010270A, // 122 -24.0 -0x000F3FBB, // 123 -24.5 -0x000E655C, // 124 -25.0 -0x000D9734, // 125 -25.5 -0x000CD494, // 126 -26.0 -0x000C1CD8, // 127 -26.5 -0x000B6F62, // 128 -27.0 -0x000ACBA1, // 129 -27.5 -0x000A3108, // 130 -28.0 -0x00099F16, // 131 -28.5 -0x0009154E, // 132 -29.0 -0x0008933A, // 133 -29.5 -0x0008186E, // 134 -30.0 -0x0007A480, // 135 -30.5 -0x0007370E, // 136 -31.0 -0x0006CFBB, // 137 -31.5 -0x00066E30, // 138 -32.0 -0x0006121A, // 139 -32.5 -0x0005BB2B, // 140 -33.0 -0x00056918, // 141 -33.5 -0x00051B9D, // 142 -34.0 -0x0004D277, // 143 -34.5 -0x00048D69, // 144 -35.0 -0x00044C38, // 145 -35.5 -0x00040EAC, // 146 -36.0 -0x0003D492, // 147 -36.5 -0x00039DB8, // 148 -37.0 -0x000369EF, // 149 -37.5 -0x0003390C, // 150 -38.0 -0x00030AE5, // 151 -38.5 -0x0002DF53, // 152 -39.0 -0x0002B631, // 153 -39.5 -0x00028F5C, // 154 -40.0 -0x00026AB3, // 155 -40.5 -0x00024817, // 156 -41.0 -0x0002276A, // 157 -41.5 -0x00020892, // 158 -42.0 -0x0001EB73, // 159 -42.5 -0x0001CFF5, // 160 -43.0 -0x0001B601, // 161 -43.5 -0x00019D81, // 162 -44.0 -0x0001865F, // 163 -44.5 -0x00017089, // 164 -45.0 -0x00015BEB, // 165 -45.5 -0x00014875, // 166 -46.0 -0x00013615, // 167 -46.5 -0x000124BD, // 168 -47.0 -0x0001145C, // 169 -47.5 -0x000104E7, // 170 -48.0 -0x0000F64F, // 171 -48.5 -0x0000E887, // 172 -49.0 -0x0000DB85, // 173 -49.5 -0x0000CF3E, // 174 -50.0 -0x0000C3A6, // 175 -50.5 -0x0000B8B4, // 176 -51.0 -0x0000AE5F, // 177 -51.5 -0x0000A49E, // 178 -52.0 -0x00009B69, // 179 -52.5 -0x000092B7, // 180 -53.0 -0x00008A82, // 181 -53.5 -0x000082C2, // 182 -54.0 -0x00007B72, // 183 -54.5 -0x0000748A, // 184 -55.0 -0x00006E05, // 185 -55.5 -0x000067DE, // 186 -56.0 -0x0000620E, // 187 -56.5 -0x00005C92, // 188 -57.0 -0x00005764, // 189 -57.5 -0x00005281, // 190 -58.0 -0x00004DE3, // 191 -58.5 -0x00004988, // 192 -59.0 -0x0000456B, // 193 -59.5 -0x00004189, // 194 -60.0 -0x00003DDE, // 195 -60.5 -0x00003A68, // 196 -61.0 -0x00003724, // 197 -61.5 -0x0000340E, // 198 -62.0 -0x00003125, // 199 -62.5 -0x00002E65, // 200 -63.0 -0x00002BCC, // 201 -63.5 -0x00002959, // 202 -64.0 -0x00002709, // 203 -64.5 -0x000024DA, // 204 -65.0 -0x000022CA, // 205 -65.5 -0x000020D8, // 206 -66.0 -0x00001F02, // 207 -66.5 -0x00001D46, // 208 -67.0 -0x00001BA2, // 209 -67.5 -0x00001A17, // 210 -68.0 -0x000018A1, // 211 -68.5 -0x00001740, // 212 -69.0 -0x000015F3, // 213 -69.5 -0x000014B9, // 214 -70.0 -0x00001390, // 215 -70.5 -0x00001278, // 216 -71.0 -0x0000116F, // 217 -71.5 -0x00001076, // 218 -72.0 -0x00000F8A, // 219 -72.5 -0x00000EAB, // 220 -73.0 -0x00000DD9, // 221 -73.5 -0x00000D13, // 222 -74.0 -0x00000C58, // 223 -74.5 -0x00000BA7, // 224 -75.0 -0x00000B00, // 225 -75.5 -0x00000A63, // 226 -76.0 -0x000009CE, // 227 -76.5 -0x00000941, // 228 -77.0 -0x000008BD, // 229 -77.5 -0x00000840, // 230 -78.0 -0x000007C9, // 231 -78.5 -0x0000075A, // 232 -79.0 -0x000006F1, // 233 -79.5 -0x0000068D, // 234 -80.0 -0x0000062F, // 235 -80.5 -0x000005D7, // 236 -81.0 -0x00000583, // 237 -81.5 -0x00000534, // 238 -82.0 -0x000004EA, // 239 -82.5 -0x000004A3, // 240 -83.0 -0x00000461, // 241 -83.5 -0x00000422, // 242 -84.0 -0x000003E7, // 243 -84.5 -0x000003AF, // 244 -85.0 -0x0000037A, // 245 -85.5 -0x00000348, // 246 -86.0 -0x00000319, // 247 -86.5 -0x000002ED, // 248 -87.0 -0x000002C3, // 249 -87.5 -0x0000029B, // 250 -88.0 -0x00000276, // 251 -88.5 -0x00000253, // 252 -89.0 -0x00000231, // 253 -89.5 -0x00000212, // 254 -90.0 +// Optimized: 51 entries for integer dB values 0 to -50 (was 183 entries at 0.5dB steps) +static const int32_t attenuation_table[51] = { + 0x01000000, // 0dB + 0x00E42905, // -1dB + 0x00CB5918, // -2dB + 0x00B53BEF, // -3dB + 0x00A1866B, // -4dB + 0x008FF599, // -5dB + 0x00804DCE, // -6dB + 0x007259DB, // -7dB + 0x0065EA59, // -8dB + 0x005AD50C, // -9dB + 0x0050F44D, // -10dB + 0x0048268D, // -11dB + 0x00404DE6, // -12dB + 0x00394FAE, // -13dB + 0x00331426, // -14dB + 0x002D8621, // -15dB + 0x002892C1, // -16dB + 0x00242934, // -17dB + 0x00203A7E, // -18dB + 0x001CB942, // -19dB + 0x00199999, // -20dB + 0x0016D0E6, // -21dB + 0x001455B5, // -22dB + 0x00121F97, // -23dB + 0x0010270A, // -24dB + 0x000E655C, // -25dB + 0x000CD494, // -26dB + 0x000B6F62, // -27dB + 0x000A3108, // -28dB + 0x0009154E, // -29dB + 0x0008186E, // -30dB + 0x0007370E, // -31dB + 0x00066E30, // -32dB + 0x0005BB2B, // -33dB + 0x00051B9D, // -34dB + 0x00048D69, // -35dB + 0x00040EAC, // -36dB + 0x00039DB8, // -37dB + 0x0003390C, // -38dB + 0x0002DF53, // -39dB + 0x00028F5C, // -40dB + 0x00024817, // -41dB + 0x00020892, // -42dB + 0x0001CFF5, // -43dB + 0x00019D81, // -44dB + 0x00017089, // -45dB + 0x00014875, // -46dB + 0x000124BD, // -47dB + 0x000104E7, // -48dB + 0x0000E887, // -49dB + 0x0000CF3E, // -50dB }; static inline int get_gain_q24(int input, unsigned multipler){ int result; long long intermediate; intermediate = (long long) input * (long long) multipler; -#if 0 - result = (int)(intermediate >> (24)); -#else intermediate = intermediate >> 24; if (intermediate > INT32_MAX) { @@ -390,108 +132,61 @@ static inline int get_gain_q24(int input, unsigned multipler){ } result = (int)(intermediate); -#endif return result; } static inline int32_t clamp_post_gain(int32_t gain) { // 限制gain范围:0 >= gain >= -50 if (gain > 0) { - debug_printf("Warning: gain %d > 0, clamping to 0\n", gain); return 0; } else if (gain < -50) { - debug_printf("Warning: gain %d < -50, clamping to -50\n", gain); return -50; } return gain; } -// 应用增益或衰减 (Q24格式) + +// apply_gain: apply attenuation (Q24 format) +// gain_db is always in range [-50, 0] static inline int32_t apply_gain(int32_t sample, int32_t gain_db) { - int32_t result; - int32_t attenuation = 0; - - if (gain_db <= 0) { - // 衰减:使用查找表 - int32_t index = -gain_db; // 将负dB值转换为正索引 - if (index > 91) index = 91; // 限制最大衰减为-90dB - attenuation = attenuation_lookup_table_minus[index * 2]; - } else { - int32_t index = 74 - gain_db * 2; - attenuation = attenuation_lookup_table[index]; - - } - if (attenuation != 0) { - result = get_gain_q24(sample, attenuation); - } - else { - result = sample; - } - - return result; + int32_t idx = -gain_db; + if (idx > 50) idx = 50; + return get_gain_q24(sample, attenuation_table[idx]); } -/** - * @brief 设置采样率标记(所有采样率共用44100Hz数组) - * @param sample_rate 目标采样率 - * @param mode 模式号 - * @return 成功返回0,失败返回-1 - * - * 改动原因:所有采样率共用44100Hz数组,参数相同,只需要设置sample_rate标记 - * 系数会根据采样率动态计算,不需要复制参数 - */ +// Helper: check if name is empty (all NUL or all spaces) +static bool is_name_empty(const char *name, int len) { + if (name[0] == '\0') return true; + for (int i = 0; i < len && name[i] != '\0'; i++) { + if (name[i] != ' ') return false; + } + return true; +} + +// copy_params_from_44100: set sample rate flag for specified mode static int copy_params_from_44100(uint32_t sample_rate, uint8_t mode) { if (mode >= NUM_EQ_MODES) { - debug_printf("Error: Invalid mode %d in copy_params_from_44100\n", mode); return -1; } - // 改动原因:所有采样率共用44100Hz数组,只需要设置sample_rate标记 - // 参数已经存储在44100Hz数组中,不需要复制 for (int ch = 0; ch < NUM_EQ_CHANS; ch++) { sEQ_data_44100HZ[mode][ch].sample_rate = sample_rate; - // 重置total_bshift(将在计算系数时更新) sEQ_data_44100HZ[mode][ch].total_bshift = 0; } - debug_printf("Set sample rate to %dHz for mode %d (using 44100Hz array)\n", sample_rate, mode); return 0; } -/** - * @brief 动态计算当前模式所有band的系数 - * @param sample_rate 采样率 - * @param mode EQ模式 (0-9) - * @return 成功返回0,失败返回-1 - * - * 根据process_send_params函数中0x8d命令的逻辑,实现当前模式所有band的系数计算 - * 如果是bypass类型,直接赋值bypass系数,不计算bshift - * 正确计算total_bshift作为所有非bypass滤波器bshift的累加 - * - * 改动原因:在计算系数前,先从44100Hz统一参数源复制参数到当前采样率 - */ +// calculate_current_mode_coefficients: dynamically compute coefficients for all bands of a mode int calculate_current_mode_coefficients(uint32_t sample_rate, uint8_t mode) { - debug_printf("calculate_current_mode_coefficients: sample_rate=%d, mode=%d\n", sample_rate, mode); - - // 改动原因:在计算系数前,先从44100Hz统一参数源复制参数到当前采样率 - // 确保所有采样率的参数都从统一源同步 - if (copy_params_from_44100(sample_rate, mode) != 0) { - debug_printf("Warning: Failed to copy params from 44100Hz, continuing with existing params\n"); - } + copy_params_from_44100(sample_rate, mode); if (mode >= NUM_EQ_MODES) { - debug_printf("Error: Invalid mode %d (max: %d)\n", mode, NUM_EQ_MODES - 1); return -1; } - // 改动原因:所有采样率共用44100Hz数组 eq_mode_data_t *eq_data = &sEQ_data_44100HZ[mode][0]; eq_mode_data_t *eq_data_2 = &sEQ_data_44100HZ[mode][1]; - if (eq_data == NULL || eq_data_2 == NULL) { - debug_printf("Error: eq_data pointer is NULL\n"); - return -1; - } - // 重置total_bshift int total_bshift = 0; @@ -501,7 +196,6 @@ int calculate_current_mode_coefficients(uint32_t sample_rate, uint8_t mode) { // 检查参数有效性 if (isnan(band->fc) || isnan(band->q) || isnan(band->bw) || isnan(band->gain)) { - debug_printf("Warning: Invalid parameters for band %d, using bypass\n", band_index); band->type = FILTER_TYPE_BYPASS; band->fc = 1000.0f; band->q = 0.707f; @@ -521,12 +215,9 @@ int calculate_current_mode_coefficients(uint32_t sample_rate, uint8_t mode) { coeffs[3] = 0; // a1 = 0 coeffs[4] = 0; // a2 = 0 calculated_bshift = 0; - - debug_printf("Band %d: BYPASS - coeffs set to bypass values, bshift=0\n", band_index); } else { // 非bypass类型:调用系数计算函数 if (eq_calculate_coefficients_from_params(band, sample_rate, coeffs, &calculated_bshift) != 0) { - debug_printf("Error: Failed to calculate coefficients for band %d, using bypass\n", band_index); // 计算失败,使用bypass coeffs[0] = 1 << 30; coeffs[1] = 0; @@ -534,7 +225,7 @@ int calculate_current_mode_coefficients(uint32_t sample_rate, uint8_t mode) { coeffs[3] = 0; coeffs[4] = 0; calculated_bshift = 0; - } + } // 累加非bypass滤波器的bshift total_bshift += calculated_bshift; } @@ -552,21 +243,12 @@ int calculate_current_mode_coefficients(uint32_t sample_rate, uint8_t mode) { eq_data->total_bshift = total_bshift; eq_data_2->total_bshift = total_bshift; - debug_printf("Mode %d coefficients calculated successfully, total_bshift=%d\n", mode, total_bshift); - return 0; } // Initialize EQ data for the specified sample rate uint32_t init_eq_data(unsigned sample_freq) { g_eq_sample_rate = sample_freq; - debug_printf("init_eq_data %d\n", g_eq_sample_rate); - - // 初始化模式信息 - - // 从Flash加载增益和模式名称 - - // 初始化单参数存储数据库 // Clear EQ status for all sample rates, modes, and channels for (int ch = 0; ch < NUM_EQ_CHANS; ch++) { @@ -574,40 +256,21 @@ uint32_t init_eq_data(unsigned sample_freq) { clear_eq_status(sample_freq, ch); } - // 改动原因:优化EQ参数存储策略,从44100Hz统一参数源复制参数到当前采样率 - // 然后为所有模式计算系数,确保参数同步 - debug_printf("Initializing EQ parameters from 44100Hz source for sample rate %d Hz\n", sample_freq); - - // 为所有模式复制参数并计算系数 + // 为所有模式计算系数 (calculate_current_mode_coefficients already calls copy_params_from_44100) for (int mode = 0; mode < NUM_EQ_MODES; mode++) { - // 从44100Hz复制参数到当前采样率 - if (copy_params_from_44100(sample_freq, mode) == 0) { - // 计算系数 - if (calculate_current_mode_coefficients(sample_freq, mode) == 0) { - debug_printf("Successfully initialized mode %d for sample rate %d Hz\n", mode, sample_freq); - } else { - debug_printf("Warning: Failed to calculate coefficients for mode %d\n", mode); - } - } else { - debug_printf("Warning: Failed to copy params for mode %d\n", mode); - } + calculate_current_mode_coefficients(sample_freq, mode); } - debug_printf("EQ initialization completed for sample rate %d Hz\n", sample_freq); - return 1; } void update_eq_post_gain(void) { for (int mode = EQ_USER_MODE_MIN; mode <= EQ_USER_MODE_MAX; mode++) { - // 改动原因:限制gain范围到0到-50dB,确保从g_mode_info读取的gain值在有效范围内 int32_t gain = clamp_post_gain(g_mode_info.mode_gains[mode]); if (gain != g_mode_info.mode_gains[mode]) { - debug_printf("Warning: mode %d gain %d clamped to %d in update_eq_post_gain\n", mode, g_mode_info.mode_gains[mode], gain); - g_mode_info.mode_gains[mode] = gain; // 更新存储的值 + g_mode_info.mode_gains[mode] = gain; } - // 改动原因:所有采样率共用44100Hz数组,直接更新即可 if (sEQ_data_44100HZ[mode][0].post_gain_db != gain) { sEQ_data_44100HZ[mode][0].post_gain_db = gain; sEQ_data_44100HZ[mode][1].post_gain_db = gain; @@ -619,7 +282,6 @@ unsigned int is_mode_changed(void) { if ((g_current_eq_mode != g_new_eq_mode) || (g_force_eq_mode_change == 1)) { - //debug_printf("request mode changed from %d to %d force_mode %d\n", g_new_eq_mode, g_current_eq_mode, g_force_eq_mode_change); g_force_eq_mode_change = 0; return 1; } @@ -629,46 +291,30 @@ unsigned int is_mode_changed(void) void change_eq_mode(unsigned sample_freq) { - if (calculate_current_mode_coefficients(sample_freq, g_new_eq_mode) == 0) { - debug_printf("Successfully change eq mode to %d\n", g_new_eq_mode); - } else { - debug_printf("Warning: Failed to change eq mode to %d\n", g_new_eq_mode); - } + calculate_current_mode_coefficients(sample_freq, g_new_eq_mode); g_current_eq_mode = g_new_eq_mode; } unsigned int is_eq_disabled(void) { - // 改动原因:将eq_enable独立为全局开关,所有模式共用一个开关 - // 直接检查g_eq_enable,不再依赖模式判断 return (g_eq_enable == 0); } void clear_eq_status(unsigned sample_freq, unsigned ch_no) { - int n; - debug_printf("clear_eq_status %d %d\n", sample_freq, ch_no); if(is_eq_disabled()) { return; } - // 改动原因:所有采样率共用44100Hz数组 - eq_mode_data_t (*eq_data)[NUM_EQ_MODES][NUM_EQ_CHANS] = &sEQ_data_44100HZ; - // Clear status for the specified sample rate, mode, and channel - for (n = 0; n < MAX_EQ_BANDS + 1; n++) { - (*eq_data)[g_current_eq_mode][ch_no].filter.state[0][n] = 0; - (*eq_data)[g_current_eq_mode][ch_no].filter.state[1][n] = 0; - } + memset(sEQ_data_44100HZ[g_current_eq_mode][ch_no].filter.state, 0, + sizeof(sEQ_data_44100HZ[g_current_eq_mode][ch_no].filter.state)); } -// 复位EQ参数函数 +// reset_eq_params: restore default EQ parameters for given mode uint8_t reset_eq_params(uint8_t mode) { - debug_printf("Resetting EQ params for mode %d\n", mode); - // 检查模式号有效性 if (mode != 0xFF && mode >= NUM_EQ_MODES) { - debug_printf("Error: Invalid mode %d\n", mode); return 0x01; // 失败 } @@ -676,7 +322,6 @@ uint8_t reset_eq_params(uint8_t mode) { delete_flash_eq_params(mode); // 删除Flash中的增益和模式名称文件(只删除相应模式的数据) - // 改动原因:修改为按模式删除,只删除指定模式的增益和名称,而不是删除所有文件 delete_flash_gain_and_names(mode); // 恢复头文件预设参数 @@ -685,55 +330,9 @@ uint8_t reset_eq_params(uint8_t mode) { // 恢复默认模式信息 init_mode_info(); - debug_printf("EQ params reset successful for mode %d\n", mode); return 0x00; // 成功 } -// 数据修复函数 - 修复损坏的EQ数据 -static void repair_eq_data(eq_mode_data_t *eq_data) { - if (eq_data == NULL) return; - - // 修复bands数组 - for (int i = 0; i < MAX_EQ_BANDS; i++) { - filter_params_t *band = &eq_data->bands[i]; - - // 检查并修复float字段 - if (isnan(band->fc) || isinf(band->fc) || band->fc < 0 || band->fc > 100000) { - band->fc = 1000.0f; - } - if (isnan(band->q) || isinf(band->q) || band->q < 0 || band->q > 100) { - band->q = 0.707f; - } - if (isnan(band->bw) || isinf(band->bw) || band->bw < 0 || band->bw > 100) { - band->bw = 1.0f; - } - if (isnan(band->gain) || isinf(band->gain) || band->gain < -100 || band->gain > 100) { - band->gain = 0.0f; - } - - // 修复索引和类型 - band->index = i; - if (band->type > FILTER_TYPE_HIGHSHELF) { - band->type = FILTER_TYPE_BYPASS; - } - } - - // 修复其他字段 - if (eq_data->sample_rate == 0 || eq_data->sample_rate > 200000) { - eq_data->sample_rate = 48000; // 默认采样率 - } - - // 修复total_bshift和post_gain_db - if (eq_data->total_bshift < -32 || eq_data->total_bshift > 32) { - eq_data->total_bshift = 0; - } - // 改动原因:修改post_gain_db范围检查为0到-50dB,与gain范围限制保持一致 - if (eq_data->post_gain_db > 0 || eq_data->post_gain_db < -50) { - debug_printf("Warning: post_gain_db %d out of range (0 to -50dB), clamping\n", eq_data->post_gain_db); - eq_data->post_gain_db = clamp_post_gain(eq_data->post_gain_db); - } -} - int32_t handler_eq_filter(unsigned sample_freq, uint32_t ch, int32_t new_sample) { int32_t temp_sample = new_sample; @@ -745,33 +344,21 @@ int32_t handler_eq_filter(unsigned sample_freq, uint32_t ch, int32_t new_sample) if (ch < NUM_EQ_CHANS) { - // 改动原因:所有采样率共用44100Hz数组 - eq_mode_data_t (*eq_data)[NUM_EQ_MODES][NUM_EQ_CHANS] = &sEQ_data_44100HZ; + eq_mode_data_t *eq = &sEQ_data_44100HZ[g_current_eq_mode][ch]; // 应用增益或衰减 (Q24格式) - // 改动原因:修改post_gain_db范围检查为0到-50dB,确保只处理有效范围内的gain值 - int32_t post_gain = (*eq_data)[g_current_eq_mode][ch].post_gain_db; + int32_t post_gain = eq->post_gain_db; if (post_gain <= 0 && post_gain >= -50) { - DPRINTF("post_gain_db: %d\n", post_gain); temp_sample = apply_gain(temp_sample, post_gain); - } else if (post_gain != 0) { - // 如果gain超出范围,记录警告但不应用 - debug_printf("Warning: post_gain_db %d out of range (0 to -50dB), skipping gain application\n", post_gain); - } - - static unsigned int count = 0; - count++; - if (count % 100000 == 0) { - debug_printf("post_gain: %d\n", post_gain); } temp_sample = temp_sample >> 1; //Q30 // 应用均衡器滤波 - temp_sample = filter_biquads_s32(&(*eq_data)[g_current_eq_mode][ch].filter, 1, temp_sample); + temp_sample = filter_biquads_s32(&eq->filter, 1, temp_sample); // 应用bshift补偿 - unsigned int bshift = (*eq_data)[g_current_eq_mode][ch].total_bshift; + unsigned int bshift = eq->total_bshift; // Q2.30 -> Q31,溢出保护 if (temp_sample > (INT32_MAX >> (bshift + 1))) { @@ -794,7 +381,6 @@ int32_t handler_eq_filter(unsigned sample_freq, uint32_t ch, int32_t new_sample) unsigned key_program = 0; -extern uint32_t get_reference_time(void); extern unsigned short XUA_Endpoint0_getProductId(); extern unsigned short XUA_Endpoint0_getVendorId(); extern char* XUA_Endpoint0_getProductStr(); @@ -803,13 +389,16 @@ extern char* XUA_Endpoint0_getSerialStr(); extern unsigned g_request_volume_set; void get_device_info(unsigned char *response) { + unsigned short pid = XUA_Endpoint0_getProductId(); + unsigned short vid = XUA_Endpoint0_getVendorId(); + // 产品ID (1字节,小端序,位置3-4) - response[2] = (uint8_t)(XUA_Endpoint0_getProductId() & 0xFF); - response[3] = (uint8_t)((XUA_Endpoint0_getProductId() >> 8) & 0xFF); + response[2] = (uint8_t)(pid & 0xFF); + response[3] = (uint8_t)((pid >> 8) & 0xFF); // 厂商ID (1字节,小端序,位置5-6) - response[4] = (uint8_t)(XUA_Endpoint0_getVendorId() & 0xFF); - response[5] = (uint8_t)((XUA_Endpoint0_getVendorId() >> 8) & 0xFF); + response[4] = (uint8_t)(vid & 0xFF); + response[5] = (uint8_t)((vid >> 8) & 0xFF); // 产品字符串 (15字节,UTF-8编码,位置7-22) memcpy(&response[6], XUA_Endpoint0_getProductStr(), 16); @@ -819,29 +408,59 @@ void get_device_info(unsigned char *response) { // 序列号字符串 (15字节,UTF-8编码,位置39-54) memcpy(&response[38], XUA_Endpoint0_getSerialStr(), 16); - - debug_printf("Device info - PID: -1x%04x, VID: 0x%04x, Product: '%s', Vendor: '%s', Serial: '%s'\n", - XUA_Endpoint0_getProductId(), XUA_Endpoint0_getVendorId(), - XUA_Endpoint0_getProductStr(), XUA_Endpoint0_getVendorStr(), XUA_Endpoint0_getSerialStr()); } +#if EQ_EN +// Shared EQ band param update logic — called from 0x8D HID handler and UART handler +static bool apply_eq_band_params(uint8_t mode, uint8_t band_index, uint8_t filter_type, + float fc, float q, float bw, float gain) +{ + if (isnan(fc) || isnan(q) || isnan(bw) || isnan(gain) || fc == 0) { + return false; + } + + filter_params_t new_params; + memset(&new_params, 0, sizeof(filter_params_t)); + new_params.index = band_index; + new_params.type = (filter_type_t)filter_type; + new_params.fc = fc; + new_params.q = q; + new_params.bw = bw; + new_params.gain = gain; + + filter_params_t *band_44100 = &sEQ_data_44100HZ[mode][0].bands[band_index]; + + if (eq_single_params_different(band_44100, &new_params)) { + *band_44100 = new_params; + memcpy(&sEQ_data_44100HZ[mode][1].bands[band_index], band_44100, sizeof(filter_params_t)); + + if (g_eq_enable == 1) { + SET_SHARED_GLOBAL(g_force_request_eq_mode_change, 1); + } + + uint32_t all_sample_rates[] = {48000, 88200, 96000, 176400, 192000}; + for (int i = 0; i < (int)(sizeof(all_sample_rates)/sizeof(all_sample_rates[0])); i++) { + calculate_current_mode_coefficients(all_sample_rates[i], mode); + } + calculate_current_mode_coefficients(44100, mode); + + eq_process_single_param_change(44100, mode, 0, band_index, band_44100); + eq_flash_mark_dirty(mode); + } + + return true; +} +#endif + void program_key(unsigned char *, signed int); -// 改动原因:声明外部EX3D命令处理函数,该函数在dsp.c中定义 #if (USE_EX3D == 1) && (HID_CONTROLS == 1) extern void hid_receive_task_in_c(unsigned char * RcvData, unsigned * SendData); #endif unsigned char process_send_params(uint8_t data[], uint16_t len) { - #if 0 - for (int i = 0; i < len; i++) { - debug_printf("data[%d]: %02x\n", i, data[i]); - } - debug_printf("len: %d\n", len); - #endif if (len < 60) { - debug_printf("Error: Data length is not 40 bytes, actual length=%d\n", len); return false; } @@ -854,20 +473,16 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { #if EQ_EN // Check sync header if (data[0] != 0x77) { - debug_printf("Error: Invalid sync header 1: 0x%02x\n", data[0]); return false; } // Process read request (0x8E) - GET_EQ_PARAMS if (data[1] == 0x8E) { - debug_printf("Received read request (GET_EQ_PARAMS)\n"); // Get mode value read_request.mode = data[2]; // Get EQ index read_request.eq_index = data[3]; - read_request.is_read_request = true; - - debug_printf("Read request - Mode: %d, EQ index: %d\n", read_request.mode, read_request.eq_index); + read_request.pending_cmd = 0x8E; } if (data[1] == 0x8a) @@ -875,44 +490,29 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { if (data[2] > EQ_MODE_MAX ) { return false; } - // 改动原因:0x8A只改变当前模式(临时),不保存到Flash,掉电后会丢失 - // eq_enable是全局开关,与模式切换独立,可以在任何enable状态下切换模式 SET_SHARED_GLOBAL(g_request_eq_mode, data[2]); - debug_printf("Received set mode command (0x8A), switching to mode %d (temporary, not saved to Flash)\n", data[2]); - debug_printf("EQ enable state: %d (independent of mode), saved startup mode: %d (set by 0x92)\n", - g_eq_enable, g_saved_eq_mode); return true; } // 处理读取EQ模式信息命令 (0x8B) - GET_EQ_MODE - // 改动原因:添加mode参数支持,0xFF表示获取当前模式,0-9表示获取指定模式信息 if (data[1] == 0x8b) { uint8_t mode = data[2]; - debug_printf("Received read EQ mode command (GET_EQ_MODE), mode: 0x%02x\n", mode); // 验证mode参数:0xFF表示获取当前模式,0-9表示获取指定模式 if (mode == 0xFF) { - // 获取当前模式信息 - debug_printf("Mode=0xFF, will return current mode info\n"); - read_request.is_read_mode_request = true; + read_request.pending_cmd = 0x8B; read_request.read_mode_request_mode = 0xFF; } else if (mode <= EQ_MODE_MAX) { // 获取指定模式信息 - debug_printf("Mode=%d, will return specified mode info\n", mode); - read_request.is_read_mode_request = true; + read_request.pending_cmd = 0x8B; read_request.read_mode_request_mode = mode; } else { - debug_printf("Error: Invalid mode value 0x%02x (valid: 0-9 or 0xFF)\n", mode); return false; } return true; } // 处理设置EQ整体增益和名称命令 (0x8C) - SET_MODE_GAIN_AND_NAME if (data[1] == 0x8c) { - eq_mode_data_t *eq_data = NULL; - eq_mode_data_t *eq_data_2 = NULL; - debug_printf("Received set EQ overall gain and name command (SET_MODE_GAIN_AND_NAME)\n"); - // 获取模式值 uint8_t mode = data[2]; @@ -922,80 +522,38 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { // 获取增益值 (4字节,范围0到-50dB) uint32_t gain = (uint32_t)data[3] | ((uint32_t)data[4] << 8) | ((uint32_t)data[5] << 16) | ((uint32_t)data[6] << 24); - // 改动原因:限制gain范围到0到-50dB,防止超出范围的值导致异常 int32_t new_gain = clamp_post_gain((int32_t)gain); - if (new_gain != (int32_t)gain) { - debug_printf("Gain value %d clamped to %d (range: 0 to -50dB)\n", (int32_t)gain, new_gain); - } // 获取模式名称 (16字节,UTF-8编码) char mode_name[17] = {0}; // 16字节 + 1个null终止符 memcpy(mode_name, &data[7], 16); mode_name[16] = '\0'; // 确保null终止 - debug_printf("EQ overall gain: %d dB, mode name: '%s' for mode %d\n", new_gain, mode_name, mode); - // 获取44100采样率下该模式的当前gain值,判断是否有变化 int32_t current_gain_44100 = sEQ_data_44100HZ[mode][0].post_gain_db; bool gain_changed = (current_gain_44100 != new_gain); - if (gain_changed) { - debug_printf("Gain changed from %d to %d, will mark dirty\n", current_gain_44100, new_gain); - } else { - debug_printf("Gain unchanged (%d), skip marking dirty\n", current_gain_44100); - } - // 获取当前模式名称,判断是否有变化 char current_name[16] = {0}; strncpy(current_name, g_mode_info.mode_names[mode], 15); current_name[15] = '\0'; - // 检查新名称是否全空(全0或全空格) - bool name_is_empty = true; - if (mode_name[0] != '\0') { - // 检查是否全是空格 - for (int i = 0; i < 16 && mode_name[i] != '\0'; i++) { - if (mode_name[i] != ' ') { - name_is_empty = false; - break; - } - } - } - // 如果第一个字符就是\0,name_is_empty保持为true - // 比较名称是否有变化 bool name_changed = (strncmp(current_name, mode_name, 15) != 0); - if (name_is_empty) { - debug_printf("Mode name is empty, skip saving\n"); - } else if (name_changed) { - debug_printf("Name changed from '%s' to '%s', will mark dirty\n", current_name, mode_name); - } else { - debug_printf("Name unchanged ('%s'), skip marking dirty\n", current_name); - } - - - if (!name_is_empty && name_changed) { + if (!is_name_empty(mode_name, 16) && name_changed) { set_mode_name(mode, mode_name); - eq_mark_name_dirty(); // 只有变化且非空时才标记脏 - debug_printf("Marked mode %d as dirty for Flash sync (name update)\n", mode); + eq_mark_name_dirty(); } if (gain_changed) { - // 设置EQ整体增益到所有采样率的指定模式 - uint32_t all_sample_rates[] = {44100, 48000, 88200, 96000, 176400, 192000}; - int num_rates = sizeof(all_sample_rates) / sizeof(all_sample_rates[0]); - - // 改动原因:所有采样率共用44100Hz数组,直接更新即可 if (mode < NUM_EQ_MODES) { sEQ_data_44100HZ[mode][0].post_gain_db = new_gain; sEQ_data_44100HZ[mode][1].post_gain_db = new_gain; - debug_printf("Set EQ overall gain to %d dB for mode %d (applied to all sample rates)\n", new_gain, mode); } set_mode_gain(mode, new_gain); - eq_mark_gain_dirty(); // 只有变化时才标记脏 - debug_printf("Marked mode %d as dirty for Flash sync (gain update)\n", mode); + eq_mark_gain_dirty(); } return true; @@ -1003,27 +561,19 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { // 处理获取设备信息命令 (0x8F) - GET_DEVICE_INFO if (data[1] == 0x8f) { - debug_printf("Received get device info command (GET_DEVICE_INFO)\n"); - // 设置读取请求,返回设备信息 - read_request.is_read_device_info_request = true; + read_request.pending_cmd = 0x8F; return true; } // 处理复位EQ参数命令 (0x90) - RESET_EQ_PARAMS if (data[1] == 0x90) { - debug_printf("Received reset EQ params command (RESET_EQ_PARAMS)\n"); - // 获取模式号 uint8_t mode = data[2]; - debug_printf("Reset mode: %d (0xFF = all modes)\n", mode); - // 立即执行复位操作并保存状态 - // 改动原因:复位操作应该在收到命令时立即执行,而不是在读取响应时执行 - // 这样可以避免在读取响应时重复执行耗时操作,提高响应效率 reset_eq_params(mode); // 设置读取请求标志,读取时只返回保存的状态 - read_request.is_reset_eq_params_request = true; + read_request.pending_cmd = 0x90; read_request.mode = mode; device_reboot(); @@ -1034,29 +584,24 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { if (data[1] == 0x91) { - debug_printf("Received get EQ mode count command (GET_EQ_MODE_COUNT)\n"); // 设置读取请求,返回EQ模式总数 - read_request.is_read_mode_count_request = true; + read_request.pending_cmd = 0x91; return true; } // 处理设置并保存EQ模式命令 (0x92) - SET_AND_SAVE_EQ_MODE if (data[1] == 0x92) { - debug_printf("Received set and save EQ mode command (SET_AND_SAVE_EQ_MODE)\n"); - read_request.is_set_and_save_mode_request = true; + read_request.pending_cmd = 0x92; // 获取模式值 uint8_t mode = data[2]; // 验证模式值范围 if (mode > EQ_MODE_MAX ) { - debug_printf("Error: Invalid mode %d (valid range: 0-9)\n", mode); read_request.mode = 0xFF; // 使用0xFF表示失败 return true; } - debug_printf("Setting and saving EQ mode to %d\n", mode); - // 设置当前模式 SET_SHARED_GLOBAL(g_request_eq_mode, mode); @@ -1065,24 +610,18 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { // 保存到Flash(只保存模式,不影响使能状态) eq_flash_save_current_mode(mode); - debug_printf("Successfully saved EQ mode %d to Flash (startup mode)\n", mode); read_request.mode = mode; return true; } // 处理设置音量命令 (0x93) - SET_VOLUME - // 改动原因:在EQ命令中添加设置音量功能,参照HID_DEVICE_PARAMS_PROTOCOL.md中的音量命令实现 if (data[1] == 0x93) { - debug_printf("Received set volume command (SET_VOLUME)\n"); // 获取音量级别 uint8_t volume_level = data[2]; - debug_printf("Setting volume level to %d\n", volume_level); - // 参数验证 if (volume_level > 60) { - debug_printf("Error: Invalid volume_level %d (must be 0-60)\n", volume_level); return false; } @@ -1091,123 +630,91 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { g_volume_level = volume_level; g_request_volume_set = 1; - debug_printf("Volume level set successfully to %d\n", volume_level); return true; } // 处理读取音量命令 (0x94) - GET_VOLUME - // 改动原因:在EQ命令中添加读取音量功能,参照HID_DEVICE_PARAMS_PROTOCOL.md中的音量命令实现 if (data[1] == 0x94) { - debug_printf("Received get volume command (GET_VOLUME)\n"); - // 设置读取请求标志 - read_request.is_read_volume_request = true; + read_request.pending_cmd = 0x94; return true; } // 处理获取LED信息命令 (0x95) - GET_LED_INFO - // 改动原因:添加LED信息查询命令,返回LED索引和名称 if (data[1] == 0x95) { - debug_printf("Received get LED info command (GET_LED_INFO)\n"); uint8_t led_index = data[2]; if (led_index > 7) { - debug_printf("Error: Invalid LED index %d (must be 0-7)\n", led_index); return false; } - led_request.is_get_led_info_request = true; + led_request.pending_cmd = 0x95; led_request.led_index = led_index; return true; } // 处理设置LED开关命令 (0x96) - SET_LED_SWITCH - // 改动原因:添加LED开关设置命令,控制LED是否显示 if (data[1] == 0x96) { - debug_printf("Received set LED switch command (SET_LED_SWITCH)\n"); uint8_t led_index = data[2]; uint8_t led_switch = data[3]; if (led_index > 7) { - debug_printf("Error: Invalid LED index %d (must be 0-7)\n", led_index); return false; } if (led_switch > 1) { - debug_printf("Error: Invalid LED switch value %d (must be 0-1)\n", led_switch); return false; } // 设置LED开关全局变量 - extern unsigned g_led_enable; - g_led_enable = led_switch; SET_SHARED_GLOBAL(g_led_enable, led_switch); - debug_printf("LED switch set to: %s (index: %d)\n", led_switch ? "ON" : "OFF", led_index); - // 设置SET命令的响应标志 - // 改动原因:SET_LED_SWITCH的响应格式与GET_LED_SWITCH不同,返回状态码 - led_request.is_set_led_switch_request = true; + led_request.pending_cmd = 0x96; led_request.led_index = led_index; return true; } // 处理获取LED开关命令 (0x97) - GET_LED_SWITCH - // 改动原因:添加LED开关查询命令,返回当前LED开关状态 if (data[1] == 0x97) { - debug_printf("Received get LED switch command (GET_LED_SWITCH)\n"); uint8_t led_index = data[2]; if (led_index > 7) { - debug_printf("Error: Invalid LED index %d (must be 0-7)\n", led_index); return false; } - led_request.is_get_led_switch_request = true; + led_request.pending_cmd = 0x97; led_request.led_index = led_index; return true; } // 处理获取LED状态命令 (0x98) - GET_LED_STATUS - // 改动原因:添加LED状态查询命令,返回LED RGB颜色和状态 if (data[1] == 0x98) { - debug_printf("Received get LED status command (GET_LED_STATUS)\n"); uint8_t led_index = data[2]; if (led_index > 7) { - debug_printf("Error: Invalid LED index %d (must be 0-7)\n", led_index); return false; } - led_request.is_get_led_status_request = true; + led_request.pending_cmd = 0x98; led_request.led_index = led_index; return true; } // 处理获取LED总数命令 (0x99) - GET_LED_COUNT - // 改动原因:添加LED总数查询命令,返回设备支持的LED总数 if (data[1] == 0x99) { - debug_printf("Received get LED count command (GET_LED_COUNT)\n"); - led_request.is_get_led_count_request = true; + led_request.pending_cmd = 0x99; return true; } // 处理获取UAC模式信息命令 (0x9A) - GET_UAC_MODE_INFO - // 改动原因:添加UAC模式信息查询命令,返回UAC模式总数和模式名称列表 if (data[1] == 0x9A) { - debug_printf("Received get UAC mode info command (GET_UAC_MODE_INFO)\n"); - uac_request.is_get_uac_mode_info_request = true; + uac_request.pending_cmd = 0x9A; return true; } // 处理设置UAC模式命令 (0x9B) - SET_UAC_MODE - // 改动原因:添加UAC模式设置命令,支持切换到UAC1.0或UAC2.0 if (data[1] == 0x9B) { - debug_printf("Received set UAC mode command (SET_UAC_MODE)\n"); - // 获取UAC模式值 uint8_t uac_mode = data[2]; // 验证模式值范围 if (uac_mode > 1) { - debug_printf("Error: Invalid UAC mode %d (must be 0-1, 0=UAC2.0, 1=UAC1.0)\n", uac_mode); return true; } - debug_printf("Setting UAC mode to: %s\n", uac_mode == 0 ? "UAC2.0" : "UAC1.0"); - // 读取当前UAC模式 extern unsigned g_uac_mode; unsigned current_uac_mode; @@ -1215,16 +722,12 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { // 如果目标模式与当前模式相同,无需操作 if ((uac_mode == 0 && current_uac_mode == 2) || (uac_mode == 1 && current_uac_mode == 1)) { - debug_printf("UAC mode is already %s, no action needed\n", uac_mode == 0 ? "UAC2.0" : "UAC1.0"); - // 改动原因:SET_UAC_MODE命令不返回响应,直接返回true表示命令已接收 return true; } // 如果设置为UAC1.0,需要reboot到uac1.0 #if (UAC2 == 1) if (uac_mode == 1) { - debug_printf("Switching to UAC1.0 mode, rebooting device...\n"); - // 改动原因:按照audiohw.xc:1157-1165的方式reboot到uac1.0 SetRoleSwitchFlag(UAC1_IN_FLAG); delay_milliseconds(1); device_reboot(); @@ -1241,71 +744,50 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { } #endif - // 改动原因:SET_UAC_MODE命令不返回响应,直接返回true表示命令已接收 return true; } // 处理获取当前UAC模式命令 (0x9C) - GET_CURRENT_UAC_MODE - // 改动原因:添加当前UAC模式查询命令,返回当前UAC模式值和名称 if (data[1] == 0x9C) { - debug_printf("Received get current UAC mode command (GET_CURRENT_UAC_MODE)\n"); - uac_request.is_get_current_uac_mode_request = true; + uac_request.pending_cmd = 0x9C; return true; } // 处理设置EQ使能开关命令 (0x9D) - SET_EQ_ENABLE - // 改动原因:添加EQ使能开关功能,禁用时保存当前模式,启用时恢复之前模式 if (data[1] == 0x9D) { uint8_t enable = data[2]; // 0=OFF禁用, 1=ON启用 - debug_printf("Received set EQ enable command (SET_EQ_ENABLE), enable: %d\n", enable); if (enable > 1) { - debug_printf("Error: Invalid EQ enable value %d (must be 0 or 1)\n", enable); return false; } - // 改动原因:将eq_enable独立为全局开关,所有模式共用一个开关 - // 在任何模式下调整enable,都会影响所有模式,不改变当前模式 g_eq_enable = enable; // 只保存EQ使能状态到Flash,不影响已保存的模式(0x92保存的模式) eq_flash_save_eq_enable(enable); - unsigned int current_mode; - GET_SHARED_GLOBAL(current_mode, g_request_eq_mode); - debug_printf("EQ enable set to %d (global switch, affects all modes), current mode: %d, saved startup mode: %d\n", - enable, current_mode, g_saved_eq_mode); - - read_request.is_set_eq_enable_request = true; - read_request.eq_enable_value = enable; + read_request.pending_cmd = 0x9D; return true; } // 处理获取EQ使能开关命令 (0x9E) - GET_EQ_ENABLE - // 改动原因:添加获取EQ使能开关状态功能 if (data[1] == 0x9E) { - debug_printf("Received get EQ enable command (GET_EQ_ENABLE)\n"); - read_request.is_get_eq_enable_request = true; + read_request.pending_cmd = 0x9E; return true; } // 处理获取采样率和格式命令 (0x9F) - GET_SAMPLE_FORMAT - // 改动原因:添加采样率和格式查询命令,返回当前采样率和DSD模式(已去掉DAC采样分辨率) if (data[1] == 0x9F) { - debug_printf("Received get sample format command (GET_SAMPLE_FORMAT)\n"); - read_request.is_read_sample_format_request = true; + read_request.pending_cmd = 0x9F; return true; } // 处理设置增益模式命令 (0xA0) - SET_GAIN_MODE - // 改动原因:添加独立的增益模式设置命令,使用EQ协议帧头0x77,与设备参数命令分离 if (data[1] == 0xA0) { - debug_printf("Received set gain mode command (SET_GAIN_MODE)\n"); uint8_t gain_mode = data[2]; // 参数验证 if (gain_mode > 1) { - debug_printf("Error: Invalid gain_mode %d (must be 0-1)\n", gain_mode); return false; } @@ -1313,28 +795,21 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { extern unsigned g_request_gain_mode; SET_SHARED_GLOBAL(g_request_gain_mode, gain_mode); - debug_printf("Gain mode set request: %d (0=低阻, 1=高阻)\n", gain_mode); return true; } // 处理获取增益模式命令 (0xA1) - GET_GAIN_MODE - // 改动原因:添加独立的增益模式读取命令,使用EQ协议帧头0x77 if (data[1] == 0xA1) { - debug_printf("Received get gain mode command (GET_GAIN_MODE)\n"); - read_request.is_get_gain_mode_request = true; + read_request.pending_cmd = 0xA1; return true; } // 处理设置滤波器模式命令 (0xA2) - SET_FILTER_MODE - // 改动原因:添加独立的滤波器模式设置命令,使用EQ协议帧头0x77,与设备参数命令分离 if (data[1] == 0xA2) { - debug_printf("Received set filter mode command (SET_FILTER_MODE)\n"); uint8_t filter_mode = data[2]; - // 参数验证 - // 改动原因:支持8种滤波器模式(0-7),与g_filter_map[8]数组定义一致 + // 参数验证:支持8种滤波器模式(0-7) if (filter_mode > 7) { - debug_printf("Error: Invalid filter_mode %d (must be 0-7)\n", filter_mode); return false; } @@ -1342,680 +817,116 @@ unsigned char process_send_params(uint8_t data[], uint16_t len) { extern unsigned g_request_filter_mode; SET_SHARED_GLOBAL(g_request_filter_mode, filter_mode); - debug_printf("Filter mode set request: %d (0-7: 8种滤波器模式)\n", filter_mode); return true; } // 处理获取滤波器模式命令 (0xA3) - GET_FILTER_MODE - // 改动原因:添加独立的滤波器模式读取命令,使用EQ协议帧头0x77 if (data[1] == 0xA3) { - debug_printf("Received get filter mode command (GET_FILTER_MODE)\n"); - read_request.is_get_filter_mode_request = true; + read_request.pending_cmd = 0xA3; return true; } // 处理设置游戏模式命令 (0xA4) - SET_GAME_MODE - // 改动原因:添加独立的游戏模式设置命令,使用EQ协议帧头0x77,与设备参数命令分离 if (data[1] == 0xA4) { - debug_printf("Received set game mode command (SET_GAME_MODE)\n"); uint8_t game_mode = data[2]; // 参数验证 if (game_mode > 2) { - debug_printf("Error: Invalid game_mode %d (must be 0-2)\n", game_mode); return false; } - // 改动原因:在UAC1模式下,调用audiohw.xc中的request_game_mode_switch函数接口 - // 该函数会判断是否需要重启,如果需要则设置重启标志,否则直接设置game_mode请求 extern unsigned g_uac_mode; unsigned uac_mode; GET_SHARED_GLOBAL(uac_mode, g_uac_mode); if (uac_mode == 1) { - // 改动原因:UAC1模式下,使用request_game_mode_switch函数接口,仿照双键长按逻辑 extern void request_game_mode_switch(unsigned game_mode); //request_game_mode_switch(game_mode); - debug_printf("Game mode switch request sent: %d (0=无音效, 1=FPS, 2=虚拟7.1)\n", game_mode); } else { - // 改动原因:UAC2模式下,直接设置game_mode请求(不需要重启) extern unsigned g_request_game_mode; SET_SHARED_GLOBAL(g_request_game_mode, game_mode); - debug_printf("Game mode set request: %d (0=无音效, 1=FPS, 2=虚拟7.1)\n", game_mode); } return true; } // 处理获取游戏模式命令 (0xA5) - GET_GAME_MODE - // 改动原因:添加独立的游戏模式读取命令,使用EQ协议帧头0x77 if (data[1] == 0xA5) { - debug_printf("Received get game mode command (GET_GAME_MODE)\n"); - read_request.is_get_game_mode_request = true; + read_request.pending_cmd = 0xA5; return true; } // 处理获取固件版本命令 (0xA6) - GET_FIRMWARE_VERSION - // 改动原因:添加独立的固件版本读取命令,使用EQ协议帧头0x77,参考user_uart.xc中的FW_VERSION实现 if (data[1] == 0xA6) { - debug_printf("Received get firmware version command (GET_FIRMWARE_VERSION)\n"); - read_request.is_get_firmware_version_request = true; + read_request.pending_cmd = 0xA6; return true; } - // 处理发送EX3D命令 (0xB0) - SET_EX3D_CMD - // 改动原因:添加EX3D命令封装,将EQ协议格式转换为EX3D协议格式并处理 - if (data[1] == 0xB0) { - debug_printf("Received set EX3D command (SET_EX3D_CMD)\n"); + // 处理发送/读取EX3D命令 (0xB0/0xB1) - SET_EX3D_CMD / GET_EX3D_CMD + if (data[1] == 0xB0 || data[1] == 0xB1) { #if (USE_EX3D == 1) && (HID_CONTROLS == 1) // 提取EX3D命令码(4字节,小端序) uint32_t ex3d_cmd = (uint32_t)data[2] | ((uint32_t)data[3] << 8) | ((uint32_t)data[4] << 16) | ((uint32_t)data[5] << 24); - debug_printf("EX3D command code: 0x%08X\n", ex3d_cmd); // 提取参数(从data[6]开始,最多56字节) ex3d_request.ex3d_command_code = ex3d_cmd; ex3d_request.ex3d_params_len = (len > 62) ? 56 : (len - 6); memcpy(ex3d_request.ex3d_params, &data[6], ex3d_request.ex3d_params_len); - ex3d_request.is_set_ex3d_cmd_request = true; + ex3d_request.pending_cmd = data[1]; return true; #else - debug_printf("EX3D support not enabled\n"); - return false; -#endif - } - - // 处理读取EX3D命令 (0xB1) - GET_EX3D_CMD - // 改动原因:添加EX3D读取命令封装,将EQ协议格式转换为EX3D协议格式并处理 - if (data[1] == 0xB1) { - debug_printf("Received get EX3D command (GET_EX3D_CMD)\n"); -#if (USE_EX3D == 1) && (HID_CONTROLS == 1) - // 提取EX3D命令码(4字节,小端序) - uint32_t ex3d_cmd = (uint32_t)data[2] | ((uint32_t)data[3] << 8) | - ((uint32_t)data[4] << 16) | ((uint32_t)data[5] << 24); - debug_printf("EX3D command code: 0x%08X\n", ex3d_cmd); - - // 提取参数(从data[6]开始,最多56字节) - ex3d_request.ex3d_command_code = ex3d_cmd; - ex3d_request.ex3d_params_len = (len > 62) ? 56 : (len - 6); - memcpy(ex3d_request.ex3d_params, &data[6], ex3d_request.ex3d_params_len); - ex3d_request.is_get_ex3d_cmd_request = true; - return true; -#else - debug_printf("EX3D support not enabled\n"); return false; #endif } if (data[1] == 0x8d) { - debug_printf("Received set EQ filter parameters command (SET_EQ_PARAMS)\n"); - - // Get mode value (跳过report_id字节) uint8_t mode = data[2]; - debug_printf("Received mode: %d\n", mode); - - // 检查模式是否允许修改:只允许修改用户模式(6-8),不允许修改预设模式(0-5)和bypass模式(9) - // 改动原因:保护预设参数不被修改,只允许用户修改用户自定义模式参数 if (mode < EQ_USER_MODE_MIN || mode > EQ_USER_MODE_MAX) { - debug_printf("Error: Cannot modify EQ parameters for mode %d. Only user modes (%d-%d) can be modified.\n", - mode, EQ_USER_MODE_MIN, EQ_USER_MODE_MAX); return false; } - - uint8_t band_index = data[3]; // 位置从7移到3 - debug_printf("Received filter index: %d\n", band_index); - - if (band_index >= MAX_EQ_BANDS) - { - debug_printf("Error: Filter index out of range: %d (max: %d)\n", band_index, MAX_EQ_BANDS - 1); + uint8_t band_index = data[3]; + if (band_index >= MAX_EQ_BANDS) { return false; } - - // 改动原因:优化EQ参数存储策略,统一参数源为44100Hz - // 先更新44100Hz的参数(统一参数源),然后通过copy_params_from_44100同步到其他采样率 - // 解析新参数(只解析一次,避免重复解析) - filter_params_t new_params; - memset(&new_params, 0, sizeof(filter_params_t)); - new_params.index = band_index; - new_params.type = (filter_type_t)data[4]; - // 解析浮点参数(小端序) - uint32_t fc_bytes = data[5] | (data[6] << 8) | (data[7] << 16) | (data[8] << 24); - memcpy(&new_params.fc, &fc_bytes, 4); - uint32_t q_bytes = data[9] | (data[10] << 8) | (data[11] << 16) | (data[12] << 24); - memcpy(&new_params.q, &q_bytes, 4); - uint32_t bw_bytes = data[13] | (data[14] << 8) | (data[15] << 16) | (data[16] << 24); - memcpy(&new_params.bw, &bw_bytes, 4); - uint32_t gain_bytes = data[17] | (data[18] << 8) | (data[19] << 16) | (data[20] << 24); - memcpy(&new_params.gain, &gain_bytes, 4); - - DPRINTF("Parsed new params: fc=%.2f, q=%.3f, bw=%.2f, gain=%.2f\n", - new_params.fc, new_params.q, new_params.bw, new_params.gain); - - // 检查参数有效性 - if ((isnan(new_params.fc) || isnan(new_params.q) || isnan(new_params.bw) || isnan(new_params.gain)) || new_params.fc == 0) { - debug_printf("Error: Invalid float values detected!\n"); - return false; - } - - // 只更新44100Hz统一参数源 - eq_mode_data_t *eq_data_44100 = &sEQ_data_44100HZ[mode][0]; - filter_params_t *band_44100 = &eq_data_44100->bands[band_index]; - - // 检查参数是否变化 - if (eq_single_params_different(band_44100, &new_params)) { - debug_printf("Parameter changed for mode %d, band %d, updating 44100Hz source\n", mode, band_index); - - // 更新44100Hz的参数(统一参数源) - band_44100->type = new_params.type; - band_44100->fc = new_params.fc; - band_44100->q = new_params.q; - band_44100->bw = new_params.bw; - band_44100->gain = new_params.gain; - - // 同步到第二个通道 - eq_mode_data_t *eq_data_44100_ch1 = &sEQ_data_44100HZ[mode][1]; - filter_params_t *band_44100_ch1 = &eq_data_44100_ch1->bands[band_index]; - memcpy(band_44100_ch1, band_44100, sizeof(filter_params_t)); - - // 改动原因:当EQ禁用时,不设置强制模式切换标志,避免实际切换模式 - if (g_eq_enable == 1) { - SET_SHARED_GLOBAL(g_force_request_eq_mode_change, 1); - } else { - debug_printf("EQ disabled: Parameter updated for saved mode %d (not switching current mode)\n", mode); - } - - // 同步参数到所有其他采样率 - uint32_t all_sample_rates[] = {48000, 88200, 96000, 176400, 192000}; - int num_rates = sizeof(all_sample_rates) / sizeof(all_sample_rates[0]); - - for (int rate_idx = 0; rate_idx < num_rates; rate_idx++) { - uint32_t fs = all_sample_rates[rate_idx]; - // 复制参数到目标采样率 - if (copy_params_from_44100(fs, mode) == 0) { - // 计算系数 - calculate_current_mode_coefficients(fs, mode); - debug_printf("Synced params and calculated coefficients for %dHz\n", fs); - } - } - - // 计算44100Hz的系数 - calculate_current_mode_coefficients(44100, mode); - - // 改动原因:标记参数已修改,需要同步到Flash - // 使用eq_process_single_param_change处理参数变化并标记为脏 - if (eq_process_single_param_change(44100, mode, 0, band_index, band_44100) == 0) { - debug_printf("Successfully processed parameter change for 44100Hz channel 0\n"); - } else { - debug_printf("Error processing parameter change for 44100Hz channel 0\n"); - } - - // 标记模式为脏,需要同步到Flash - eq_flash_mark_dirty(mode); - debug_printf("Marked mode %d as dirty for Flash sync\n", mode); - - debug_printf("Parameter update completed for mode %d, band %d\n", mode, band_index); - } else { - debug_printf("Parameter unchanged for mode %d, band %d\n", mode, band_index); - } - - return true; + float fc, q, bw, gain; + uint32_t v; + v = data[5]|(data[6]<<8)|(data[7]<<16)|(data[8]<<24); memcpy(&fc, &v, 4); + v = data[9]|(data[10]<<8)|(data[11]<<16)|(data[12]<<24); memcpy(&q, &v, 4); + v = data[13]|(data[14]<<8)|(data[15]<<16)|(data[16]<<24);memcpy(&bw, &v, 4); + v = data[17]|(data[18]<<8)|(data[19]<<16)|(data[20]<<24);memcpy(&gain, &v, 4); + return apply_eq_band_params(mode, band_index, data[4], fc, q, bw, gain); } -/* Dead code removed - old 0x8d handler was refactored above */ #endif // #if EQ_EN (for process_send_params EQ commands section starting at line 1228) return true; } #if EQ_EN -// ==================== 串口EQ命令处理函数 ==================== - -/** - * @brief 处理串口EQ模式设置 (0x30) - * @param mode EQ模式值 (0-9) - * @return 处理结果 - */ -uint8_t process_uart_set_eq_mode(uint8_t mode) -{ - debug_printf("UART SET_EQ_MODE: mode=%d\n", mode); - - // 验证模式值范围 - if (mode > EQ_MODE_MAX ) { - debug_printf("Invalid EQ mode: %d\n", mode); - return 0x01; // 失败 - } - - // 设置EQ模式 - SET_SHARED_GLOBAL(g_request_eq_mode, mode); - debug_printf("EQ mode set to: %d\n", mode); - - return 0x00; // 成功 -} - -/** - * @brief UART校验和计算函数,直接复用user_func.c的check_sum函数 - */ -#define uart_checksum(pbuf, len) check_sum((unsigned char *)(pbuf), (unsigned)(len)) - -/** - * @brief 处理串口获取EQ模式信息 (0x31) - * @param response 响应数据缓冲区 - * @param response_size 响应缓冲区大小 - * @return 响应数据长度 - */ -uint8_t process_uart_get_eq_mode(uint8_t *response, uint8_t response_size) -{ - if (response_size < 22) { - debug_printf("Response buffer too small\n"); - return 0; - } - - uint8_t current_mode; - int32_t mode_gain = 0; - char mode_name[16] = {0}; - - GET_SHARED_GLOBAL(current_mode, g_request_eq_mode); - - // 获取模式增益和名称 - if (current_mode < NUM_EQ_MODES) { - // 从44100Hz数据中获取模式增益 - eq_mode_data_t (*eq_data)[NUM_EQ_MODES][NUM_EQ_CHANS] = &sEQ_data_44100HZ; - mode_gain = (*eq_data)[current_mode][0].post_gain_db; - - // 从全局模式信息中获取模式名称 - memcpy(mode_name, g_mode_info.mode_names[current_mode], 16); - } - - debug_printf("UART GET_EQ_MODE: mode=%d, gain=%d, name=%s\n", current_mode, mode_gain, mode_name); - - // 构建响应数据 (串口格式: 55 AA 00 41 15 ...) - // 改动原因:使用枚举名称替代数字命令码,提高代码可读性和维护性 - response[0] = 0x55; // 帧头1 - response[1] = 0xAA; // 帧头2 - response[2] = 0x00; // 版本 - response[3] = GET_EQ_MODE; // 命令码 - response[4] = 0x15; // 数据长度 (21字节) - - response[5] = current_mode; // 模式值 - response[6] = (mode_gain >> 0) & 0xFF; // 增益值 (小端序) - response[7] = (mode_gain >> 8) & 0xFF; - response[8] = (mode_gain >> 16) & 0xFF; - response[9] = (mode_gain >> 24) & 0xFF; - - // 复制模式名称 - for (int i = 0; i < 16; i++) { - response[10 + i] = mode_name[i]; - } - - // 计算校验和 - uint8_t checksum_pos = 26; - response[checksum_pos] = uart_checksum(response, checksum_pos); - - return 27; // 总长度 -} - -/** - * @brief 处理串口设置模式增益和名称 (0x32) - * @param data 接收到的数据包 - * @return 处理结果 - */ -uint8_t process_uart_set_mode_gain_and_name(uint8_t *data) -{ - uint8_t mode = data[5]; // 串口数据位置 - int32_t gain = (data[9] << 24) | (data[8] << 16) | (data[7] << 8) | data[6]; // 小端序 - char mode_name[16] = {0}; - bool name_is_empty = true; - bool gain_changed = false; - bool name_changed = false; - - // 复制模式名称 - for (int i = 0; i < 16; i++) { - mode_name[i] = data[10 + i]; - } - - debug_printf("UART SET_MODE_GAIN_AND_NAME: mode=%d, gain=%d, name=%s\n", mode, gain, mode_name); - - // 验证模式值范围 - if (mode > EQ_MODE_MAX ) { - debug_printf("Invalid EQ mode: %d\n", mode); - return 0x01; // 失败 - } - - // 检查模式是否允许修改:只允许修改用户模式(6-8),不允许修改预设模式(0-5)和bypass模式(9) - // 改动原因:保护预设参数不被修改,只允许用户修改用户自定义模式参数 - if (mode < EQ_USER_MODE_MIN || mode > EQ_USER_MODE_MAX) { - debug_printf("Error: Cannot modify EQ gain/name for mode %d. Only user modes (%d-%d) can be modified.\n", - mode, EQ_USER_MODE_MIN, EQ_USER_MODE_MAX); - return 0x01; // 失败 - } - - // 设置模式增益和名称(只处理用户模式) - if (mode < NUM_EQ_MODES) { - // 获取44100采样率下该模式的当前gain值,判断是否有变化 - int32_t current_gain_44100 = sEQ_data_44100HZ[mode][0].post_gain_db; - bool gain_changed = (current_gain_44100 != gain); - - if (gain_changed) { - debug_printf("UART: Gain changed from %d to %d, will mark dirty\n", current_gain_44100, gain); - } else { - debug_printf("UART: Gain unchanged (%d), skip marking dirty\n", current_gain_44100); - } - - // 获取当前模式名称,判断是否有变化 - char current_name[16] = {0}; - strncpy(current_name, g_mode_info.mode_names[mode], 15); - current_name[15] = '\0'; - - // 检查新名称是否全空(全0或全空格) - if (mode_name[0] != '\0') { - // 检查是否全是空格 - for (int i = 0; i < 16 && mode_name[i] != '\0'; i++) { - if (mode_name[i] != ' ') { - name_is_empty = false; - break; - } - } - } - // 如果第一个字符就是\0,name_is_empty保持为true - - // 比较名称是否有变化 - name_changed = (strncmp(current_name, mode_name, 15) != 0); - - if (name_is_empty) { - debug_printf("UART: Mode name is empty, skip saving\n"); - } else if (name_changed) { - debug_printf("UART: Name changed from '%s' to '%s', will mark dirty\n", current_name, mode_name); - } else { - debug_printf("UART: Name unchanged ('%s'), skip marking dirty\n", current_name); - } - - // 设置到所有采样率的EQ数据中 - // 改动原因:限制gain范围到0到-50dB,确保所有post_gain_db都在有效范围内 - int32_t clamped_gain = clamp_post_gain(gain); - if (clamped_gain != gain) { - debug_printf("UART: Gain value %d clamped to %d (range: 0 to -50dB)\n", gain, clamped_gain); - } - uint32_t all_sample_rates[] = {44100, 48000, 88200, 96000, 176400, 192000}; - // 改动原因:所有采样率共用44100Hz数组,直接更新即可 - sEQ_data_44100HZ[mode][0].post_gain_db = clamped_gain; - sEQ_data_44100HZ[mode][1].post_gain_db = clamped_gain; - - // 设置到全局模式信息中(根据判断结果决定是否标记脏标志) - // 改动原因:使用限制后的gain值,确保存储的gain值在有效范围内 - set_mode_gain(mode, clamped_gain); - eq_mark_gain_dirty(); // 只有变化时才标记脏 - } - - if (!name_is_empty && name_changed) { - set_mode_name(mode, mode_name); - eq_mark_name_dirty(); // 只有变化且非空时才标记脏 - } - return 0x00; // 成功 -} - -/** - * @brief 处理串口设置EQ参数 (0x33) - * @param data 接收到的数据包 - * @return 处理结果 - */ -uint8_t process_uart_set_eq_params(uint8_t *data) -{ - uint8_t mode = data[5]; // 模式值 - uint8_t band_index = data[6]; // 滤波器索引 - uint8_t filter_type = data[7]; // 滤波器类型 - float fc, q, bw, gain; - uint8_t result = 0x00; - - // 检查模式是否允许修改:只允许修改用户模式(6-8),不允许修改预设模式(0-5)和bypass模式(9) - // 改动原因:保护预设参数不被UART修改,只允许用户通过UART修改用户自定义模式参数 - if (mode < EQ_USER_MODE_MIN || mode > EQ_USER_MODE_MAX) { - debug_printf("UART Error: Cannot modify EQ parameters for mode %d. Only user modes (%d-%d) can be modified.\n", - mode, EQ_USER_MODE_MIN, EQ_USER_MODE_MAX); - return 0x01; // 失败 - } - // 解析浮点参数 (小端序) - fc = *(float*)(&data[8]); - q = *(float*)(&data[12]); - bw = *(float*)(&data[16]); - gain = *(float*)(&data[20]); - - // 验证参数范围(band_index检查) - if ((mode > (EQ_MODE_MAX - 1)) || (band_index >= MAX_EQ_BANDS)) { - debug_printf("Invalid parameters: mode=%d, band=%d\n", mode, band_index); - return 0x01; // 失败 - } - - // 使用现有的参数处理逻辑 - // 构建临时数据包,调用现有的HID处理函数 - uint8_t temp_data[60]; - temp_data[0] = 0x77; // 同步头 - temp_data[1] = 0x8D; // HID命令码 - temp_data[2] = mode; - temp_data[3] = band_index; - temp_data[4] = filter_type; - - // 复制浮点参数 - memcpy(&temp_data[5], &fc, 4); - memcpy(&temp_data[9], &q, 4); - memcpy(&temp_data[13], &bw, 4); - memcpy(&temp_data[17], &gain, 4); - - // 调用现有的HID处理函数 - if (process_send_params(temp_data, 60) == false) { - result = 0x01; // 失败 - } - - return result; -} - -/** - * @brief 处理串口获取EQ参数 (0x34) - * @param data 请求数据包 - * @param response 响应数据缓冲区 - * @param response_size 响应缓冲区大小 - * @return 响应数据长度 - */ -uint8_t process_uart_get_eq_params(uint8_t *data, uint8_t *response, uint8_t response_size) -{ - if (response_size < 27) { - debug_printf("Response buffer too small\n"); - return 0; - } - - uint8_t mode = data[5]; // 模式值 - uint8_t band_index = data[6]; // EQ索引 - - debug_printf("UART GET_EQ_PARAMS: mode=%d, band=%d\n", mode, band_index); - - // 验证参数范围 - if (mode > (EQ_MODE_MAX - 1) || band_index > 7) { - debug_printf("Invalid parameters: mode=%d, band=%d\n", mode, band_index); - return 0; - } - - // 构建响应数据 (串口格式: 55 AA 00 44 13 ...) - // 改动原因:使用枚举名称替代数字命令码,提高代码可读性和维护性 - response[0] = 0x55; // 帧头1 - response[1] = 0xAA; // 帧头2 - response[2] = 0x00; // 版本 - response[3] = GET_EQ_PARAMS; // 命令码 - response[4] = 0x13; // 数据长度 (19字节: 1字节模式 + 1字节索引 + 1字节类型 + 4个float参数共16字节) - - response[5] = mode; - response[6] = band_index; - - // 从EQ数据中读取参数 - // 选择对应的EQ数据数组 (使用44100Hz,因为串口设置参数时主要存储到44100Hz) - eq_mode_data_t (*eq_data)[NUM_EQ_MODES][NUM_EQ_CHANS] = NULL; - // 优先使用44100Hz数据,因为串口设置参数时主要存储到44100Hz - eq_data = &sEQ_data_44100HZ; - debug_printf("Using 44100Hz data for UART read (g_eq_sample_rate=%d)\n", g_eq_sample_rate); - - if (eq_data != NULL && mode < NUM_EQ_MODES) { - // 复制滤波器参数 - filter_params_t *band = &(*eq_data)[mode][0].bands[band_index]; - response[7] = band->type; - - // 复制浮点参数 - memcpy(&response[8], &band->fc, 4); - memcpy(&response[12], &band->q, 4); - memcpy(&response[16], &band->bw, 4); - memcpy(&response[20], &band->gain, 4); - - } else { - // 返回默认值 - response[7] = 0x00; // 滤波器类型 - float default_fc = 1000.0f; - float default_q = 0.707f; - float default_bw = 1.0f; - float default_gain = 0.0f; - memcpy(&response[8], &default_fc, 4); - memcpy(&response[12], &default_q, 4); - memcpy(&response[16], &default_bw, 4); - memcpy(&response[20], &default_gain, 4); - } - - // 计算校验和 - // 改动原因:数据长度是19字节(0x13),所以校验和位置应该是5(头部) + 19(数据) = 24 - uint8_t checksum_pos = 24; - response[checksum_pos] = uart_checksum(response, checksum_pos); - - return 25; // 总长度:5(头部) + 19(数据) + 1(校验和) = 25 -} - -/** - * @brief 处理串口复位EQ参数 (0x35) - * @param data 接收到的数据包 - * @return 处理结果 - */ -/** - * @brief 处理串口获取EQ模式总数 (0x36) - * @param response 响应缓冲区 - * @param response_size 响应缓冲区大小 - * @return 响应数据长度 - */ -uint8_t process_uart_get_eq_mode_count(uint8_t *response, uint8_t response_size) -{ - if (response_size < 9) { - debug_printf("Response buffer too small\n"); - return 0; - } - - // 模式总数:预定义模式(0-5) + 用户模式(6-8) = 9,不包含禁用模式(10) - uint8_t mode_count = 9; - // 预定义模式数量:0-5共6个预设模式 - uint8_t preset_mode_count = 6; // EQ_PRESET_MODE_MAX + 1 = 5 + 1 = 6 - - debug_printf("UART GET_EQ_MODE_COUNT: count=%d, preset_count=%d\n", mode_count, preset_mode_count); - - // 构建响应数据 (串口格式: 55 AA 00 47 02 ...) - // 改动原因:使用枚举名称替代数字命令码,提高代码可读性和维护性 - response[0] = 0x55; // 帧头1 - response[1] = 0xAA; // 帧头2 - response[2] = 0x00; // 版本 - response[3] = GET_EQ_MODE_COUNT; // 命令码 - response[4] = 0x02; // 数据长度 (2字节) - response[5] = mode_count; // 模式总数 - response[6] = preset_mode_count; // 预定义模式数量 - - // 计算校验和 - uint8_t checksum_pos = 7; - response[checksum_pos] = uart_checksum(response, checksum_pos); - - return 8; // 总长度 -} - -/** - * @brief 处理串口设置并保存EQ模式 (0x37) - * @param data 接收到的数据包 - * @return 状态码 (0x00=成功, 0x01=失败) - */ -uint8_t process_uart_set_and_save_eq_mode(uint8_t *data) -{ - uint8_t mode = data[5]; // 模式值 - uint8_t status = 0x00; // 成功 - - debug_printf("UART SET_AND_SAVE_EQ_MODE: mode=%d\n", mode); - - // 验证模式值范围 - if (mode > EQ_MODE_MAX ) { - debug_printf("Error: Invalid mode %d (valid range: 0-9)\n", mode); - return 0x01; // 失败 - } - - // 设置当前模式 - g_request_eq_mode = mode; - SET_SHARED_GLOBAL(g_request_eq_mode, mode); - - // 保存到Flash - int result = eq_flash_save_current_mode(mode); - if (result != 0) { - debug_printf("Failed to save EQ mode %d to Flash\n", mode); - status = 0x01; // 失败 - } else { - debug_printf("Successfully set and saved EQ mode %d to Flash\n", mode); - } - - return status; -} - - -#define LED_RED 0b01011111 // 修改:清除最高位,只保留低7位 -#define LED_GREEN 0b01101111 // 修改:清除最高位,只保留低7位 -#define LED_BLUE 0b00111111 // 修改:清除最高位,只保留低7位 - -uint8_t process_uart_reset_eq_params(uint8_t *data) -{ - uint8_t mode = data[5]; // 模式号 - uint8_t status = 0x00; // 成功 - - debug_printf("UART RESET_EQ_PARAMS: mode=%d\n", mode); - - // 验证模式值范围 - if (mode > (EQ_MODE_MAX - 1) && mode != 0xFF) { - debug_printf("Invalid EQ mode: %d\n", mode); - status = 0x01; // 失败 - } else { - // 调用复位函数 - uint8_t result = reset_eq_params(mode); - if (result != 0x00) { - status = 0x01; // 失败 - } - } - - return status; -} // 标记增益和模式名称为脏(延迟保存) extern void eq_mark_gain_dirty(void); extern void eq_mark_name_dirty(void); -// 设置模式增益函数 +// set_mode_gain: set gain for specified mode void set_mode_gain(uint8_t mode, int32_t gain) { if (mode >= NUM_EQ_MODES) { - debug_printf("Error: Invalid mode %d\n", mode); return; } - // 改动原因:限制gain范围到0到-50dB,确保存储的gain值在有效范围内 int32_t clamped_gain = clamp_post_gain(gain); - if (clamped_gain != gain) { - debug_printf("Warning: gain %d clamped to %d (range: 0 to -50dB) for mode %d\n", gain, clamped_gain, mode); - } // 设置模式增益 g_mode_info.mode_gains[mode] = clamped_gain; - debug_printf("Set mode %d: gain=%d dB\n", mode, clamped_gain); - // 注意:不在这里自动标记脏标志,由调用者根据实际情况决定是否标记 } -// 设置模式名称函数 +// set_mode_name: set name for specified mode void set_mode_name(uint8_t mode, const char* name) { if (mode >= NUM_EQ_MODES || name == NULL) { - debug_printf("Error: Invalid mode %d or null name\n", mode); return; } @@ -2023,37 +934,29 @@ void set_mode_name(uint8_t mode, const char* name) { strncpy(g_mode_info.mode_names[mode], name, 15); g_mode_info.mode_names[mode][15] = '\0'; // 确保null终止 - debug_printf("Set mode %d: name='%s'\n", mode, name); - // 注意:不在这里自动标记脏标志,由调用者根据实际情况决定是否标记 } -// 获取模式增益函数 +// get_mode_gain: retrieve gain for specified mode void get_mode_gain(uint8_t mode, int32_t* gain) { if (mode >= NUM_EQ_MODES || gain == NULL) { - debug_printf("Error: Invalid parameters for mode %d\n", mode); return; } *gain = g_mode_info.mode_gains[mode]; - - debug_printf("Get mode %d: gain=%d dB\n", mode, *gain); } -// 获取模式名称函数 +// get_mode_name: retrieve name for specified mode void get_mode_name(uint8_t mode, char* name) { if (mode >= NUM_EQ_MODES || name == NULL) { - debug_printf("Error: Invalid parameters for mode %d\n", mode); return; } strncpy(name, g_mode_info.mode_names[mode], 15); name[15] = '\0'; // 确保null终止 - - debug_printf("Get mode %d: name='%s'\n", mode, name); } -// 初始化模式信息函数 +// init_mode_info: initialize mode info with default names and gains void init_mode_info(void) { // 初始化默认模式名称 const char* default_names[NUM_EQ_MODES] = { @@ -2067,48 +970,48 @@ void init_mode_info(void) { strncpy(g_mode_info.mode_names[i], default_names[i], 15); g_mode_info.mode_names[i][15] = '\0'; } - - debug_printf("Mode info initialized with %d modes\n", NUM_EQ_MODES); } #endif void get_key_ret(uint8_t *buffer); -// 改动原因:声明外部全局变量,用于音量和LED状态变化的HID上报 extern unsigned char g_hid_status_report_data[63]; extern unsigned int g_hid_status_report_index; void user_read_hid_status(unsigned char hidPassData[]) { - // 改动原因:只有当g_hid_status_report_index > 0时才读取数据,读取后清除标志 - if (g_hid_status_report_index > 0) - { - for (int i = 0; i < 63; i++) - { - hidPassData[i] = g_hid_status_report_data[i]; - } - // 清除上报标志,表示数据已读取 + if (g_hid_status_report_index > 0) { + memcpy(hidPassData, g_hid_status_report_data, 63); g_hid_status_report_index = 0; - debug_printf("user_read_hid_status: data read, cleared index\n"); - } - else - { - // 如果没有新数据,填充0 - for (int i = 0; i < 63; i++) - { - hidPassData[i] = 0x00; - } + } else { + memset(hidPassData, 0, 63); } } -// Process read parameter request - Get bands and coeffs data from eq_hid_params + +#if (USE_EX3D == 1) && (HID_CONTROLS == 1) +static void ex3d_build_and_call(unsigned char rcv_data[64], unsigned send_data[16]) { + memset(rcv_data, 0, 64); + memset(send_data, 0, sizeof(unsigned) * 16); + rcv_data[0] = 0x01; + unsigned *pRcvBuf = (unsigned *)(rcv_data + 4); + pRcvBuf[0] = ex3d_request.ex3d_command_code; + for (int i = 0; i < ex3d_request.ex3d_params_len / 4 && i < 14; i++) { + pRcvBuf[i + 1] = (uint32_t)ex3d_request.ex3d_params[i * 4] | + ((uint32_t)ex3d_request.ex3d_params[i * 4 + 1] << 8) | + ((uint32_t)ex3d_request.ex3d_params[i * 4 + 2] << 16) | + ((uint32_t)ex3d_request.ex3d_params[i * 4 + 3] << 24); + } + hid_receive_task_in_c(rcv_data, send_data); +} +#endif + +// process_read_params: build HID response from pending request state unsigned char process_read_params(uint8_t response[]) { - debug_printf("Start processing read parameter request\n"); + memset(response, 0, 63); #if EQ_EN // 处理读取当前EQ模式的请求 (0x8B) - GET_EQ_MODE - if (read_request.is_read_mode_request == true) { - debug_printf("Building 0x8B response data:\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x8B; // Sync header 2 + if (read_request.pending_cmd == 0x8B) { + RSP_HDR(response, 0x8B); // 确定要返回的模式值 uint8_t target_mode; @@ -2117,40 +1020,26 @@ unsigned char process_read_params(uint8_t response[]) { unsigned int current_mode; GET_SHARED_GLOBAL(current_mode, g_request_eq_mode); target_mode = current_mode; - debug_printf("Mode=0xFF, returning current mode: %d (EQ enable: %d, independent)\n", target_mode, g_eq_enable); } else { // 获取指定模式 target_mode = read_request.read_mode_request_mode; - debug_printf("Mode=%d, returning specified mode info\n", target_mode); } // 模式值 response[2] = target_mode; - // 改动原因:检查模式是否在有效范围内(0-9),如果是禁用模式(10)或超出范围,返回全0 if (target_mode >= EQ_DISABLED_MODE || target_mode > EQ_MODE_MAX) { - debug_printf("Mode %d is disabled or invalid, returning all zeros\n", target_mode); // 返回全0 memset(&response[3], 0, 20); // 整体增益(4字节) + 模式名称(16字节) = 20字节 } else { // 获取指定模式的整体增益 (从当前采样率获取) int32_t target_post_gain = 0; if (target_mode < NUM_EQ_MODES) { - // 根据当前采样率选择对应的EQ数据数组 - // 改动原因:使用公用数组,通过函数获取对应采样率的数组指针 - eq_mode_data_t (*eq_data)[NUM_EQ_MODES][NUM_EQ_CHANS] = get_eq_array_ptr_common(g_eq_sample_rate); - - if (eq_data != NULL) { - target_post_gain = (*eq_data)[target_mode][0].post_gain_db; - debug_printf("Reading post_gain from mode %d: %d dB\n", target_mode, target_post_gain); - } + target_post_gain = sEQ_data_44100HZ[target_mode][0].post_gain_db; } - // 整体增益 (4字节,位置3-6) - 根据简化协议修正位置 - response[3] = (uint8_t)(target_post_gain & 0xFF); - response[4] = (uint8_t)((target_post_gain >> 8) & 0xFF); - response[5] = (uint8_t)((target_post_gain >> 16) & 0xFF); - response[6] = (uint8_t)((target_post_gain >> 24) & 0xFF); + // 整体增益 (4字节,位置3-6) + memcpy(&response[3], &target_post_gain, 4); // 模式名称 (16字节,UTF-8编码,位置7-22) if (target_mode < NUM_EQ_MODES) { @@ -2161,47 +1050,37 @@ unsigned char process_read_params(uint8_t response[]) { } } - - read_request.is_read_mode_request = false; // Clear read request flag - read_request.read_mode_request_mode = 0; // Clear mode parameter + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理获取设备信息的请求 (0x8F) - GET_DEVICE_INFO - if (read_request.is_read_device_info_request == true) { - debug_printf("Building 0x8F response data:\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x8F; // Sync header 2 + if (read_request.pending_cmd == 0x8F) { + RSP_HDR(response, 0x8F); get_device_info(response); - read_request.is_read_device_info_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理复位EQ参数的请求 (0x90) - RESET_EQ_PARAMS - if (read_request.is_reset_eq_params_request == true) { - debug_printf("Building 0x90 response data:\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x90; // Sync header 2 + if (read_request.pending_cmd == 0x90) { + RSP_HDR(response, 0x90); - // 只返回之前保存的状态,不再执行复位操作 - // 改动原因:复位操作已在收到命令时执行并保存状态,这里只需返回保存的状态码 if (read_request.mode != 0xFF && read_request.mode >= EQ_PRESET_MODE_MIN && read_request.mode <= EQ_USER_MODE_MAX) { response[3] = 0x01; // 失败 } else { response[3] = 0x00; // 成功 } - read_request.is_reset_eq_params_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理获取EQ模式总数的请求 (0x91) - GET_EQ_MODE_COUNT - if (read_request.is_read_mode_count_request == true) { - debug_printf("Building 0x91 response data:\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x91; // Sync header 2 + if (read_request.pending_cmd == 0x91) { + RSP_HDR(response, 0x91); // 模式总数:预定义模式(0-5) + 用户模式(6-8) = 9,不包含禁用模式(10) // 注意:NUM_EQ_MODES = 10,但实际可用模式是0-8共9个(不包含禁用模式10) @@ -2212,38 +1091,24 @@ unsigned char process_read_params(uint8_t response[]) { uint8_t preset_mode_count = EQ_PRESET_MODE_MAX + 1; response[3] = preset_mode_count; - debug_printf("EQ mode count: %d, preset mode count: %d (preset modes 0-5 + user modes 6-8, excluding disabled mode 10)\n", - mode_count, preset_mode_count); - - read_request.is_read_mode_count_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理设置并保存EQ模式的请求 (0x92) - SET_AND_SAVE_EQ_MODE - if (read_request.is_set_and_save_mode_request == true) { - debug_printf("Building 0x92 response data:\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x92; // Sync header 2 + if (read_request.pending_cmd == 0x92) { + RSP_HDR(response, 0x92); // 状态码:0x00=成功, 0x01=失败 uint8_t status = (read_request.mode == 0xFF) ? 0x01 : 0x00; response[2] = status; - if (status == 0x00) { - debug_printf("EQ mode %d set and saved successfully\n", read_request.mode); - } else { - debug_printf("Failed to set and save EQ mode\n"); - } - - read_request.is_set_and_save_mode_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理读取音量请求 (0x94) - GET_VOLUME - // 改动原因:在EQ命令中添加读取音量响应,参照HID_DEVICE_PARAMS_PROTOCOL.md中的音量命令实现 - if (read_request.is_read_volume_request == true) { - debug_printf("Building 0x94 response data (GET_VOLUME):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x94; // Sync header 2 + if (read_request.pending_cmd == 0x94) { + RSP_HDR(response, 0x94); // 读取全局变量(直接读取,因为在C文件中) extern unsigned g_volume_level; @@ -2252,23 +1117,13 @@ unsigned char process_read_params(uint8_t response[]) { // 当前音量级别 (1字节,位置2) response[2] = (uint8_t)volume_level; - debug_printf("Current volume level: %d\n", volume_level); - - // 其余字节填充为0 - for (int i = 3; i < 63; i++) { - response[i] = 0x00; - } - - read_request.is_read_volume_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理获取LED信息请求 (0x95) - GET_LED_INFO - // 改动原因:添加LED信息查询响应,返回LED索引和名称 - if (led_request.is_get_led_info_request == true) { - debug_printf("Building 0x95 response data (GET_LED_INFO):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x95; // Sync header 2 + if (led_request.pending_cmd == 0x95) { + RSP_HDR(response, 0x95); // LED索引 response[2] = led_request.led_index; @@ -2277,98 +1132,57 @@ unsigned char process_read_params(uint8_t response[]) { // 当前设备只有1个LED,索引为0,名称为"Main LED" const char* led_name = "Main LED"; memcpy(&response[3], led_name, strlen(led_name)); - // 剩余字节填充为0 - debug_printf("LED info - Index: %d, Name: %s\n", led_request.led_index, led_name); - - // 其余字节填充为0 - for (int i = 3 + strlen(led_name); i < 63; i++) { - response[i] = 0x00; - } - - led_request.is_get_led_info_request = false; // Clear request flag + led_request.pending_cmd = 0; // Clear request flag return true; } // 处理获取LED开关请求 (0x97) - GET_LED_SWITCH - // 改动原因:添加LED开关查询响应,返回当前LED开关状态 - if (led_request.is_get_led_switch_request == true) { - debug_printf("Building 0x97 response data (GET_LED_SWITCH):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x97; // Sync header 2 + if (led_request.pending_cmd == 0x97) { + RSP_HDR(response, 0x97); // LED索引 response[2] = led_request.led_index; // 读取LED开关全局变量 extern unsigned g_led_enable; - unsigned led_enable = g_led_enable; + unsigned led_enable; GET_SHARED_GLOBAL(led_enable, g_led_enable); // LED开关状态 (0=OFF, 1=ON) response[3] = (uint8_t)led_enable; - debug_printf("LED switch - Index: %d, Switch: %s\n", led_request.led_index, led_enable ? "ON" : "OFF"); - - // 其余字节填充为0 - for (int i = 4; i < 63; i++) { - response[i] = 0x00; - } - - led_request.is_get_led_switch_request = false; // Clear request flag + led_request.pending_cmd = 0; // Clear request flag return true; } // 处理设置LED开关响应 (0x96) - SET_LED_SWITCH响应 - // 改动原因:SET_LED_SWITCH命令需要返回状态码,使用独立的响应处理 - if (led_request.is_set_led_switch_request == true) { - debug_printf("Building 0x96 response data (SET_LED_SWITCH):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x96; // Sync header 2 + if (led_request.pending_cmd == 0x96) { + RSP_HDR(response, 0x96); // 状态码 (0x00=成功, 0x01=失败) // 由于SET命令已经在process_send_params中执行,这里直接返回成功 response[2] = 0x00; // 成功 - debug_printf("LED switch set response - Status: Success\n"); - - // 其余字节填充为0 - for (int i = 3; i < 63; i++) { - response[i] = 0x00; - } - - led_request.is_set_led_switch_request = false; // Clear request flag + led_request.pending_cmd = 0; // Clear request flag return true; } // 处理获取LED总数请求 (0x99) - GET_LED_COUNT - // 改动原因:添加LED总数查询响应,返回设备支持的LED总数 - if (led_request.is_get_led_count_request == true) { - debug_printf("Building 0x99 response data (GET_LED_COUNT):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x99; // Sync header 2 + if (led_request.pending_cmd == 0x99) { + RSP_HDR(response, 0x99); // LED总数(当前设备只有1个LED) uint8_t led_count = 1; response[2] = led_count; - debug_printf("LED count: %d\n", led_count); - - // 其余字节填充为0 - for (int i = 3; i < 63; i++) { - response[i] = 0x00; - } - - led_request.is_get_led_count_request = false; // Clear request flag + led_request.pending_cmd = 0; // Clear request flag return true; } // 处理获取LED状态请求 (0x98) - GET_LED_STATUS - // 改动原因:添加LED状态查询响应,返回LED RGB颜色和状态 - if (led_request.is_get_led_status_request == true) { - debug_printf("Building 0x98 response data (GET_LED_STATUS):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x98; // Sync header 2 + if (led_request.pending_cmd == 0x98) { + RSP_HDR(response, 0x98); // LED索引 response[2] = led_request.led_index; @@ -2379,7 +1193,6 @@ unsigned char process_read_params(uint8_t response[]) { GET_SHARED_GLOBAL(led_color, g_led_color); // 转换LED颜色为RGB值 - // 改动原因:修正RGB转换逻辑,LED颜色是低电平有效,应该检查bit位是否为0 // LED颜色定义:bit 1=0表示红色开启,bit 2=0表示绿色开启,bit 3=0表示蓝色开启 uint8_t r = 0, g = 0, b = 0; @@ -2391,12 +1204,8 @@ unsigned char process_read_params(uint8_t response[]) { response[4] = (uint8_t)g; // G颜色值 response[5] = (uint8_t)b; // B颜色值 - // 获取LED状态(需要在audiohw.xc中声明get_led_status函数) - // 由于get_led_status在XC文件中,这里根据当前状态判断 - extern unsigned g_playback_format, g_audio_type, g_gain_mode, g_uac_mode, g_game_mode; - unsigned playback_format, audio_type, gain_mode, uac_mode, game_mode; - GET_SHARED_GLOBAL(playback_format, g_playback_format); - GET_SHARED_GLOBAL(audio_type, g_audio_type); + extern unsigned g_gain_mode, g_uac_mode, g_game_mode; + unsigned gain_mode, uac_mode, game_mode; GET_SHARED_GLOBAL(gain_mode, g_gain_mode); GET_SHARED_GLOBAL(uac_mode, g_uac_mode); GET_SHARED_GLOBAL(game_mode, g_game_mode); @@ -2428,59 +1237,31 @@ unsigned char process_read_params(uint8_t response[]) { response[6] = led_status; // LED状态 - debug_printf("LED status - Index: %d, RGB: (%d,%d,%d), Status: %d\n", - led_request.led_index, r, g, b, led_status); - - // 其余字节填充为0 - for (int i = 7; i < 63; i++) { - response[i] = 0x00; - } - - led_request.is_get_led_status_request = false; // Clear request flag + led_request.pending_cmd = 0; // Clear request flag return true; } // 处理获取UAC模式信息请求 (0x9A) - GET_UAC_MODE_INFO - // 改动原因:添加UAC模式信息查询响应,返回UAC模式总数和模式名称列表 - if (uac_request.is_get_uac_mode_info_request == true) { - debug_printf("Building 0x9A response data (GET_UAC_MODE_INFO):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x9A; // Sync header 2 + if (uac_request.pending_cmd == 0x9A) { + RSP_HDR(response, 0x9A); // UAC模式总数(当前为2:UAC2.0和UAC1.0) uint8_t uac_mode_count = 2; response[2] = uac_mode_count; - // 模式0名称:UAC2.0 (8字节) - const char* mode0_name = "UAC2.0"; - memcpy(&response[3], mode0_name, strlen(mode0_name)); - // 剩余字节填充为0 + // 模式0名称:UAC2.0 (8字节), 模式1名称:UAC1.0 (8字节) + memcpy(&response[3], "UAC2.0", 6); + memcpy(&response[11], "UAC1.0", 6); - // 模式1名称:UAC1.0 (8字节) - const char* mode1_name = "UAC1.0"; - memcpy(&response[11], mode1_name, strlen(mode1_name)); - // 剩余字节填充为0 - - // 其余字节填充为0 - for (int i = 19; i < 63; i++) { - response[i] = 0x00; - } - - debug_printf("UAC mode info - Count: %d, Mode0: '%s', Mode1: '%s'\n", - uac_mode_count, mode0_name, mode1_name); - - uac_request.is_get_uac_mode_info_request = false; // Clear request flag + uac_request.pending_cmd = 0; // Clear request flag return true; } - // 改动原因:SET_UAC_MODE命令不返回响应,已移除响应处理逻辑 + // SET_UAC_MODE命令不返回响应 // 处理获取当前UAC模式请求 (0x9C) - GET_CURRENT_UAC_MODE - // 改动原因:添加当前UAC模式查询响应,返回当前UAC模式值和名称 - if (uac_request.is_get_current_uac_mode_request == true) { - debug_printf("Building 0x9C response data (GET_CURRENT_UAC_MODE):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x9C; // Sync header 2 + if (uac_request.pending_cmd == 0x9C) { + RSP_HDR(response, 0x9C); // 读取当前UAC模式 extern unsigned g_uac_mode; @@ -2493,28 +1274,15 @@ unsigned char process_read_params(uint8_t response[]) { response[2] = uac_mode_value; // 当前UAC模式名称 (8字节) - const char* current_mode_name = (uac_mode_value == 0) ? "UAC2.0" : "UAC1.0"; - memcpy(&response[3], current_mode_name, strlen(current_mode_name)); - // 剩余字节填充为0 + memcpy(&response[3], (uac_mode_value == 0) ? "UAC2.0" : "UAC1.0", 6); - // 其余字节填充为0 - for (int i = 11; i < 63; i++) { - response[i] = 0x00; - } - - debug_printf("Current UAC mode - Value: %d (%s), Name: '%s'\n", - uac_mode_value, uac_mode_value == 0 ? "UAC2.0" : "UAC1.0", current_mode_name); - - uac_request.is_get_current_uac_mode_request = false; // Clear request flag + uac_request.pending_cmd = 0; // Clear request flag return true; } // 处理设置EQ使能开关请求 (0x9D) - SET_EQ_ENABLE - // 改动原因:0x9D命令只涉及eq_enable的修改,只返回状态码和使能状态,不返回模式信息 - if (read_request.is_set_eq_enable_request == true) { - debug_printf("Building 0x9D response data (SET_EQ_ENABLE):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x9D; // Sync header 2 + if (read_request.pending_cmd == 0x9D) { + RSP_HDR(response, 0x9D); // 状态码:0x00=成功, 0x01=失败 response[2] = 0x00; // 成功 @@ -2522,44 +1290,24 @@ unsigned char process_read_params(uint8_t response[]) { // 当前EQ使能状态(全局开关,所有模式共用) response[3] = g_eq_enable; - // 其余字节填充为0 - for (int i = 4; i < 63; i++) { - response[i] = 0x00; - } - - debug_printf("EQ enable set - Status: 0x00, Enable: %d (global switch)\n", g_eq_enable); - - read_request.is_set_eq_enable_request = false; // Clear request flag + read_request.pending_cmd = 0; // Clear request flag return true; } // 处理获取EQ使能开关请求 (0x9E) - GET_EQ_ENABLE - // 改动原因:0x9E命令只返回eq_enable状态,不返回模式信息,保持命令功能单一 - if (read_request.is_get_eq_enable_request == true) { - debug_printf("Building 0x9E response data (GET_EQ_ENABLE):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x9E; // Sync header 2 + if (read_request.pending_cmd == 0x9E) { + RSP_HDR(response, 0x9E); // EQ使能开关状态(全局开关,所有模式共用) response[2] = g_eq_enable; - // 其余字节填充为0 - for (int i = 3; i < 63; i++) { - response[i] = 0x00; - } - - debug_printf("EQ enable status - Enable: %d (global switch)\n", g_eq_enable); - - read_request.is_get_eq_enable_request = false; // Clear request flag + read_request.pending_cmd = 0; // Clear request flag return true; } // 处理获取采样率和格式请求 (0x9F) - GET_SAMPLE_FORMAT - // 改动原因:添加采样率和格式查询响应,返回当前采样率和DSD模式(已去掉DAC采样分辨率) - if (read_request.is_read_sample_format_request == true) { - debug_printf("Building 0x9F response data (GET_SAMPLE_FORMAT):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0x9F; // Sync header 2 + if (read_request.pending_cmd == 0x9F) { + RSP_HDR(response, 0x9F); // 读取全局变量(使用SHARED_GLOBAL宏) extern unsigned g_samfreq, g_dsd_mode; @@ -2568,32 +1316,18 @@ unsigned char process_read_params(uint8_t response[]) { GET_SHARED_GLOBAL(dsd_mode, g_dsd_mode); // 采样率(32位,小端序,位置2-5) - response[2] = (uint8_t)(samfreq & 0xFF); - response[3] = (uint8_t)((samfreq >> 8) & 0xFF); - response[4] = (uint8_t)((samfreq >> 16) & 0xFF); - response[5] = (uint8_t)((samfreq >> 24) & 0xFF); + memcpy(&response[2], &samfreq, 4); // DSD模式(1字节,位置6) response[6] = (uint8_t)dsd_mode; - debug_printf("Sample format - samFreq: %d, dsdMode: %d\n", - samfreq, dsd_mode); - - // 其余字节填充为0 - for (int i = 7; i < 63; i++) { - response[i] = 0x00; - } - - read_request.is_read_sample_format_request = false; // Clear request flag + read_request.pending_cmd = 0; // Clear request flag return true; } // 处理获取增益模式请求 (0xA1) - GET_GAIN_MODE - // 改动原因:添加获取增益模式响应,返回当前增益模式值 - if (read_request.is_get_gain_mode_request == true) { - debug_printf("Building 0xA1 response data (GET_GAIN_MODE):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0xA1; // Sync header 2 + if (read_request.pending_cmd == 0xA1) { + RSP_HDR(response, 0xA1); // 读取全局变量(使用SHARED_GLOBAL宏) extern unsigned g_gain_mode; @@ -2603,23 +1337,13 @@ unsigned char process_read_params(uint8_t response[]) { // 当前增益模式 (1字节,位置2) response[2] = (uint8_t)gain_mode; - debug_printf("Current gain mode: %d (0=低阻, 1=高阻)\n", gain_mode); - - // 其余字节填充为0 - for (int i = 3; i < 63; i++) { - response[i] = 0x00; - } - - read_request.is_get_gain_mode_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理获取滤波器模式请求 (0xA3) - GET_FILTER_MODE - // 改动原因:添加获取滤波器模式响应,返回当前滤波器模式值 - if (read_request.is_get_filter_mode_request == true) { - debug_printf("Building 0xA3 response data (GET_FILTER_MODE):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0xA3; // Sync header 2 + if (read_request.pending_cmd == 0xA3) { + RSP_HDR(response, 0xA3); // 读取全局变量(使用SHARED_GLOBAL宏) extern unsigned g_filter_mode; @@ -2629,23 +1353,13 @@ unsigned char process_read_params(uint8_t response[]) { // 当前滤波器模式 (1字节,位置2) response[2] = (uint8_t)filter_mode; - debug_printf("Current filter mode: %d (0-6: 7种滤波器模式)\n", filter_mode); - - // 其余字节填充为0 - for (int i = 3; i < 63; i++) { - response[i] = 0x00; - } - - read_request.is_get_filter_mode_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理获取游戏模式请求 (0xA5) - GET_GAME_MODE - // 改动原因:添加获取游戏模式响应,返回当前游戏模式值 - if (read_request.is_get_game_mode_request == true) { - debug_printf("Building 0xA5 response data (GET_GAME_MODE):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0xA5; // Sync header 2 + if (read_request.pending_cmd == 0xA5) { + RSP_HDR(response, 0xA5); // 读取全局变量(使用SHARED_GLOBAL宏) extern unsigned g_game_mode; @@ -2659,80 +1373,36 @@ unsigned char process_read_params(uint8_t response[]) { response[2] = (uint8_t)game_mode; #endif - debug_printf("Current game mode: %d (0=无音效, 1=FPS, 2=虚拟7.1)\n", game_mode); - - // 其余字节填充为0 - for (int i = 3; i < 63; i++) { - response[i] = 0x00; - } - - read_request.is_get_game_mode_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理获取固件版本请求 (0xA6) - GET_FIRMWARE_VERSION - // 改动原因:添加获取固件版本响应,返回BCD格式的版本号(主版本、次版本、修订版本),参考user_uart.xc中的FW_VERSION实现 - if (read_request.is_get_firmware_version_request == true) { - debug_printf("Building 0xA6 response data (GET_FIRMWARE_VERSION):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0xA6; // Sync header 2 + if (read_request.pending_cmd == 0xA6) { + RSP_HDR(response, 0xA6); - // 读取固件版本号(BCD格式),参考user_uart.xc:414-418的实现 - // BCD_DEVICE_J: 主版本号 - // BCD_DEVICE_M: 次版本号 - // BCD_DEVICE_N: 修订版本号 + // 读取固件版本号(BCD格式) response[2] = (uint8_t)BCD_DEVICE_J; // 主版本号 response[3] = (uint8_t)BCD_DEVICE_M; // 次版本号 response[4] = (uint8_t)BCD_DEVICE_N; // 修订版本号 - debug_printf("Firmware version: %d.%d.%d (BCD format: 0x%02X 0x%02X 0x%02X)\n", - BCD_DEVICE_J, BCD_DEVICE_M, BCD_DEVICE_N, - BCD_DEVICE_J, BCD_DEVICE_M, BCD_DEVICE_N); - - // 其余字节填充为0 - for (int i = 5; i < 63; i++) { - response[i] = 0x00; - } - - read_request.is_get_firmware_version_request = false; // Clear read request flag + read_request.pending_cmd = 0; // Clear read request flag return true; } // 处理发送EX3D命令响应 (0xB0) - SET_EX3D_CMD - // 改动原因:添加EX3D SET命令响应处理,调用hid_receive_task_in_c处理命令并返回结果 - if (ex3d_request.is_set_ex3d_cmd_request == true) { - debug_printf("Building 0xB0 response data (SET_EX3D_CMD):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0xB0; // Sync header 2 + if (ex3d_request.pending_cmd == 0xB0) { + RSP_HDR(response, 0xB0); #if (USE_EX3D == 1) && (HID_CONTROLS == 1) - // 构建hid_receive_task_in_c需要的输入格式 - // RcvData格式: [0-3: 保留] [4开始: 32位对齐的数据] - unsigned char rcv_data[64] = {0}; - unsigned send_data[16] = {0}; // hid_receive_task_in_c的输出缓冲区 - - // 设置Report ID(hid_receive_task_in_c期望从RcvData[4]开始) - rcv_data[0] = 0x01; // Report ID - // 从RcvData[4]开始放置数据(即pRcvBuf[0]) - unsigned *pRcvBuf = (unsigned *)(rcv_data + 4); - pRcvBuf[0] = ex3d_request.ex3d_command_code; // EX3D命令码 - - // 复制参数(最多14个uint32) - for (int i = 0; i < ex3d_request.ex3d_params_len / 4 && i < 14; i++) { - pRcvBuf[i + 1] = (uint32_t)ex3d_request.ex3d_params[i * 4] | - ((uint32_t)ex3d_request.ex3d_params[i * 4 + 1] << 8) | - ((uint32_t)ex3d_request.ex3d_params[i * 4 + 2] << 16) | - ((uint32_t)ex3d_request.ex3d_params[i * 4 + 3] << 24); - } - - // 调用EX3D命令处理函数 - hid_receive_task_in_c(rcv_data, send_data); + unsigned char rcv_data[64]; + unsigned send_data[16]; + ex3d_build_and_call(rcv_data, send_data); // 状态码:检查send_data[1]是否有错误返回值(0xFFFFFFFF表示参数错误) uint8_t status = 0x00; // 默认成功 if (send_data[1] == 0xFFFFFFFF) { status = 0x01; // 失败 - debug_printf("EX3D command returned error (0xFFFFFFFF)\n"); } response[2] = status; @@ -2757,51 +1427,22 @@ unsigned char process_read_params(uint8_t response[]) { response[9] = 0xFF; response[10] = 0xFF; } - - // 其余字节填充为0 - for (int i = 11; i < 63; i++) { - response[i] = 0x00; - } #else response[2] = 0x01; // 失败:EX3D支持未启用 - // 其余字节填充为0 - for (int i = 3; i < 63; i++) { - response[i] = 0x00; - } #endif - ex3d_request.is_set_ex3d_cmd_request = false; // Clear request flag + ex3d_request.pending_cmd = 0; // Clear request flag return true; } // 处理读取EX3D命令响应 (0xB1) - GET_EX3D_CMD - // 改动原因:添加EX3D GET命令响应处理,调用hid_receive_task_in_c处理命令并返回结果 - if (ex3d_request.is_get_ex3d_cmd_request == true) { - debug_printf("Building 0xB1 response data (GET_EX3D_CMD):\n"); - response[0] = 0x77; // Sync header 1 - response[1] = 0xB1; // Sync header 2 + if (ex3d_request.pending_cmd == 0xB1) { + RSP_HDR(response, 0xB1); #if (USE_EX3D == 1) && (HID_CONTROLS == 1) - // 构建hid_receive_task_in_c需要的输入格式 - unsigned char rcv_data[64] = {0}; - unsigned send_data[16] = {0}; // hid_receive_task_in_c的输出缓冲区 - - // 设置Report ID - rcv_data[0] = 0x01; // Report ID - // 从RcvData[4]开始放置数据(即pRcvBuf[0]) - unsigned *pRcvBuf = (unsigned *)(rcv_data + 4); - pRcvBuf[0] = ex3d_request.ex3d_command_code; // EX3D命令码 - - // 复制参数(最多14个uint32) - for (int i = 0; i < ex3d_request.ex3d_params_len / 4 && i < 14; i++) { - pRcvBuf[i + 1] = (uint32_t)ex3d_request.ex3d_params[i * 4] | - ((uint32_t)ex3d_request.ex3d_params[i * 4 + 1] << 8) | - ((uint32_t)ex3d_request.ex3d_params[i * 4 + 2] << 16) | - ((uint32_t)ex3d_request.ex3d_params[i * 4 + 3] << 24); - } - - // 调用EX3D命令处理函数 - hid_receive_task_in_c(rcv_data, send_data); + unsigned char rcv_data[64]; + unsigned send_data[16]; + ex3d_build_and_call(rcv_data, send_data); // 回显命令码(4字节) response[2] = (uint8_t)(ex3d_request.ex3d_command_code & 0xFF); @@ -2819,66 +1460,39 @@ unsigned char process_read_params(uint8_t response[]) { response[response_idx++] = (uint8_t)((send_data[i] >> 24) & 0xFF); } } - - // 其余字节填充为0 - while (response_idx < 63) { - response[response_idx++] = 0x00; - } #else // EX3D支持未启用,返回错误 response[2] = 0xFF; response[3] = 0xFF; response[4] = 0xFF; response[5] = 0xFF; - // 其余字节填充为0 - for (int i = 6; i < 63; i++) { - response[i] = 0x00; - } #endif - ex3d_request.is_get_ex3d_cmd_request = false; // Clear request flag + ex3d_request.pending_cmd = 0; // Clear request flag return true; } - if (read_request.is_read_request == true) { - - debug_printf("Read request information:\n"); - debug_printf(" Mode: %d\n", read_request.mode); - debug_printf(" EQ index: %d\n", read_request.eq_index); + if (read_request.pending_cmd == 0x8E) { // 检查EQ索引范围 if (read_request.eq_index >= MAX_EQ_BANDS) { - debug_printf("Error: EQ index out of range: %d (max: %d)\n", read_request.eq_index, MAX_EQ_BANDS - 1); return false; } // Build response data - debug_printf("Building response data: %d\n", g_eq_sample_rate); - response[0] = 0x77; // Sync header 1 - response[1] = 0x8E; // Sync header 2 + RSP_HDR(response, 0x8E); response[2] = read_request.mode; // Mode - debug_printf(" Mode: %d\n", read_request.mode); response[3] = read_request.eq_index & 0xFF; // EQ index - debug_printf("read EQ index: %d\n", read_request.eq_index); - // 改动原因:检查模式是否在有效范围内(0-9),如果是禁用模式(10)或超出范围,返回全0 if (read_request.mode >= EQ_DISABLED_MODE || read_request.mode > EQ_MODE_MAX) { - debug_printf("Mode %d is disabled or invalid, returning all zeros\n", read_request.mode); // 返回全0 response[4] = 0; // Filter type memset(&response[5], 0, 16); // 所有参数(fc, q, bw, gain)都设为0 } else { - // 选择对应的EQ数据数组 (使用44100Hz,因为串口设置参数时主要存储到44100Hz) - eq_mode_data_t (*eq_data)[NUM_EQ_MODES][NUM_EQ_CHANS] = NULL; // 优先使用44100Hz数据,因为串口设置参数时主要存储到44100Hz - eq_data = &sEQ_data_44100HZ; - debug_printf("Using 44100Hz data for HID read (g_eq_sample_rate=%d)\n", g_eq_sample_rate); - - // Copy filter parameters from the correct EQ data array - filter_params_t *band = &(*eq_data)[read_request.mode][0].bands[read_request.eq_index]; + filter_params_t *band = &sEQ_data_44100HZ[read_request.mode][0].bands[read_request.eq_index]; response[4] = band->type; - debug_printf(" Filter type: 0x%02x\n", band->type); // Copy parameters - 直接写入float值 (位置调整) memcpy(&response[5], &band->fc, 4); @@ -2887,19 +1501,8 @@ unsigned char process_read_params(uint8_t response[]) { memcpy(&response[17], &band->gain, 4); } - // 简化版本不包含系数,只包含基本参数 - - #if DEBUG_PRINT_ENABLE == 1 - DPRINTF(" Filter parameters:\n"); - DPRINTF(" fc : %08x Hz\n", band->fc); - DPRINTF(" Q : %08x\n", band->q); - DPRINTF(" bw : %08x Hz\n", band->bw); - DPRINTF(" gain : %08x dB\n", band->gain); - #endif - // Copy coefficients - read_request.is_read_request = false; // Clear read request flag - debug_printf("Parameter read processing completed\n"); + read_request.pending_cmd = 0; // Clear read request flag return true; } @@ -2908,7 +1511,6 @@ unsigned char process_read_params(uint8_t response[]) { if (key_program == 1) { key_program = 0; - debug_printf("Key program completed\n"); get_key_ret(response); return true; }