From 6b3c37263a406a294fb9d4433028acc2dc4a37fb Mon Sep 17 00:00:00 2001 From: wangbeihong Date: Mon, 16 Feb 2026 00:36:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=B2=E5=8F=A3?= =?UTF-8?q?=E5=B1=8F=E6=96=87=E6=9C=AC=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF?= =?UTF-8?q?+=E5=AE=8C=E5=96=84=E6=8C=89=E6=91=A9=E4=BB=AA=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复GBK文本数组多重定义编译错误(拆分.h/.c文件) 2. 修复sizeof计算不完整类型错误(预计算文本长度) 3. 新增按摩仪界面所需的全部中文文本定义 --- CMakeLists.txt | 1 + Core/Inc/gbk_text.h | 145 ++++++------------ Core/Src/freertos.c | 4 + Core/Src/gbk_text.c | 82 ++++++++++ .../UTF8-GBK/utf8_to_gbk.py | 117 +++++++++----- 5 files changed, 212 insertions(+), 137 deletions(-) create mode 100644 Core/Src/gbk_text.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1509fe2..b9ff499 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE Core/Src/easylogger/port/elog_port.c Core/Src/motor_driver.c Core/Src/screen.c + Core/Src/gbk_text.c ) # Add include paths diff --git a/Core/Inc/gbk_text.h b/Core/Inc/gbk_text.h index 208ae86..0b21f1f 100644 --- a/Core/Inc/gbk_text.h +++ b/Core/Inc/gbk_text.h @@ -3,125 +3,70 @@ #include -// 对应文本(UTF8):智能按摩仪 -// 对应GBK编码:0xD6, 0xC7, 0xC4, 0xDC, 0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xC7 -const uint8_t text_device_name_GBK[] = {0xD6, 0xC7, 0xC4, 0xDC, 0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xC7}; -#define text_device_name_LEN (sizeof(text_device_name_GBK)) +// GBK文本数组声明(定义在gbk_text.c中) +extern const uint8_t text_device_name_GBK[]; +extern const uint8_t text_device_loading_GBK[]; +extern const uint8_t text_start_massage_GBK[]; +extern const uint8_t text_start_heating_GBK[]; +extern const uint8_t text_stop_massage_GBK[]; +extern const uint8_t text_stop_heating_GBK[]; +extern const uint8_t text_massage_gear_1_GBK[]; +extern const uint8_t text_massage_gear_2_GBK[]; +extern const uint8_t text_massage_gear_3_GBK[]; +extern const uint8_t text_massage_time_10_GBK[]; +extern const uint8_t text_massage_time_15_GBK[]; +extern const uint8_t text_massage_time_20_GBK[]; +extern const uint8_t text_massage_time_30_GBK[]; +extern const uint8_t text_massage_time_cont_GBK[]; +extern const uint8_t text_remaining_time_GBK[]; +extern const uint8_t text_heating_on_GBK[]; +extern const uint8_t text_heating_off_GBK[]; +extern const uint8_t text_massage_running_GBK[]; +extern const uint8_t text_massage_paused_GBK[]; +extern const uint8_t text_massage_ended_GBK[]; + +// GBK文本长度宏(预计算,不含\0) +#define text_device_name_LEN (10) +#define text_device_loading_LEN (16) +#define text_start_massage_LEN (8) +#define text_start_heating_LEN (8) +#define text_stop_massage_LEN (8) +#define text_stop_heating_LEN (8) +#define text_massage_gear_1_LEN (13) +#define text_massage_gear_2_LEN (13) +#define text_massage_gear_3_LEN (13) +#define text_massage_time_10_LEN (16) +#define text_massage_time_15_LEN (16) +#define text_massage_time_20_LEN (16) +#define text_massage_time_30_LEN (16) +#define text_massage_time_cont_LEN (14) +#define text_remaining_time_LEN (18) +#define text_heating_on_LEN (10) +#define text_heating_off_LEN (10) +#define text_massage_running_LEN (6) +#define text_massage_paused_LEN (10) +#define text_massage_ended_LEN (10) + +// 兼容旧代码的别名 #define text_device_name text_device_name_GBK - -// 对应文本(UTF8):设备加载中请稍候 -// 对应GBK编码:0xC9, 0xE8, 0xB1, 0xB8, 0xBC, 0xD3, 0xD4, 0xD8, 0xD6, 0xD0, 0xC7, 0xEB, 0xC9, 0xD4, 0xBA, 0xF2 -const uint8_t text_device_loading_GBK[] = {0xC9, 0xE8, 0xB1, 0xB8, 0xBC, 0xD3, 0xD4, 0xD8, 0xD6, 0xD0, 0xC7, 0xEB, 0xC9, 0xD4, 0xBA, 0xF2}; -#define text_device_loading_LEN (sizeof(text_device_loading_GBK)) #define text_device_loading text_device_loading_GBK - -// 对应文本(UTF8):开始按摩 -// 对应GBK编码:0xBF, 0xAA, 0xCA, 0xBC, 0xB0, 0xB4, 0xC4, 0xA6 -const uint8_t text_start_massage_GBK[] = {0xBF, 0xAA, 0xCA, 0xBC, 0xB0, 0xB4, 0xC4, 0xA6}; -#define text_start_massage_LEN (sizeof(text_start_massage_GBK)) #define text_start_massage text_start_massage_GBK - -// 对应文本(UTF8):开始加热 -// 对应GBK编码:0xBF, 0xAA, 0xCA, 0xBC, 0xBC, 0xD3, 0xC8, 0xC8 -const uint8_t text_start_heating_GBK[] = {0xBF, 0xAA, 0xCA, 0xBC, 0xBC, 0xD3, 0xC8, 0xC8}; -#define text_start_heating_LEN (sizeof(text_start_heating_GBK)) #define text_start_heating text_start_heating_GBK - -// 对应文本(UTF8):停止按摩 -// 对应GBK编码:0xCD, 0xA3, 0xD6, 0xB9, 0xB0, 0xB4, 0xC4, 0xA6 -const uint8_t text_stop_massage_GBK[] = {0xCD, 0xA3, 0xD6, 0xB9, 0xB0, 0xB4, 0xC4, 0xA6}; -#define text_stop_massage_LEN (sizeof(text_stop_massage_GBK)) #define text_stop_massage text_stop_massage_GBK - -// 对应文本(UTF8):停止加热 -// 对应GBK编码:0xCD, 0xA3, 0xD6, 0xB9, 0xBC, 0xD3, 0xC8, 0xC8 -const uint8_t text_stop_heating_GBK[] = {0xCD, 0xA3, 0xD6, 0xB9, 0xBC, 0xD3, 0xC8, 0xC8}; -#define text_stop_heating_LEN (sizeof(text_stop_heating_GBK)) #define text_stop_heating text_stop_heating_GBK - -// 对应文本(UTF8):按摩档位:1级 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x31, 0xBC, 0xB6 -const uint8_t text_massage_gear_1_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x31, 0xBC, 0xB6}; -#define text_massage_gear_1_LEN (sizeof(text_massage_gear_1_GBK)) #define text_massage_gear_1 text_massage_gear_1_GBK - -// 对应文本(UTF8):按摩档位:2级 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x32, 0xBC, 0xB6 -const uint8_t text_massage_gear_2_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x32, 0xBC, 0xB6}; -#define text_massage_gear_2_LEN (sizeof(text_massage_gear_2_GBK)) #define text_massage_gear_2 text_massage_gear_2_GBK - -// 对应文本(UTF8):按摩档位:3级 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x33, 0xBC, 0xB6 -const uint8_t text_massage_gear_3_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x33, 0xBC, 0xB6}; -#define text_massage_gear_3_LEN (sizeof(text_massage_gear_3_GBK)) #define text_massage_gear_3 text_massage_gear_3_GBK - -// 对应文本(UTF8):按摩时间:10分钟 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x31, 0x30, 0xB7, 0xD6, 0xD6, 0xD3 -const uint8_t text_massage_time_10_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x31, 0x30, 0xB7, 0xD6, 0xD6, 0xD3}; -#define text_massage_time_10_LEN (sizeof(text_massage_time_10_GBK)) #define text_massage_time_10 text_massage_time_10_GBK - -// 对应文本(UTF8):按摩时间:15分钟 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x31, 0x35, 0xB7, 0xD6, 0xD6, 0xD3 -const uint8_t text_massage_time_15_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x31, 0x35, 0xB7, 0xD6, 0xD6, 0xD3}; -#define text_massage_time_15_LEN (sizeof(text_massage_time_15_GBK)) #define text_massage_time_15 text_massage_time_15_GBK - -// 对应文本(UTF8):按摩时间:20分钟 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x32, 0x30, 0xB7, 0xD6, 0xD6, 0xD3 -const uint8_t text_massage_time_20_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x32, 0x30, 0xB7, 0xD6, 0xD6, 0xD3}; -#define text_massage_time_20_LEN (sizeof(text_massage_time_20_GBK)) #define text_massage_time_20 text_massage_time_20_GBK - -// 对应文本(UTF8):按摩时间:30分钟 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x33, 0x30, 0xB7, 0xD6, 0xD6, 0xD3 -const uint8_t text_massage_time_30_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x33, 0x30, 0xB7, 0xD6, 0xD6, 0xD3}; -#define text_massage_time_30_LEN (sizeof(text_massage_time_30_GBK)) #define text_massage_time_30 text_massage_time_30_GBK - -// 对应文本(UTF8):按摩时间:持续 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0xB3, 0xD6, 0xD0, 0xF8 -const uint8_t text_massage_time_cont_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0xB3, 0xD6, 0xD0, 0xF8}; -#define text_massage_time_cont_LEN (sizeof(text_massage_time_cont_GBK)) #define text_massage_time_cont text_massage_time_cont_GBK - -// 对应文本(UTF8):剩余按摩时间为:%d -// 对应GBK编码:0xCA, 0xA3, 0xD3, 0xE0, 0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xCE, 0xAA, 0xA3, 0xBA, 0x25, 0x64 -const uint8_t text_remaining_time_GBK[] = {0xCA, 0xA3, 0xD3, 0xE0, 0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xCE, 0xAA, 0xA3, 0xBA, 0x25, 0x64}; -#define text_remaining_time_LEN (sizeof(text_remaining_time_GBK)) #define text_remaining_time text_remaining_time_GBK - -// 对应文本(UTF8):加热已开启 -// 对应GBK编码:0xBC, 0xD3, 0xC8, 0xC8, 0xD2, 0xD1, 0xBF, 0xAA, 0xC6, 0xF4 -const uint8_t text_heating_on_GBK[] = {0xBC, 0xD3, 0xC8, 0xC8, 0xD2, 0xD1, 0xBF, 0xAA, 0xC6, 0xF4}; -#define text_heating_on_LEN (sizeof(text_heating_on_GBK)) #define text_heating_on text_heating_on_GBK - -// 对应文本(UTF8):加热已关闭 -// 对应GBK编码:0xBC, 0xD3, 0xC8, 0xC8, 0xD2, 0xD1, 0xB9, 0xD8, 0xB1, 0xD5 -const uint8_t text_heating_off_GBK[] = {0xBC, 0xD3, 0xC8, 0xC8, 0xD2, 0xD1, 0xB9, 0xD8, 0xB1, 0xD5}; -#define text_heating_off_LEN (sizeof(text_heating_off_GBK)) #define text_heating_off text_heating_off_GBK - -// 对应文本(UTF8):按摩中 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xD6, 0xD0 -const uint8_t text_massage_running_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xD6, 0xD0}; -#define text_massage_running_LEN (sizeof(text_massage_running_GBK)) #define text_massage_running text_massage_running_GBK - -// 对应文本(UTF8):按摩已暂停 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xD1, 0xD4, 0xDD, 0xCD, 0xA3 -const uint8_t text_massage_paused_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xD1, 0xD4, 0xDD, 0xCD, 0xA3}; -#define text_massage_paused_LEN (sizeof(text_massage_paused_GBK)) #define text_massage_paused text_massage_paused_GBK - -// 对应文本(UTF8):按摩已结束 -// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xD1, 0xBD, 0xE1, 0xCA, 0xF8 -const uint8_t text_massage_ended_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xD1, 0xBD, 0xE1, 0xCA, 0xF8}; -#define text_massage_ended_LEN (sizeof(text_massage_ended_GBK)) #define text_massage_ended text_massage_ended_GBK - #endif // __GBK_TEXT_H__ diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index a154b30..c9ed2d3 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -20,6 +20,7 @@ /* Includes ------------------------------------------------------------------*/ #include "FreeRTOS.h" #include "cmsis_os.h" +#include "gbk_text.h" #include "main.h" #include "task.h" @@ -35,6 +36,7 @@ #include #include + /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -187,6 +189,8 @@ void MX_FREERTOS_Init(void) { void StartDefaultTask(void *argument) { /* USER CODE BEGIN StartDefaultTask */ Screen_Init(); + + Screen_DrawText16_GBK(1, 30, text_device_loading,text_device_loading_LEN, TEXT_COLOR); // 初始化命令 uint8_t init_cmd1[] = {0x7E, 0xFF, 0x06, 0x06, 0x00, 0x00, 0x1E, 0xFE, 0xD7, 0xEF}; // 开启声音 diff --git a/Core/Src/gbk_text.c b/Core/Src/gbk_text.c new file mode 100644 index 0000000..5c1e019 --- /dev/null +++ b/Core/Src/gbk_text.c @@ -0,0 +1,82 @@ +#include "gbk_text.h" + +// 对应文本(UTF8):智能按摩仪 +// 对应GBK编码:0xD6, 0xC7, 0xC4, 0xDC, 0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xC7 +const uint8_t text_device_name_GBK[] = {0xD6, 0xC7, 0xC4, 0xDC, 0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xC7}; + +// 对应文本(UTF8):设备加载中请稍候 +// 对应GBK编码:0xC9, 0xE8, 0xB1, 0xB8, 0xBC, 0xD3, 0xD4, 0xD8, 0xD6, 0xD0, 0xC7, 0xEB, 0xC9, 0xD4, 0xBA, 0xF2 +const uint8_t text_device_loading_GBK[] = {0xC9, 0xE8, 0xB1, 0xB8, 0xBC, 0xD3, 0xD4, 0xD8, 0xD6, 0xD0, 0xC7, 0xEB, 0xC9, 0xD4, 0xBA, 0xF2}; + +// 对应文本(UTF8):开始按摩 +// 对应GBK编码:0xBF, 0xAA, 0xCA, 0xBC, 0xB0, 0xB4, 0xC4, 0xA6 +const uint8_t text_start_massage_GBK[] = {0xBF, 0xAA, 0xCA, 0xBC, 0xB0, 0xB4, 0xC4, 0xA6}; + +// 对应文本(UTF8):开始加热 +// 对应GBK编码:0xBF, 0xAA, 0xCA, 0xBC, 0xBC, 0xD3, 0xC8, 0xC8 +const uint8_t text_start_heating_GBK[] = {0xBF, 0xAA, 0xCA, 0xBC, 0xBC, 0xD3, 0xC8, 0xC8}; + +// 对应文本(UTF8):停止按摩 +// 对应GBK编码:0xCD, 0xA3, 0xD6, 0xB9, 0xB0, 0xB4, 0xC4, 0xA6 +const uint8_t text_stop_massage_GBK[] = {0xCD, 0xA3, 0xD6, 0xB9, 0xB0, 0xB4, 0xC4, 0xA6}; + +// 对应文本(UTF8):停止加热 +// 对应GBK编码:0xCD, 0xA3, 0xD6, 0xB9, 0xBC, 0xD3, 0xC8, 0xC8 +const uint8_t text_stop_heating_GBK[] = {0xCD, 0xA3, 0xD6, 0xB9, 0xBC, 0xD3, 0xC8, 0xC8}; + +// 对应文本(UTF8):按摩档位:1级 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x31, 0xBC, 0xB6 +const uint8_t text_massage_gear_1_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x31, 0xBC, 0xB6}; + +// 对应文本(UTF8):按摩档位:2级 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x32, 0xBC, 0xB6 +const uint8_t text_massage_gear_2_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x32, 0xBC, 0xB6}; + +// 对应文本(UTF8):按摩档位:3级 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x33, 0xBC, 0xB6 +const uint8_t text_massage_gear_3_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xB5, 0xB5, 0xCE, 0xBB, 0xA3, 0xBA, 0x33, 0xBC, 0xB6}; + +// 对应文本(UTF8):按摩时间:10分钟 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x31, 0x30, 0xB7, 0xD6, 0xD6, 0xD3 +const uint8_t text_massage_time_10_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x31, 0x30, 0xB7, 0xD6, 0xD6, 0xD3}; + +// 对应文本(UTF8):按摩时间:15分钟 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x31, 0x35, 0xB7, 0xD6, 0xD6, 0xD3 +const uint8_t text_massage_time_15_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x31, 0x35, 0xB7, 0xD6, 0xD6, 0xD3}; + +// 对应文本(UTF8):按摩时间:20分钟 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x32, 0x30, 0xB7, 0xD6, 0xD6, 0xD3 +const uint8_t text_massage_time_20_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x32, 0x30, 0xB7, 0xD6, 0xD6, 0xD3}; + +// 对应文本(UTF8):按摩时间:30分钟 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x33, 0x30, 0xB7, 0xD6, 0xD6, 0xD3 +const uint8_t text_massage_time_30_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0x33, 0x30, 0xB7, 0xD6, 0xD6, 0xD3}; + +// 对应文本(UTF8):按摩时间:持续 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0xB3, 0xD6, 0xD0, 0xF8 +const uint8_t text_massage_time_cont_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xA3, 0xBA, 0xB3, 0xD6, 0xD0, 0xF8}; + +// 对应文本(UTF8):剩余按摩时间为:%d +// 对应GBK编码:0xCA, 0xA3, 0xD3, 0xE0, 0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xCE, 0xAA, 0xA3, 0xBA, 0x25, 0x64 +const uint8_t text_remaining_time_GBK[] = {0xCA, 0xA3, 0xD3, 0xE0, 0xB0, 0xB4, 0xC4, 0xA6, 0xCA, 0xB1, 0xBC, 0xE4, 0xCE, 0xAA, 0xA3, 0xBA, 0x25, 0x64}; + +// 对应文本(UTF8):加热已开启 +// 对应GBK编码:0xBC, 0xD3, 0xC8, 0xC8, 0xD2, 0xD1, 0xBF, 0xAA, 0xC6, 0xF4 +const uint8_t text_heating_on_GBK[] = {0xBC, 0xD3, 0xC8, 0xC8, 0xD2, 0xD1, 0xBF, 0xAA, 0xC6, 0xF4}; + +// 对应文本(UTF8):加热已关闭 +// 对应GBK编码:0xBC, 0xD3, 0xC8, 0xC8, 0xD2, 0xD1, 0xB9, 0xD8, 0xB1, 0xD5 +const uint8_t text_heating_off_GBK[] = {0xBC, 0xD3, 0xC8, 0xC8, 0xD2, 0xD1, 0xB9, 0xD8, 0xB1, 0xD5}; + +// 对应文本(UTF8):按摩中 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xD6, 0xD0 +const uint8_t text_massage_running_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xD6, 0xD0}; + +// 对应文本(UTF8):按摩已暂停 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xD1, 0xD4, 0xDD, 0xCD, 0xA3 +const uint8_t text_massage_paused_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xD1, 0xD4, 0xDD, 0xCD, 0xA3}; + +// 对应文本(UTF8):按摩已结束 +// 对应GBK编码:0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xD1, 0xBD, 0xE1, 0xCA, 0xF8 +const uint8_t text_massage_ended_GBK[] = {0xB0, 0xB4, 0xC4, 0xA6, 0xD2, 0xD1, 0xBD, 0xE1, 0xCA, 0xF8}; + diff --git a/Development_Docs/Serial_Screen_Docs/UTF8-GBK/utf8_to_gbk.py b/Development_Docs/Serial_Screen_Docs/UTF8-GBK/utf8_to_gbk.py index 6f8baa0..dee2ad9 100644 --- a/Development_Docs/Serial_Screen_Docs/UTF8-GBK/utf8_to_gbk.py +++ b/Development_Docs/Serial_Screen_Docs/UTF8-GBK/utf8_to_gbk.py @@ -11,10 +11,18 @@ def get_correct_path(path): else: return path -def utf8_to_gbk_header(utf8_file, gbk_header_file): +def utf8_to_gbk_header_and_c(utf8_file, gbk_header_file, gbk_c_file): + """ + 生成拆分后的gbk_text.h(仅声明)和gbk_text.c(定义),预计算长度避免sizeof错误 + :param utf8_file: 输入的UTF8文本文件路径 + :param gbk_header_file: 输出的头文件路径(gbk_text.h) + :param gbk_c_file: 输出的C文件路径(gbk_text.c) + """ utf8_file = get_correct_path(utf8_file) gbk_header_file = get_correct_path(gbk_header_file) + gbk_c_file = get_correct_path(gbk_c_file) + # 1. 读取UTF8文本文件 try: with open(utf8_file, 'r', encoding='utf-8') as f: lines = f.readlines() @@ -25,58 +33,93 @@ def utf8_to_gbk_header(utf8_file, gbk_header_file): print(f"❌ 读取文件失败:{e}") return - output_dir = os.path.dirname(gbk_header_file) - if not os.path.exists(output_dir): - os.makedirs(output_dir) + # 2. 确保输出目录存在 + for output_file in [gbk_header_file, gbk_c_file]: + output_dir = os.path.dirname(output_file) + if not os.path.exists(output_dir): + os.makedirs(output_dir) + # 3. 解析文本行,生成GBK编码数据(记录长度) + gbk_data_list = [] # 存储(key, value, gbk_bytes, gbk_len) + for line_num, line in enumerate(lines, 1): + line = line.strip() + if not line or line.startswith("//"): + continue + if '=' not in line: + print(f"⚠️ 警告:第{line_num}行格式错误,跳过 → {line}") + continue + key, value = line.split('=', 1) + key = key.strip() + value = value.strip() + + try: + gbk_bytes = value.encode('gbk') + gbk_len = len(gbk_bytes) # 预计算长度 + gbk_data_list.append((key, value, gbk_bytes, gbk_len)) + except Exception as e: + print(f"⚠️ 警告:第{line_num}行转换失败 → {value},错误:{e}") + continue + + if not gbk_data_list: + print("⚠️ 警告:无有效文本行,未生成任何内容") + return + + # 4. 生成gbk_text.h(仅extern声明 + 固定长度宏) try: - # 头文件整体用UTF8编码写入,注释不乱码 with open(gbk_header_file, 'w', encoding='utf-8') as f: + # 头文件保护 header_define = "__" + os.path.basename(gbk_header_file).upper().replace(".", "_") + "__" f.write(f"#ifndef {header_define}\n") f.write(f"#define {header_define}\n\n") f.write("#include \n\n") - for line_num, line in enumerate(lines, 1): - line = line.strip() - if not line or line.startswith("//"): - continue - if '=' not in line: - print(f"⚠️ 警告:第{line_num}行格式错误,跳过 → {line}") - continue - key, value = line.split('=', 1) - key = key.strip() - value = value.strip() - - try: - gbk_bytes = value.encode('gbk') - except Exception as e: - print(f"⚠️ 警告:第{line_num}行转换失败 → {value},错误:{e}") - continue - - # ========== 核心修复:拆分定义,避免自动追加\0 ========== - # 1. 定义纯GBK字节数组(无\0) + # 第一步:写extern声明 + f.write("// GBK文本数组声明(定义在gbk_text.c中)\n") + for key, _, _, _ in gbk_data_list: + f.write(f"extern const uint8_t {key}_GBK[];\n") + f.write("\n") + + # 第二步:写固定长度宏(直接用预计算的数值,避免sizeof) + f.write("// GBK文本长度宏(预计算,不含\\0)\n") + for key, _, _, gbk_len in gbk_data_list: + f.write(f"#define {key}_LEN ({gbk_len})\n") # 直接写数值 + f.write("\n") + + # 第三步:写兼容别名 + f.write("// 兼容旧代码的别名\n") + for key, _, _, _ in gbk_data_list: + f.write(f"#define {key} {key}_GBK\n") + f.write("\n") + + f.write(f"#endif // {header_define}\n") + print(f"✅ 成功生成头文件:{gbk_header_file}") + except Exception as e: + print(f"❌ 写入头文件失败:{e}") + return + + # 5. 生成gbk_text.c(数组定义) + try: + with open(gbk_c_file, 'w', encoding='utf-8') as f: + # 包含头文件 + f.write(f'#include "{os.path.basename(gbk_header_file)}"\n\n') + + # 写数组定义 + for key, value, gbk_bytes, _ in gbk_data_list: f.write(f"// 对应文本(UTF8):{value}\n") f.write(f"// 对应GBK编码:{', '.join([f'0x{byte:02X}' for byte in gbk_bytes])}\n") f.write(f"const uint8_t {key}_GBK[] = {{") f.write(", ".join([f"0x{byte:02X}" for byte in gbk_bytes])) - f.write("};\n") - - # 2. 定义有效长度宏(不含\0,直接可用) - f.write(f"#define {key}_LEN (sizeof({key}_GBK))\n") - - # 3. 可选:兼容旧代码的别名(如需保留原变量名) - f.write(f"#define {key} {key}_GBK\n\n") - - f.write(f"\n#endif // {header_define}\n") - print(f"✅ 成功生成GBK头文件:{gbk_header_file}") + f.write("};\n\n") + print(f"✅ 成功生成C文件:{gbk_c_file}") except Exception as e: - print(f"❌ 写入文件失败:{e}") + print(f"❌ 写入C文件失败:{e}") return if __name__ == "__main__": - # 你的完整英文路径(确认路径正确) + # 配置路径(根据你的工程修改) input_utf8_file = r"E:\My_Workpace\Stm_Projects\SmartMassager_STM32\Development_Docs\Serial_Screen_Docs\UTF8-GBK\text_utf8.txt" output_gbk_header = r"E:\My_Workpace\Stm_Projects\SmartMassager_STM32\Core\Inc\gbk_text.h" + output_gbk_c = r"E:\My_Workpace\Stm_Projects\SmartMassager_STM32\Core\Src\gbk_text.c" - utf8_to_gbk_header(input_utf8_file, output_gbk_header) \ No newline at end of file + # 执行生成 + utf8_to_gbk_header_and_c(input_utf8_file, output_gbk_header, output_gbk_c) \ No newline at end of file