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