Windows/Linux 给 lvgl8 添加本地文件系统支持。
-
转载: LVGL V8之使用PC文件系统驱动适配lvgl文件系统
注册lvgl文件系统
void init_fs_config(void) { static lv_fs_drv_t pcfs_drv; /*A driver descriptor*/ lv_fs_drv_init(&pcfs_drv); pcfs_drv.user_data = NULL; pcfs_drv.letter = 'P'; pcfs_drv.ready_cb = pcfs_ready; pcfs_drv.write_cb = pcfs_write; pcfs_drv.open_cb = pcfs_open; pcfs_drv.close_cb = pcfs_close; pcfs_drv.read_cb = pcfs_read; pcfs_drv.seek_cb = pcfs_seek; pcfs_drv.tell_cb = pcfs_tell; lv_fs_drv_register(&pcfs_drv); }
文件系统是否准备好被回调函数pcfs_ready
bool pcfs_ready(lv_fs_drv_t* drv) { (void)drv; /*Unused*/ return true; //这里仅返回true,如果是嵌入式,则是返回嵌入式文件系统挂载成功与否的标志 }
打开文件被回调的函数pcfs_open
static void* pcfs_open(lv_fs_drv_t* drv, const char* fn, lv_fs_mode_t mode) { (void)drv; /*Unused*/ errno = 0; const char* flags = ""; if (mode == LV_FS_MODE_WR) flags = "wb"; else if (mode == LV_FS_MODE_RD) flags = "rb"; else if (mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) flags = "a+"; /*Make the path relative to the current directory (the projects root folder)*/ char buf[256]; sprintf(buf, "./%s", fn); return fopen(buf, flags); }
写入文件被回调的函数pcfs_write
static lv_fs_res_t pcfs_write(struct _lv_fs_drv_t* drv, void* file_p, const void* buf, uint32_t btw, uint32_t* bw) { (void)drv; /*Unused*/ *bw = (uint32_t)fwrite(buf, 1, btw, file_p); return LV_FS_RES_OK; }
读取文件被回调的函数pcfs_read
static lv_fs_res_t pcfs_read(lv_fs_drv_t* drv, void* file_p, void* buf, uint32_t btr, uint32_t* br) { (void)drv; /*Unused*/ //pc_file_t* fp = file_p; /*Just avoid the confusing casings*/ *br = (uint32_t)fread(buf, 1, btr, file_p); return LV_FS_RES_OK; }
关闭文件被回调的函数pcfs_close
static lv_fs_res_t pcfs_close(lv_fs_drv_t* drv, void* file_p) { (void)drv; /*Unused*/ return fclose(file_p); }
移动文件位置被回调的函数pcfs_seek
static lv_fs_res_t pcfs_seek(lv_fs_drv_t* drv, void* file_p, uint32_t pos,lv_fs_whence_t whence) { (void)drv; /*Unused*/ return fseek(file_p, pos, whence); }
获取文件位置被回调的函数pcfs_tell
static lv_fs_res_t pcfs_tell(lv_fs_drv_t* drv, void* file_p, uint32_t* pos_p) { (void)drv; /*Unused*/ *pos_p = ftell(file_p); return LV_FS_RES_OK; }
初时化调用init_fs_config函数
init_fs_config();
加载外部图片
lv_obj_t* obj = lv_img_create(lv_scr_act()); lv_img_set_src(obj, "P:/lock_icon.bin"); lv_obj_center(obj);
效果图
加载外部字体需要匹配读取数据函数
static uint8_t __g_font_buf[364];//如bin文件存在SPI FLASH可使用此buff static uint8_t *__user_font_getdata(int offset, int size){ //如字模保存在SPI FLASH, SPIFLASH_Read(__g_font_buf,offset,size); //如字模已加载到SDRAM,直接返回偏移地址即可如:return (uint8_t*)(sdram_fontddr+offset); lv_fs_file_t file; lv_fs_res_t result; result = lv_fs_open(&file, "P:/myFont.bin", LV_FS_MODE_RD); if (result != LV_FS_RES_OK) return NULL; lv_fs_seek(&file, offset, LV_FS_SEEK_CUR); uint32_t len; lv_fs_read(&file, __g_font_buf, size, &len); lv_fs_close(&file); return __g_font_buf; }
使用外部字显示
lv_obj_t* obj = lv_btn_create(lv_scr_act()); lv_obj_set_size(obj, 300, 300); lv_obj_center(obj); lv_obj_t* label = lv_label_create(obj); lv_obj_set_style_text_font(label, &_myFont, 0); lv_label_set_text(label, "外部字体测试"); lv_obj_center(label);
中文内容需要utf-8格式编译
#pragma execution_character_set("utf-8") `
-
用这代码, 显示本地 gif 图片成功。
-
@whycan lvgl本身就支持吧,只要在lv_config.h配置一下就可以用了
*---------------------- 3rd party libraries
--------------------/
/*File system interfaces for common APIs
To enable set a driver letter for that API/
#define LV_USE_FS_STDIO '\0' /Uses fopen, fread, etc/
//#define LV_FS_STDIO_PATH "/home/john/" /*Set the working directory. If commented it will be "./" */#define LV_USE_FS_POSIX 1 /Uses open, read, etc/
#define CONFIG_LV_FS_POSIX_LETTER 'S' /Uses open, read, etc/
//#define LV_FS_POSIX_PATH "/" /*Set the working directory. If commented it will be "./" */#define LV_USE_FS_WIN32 '\0' /Uses CreateFile, ReadFile, etc/
//#define LV_FS_WIN32_PATH "C:\Users\john\" /*Set the working directory. If commented it will be ".\" */#define LV_USE_FS_FATFS '\0' /Uses f_open, f_read, etc/
- 3rd party libraries
-
@xfdr0805
感谢指教, 我搜索了一下本地文件确实没有, 是比较旧的 v8.0.x看了一下主线, 真有了:
https://github.com/lvgl/lvgl/commits/master/src/extra/libs/fsdrv/lv_fs_posix.c
Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号