Description
I’m trying to implement some File System callbacks on my ESP32 using SPIFFS, but I’m having a hard time finding any examples to show me how to implement the callback properly.
What MCU/Processor/Board and compiler are you using?
Generic ESP32 Dev board, Arduino using PlatformIO
What LVGL version are you using?
7+
What do you want to achieve?
Write a proper file_open callback for my file system driver
What have you tried so far?
- I have implemented the File System Driver (working)
- But I cannot get a proper callback to compile.
The problem is lack of examples available. The documentation covers how to create the File system driver, but does not cover an examples of the needed callbacks, not is there any documentation explaining the arguments passed to the callbacks.
I’ve found some forum posts that cover this topic, but the results either do not compile for me, or the thread dies without a proper conclusion.
I’d love to see some full “official” examples of how to implement these callbacks.
Code to reproduce
- This may be a simple fix, but my lack of C/C++ has me stumped.
Following this thread here on Github, I tried to implement kisvegabor’s final suggestion in the thread (April 20th), but the line (File * fp = file_p;) fails to compile with the following error:
error: invalid conversion from 'void*' to 'fs::File*' [-fpermissive]
As a test, I wrote my file_open callback to simply print the path it was handed, and that works, so I know the callback is being called.
The code block(s) should be formatted like:
int my_file_object = 1000;
int my_dir_object = 1000;
lv_fs_res_t open_SPIFFS_file(struct _lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode)
{
(void) drv; /*Unused*/
File * fp = file_p; // <-- THIS LINE FAILS TO COMPILE
if (*fp) {
return LV_FS_RES_OK;
}
char fullPath[64];
sprintf(fullPath,"/%s", path);
(*fp) = SPIFFS.open(fullPath, mode == LV_FS_MODE_WR ? FILE_WRITE : FILE_READ);
if(!(*fp) || (*fp).isDirectory()){
return LV_FS_RES_UNKNOWN;
} else{
return LV_FS_RES_OK;
}
}
bool init_file_system_driver()
{
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 = NULL; //my_ready_cb; /*Callback to tell if the drive is ready to use */
drv.open_cb = open_SPIFFS_file; /*Callback to open a file */
drv.close_cb = NULL; //my_close_cb; /*Callback to close a file */
drv.read_cb = NULL; //my_read_cb; /*Callback to read a file */
drv.write_cb = NULL; //my_write_cb; /*Callback to write a file */
drv.seek_cb = NULL; //my_seek_cb; /*Callback to seek in a file (Move cursor) */
drv.tell_cb = NULL; //my_tell_cb; /*Callback to tell the cursor position */
drv.trunc_cb = NULL; //my_trunc_cb; /*Callback to delete a file */
drv.size_cb = NULL; //my_size_cb; /*Callback to tell a file's size */
drv.rename_cb = NULL; //my_rename_cb; /*Callback to rename a file */
drv.dir_open_cb = NULL; //my_dir_open_cb; /*Callback to open directory to read its content */
drv.dir_read_cb = NULL; //my_dir_read_cb; /*Callback to read a directory's content */
drv.dir_close_cb = NULL; //my_dir_close_cb; /*Callback to close a directory */
drv.free_space_cb = NULL; //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*/
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return false;
}
return true;
}
void setup()
{
init_file_system_driver();
}