esp32 lvgl 配置文件系统

一般来讲下载了 https://github.com/lvgl/lvgl.git 就能够使用文件系统了,不需要额外下载lv_fs_if那个代码。

参考lvgl/examples/porting目录下的lv_port_fs_template.c文件实现各种方法即可。


static lv_fs_res_t open_cb(struct _lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode)
{
    (void) drv;
    //ESP_LOGI(TAG, "lvgl open file: path=%s mode=%d", path, mode);
    FILE *fp = NULL;
    if (mode == LV_FS_MODE_RD) {
        fp = fopen(path, "rb"); // only reading is supported
    } else if (mode == LV_FS_MODE_WR) {
        fp = fopen(path, "wb");
    } else {
        ESP_LOGE(TAG, "lvfs open mode error!");
        return LV_FS_RES_UNKNOWN;
    }   
    *((FILE **)file_p) = fp; 
    return NULL == fp ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK;
}

static lv_fs_res_t close_cb(struct _lv_fs_drv_t * drv, void * file_p)
{
    (void) drv;
    FILE * fp = *((FILE **) file_p);
    fclose(fp);
    return LV_FS_RES_OK;
}

static lv_fs_res_t read_cb(struct _lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) 
{
    (void) drv;
    FILE * fp = *((FILE **) file_p);
    *br = fread(buf, 1, btr, fp);
    return (*br <= 0) ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK;
}

static lv_fs_res_t write_cb(struct _lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw) 
{
    (void)drv;
    FILE * fp = *((FILE **) file_p);
    *bw = fwrite(buf, 1, btw, fp);
    return (*bw != btw) ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK;
}

static lv_fs_res_t seek_cb(struct _lv_fs_drv_t * drv, void * file_p, uint32_t pos)
{
    (void) drv;

    //printf("--->fs seek\r\n");
    FILE * fp = *((FILE **) file_p);
    fseek (fp, pos, SEEK_SET);

    return LV_FS_RES_OK;
}

static lv_fs_res_t tell_cb(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p)
{
    (void) drv;

    FILE * fp = *((FILE **) file_p);
    *pos_p = ftell(fp);

    return LV_FS_RES_OK;
}

static bool ready_cb(struct _lv_fs_drv_t * drv)
{
    (void) drv;
    return true;
}

static lv_fs_res_t size_cb(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * size_p)
{
    (void)drv;
    FILE * fp = *((FILE **) file_p);
    uint32_t curpos, length;
    curpos = ftell(fp);
    fseek(fp, 0L, SEEK_END);
    length = ftell(fp);
    fseek(fp, curpos, SEEK_SET);
    *size_p = length;
    return LV_FS_RES_OK;
}
 

在lvgl_init初始化之后需要调用注册函数将以上定义的接口注册到lvgl系统中

    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(FILE *);   /*Size required to store a file object*/
    drv.ready_cb = ready_cb;               /*Callback to tell if the drive is ready to use */
    drv.open_cb = open_cb;                 /*Callback to open a file */
    drv.close_cb = close_cb;               /*Callback to close a file */
    drv.read_cb = read_cb;                 /*Callback to read a file */
    drv.write_cb = write_cb;
    drv.seek_cb = seek_cb;                 /*Callback to seek in a file (Move cursor) */
    drv.tell_cb = tell_cb;                 /*Callback to tell the cursor position  */
    drv.size_cb = size_cb;

    lv_fs_drv_register(&drv);                 /*Finally register the drive*/

在调用lv_fs_open的时候需要注意一下文件的开始是"S:"

比如我们在spiffs中保存的文件路径为"/spiffs/image.bin",那么使用lvgl_fs_open的时候输入的文件路径就需要是"S:/spiffs/image.bin",这样才能正确的寻找到文件。

以上就是在lvgl中使用文件系统的简单记录。

你可能感兴趣的:(单片机,物联网,经验分享)