How to use LittleFS as driver in LVGL
Posted: Sun Feb 20, 2022 7:54 pm
Hi everyone,
I’m currently trying to use LittleFS for Esp32 but I’m facing an issue with how the fs_open is working.
The current code raises
I tried with another class which was encapsulate the File element, but same, it's not working, does someone already do something like that?
Is it possible to use File object as pointer?
I’m currently trying to use LittleFS for Esp32 but I’m facing an issue with how the fs_open is working.
- #include <Arduino.h>
- #include "lv_port_fs_littlefs.h"
- #include <LittleFS.h>
- static void fs_init(void);
- static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode);
- static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p);
- static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br);
- static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw);
- static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence);
- static lv_fs_res_t fs_size(lv_fs_drv_t * drv, void * file_p, uint32_t * size_p);
- static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p);
- static void * fs_dir_open(lv_fs_drv_t * drv, const char * path);
- static lv_fs_res_t fs_dir_read(lv_fs_drv_t * drv, void * rddir_p, char * fn);
- static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * rddir_p);
- void lv_port_littlefs_init(void)
- {
- fs_init();
- /*Add a simple drive to open images*/
- static lv_fs_drv_t fs_drv;
- lv_fs_drv_init(&fs_drv);
- fs_drv.letter = 'L';
- fs_drv.open_cb = fs_open;
- fs_drv.close_cb = fs_close;
- fs_drv.read_cb = fs_read;
- fs_drv.write_cb = fs_write;
- fs_drv.seek_cb = fs_seek;
- fs_drv.tell_cb = NULL;
- fs_drv.dir_close_cb = NULL;
- fs_drv.dir_open_cb = NULL;
- fs_drv.dir_read_cb = NULL;
- lv_fs_drv_register(&fs_drv);
- }
- /**********************
- * STATIC FUNCTIONS
- **********************/
- /*Initialize your Storage device and File system.*/
- static void fs_init(void)
- {
- LittleFS.begin();
- }
- /**
- * Open a file
- * @param drv pointer to a driver where this function belongs
- * @param path path to the file beginning with the driver letter (e.g. S:/folder/file.txt)
- * @param mode read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR
- * @return a file descriptor or NULL on error
- */
- static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
- {
- LV_UNUSED(drv);
- const char * flags = "";
- if (mode == LV_FS_MODE_WR) flags = FILE_WRITE;
- else if (mode == LV_FS_MODE_RD) flags = FILE_READ;
- else if (mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) flags = FILE_WRITE;
- char complete_path[strlen(path) + 1];
- complete_path[0] = '/';
- complete_path[1] = '\0';
- strcat(complete_path, path);
- Serial.printf("Try to open file: %s\n", complete_path);
- File f = LittleFS.open(complete_path, flags);
- if (!f) {
- Serial.printf("Error with file: %s\n", complete_path);
- } else {
- Serial.println(f.name());
- time_t t = f.getLastWrite();
- struct tm * tmstruct = localtime(&t);
- Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
- }
- return (void*)f;
- }
- /**
- * Close an opened file
- * @param drv pointer to a driver where this function belongs
- * @param file_p pointer to a file_t variable. (opened with fs_open)
- * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum
- */
- static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p)
- {
- LV_UNUSED(drv);
- File* file = (File*)file_p;
- file->close();
- return LV_FS_RES_OK;
- }
- /**
- * Read data from an opened file
- * @param drv pointer to a driver where this function belongs
- * @param file_p pointer to a file_t variable.
- * @param buf pointer to a memory block where to store the read data
- * @param btr number of Bytes To Read
- * @param br the real number of read bytes (Byte Read)
- * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum
- */
- static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br)
- {
- LV_UNUSED(drv);
- File* file = (File*)file_p;
- *br = file->read((uint8_t*)buf, btr);
- return (int32_t)(*br) < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK;
- }
- /**
- * Write into a file
- * @param drv pointer to a driver where this function belongs
- * @param file_p pointer to a file_t variable
- * @param buf pointer to a buffer with the bytes to write
- * @param btw Bytes To Write
- * @param bw the number of real written bytes (Bytes Written). NULL if unused.
- * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum
- */
- static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw)
- {
- LV_UNUSED(drv);
- File* file = (File*)file_p;
- *bw = file->write((uint8_t*)buf, btw);
- return (int32_t)(*bw) < 0 ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK;
- }
- /**
- * Set the read write pointer. Also expand the file size if necessary.
- * @param drv pointer to a driver where this function belongs
- * @param file_p pointer to a file_t variable. (opened with fs_open )
- * @param pos the new position of read write pointer
- * @param whence tells from where to interpret the `pos`. See @lv_fs_whence_t
- * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum
- */
- static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence)
- {
- LV_UNUSED(drv);
- SeekMode mode;
- if (whence == LV_FS_SEEK_SET) mode = SeekSet;
- else if (whence == LV_FS_SEEK_CUR) mode = SeekCur;
- else if (whence == LV_FS_SEEK_END) mode = SeekEnd;
- File* file = (File*)file_p;
- file->seek(pos, mode);
- return LV_FS_RES_OK;
- }
- lib/lv_conf/lv_port_fs_littlefs.cpp: In function 'void* fs_open(lv_fs_drv_t*, const char*, lv_fs_mode_t)':
- lib/lv_conf/lv_port_fs_littlefs.cpp:85:17: error: invalid cast from type 'fs::File' to type 'void*'
- return (void*)f;
- ^
- *** [.pio/build/m5stack-core2/libe34/lv_conf/lv_port_fs_littlefs.cpp.o] Error 1
Is it possible to use File object as pointer?