ESP32-S3 + LVGL9 + FreeType 动态显示中文汉字指南ILI9488屏幕多字号支持

一、环境背景介绍

1、Linux虚机开发环境

root@bogon:/export/esp32/esp-idf-5.3/esp-idf/esp_idf_to_arduino# uname -a
Linux bogon 4.15.0-109-generic #110-Ubuntu SMP Tue Jun 23 02:39:32 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

2、esp32 IDF框架下适配了IDE,IDF版本是arduino-esp32-idf-release-v5.3

3、LVGL-9.2.2

4、freetype-2.13.3

二、GitHub上下载源码

三、freetype编译环境搭建

        默认已经搭建好LVGL的编译环境

1、下载的源码拷贝到components下

        把freetype的安装包拷贝到工程的components路径下解压(所有新增的库基本都在这里),比如我的路径如下,esp_idf_to_arduino是我自己的工程文件夹:

root@bogon:/export/esp32/esp-idf-5.3/esp-idf/esp_idf_to_arduino/components# ls
Adafruit_NeoPixel  ArduinoJson        AsyncTCP  ESP32-audioI2S-master  ESPAsyncWebServer  freetype-2.13.3  lvgl-9.2.2  U8g2_for_TFT_eSPI-master
arduino            ArduinoWebsockets  custom    ESPAsyncTCP            EspSoftwareSerial  lv_conf.h        TFT_eSPI    UrlEncode
root@bogon:/export/esp32/esp-idf-5.3/esp-idf/esp_idf_to_arduino/components# pwd
/export/esp32/esp-idf-5.3/esp-idf/esp_idf_to_arduino/components

2、更新CMakeLists.txt文件,更新后如下:

