文件系统(File system)¶
LVGL具有 “文件系统” (File system)抽象模块,使可以附加任何类型的文件系统。文件系统由驱动器号标识。例如,如果SD卡与字母 `S`
相关联,则可以访问 "S:path/to/file.txt"
之类的文件。
添加驱动程序¶
要添加驱动程序, lv_fs_drv_t
需要这样初始化:
1 lv_fs_drv_t drv;
2 lv_fs_drv_init(&drv); /*Basic initialization*/
3
4 drv.letter = 'S'; /*An uppercase letter to identify the drive */
5 drv.file_size = sizeof(my_file_object); /*Size required to store a file object*/
6 drv.rddir_size = sizeof(my_dir_object); /*Size required to store a directory object (used by dir_open/close/read)*/
7 drv.ready_cb = my_ready_cb; /*Callback to tell if the drive is ready to use */
8 drv.open_cb = my_open_cb; /*Callback to open a file */
9 drv.close_cb = my_close_cb; /*Callback to close a file */
10 drv.read_cb = my_read_cb; /*Callback to read a file */
11 drv.write_cb = my_write_cb; /*Callback to write a file */
12 drv.seek_cb = my_seek_cb; /*Callback to seek in a file (Move cursor) */
13 drv.tell_cb = my_tell_cb; /*Callback to tell the cursor position */
14 drv.trunc_cb = my_trunc_cb; /*Callback to delete a file */
15 drv.size_cb = my_size_cb; /*Callback to tell a file's size */
16 drv.rename_cb = my_rename_cb; /*Callback to rename a file */
17
18 drv.dir_open_cb = my_dir_open_cb; /*Callback to open directory to read its content */
19 drv.dir_read_cb = my_dir_read_cb; /*Callback to read a directory's content */
20 drv.dir_close_cb = my_dir_close_cb; /*Callback to close a directory */
21
22 drv.free_space_cb = my_free_space_cb; /*Callback to tell free space on the drive */
23
24 drv.user_data = my_user_data; /*Any custom data if required*/
25
26 lv_fs_drv_register(&drv); /*Finally register the drive*/
任何回调都可以为 NULL
,以指示不支持该操作。
作为使用回调的示例,如果使用 lv_fs_open(&file, "S:/folder/file.txt", LV_FS_MODE_WR)
,LVGL会:
验证是否存在带字母“ S”的已注册驱动器。
检查是否实现了
open_cb
(非NULL
)。调用
open_cb
设置"folder/file.txt"
路径。
使用范例¶
下面的示例显示如何从文件读取:
1 lv_fs_file_t f;
2 lv_fs_res_t res;
3 res = lv_fs_open(&f, "S:folder/file.txt", LV_FS_MODE_RD);
4 if(res != LV_FS_RES_OK) my_error_handling();
5
6 uint32_t read_num;
7 uint8_t buf[8];
8 res = lv_fs_read(&f, buf, 8, &read_num);
9 if(res != LV_FS_RES_OK || read_num != 8) my_error_handling();
10
11 lv_fs_close(&f);
lv_fs_open
中的模式可以是 LV_FS_MODE_WR
以打开以进行写入,也可以是 LV_FS_MODE_RD
。两者的 LV_FS_MODE_WR
本示例说明如何读取目录的内容。由驱动程序决定如何标记目录,但是在目录名称前面插入 '/'
可能是一个好习惯。
1 lv_fs_dir_t dir;
2 lv_fs_res_t res;
3 res = lv_fs_dir_open(&dir, "S:/folder");
4 if(res != LV_FS_RES_OK) my_error_handling();
5
6 char fn[256];
7 while(1) {
8 res = lv_fs_dir_read(&dir, fn);
9 if(res != LV_FS_RES_OK) {
10 my_error_handling();
11 break;
12 }
13
14 /*fn is empty, if not more files to read*/
15 if(strlen(fn) == 0) {
16 break;
17 }
18
19 printf("%s\n", fn);
20 }
21
22 lv_fs_dir_close(&dir);
相关API¶
TODO