fix: 修复串口屏文本编译错误+完善按摩仪文本配置

1. 修复GBK文本数组多重定义编译错误(拆分.h/.c文件)
2. 修复sizeof计算不完整类型错误(预计算文本长度)
3. 新增按摩仪界面所需的全部中文文本定义
This commit is contained in:
2026-02-16 00:36:10 +08:00
parent 87478484ca
commit 6b3c37263a
5 changed files with 212 additions and 137 deletions

View File

@@ -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 <stdint.h>\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)
# 执行生成
utf8_to_gbk_header_and_c(input_utf8_file, output_gbk_header, output_gbk_c)