file(GLOB_RECURSE SOURCES ./src/base/*.c ./src/sfnt/*.c ./src/smooth/*.c ./src/base/ftmac.c ./src/truetype/*.c ./src/gzip/*.c ./src/autofit/*.c )

file(GLOB_RECURSE MY_INCLUDE_DIRS ./src/base ./src/autofit ./src/bdf ./src/smooth ./src/raster ./src/bzip2
        ./src/cache ./src/cff ./src/cid ./src/dlg ./src/gxvalid ./src/gzip ./src/lzw
        ./src/otvalid ./src/pcf ./src/pfr ./src/psaux ./src/pshinter ./src/psnames ./src/sdf
        ./src/sfnt ./src/svg ./src/tools ./src/truetype ./src/type1 ./src/type42 ./src/winfonts)

idf_component_register(SRCS ${SOURCES}
                    INCLUDE_DIRS "./include" "./include/dlg" "./include/freetype" "./include/freetype/config" "./include/freetype/internal" "./include/freet
ype/internal/services" ${MY_INCLUDE_DIRS}
                    REQUIRES freetype-2.13.3)

3、需要改动的文件

freetype-2.13.3/src路径下不需要全部编译,本人只编译了./src/base/*.c ./src/sfnt/*.c ./src/smooth/*.c ./src/base/ftmac.c ./src/truetype/*.c ./src/gzip/*.c ./src/autofit/*.c这些文件夹下的文件。

这些文件需要改动的地方如下:

改动点1:./src/autofit/autofit.h

注释掉所有代码

/****************************************************************************
 *
 * autofit.c
 *
 *   Auto-fitter module (body).
 *
 * Copyright (C) 2003-2024 by
 * David Turner, Robert Wilhelm, and Werner Lemberg.
 *
 * This file is part of the FreeType project, and may only be used,
 * modified, and distributed under the terms of the FreeType project
 * license, LICENSE.TXT.  By continuing to use, modify, or distribute
 * this file you indicate that you have read the license and
 * understand and accept it fully.
 *
 */


#define FT_MAKE_OPTION_SINGLE_OBJECT

#if 0
#include "ft-hb.c"
#include "afblue.c"
#include "afcjk.c"
#include "afdummy.c"
#include "afglobal.c"
#include "afhints.c"
#include "afindic.c"
#include "aflatin.c"
#include "afloader.c"
#include "afmodule.c"
#include "afranges.c"
#include "afshaper.c"
#endif

/* END */

改动点2:./src/base/fterrors.c

改动点3:./src/gzip/inftrees.c

改动点4:./src/gzip/zutil.h

改动点5:./src/sfnt/ttsvg.c

改动点5:./src/smooth/ftgrays.c

改动点6:./src/smooth/smooth.c

改动点7:./src/truetype/ttgload.c

改动点8:./include/freetype/config/ftmodule.h

/*
 * This file registers the FreeType modules compiled into the library.
 *
 * If you use GNU make, this file IS NOT USED!  Instead, it is created in
 * the objects directory (normally `<topdir>/objs/`) based on information
 * from `<topdir>/modules.cfg`.
 *
 * Please read `docs/INSTALL.ANY` and `docs/CUSTOMIZE` how to compile
 * FreeType without GNU make.
 *
 */


/*FT_USE_MODULE( FT_Module_Class, autofit_module_class )
FT_USE_MODULE( FT_Driver_ClassRec, tt_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, t1_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, cff_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, t1cid_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, pfr_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, t42_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, winfnt_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, pcf_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, bdf_driver_class )
FT_USE_MODULE( FT_Module_Class, psaux_module_class )
FT_USE_MODULE( FT_Module_Class, psnames_module_class )
FT_USE_MODULE( FT_Module_Class, pshinter_module_class )
FT_USE_MODULE( FT_Module_Class, sfnt_module_class )
FT_USE_MODULE( FT_Renderer_Class, ft_smooth_renderer_class )
FT_USE_MODULE( FT_Renderer_Class, ft_raster1_renderer_class )
FT_USE_MODULE( FT_Renderer_Class, ft_sdf_renderer_class )
FT_USE_MODULE( FT_Renderer_Class, ft_bitmap_sdf_renderer_class )
FT_USE_MODULE( FT_Renderer_Class, ft_svg_renderer_class )
*/

FT_USE_MODULE( FT_Renderer_Class, ft_smooth_renderer_class )
FT_USE_MODULE( FT_Module_Class, sfnt_module_class )
FT_USE_MODULE( FT_Module_Class, autofit_module_class )
FT_USE_MODULE( FT_Driver_ClassRec, tt_driver_class )
/* EOF */

改动点9:./include/freetype/internal/ftdebug.h

改动点10:./include/freetype/fttypes.h

四、LVGL9组件库需要改一下lv_conf.h

LV_USE_FREETYPE 改为1

/*FreeType library*/
#define LV_USE_FREETYPE 1
#if LV_USE_FREETYPE
    /*Let FreeType to use LVGL memory and file porting*/
    #define LV_FREETYPE_USE_LVGL_PORT 0

    /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached.
     *The higher the value, the more memory will be used.*/
    #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 4096
#endif

五、配置ESP-IDF环境

使用idf.py menuconfig

component config-->LVGL configuration-->3rd Party Libraries-->freetype libriary

六、代码逻辑实现

#include "freetype/lv_freetype.h"
void freetype_font_test()
{
    lv_init();

    /*Create a font (创建字体)*/
    lv_font_t *font = lv_freetype_font_create(
        "/spiffs/STLITI123.ttf",
        LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
        24,
        LV_FREETYPE_FONT_STYLE_ITALIC
    );
    if (!font)
    {
        Serial.println( "freetype font create failed." );
        return;
    }

    Serial.println( "freetype_font_test start" );
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_font(&style, font_small);
    lv_style_set_bg_color(&style, lv_color_hex(0x00ff00));
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);

    /*Create a label with the new style*/
    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_obj_set_style_text_font(label, font_small, LV_PART_MAIN|LV_STATE_DEFAULT);
     lv_label_set_long_mode(label, LV_LABEL_LONG_WRAP);
     lv_obj_set_width(label, 480);
     lv_label_set_text(label, "Hello world 123456\n你好世界进范德萨放假啦电话费等哈飞机卡手打飞机很大洒基发动机啊话费卡交电话费科技!");
     lv_obj_align( label, LV_ALIGN_TOP_LEFT, 0, 0 );

     lv_obj_t * g_label = lv_label_create(lv_scr_act());
     lv_obj_set_style_text_font(g_label, font_small, LV_PART_MAIN|LV_STATE_DEFAULT);
     //lv_obj_add_style(label, &style, 0);
     lv_obj_set_width(g_label, 480);
     lv_label_set_text(g_label, "Hello world 123456\n你好世界进范德萨放假啦电话费等哈飞机卡手打飞机很大洒基发动机啊话费卡交电话费科技!");
     lv_obj_align( g_label, LV_ALIGN_LEFT_MID, 0, 0 );

     Serial.println( "freetype_font_test done" );
 }
