当前位置: 首页 > news >正文

解决LVGL与FATFS编码格式冲突及外挂字库方案

问题描述

在使用LVGL和FATFS文件系统时,遇到了编码格式不一致的问题:

  • LVGL 默认使用 UTF-8 编码
  • CubeMX生成的FATFS 使用 GBK 编码
  • 从SD卡读取的字符文字为GBK格式,导致在LVGL中显示时出现乱码

stm-loc1.png

参考解决方案:http://www.openedv.com/forum.php?mod=viewthread&tid=348136&highlight=lvgl%2B%D6%D0%CE%C4

编码转换方案

转换逻辑

  • LVGL中文存入SD卡:UTF-8 → GBK
  • LVGL显示SD卡文字:GBK → UTF-8

实现步骤

  1. 下载转换文件
    GBK转UTF-8修改好的文件

  2. 导入工程
    将文件导入Keil工程

  3. 代码示例

#include "gbk2utf8.h"char name[] = "你好你好你好";
char utf8_text[64];
str_gbk2utf8(name, utf8_text);
lv_label_set_text_fmt(guider_ui.screen_text_name, "%s", utf8_text);

LVGL外挂字库方案

准备工作

使用 LvglFontTool 工具生成字库文件

操作步骤

1. 选择字体和大小

stm-loc2.png

2. 添加字符集

  • 点击"加入常用汉字"
  • 添加可能用到的生僻字和中文符号,避免显示为方框
    stm-loc3.png

3. 可选:添加Awesome图标

stm-loc4.png

4. 设置输出参数

  • 设置字体名称和尺寸
  • 类型选择"外部bin文件"
    stm-loc5.png

5. 生成文件

  • 点击"开始转换"

  • 生成两个文件:

    • .bin文件 → 拷贝到SD卡

      stm-loc6.png

    • .c文件 → 加入Keil工程
      stm-loc7.png

代码修改

修改 YaHeiFont12.c 文件:

/*
*---------------------------------------------------------------
*                        Lvgl Font Tool                         
*                                                               
* 注:使用unicode编码                                              
* 注:本字体文件由Lvgl Font Tool V0.4 生成                          
* 作者:阿里(qq:617622104)                                         
*---------------------------------------------------------------
*/#include "lvgl.h"
#include "fatfs.h"
#include "ff.h"
#include <stdio.h>typedef struct{uint16_t min;uint16_t max;uint8_t  bpp;uint8_t  reserved[3];
}x_header_t;typedef struct{uint32_t pos;
}x_table_t;typedef struct{uint8_t adv_w;uint8_t box_w;uint8_t box_h;int8_t  ofs_x;int8_t  ofs_y;uint8_t r;
}glyph_dsc_t;static x_header_t __g_xbf_hd = {.min = 0x0020,.max = 0xf244,.bpp = 1,
};static uint8_t __g_font_buf[63];  // 字体读取缓冲区static uint8_t *__user_font_getdata(int offset, int size){uint32_t br;// 注意修改SD卡中字库文件路径if( f_open(&SDFile, (const TCHAR*)"0:/Font/YaHeiFont12.bin", FA_READ) != FR_OK ) {printf("font.bin open failed\r\n");} else {if( f_lseek(&SDFile, (FSIZE_t)offset) != FR_OK ) {printf("font lseek failed\r\n");}if( f_read(&SDFile, __g_font_buf, (UINT)size, (UINT*)&br) != FR_OK ) {printf("font read failed\r\n");}f_close(&SDFile);}return __g_font_buf;
}static const uint8_t * __user_font_get_bitmap(const lv_font_t * font, uint32_t unicode_letter) {if( unicode_letter>__g_xbf_hd.max || unicode_letter<__g_xbf_hd.min ) {return NULL;}uint32_t unicode_offset = sizeof(x_header_t)+(unicode_letter-__g_xbf_hd.min)*4;uint32_t *p_pos = (uint32_t *)__user_font_getdata(unicode_offset, 4);if( p_pos[0] != 0 ) {uint32_t pos = p_pos[0];glyph_dsc_t * gdsc = (glyph_dsc_t*)__user_font_getdata(pos, sizeof(glyph_dsc_t));return __user_font_getdata(pos+sizeof(glyph_dsc_t), gdsc->box_w*gdsc->box_h*__g_xbf_hd.bpp/8);}return NULL;
}static bool __user_font_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, uint32_t unicode_letter_next) {if( unicode_letter>__g_xbf_hd.max || unicode_letter<__g_xbf_hd.min ) {return NULL;}uint32_t unicode_offset = sizeof(x_header_t)+(unicode_letter-__g_xbf_hd.min)*4;uint32_t *p_pos = (uint32_t *)__user_font_getdata(unicode_offset, 4);if( p_pos[0] != 0 ) {glyph_dsc_t * gdsc = (glyph_dsc_t*)__user_font_getdata(p_pos[0], sizeof(glyph_dsc_t));dsc_out->adv_w = gdsc->adv_w;dsc_out->box_h = gdsc->box_h;dsc_out->box_w = gdsc->box_w;dsc_out->ofs_x = gdsc->ofs_x;dsc_out->ofs_y = gdsc->ofs_y;dsc_out->bpp   = __g_xbf_hd.bpp;return true;}return false;
}// YaHei Consolas Hybrid,YaHei Consolas Hybrid Regular,12
// 字模高度:21
// XBF字体,外部bin文件
const lv_font_t YaHeiFont12 = {.get_glyph_bitmap = __user_font_get_bitmap,.get_glyph_dsc = __user_font_get_glyph_dsc,.line_height = 21,.base_line = 0,
};

配置LVGL

1. 在 lv_conf.h 中添加自定义字体

stm-loc8.png

2. 设置标签控件的字体

stm-loc9.png

最终效果

成功实现外挂字库显示中文:
stm-loc10.png

通过以上方案,完美解决了LVGL与FATFS编码格式冲突问题,并实现了灵活的外挂字库功能。

http://www.jsqmd.com/news/54388/

相关文章:

  • 我是如何用浏览器插件轻松抓取抖音评论并实现精准搜索分析的
  • 重练算法(代码随想录版) day24 - 回溯part3
  • useEffect详解
  • 详解np.random.normal(0, 3, size=x.shape)
  • 代码随想录Day23_回溯_组合.md
  • 详细介绍:【JUnit实战3_21】第十二章:JUnit 5 与主流 IDE 的集成 + 第十三章:用 JUnit 5 做持续集成(上):在本地安装 Jenkins
  • 代码随想录Day24_回溯_复原IP.md
  • 何以为生
  • GraphRAG进阶:基于Neo4j与LlamaIndex的DRIFT搜索实现详解
  • Gemini3疯了!0.09接入Nano Banana Pro 4k画质API(附实战教程)
  • 11/28
  • noip板子
  • Webstorm常用配置
  • 东方博宜OJ 1119:求各位数字之和 ← 循环结构
  • 2025.11.28
  • 10个免费查重降重工具分享,降AIGC率工具
  • Linux_Socket_浅谈UDP - 教程
  • Jetlinks 物联网平台 开源版学习源码分析
  • Java 线程池深度解析:原理、策略与生产环境调优指南
  • Tita CRM一体化平台:破解销售管理五大痛点,实现业绩可持续增长
  • NOIP 算法合集
  • 会赢吗
  • 直接通过electron创建项目
  • 东方博宜OJ 1246:请输出n行的9*9乘法表 ← 嵌套循环
  • 使用cnpm(中国镜像源的npm客户端)来安装electron
  • 2025年11月电动叉车销售企业避坑指南:市场主流品牌横向对比
  • 2025年11月中国电动叉车销售公司推荐榜单:主流品牌综合对比分析
  • 详细介绍:Qt样式深度解析
  • 文档抽取科技:利用自然语言处理技术自动识别和提取合同、判决书等法律文书中的关键信息,并将其转化为结构化数据
  • 替代模型简化复杂物理仿真任务