void setup() {
  freetype_font_test();
}

void loop() {
    lv_timer_handler(); /* let the GUI do its work */
    delay(5); /* let this time pass */
}

七、效果展示
整体流程已经基本调通,效果展示后面拍好视频后补上来。

本次测试环境是Linux下进行的,选择的原因是Linux下编译效率比较快,节省时间。

从无到有实现目前的效果,有参考其他大佬的帖子,也有自己摸索实现的部分,知识劳动成果,实属不易。

如果需要技术支持,欢迎骚扰(+v:Sw-striving)!

### ESP32 使用 LVGLFreeType 进行显示开发 #### 安装依赖项 为了在 ESP32 上使用 LVGL 图形库并集成 FreeType 字体引擎,需先安装必要的软件包。确保已配置好 VSCode 及 PlatformIO 开发环境[^1]。 ```bash pip install platformio pio lib install "lvgl" ``` 对于 FreeType支持,则需要额外下载其源码文件,并将其添加至项目中。可以从官方 GitHub 仓库获取最新版本的 FreeType 库[^2]。 #### 配置 `platformio.ini` 文件 编辑项目的 `platformio.ini` 文件来指定所需的平台和库: ```ini [env:esp32dev] platform = espressif32 board = esp32dev framework = arduino lib_deps = lvgl build_flags = -D LV_CONF_INCLUDE_SIMPLE=1 -I ${PROJECT_DIR}/libs/freetype/include/ upload_speed = 921600 monitor_speed = 115200 ``` 此设置指定了编译选项 `-D LV_CONF_INCLUDE_SIMPLE=1` 来简化 LVGL 的配置过程;并通过 `-I` 参数引入了 FreeType 头文件路径。 #### 初始化 LVGL 并加载自定义字体 下面是一个简单的初始化函数示例,在其中设置了屏幕分辨率以及注册了一个由 FreeType 渲染出来的中文字符集作为全局默认字体。 ```c #include <Arduino.h> #include "lvgl/lvgl.h" // 假设已经完成了硬件接口层(HAL)实现, 如 SPI 或 I2C 屏幕驱动程序. extern void my_disp_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p); void setup() { Serial.begin(115200); // Initialize the display driver with flush callback function static lv_disp_buf_t disp_buf; static lv_color_t buf[LV_HOR_RES_MAX * 10]; lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10); lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); disp_drv.flush_cb = my_disp_flush; /* 设置刷新回调 */ disp_drv.buffer = &disp_buf; lv_disp_t* disp = lv_disp_drv_register(&disp_drv); // Load custom font using freetype (假设 ft_font 已经被正确创建) extern const lv_font_t* ft_font; lv_font_add(ft_font, "ft"); /* 添加新字体 */ // Set global default font to our newly added one lv_theme_default_init(disp, lv_theme_material_init(NULL), LV_COLOR_MAKE(0x3b, 0x59, 0x98), LV_THEME_MATERIAL_FLAG_DARKER, ft_font); } void loop() { lv_task_handler(); /* 让LVGL处理事件 */ } ``` 上述代码片段展示了如何通过调用 `lv_font_add()` 方法向系统中加入一个新的字体资源,并把它设定成应用程序内的标准样式。 请注意,实际应用时还需要完成具体的 HAL 实现部分(即 `my_disp_flush` 函数),这取决于所使用的显示屏控制器型号及其通信协议(SPI/I2C 等)[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值