From 608d06e47aa7e2f136c6b736e4265a2333d21a33 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 18 Oct 2021 06:00:47 -0500 Subject: [PATCH] fix(fsdrv) minor fs issue (#2682) * fix(fs): replace all tab to space and other minor style fix Signed-off-by: Xiang Xiao * fix(extra/fs): rename lv_fs_libs.h to lv_fsdrv.h Signed-off-by: Xiang Xiao * fix(extra/fs/stdio): fix the wrong directory path in fs_dir_open and remove the duplicated or platform specific code Signed-off-by: Xiang Xiao * fix(extra/fs/posix): implement in fs_dir_read and fix the wrong directory path in fs_dir_open Signed-off-by: Xiang Xiao * fix(extra/fs/posix): return file handle directly to avoid malloc Signed-off-by: Xiang Xiao --- docs/libs/{fs.md => fsdrv.md} | 2 +- examples/porting/lv_port_fs_template.c | 38 +- src/extra/libs/fs/lv_fs_stdio.c | 330 ------------------ src/extra/libs/{fs => fsdrv}/lv_fs_fatfs.c | 111 +++--- src/extra/libs/{fs => fsdrv}/lv_fs_posix.c | 168 ++++----- src/extra/libs/fsdrv/lv_fs_stdio.c | 314 +++++++++++++++++ .../{fs/lv_fs_libs.h => fsdrv/lv_fsdrv.h} | 8 +- src/extra/libs/lv_libs.h | 2 +- src/extra/lv_extra.c | 2 - 9 files changed, 471 insertions(+), 504 deletions(-) rename docs/libs/{fs.md => fsdrv.md} (94%) delete mode 100644 src/extra/libs/fs/lv_fs_stdio.c rename src/extra/libs/{fs => fsdrv}/lv_fs_fatfs.c (66%) rename src/extra/libs/{fs => fsdrv}/lv_fs_posix.c (60%) create mode 100644 src/extra/libs/fsdrv/lv_fs_stdio.c rename src/extra/libs/{fs/lv_fs_libs.h => fsdrv/lv_fsdrv.h} (87%) diff --git a/docs/libs/fs.md b/docs/libs/fsdrv.md similarity index 94% rename from docs/libs/fs.md rename to docs/libs/fsdrv.md index ad5965ba0..4a4f1218a 100644 --- a/docs/libs/fs.md +++ b/docs/libs/fsdrv.md @@ -1,6 +1,6 @@ ```eval_rst .. include:: /header.rst -:github_url: |github_link_base|/libs/fs.md +:github_url: |github_link_base|/libs/fsdrv.md ``` # File System Interfaces diff --git a/examples/porting/lv_port_fs_template.c b/examples/porting/lv_port_fs_template.c index 20e4f66f4..2d3ec862c 100644 --- a/examples/porting/lv_port_fs_template.c +++ b/examples/porting/lv_port_fs_template.c @@ -25,17 +25,17 @@ **********************/ static void fs_init(void); -static lv_fs_res_t fs_open (lv_fs_drv_t * drv, void * file_p, 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_open(lv_fs_drv_t * drv, void * file_p, 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 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 lv_fs_res_t fs_dir_open (lv_fs_drv_t * drv, void * rddir_p, 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); +static lv_fs_res_t fs_dir_open(lv_fs_drv_t * drv, void * rddir_p, 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); /********************** * STATIC VARIABLES @@ -103,7 +103,7 @@ static void fs_init(void) * @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) +static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) { lv_fs_res_t res = LV_FS_RES_NOT_IMP; @@ -131,10 +131,10 @@ static void * fs_open (lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) /** * 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 lv_ufs_open) + * @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) +static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p) { lv_fs_res_t res = LV_FS_RES_NOT_IMP; @@ -152,7 +152,7 @@ static lv_fs_res_t fs_close (lv_fs_drv_t * drv, void * file_p) * @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) +static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) { lv_fs_res_t res = LV_FS_RES_NOT_IMP; @@ -182,12 +182,12 @@ static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, /** * 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 lv_ufs_open ) + * @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) +static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence) { lv_fs_res_t res = LV_FS_RES_NOT_IMP; @@ -202,7 +202,7 @@ static lv_fs_res_t fs_seek (lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_f * @param pos_p pointer to to store the result * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum */ -static lv_fs_res_t fs_tell (lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) +static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) { lv_fs_res_t res = LV_FS_RES_NOT_IMP; @@ -217,7 +217,7 @@ static lv_fs_res_t fs_tell (lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) * @param path path to a directory * @return pointer to the directory read descriptor or NULL on error */ -static void * fs_dir_open (lv_fs_drv_t * drv, void * rddir_p, const char *path) +static void * fs_dir_open(lv_fs_drv_t * drv, void * rddir_p, const char *path) { void * dir = NULL; /*Add your code here*/ @@ -233,7 +233,7 @@ static void * fs_dir_open (lv_fs_drv_t * drv, void * rddir_p, const char *path) * @param fn pointer to a buffer to store the filename * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum */ -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_read(lv_fs_drv_t * drv, void * rddir_p, char *fn) { lv_fs_res_t res = LV_FS_RES_NOT_IMP; @@ -248,7 +248,7 @@ static lv_fs_res_t fs_dir_read (lv_fs_drv_t * drv, void * rddir_p, char *fn) * @param rddir_p pointer to an initialized 'lv_fs_dir_t' variable * @return LV_FS_RES_OK: no error or any error from @lv_fs_res_t enum */ -static lv_fs_res_t fs_dir_close (lv_fs_drv_t * drv, void * rddir_p) +static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * rddir_p) { lv_fs_res_t res = LV_FS_RES_NOT_IMP; diff --git a/src/extra/libs/fs/lv_fs_stdio.c b/src/extra/libs/fs/lv_fs_stdio.c deleted file mode 100644 index d72ceb813..000000000 --- a/src/extra/libs/fs/lv_fs_stdio.c +++ /dev/null @@ -1,330 +0,0 @@ -/** - * @file lv_fs_stdio.c - * - */ - - -/********************* - * INCLUDES - *********************/ -#include "../../../lvgl.h" -#if LV_USE_FS_STDIO != '\0' - -#include -#include -#include -#include -#ifdef WIN32 -#include -#endif - -/********************* - * DEFINES - *********************/ -#ifndef LV_FS_STDIO_PATH -# ifndef WIN32 -# define LV_FS_STDIO_PATH "./" /*Project root*/ -# else -# define LV_FS_STDIO_PATH ".\\" /*Project root*/ -# endif -#endif /*LV_FS_STDIO_PATH*/ - -/********************** - * TYPEDEFS - **********************/ - -/********************** - * STATIC PROTOTYPES - **********************/ -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_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 * dir_p, char *fn); -static lv_fs_res_t fs_dir_close (lv_fs_drv_t * drv, void * dir_p); - -/********************** - * STATIC VARIABLES - **********************/ - -/********************** - * MACROS - **********************/ - -/********************** - * GLOBAL FUNCTIONS - **********************/ - -/** - * Register a driver for the File system interface - */ -void lv_fs_stdio_init(void) -{ - /*--------------------------------------------------- - * Register the file system interface in LittlevGL - *--------------------------------------------------*/ - - /* Add a simple drive to open images */ - static lv_fs_drv_t fs_drv; /*A driver descriptor*/ - lv_fs_drv_init(&fs_drv); - - /*Set up fields...*/ - fs_drv.letter = LV_USE_FS_STDIO; - 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 = fs_tell; - - fs_drv.dir_close_cb = fs_dir_close; - fs_drv.dir_open_cb = fs_dir_open; - fs_drv.dir_read_cb = fs_dir_read; - - lv_fs_drv_register(&fs_drv); - - char cur_path[512]; - getcwd(cur_path, sizeof(cur_path)); - LV_LOG_INFO("STDIO file system is initialized with %s root directory.", cur_path); -} - -/********************** - * STATIC FUNCTIONS - **********************/ - -/** - * Open a file - * @param drv pointer to a driver where this function belongs - * @param file_p pointer to a file_t variable - * @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 LV_FS_RES_OK or any error from lv_fs_res_t enum - */ -static void * fs_open (lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) -{ - (void) drv; /*Unused*/ - errno = 0; - - const char * flags = ""; - - if(mode == LV_FS_MODE_WR) flags = "wb"; - else if(mode == LV_FS_MODE_RD) flags = "rb"; - else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) flags = "rb+"; - - /*Make the path relative to the current directory (the projects root folder)*/ - -#ifndef WIN32 - char buf[256]; - sprintf(buf, LV_FS_STDIO_PATH "%s", path); -#else - char buf[256]; - sprintf(buf, LV_FS_STDIO_PATH "%s", path); -#endif - - FILE * f = fopen(buf, flags); - if(f == NULL) return NULL; - - /*Be sure we are the beginning of the file*/ - fseek(f, 0, SEEK_SET); - - return 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 lv_ufs_open) - * @return LV_FS_RES_OK: no error, the file is read - * any error from lv_fs_res_t enum - */ -static lv_fs_res_t fs_close (lv_fs_drv_t * drv, void * file_p) -{ - (void) drv; /*Unused*/ - fclose(file_p); - 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, the file is read - * 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) -{ - (void) drv; /*Unused*/ - *br = fread(buf, 1, btr, file_p); - return 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 btr Bytes To Write - * @param br the number of real written bytes (Bytes Written). NULL if unused. - * @return LV_FS_RES_OK 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) -{ - (void) drv; /*Unused*/ - *bw = fwrite(buf, 1, btw, file_p); - return 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 lv_ufs_open ) - * @param pos the new position of read write pointer - * @return LV_FS_RES_OK: no error, the file is read - * 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) -{ - (void) drv; /*Unused*/ - fseek(file_p, pos, whence); - return LV_FS_RES_OK; -} - -/** - * Give the position of the read write pointer - * @param drv pointer to a driver where this function belongs - * @param file_p pointer to a file_t variable. - * @param pos_p pointer to to store the result - * @return LV_FS_RES_OK: no error, the file is read - * any error from lv_fs_res_t enum - */ -static lv_fs_res_t fs_tell (lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) -{ - (void) drv; /*Unused*/ - *pos_p = ftell(file_p); - return LV_FS_RES_OK; -} - - -#ifdef WIN32 -static char next_fn[256]; -#endif - -/** - * Initialize a 'fs_read_dir_t' variable for directory reading - * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to a 'fs_read_dir_t' variable - * @param path path to a directory - * @return LV_FS_RES_OK or any error from lv_fs_res_t enum - */ -static void * fs_dir_open (lv_fs_drv_t * drv, const char *path) -{ - (void) drv; /*Unused*/ -#ifndef WIN32 - /*Make the path relative to the current directory (the projects root folder)*/ - char buf[256]; - sprintf(buf, LV_FS_STDIO_PATH "/%s", path); - return opendir(buf); -#else - HANDLE d = INVALID_HANDLE_VALUE; - WIN32_FIND_DATA fdata; - - /*Make the path relative to the current directory (the projects root folder)*/ - char buf[256]; - sprintf(buf, LV_FS_PC_PATH "\\%s\\*", path); - - strcpy(next_fn, ""); - d = FindFirstFile(buf, &fdata); - do { - if (strcmp(fdata.cFileName, ".") == 0 || strcmp(fdata.cFileName, "..") == 0) { - continue; - } else { - - if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - { - sprintf(next_fn, "/%s", fdata.cFileName); - } else { - sprintf(next_fn, "%s", fdata.cFileName); - } - break; - } - } while(FindNextFileA(d, &fdata)); - - return d; -#endif -} - -/** - * Read the next filename form a directory. - * The name of the directories will begin with '/' - * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to an initialized 'fs_read_dir_t' variable - * @param fn pointer to a buffer to store the filename - * @return LV_FS_RES_OK or any error from lv_fs_res_t enum - */ -static lv_fs_res_t fs_dir_read (lv_fs_drv_t * drv, void * dir_p, char *fn) -{ - (void) drv; /*Unused*/ - -#ifndef WIN32 - struct dirent *entry; - do { - entry = readdir(dir_p); - - if(entry) { - if(entry->d_type == DT_DIR) sprintf(fn, "/%s", entry->d_name); - else strcpy(fn, entry->d_name); - } else { - strcpy(fn, ""); - } - } while(strcmp(fn, "/.") == 0 || strcmp(fn, "/..") == 0); -#else - strcpy(fn, next_fn); - - strcpy(next_fn, ""); - WIN32_FIND_DATA fdata; - - if(FindNextFile(dir_p, &fdata) == false) return LV_FS_RES_OK; - do { - if (strcmp(fdata.cFileName, ".") == 0 || strcmp(fdata.cFileName, "..") == 0) { - continue; - } else { - - if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - { - sprintf(next_fn, "/%s", fdata.cFileName); - } else { - sprintf(next_fn, "%s", fdata.cFileName); - } - break; - } - } while(FindNextFile(dir_p, &fdata)); - -#endif - return LV_FS_RES_OK; -} - -/** - * Close the directory reading - * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to an initialized 'fs_read_dir_t' variable - * @return LV_FS_RES_OK or any error from lv_fs_res_t enum - */ -static lv_fs_res_t fs_dir_close (lv_fs_drv_t * drv, void * dir_p) -{ - (void) drv; /*Unused*/ -#ifndef WIN32 - closedir(dir_p); -#else - FindClose(dir_p); -#endif - return LV_FS_RES_OK; -} - -#endif /*LV_FS_FATFS*/ diff --git a/src/extra/libs/fs/lv_fs_fatfs.c b/src/extra/libs/fsdrv/lv_fs_fatfs.c similarity index 66% rename from src/extra/libs/fs/lv_fs_fatfs.c rename to src/extra/libs/fsdrv/lv_fs_fatfs.c index 26d1f0233..2e242ab01 100644 --- a/src/extra/libs/fs/lv_fs_fatfs.c +++ b/src/extra/libs/fsdrv/lv_fs_fatfs.c @@ -24,15 +24,15 @@ **********************/ 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 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_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 * dir_p, char *fn); -static lv_fs_res_t fs_dir_close (lv_fs_drv_t * drv, void * dir_p); +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_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 * dir_p, char * fn); +static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p); /********************** * STATIC VARIABLES @@ -46,7 +46,7 @@ static lv_fs_res_t fs_dir_close (lv_fs_drv_t * drv, void * dir_p); * GLOBAL FUNCTIONS **********************/ -void lv_fs_if_fatfs_init(void) +void lv_fs_fatfs_init(void) { /*---------------------------------------------------- * Initialize your storage device and File System @@ -54,12 +54,12 @@ void lv_fs_if_fatfs_init(void) fs_init(); /*--------------------------------------------------- - * Register the file system interface in LittlevGL + * Register the file system interface in LittlevGL *--------------------------------------------------*/ /* Add a simple drive to open images */ - static lv_fs_drv_t fs_drv; /*A driver descriptor*/ - lv_fs_drv_init(&fs_drv); + static lv_fs_drv_t fs_drv; /*A driver descriptor*/ + lv_fs_drv_init(&fs_drv); /*Set up fields...*/ fs_drv.letter = LV_USE_FS_FATFS; @@ -91,13 +91,13 @@ static void fs_init(void) /** * Open a file * @param drv pointer to a driver where this function belongs - * @param file_p pointer to a file_t variable * @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 LV_FS_RES_OK or any error from lv_fs_res_t enum + * @return pointer to FIL struct or NULL in case of fail */ -static void * fs_open (lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) +static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) { + LV_UNUSED(drv); uint8_t flags = 0; if(mode == LV_FS_MODE_WR) flags = FA_WRITE | FA_OPEN_ALWAYS; @@ -110,24 +110,24 @@ static void * fs_open (lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) FRESULT res = f_open(f, path, flags); if(res == FR_OK) { - f_lseek(f, 0); - return f; + f_lseek(f, 0); + return f; } else { lv_mem_free(f); - return NULL; + return NULL; } } - /** * 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 lv_ufs_open) + * @param file_p pointer to a FIL variable. (opened with fs_open) * @return LV_FS_RES_OK: no error, the file is read * any error from lv_fs_res_t enum */ -static lv_fs_res_t fs_close (lv_fs_drv_t * drv, void * file_p) +static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p) { + LV_UNUSED(drv); f_close(file_p); lv_mem_free(file_p); return LV_FS_RES_OK; @@ -136,15 +136,16 @@ static lv_fs_res_t fs_close (lv_fs_drv_t * drv, void * file_p) /** * 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 file_p pointer to a FIL 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, the file is read * 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) +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); FRESULT res = f_read(file_p, buf, btr, (UINT*)br); if(res == FR_OK) return LV_FS_RES_OK; else return LV_FS_RES_UNKNOWN; @@ -153,7 +154,7 @@ static lv_fs_res_t fs_read (lv_fs_drv_t * drv, void * file_p, void * buf, uint32 /** * Write into a file * @param drv pointer to a driver where this function belongs - * @param file_p pointer to a file_t variable + * @param file_p pointer to a FIL variable * @param buf pointer to a buffer with the bytes to write * @param btr Bytes To Write * @param br the number of real written bytes (Bytes Written). NULL if unused. @@ -161,7 +162,8 @@ static lv_fs_res_t fs_read (lv_fs_drv_t * drv, void * file_p, void * buf, uint32 */ static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw) { - FRESULT res = f_write(file_p, buf, btw, (UINT*)bw); + LV_UNUSED(drv); + FRESULT res = f_write(file_p, buf, btw, (UINT*)bw); if(res == FR_OK) return LV_FS_RES_OK; else return LV_FS_RES_UNKNOWN; } @@ -169,24 +171,24 @@ static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, /** * 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 lv_ufs_open ) + * @param file_p pointer to a FIL variable. (opened with fs_open ) * @param pos the new position of read write pointer * @param whence only LV_SEEK_SET is supported * @return LV_FS_RES_OK: no error, the file is read * 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) +static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence) { - switch (whence) - { + LV_UNUSED(drv); + switch (whence) { case LV_FS_SEEK_SET: f_lseek(file_p, pos); break; case LV_FS_SEEK_CUR: - f_lseek(file_p, f_tell((FIL *)file_p) + pos); + f_lseek(file_p, f_tell(file_p) + pos); break; case LV_FS_SEEK_END: - f_lseek(file_p, f_size((FIL *)file_p) + pos); + f_lseek(file_p, f_size(file_p) + pos); break; default: break; @@ -197,26 +199,27 @@ static lv_fs_res_t fs_seek (lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_f /** * Give the position of the read write pointer * @param drv pointer to a driver where this function belongs - * @param file_p pointer to a file_t variable. + * @param file_p pointer to a FIL variable. * @param pos_p pointer to to store the result * @return LV_FS_RES_OK: no error, the file is read * any error from lv_fs_res_t enum */ -static lv_fs_res_t fs_tell (lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) +static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) { - *pos_p = f_tell(((FIL *)file_p)); + LV_UNUSED(drv); + *pos_p = f_tell(file_p); return LV_FS_RES_OK; } /** - * Initialize a 'fs_read_dir_t' variable for directory reading + * Initialize a 'DIR' variable for directory reading * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to a 'fs_read_dir_t' variable * @param path path to a directory - * @return LV_FS_RES_OK or any error from lv_fs_res_t enum + * @return pointer to an initialized 'DIR' variable */ -static void * fs_dir_open (lv_fs_drv_t * drv, const char *path) +static void * fs_dir_open(lv_fs_drv_t * drv, const char * path) { + LV_UNUSED(drv); DIR * d = lv_mem_alloc(sizeof(DIR)); if(d == NULL) return NULL; @@ -232,25 +235,26 @@ static void * fs_dir_open (lv_fs_drv_t * drv, const char *path) * Read the next filename form a directory. * The name of the directories will begin with '/' * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to an initialized 'fs_read_dir_t' variable + * @param dir_p pointer to an initialized 'DIR' variable * @param fn pointer to a buffer to store the filename * @return LV_FS_RES_OK or any error from lv_fs_res_t enum */ -static lv_fs_res_t fs_dir_read (lv_fs_drv_t * drv, void * dir_p, char *fn) +static lv_fs_res_t fs_dir_read(lv_fs_drv_t * drv, void * dir_p, char * fn) { - FRESULT res; - FILINFO fno; - fn[0] = '\0'; + LV_UNUSED(drv); + FRESULT res; + FILINFO fno; + fn[0] = '\0'; do { - res = f_readdir(dir_p, &fno); - if(res != FR_OK) return LV_FS_RES_UNKNOWN; + res = f_readdir(dir_p, &fno); + if(res != FR_OK) return LV_FS_RES_UNKNOWN; - if(fno.fattrib & AM_DIR) { - fn[0] = '/'; - strcpy(&fn[1], fno.fname); - } - else strcpy(fn, fno.fname); + if(fno.fattrib & AM_DIR) { + fn[0] = '/'; + strcpy(&fn[1], fno.fname); + } + else strcpy(fn, fno.fname); } while(strcmp(fn, "/.") == 0 || strcmp(fn, "/..") == 0); @@ -260,12 +264,13 @@ static lv_fs_res_t fs_dir_read (lv_fs_drv_t * drv, void * dir_p, char *fn) /** * Close the directory reading * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to an initialized 'fs_read_dir_t' variable + * @param dir_p pointer to an initialized 'DIR' variable * @return LV_FS_RES_OK or any error from lv_fs_res_t enum */ -static lv_fs_res_t fs_dir_close (lv_fs_drv_t * drv, void * dir_p) +static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p) { - f_closedir(dir_p); + LV_UNUSED(drv); + f_closedir(dir_p); lv_mem_free(dir_p); return LV_FS_RES_OK; } diff --git a/src/extra/libs/fs/lv_fs_posix.c b/src/extra/libs/fsdrv/lv_fs_posix.c similarity index 60% rename from src/extra/libs/fs/lv_fs_posix.c rename to src/extra/libs/fsdrv/lv_fs_posix.c index fa961b1c9..753c0227c 100644 --- a/src/extra/libs/fs/lv_fs_posix.c +++ b/src/extra/libs/fsdrv/lv_fs_posix.c @@ -12,10 +12,11 @@ #include #include +#include +#ifndef WIN32 #include #include -#include -#ifdef WIN32 +#else #include #endif @@ -28,7 +29,7 @@ # else # define LV_FS_POSIX_PATH ".\\" /*Project root*/ # endif -#endif /*LV_FS_PATH*/ +#endif /*LV_FS_POSIX_PATH*/ /********************** * TYPEDEFS @@ -37,15 +38,15 @@ /********************** * STATIC PROTOTYPES **********************/ -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 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_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 * dir_p, char *fn); -static lv_fs_res_t fs_dir_close (lv_fs_drv_t * drv, void * dir_p); +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_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 * dir_p, char * fn); +static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p); /********************** * STATIC VARIABLES @@ -69,7 +70,7 @@ void lv_fs_posix_init(void) *--------------------------------------------------*/ /* Add a simple drive to open images */ - static lv_fs_drv_t fs_drv; /*A driver descriptor*/ + static lv_fs_drv_t fs_drv; /*A driver descriptor*/ lv_fs_drv_init(&fs_drv); /*Set up fields...*/ @@ -95,14 +96,13 @@ void lv_fs_posix_init(void) /** * Open a file * @param drv pointer to a driver where this function belongs - * @param file_p pointer to a file_t variable * @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 LV_FS_RES_OK or any error from lv_fs_res_t enum + * @return a file handle or -1 in case of fail */ -static void * fs_open (lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) +static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) { - (void) drv; /*Unused*/ + LV_UNUSED(drv); errno = 0; uint32_t flags = 0; @@ -114,59 +114,50 @@ static void * fs_open (lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) char buf[256]; sprintf(buf, LV_FS_POSIX_PATH "%s", path); - int f = open(buf, flags); if(f < 0) return NULL; /*Be sure we are the beginning of the file*/ lseek(f, 0, SEEK_SET); - int * fp = lv_mem_alloc(sizeof(int)); - if(fp == NULL) return NULL; - *fp = f; - - return fp; + return (void *)(lv_uintptr_t)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 lv_ufs_open) + * @param file_p a file handle. (opened with fs_open) * @return LV_FS_RES_OK: no error, the file is read * any error from lv_fs_res_t enum */ -static lv_fs_res_t fs_close (lv_fs_drv_t * drv, void * file_p) +static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p) { - (void) drv; /*Unused*/ - int * fp = file_p; - close(*fp); - lv_mem_free(file_p); + LV_UNUSED(drv); + close((lv_uintptr_t)file_p); 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 file_p a file handle 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, the file is read * 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) +static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) { - (void) drv; /*Unused*/ - int * fp = file_p; - *br = read(*fp, buf, btr); + LV_UNUSED(drv); + *br = read((lv_uintptr_t)file_p, buf, btr); return 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 file_p a file handle variable * @param buf pointer to a buffer with the bytes to write * @param btr Bytes To Write * @param br the number of real written bytes (Bytes Written). NULL if unused. @@ -174,45 +165,41 @@ static lv_fs_res_t fs_read (lv_fs_drv_t * drv, void * file_p, void * buf, uint32 */ static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw) { - (void) drv; /*Unused*/ - int * fp = file_p; - *bw = write(*fp, buf, btw); + LV_UNUSED(drv); + *bw = write((lv_uintptr_t)file_p, buf, btw); return 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 lv_ufs_open ) + * @param file_p a file handle variable. (opened with fs_open ) * @param pos the new position of read write pointer * @return LV_FS_RES_OK: no error, the file is read * 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) +static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence) { - (void) drv; /*Unused*/ - int * fp = file_p; - lseek(*fp, pos, whence); + LV_UNUSED(drv); + lseek((lv_uintptr_t)file_p, pos, whence); return LV_FS_RES_OK; } /** * Give the position of the read write pointer * @param drv pointer to a driver where this function belongs - * @param file_p pointer to a file_t variable. + * @param file_p a file handle variable. * @param pos_p pointer to to store the result * @return LV_FS_RES_OK: no error, the file is read * any error from lv_fs_res_t enum */ -static lv_fs_res_t fs_tell (lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) +static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) { - (void) drv; /*Unused*/ - int * fp = file_p; - *pos_p = lseek(*fp, 0, SEEK_CUR); + LV_UNUSED(drv); + *pos_p = lseek((lv_uintptr_t)file_p, 0, SEEK_CUR); return LV_FS_RES_OK; } - #ifdef WIN32 static char next_fn[256]; #endif @@ -220,18 +207,17 @@ static char next_fn[256]; /** * Initialize a 'fs_read_dir_t' variable for directory reading * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to a 'fs_read_dir_t' variable * @param path path to a directory - * @return LV_FS_RES_OK or any error from lv_fs_res_t enum + * @return pointer to an initialized 'DIR' or 'HANDLE' variable */ -static void * fs_dir_open (lv_fs_drv_t * drv, const char *path) +static void * fs_dir_open(lv_fs_drv_t * drv, const char * path) { LV_UNUSED(drv); #ifndef WIN32 /*Make the path relative to the current directory (the projects root folder)*/ char buf[256]; - sprintf(buf, LV_FS_POSIX_PATH "/%s", path); + sprintf(buf, LV_FS_POSIX_PATH "%s", path); return opendir(buf); #else HANDLE d = INVALID_HANDLE_VALUE; @@ -239,7 +225,7 @@ static void * fs_dir_open (lv_fs_drv_t * drv, const char *path) /*Make the path relative to the current directory (the projects root folder)*/ char buf[256]; - sprintf(buf, LV_FS_PC_PATH "\\%s\\*", path); + sprintf(buf, LV_FS_POSIX_PATH "%s\\*", path); strcpy(next_fn, ""); d = FindFirstFile(buf, &fdata); @@ -247,9 +233,7 @@ static void * fs_dir_open (lv_fs_drv_t * drv, const char *path) if (strcmp(fdata.cFileName, ".") == 0 || strcmp(fdata.cFileName, "..") == 0) { continue; } else { - - if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - { + if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { sprintf(next_fn, "/%s", fdata.cFileName); } else { sprintf(next_fn, "%s", fdata.cFileName); @@ -266,61 +250,57 @@ static void * fs_dir_open (lv_fs_drv_t * drv, const char *path) * Read the next filename form a directory. * The name of the directories will begin with '/' * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to an initialized 'fs_read_dir_t' variable + * @param dir_p pointer to an initialized 'DIR' or 'HANDLE' variable * @param fn pointer to a buffer to store the filename * @return LV_FS_RES_OK or any error from lv_fs_res_t enum */ -static lv_fs_res_t fs_dir_read (lv_fs_drv_t * drv, void * dir_p, char *fn) +static lv_fs_res_t fs_dir_read(lv_fs_drv_t * drv, void * dir_p, char * fn) { LV_UNUSED(drv); - LV_UNUSED(dir_p); - LV_UNUSED(fn); -// #ifndef WIN32 -// struct dirent *entry; -// do { -// entry = readdir(dir_p); +#ifndef WIN32 + struct dirent *entry; + do { + entry = readdir(dir_p); -// if(entry) { -// if(entry->d_type == DT_DIR) sprintf(fn, "/%s", entry->d_name); -// else strcpy(fn, entry->d_name); -// } else { -// strcpy(fn, ""); -// } -// } while(strcmp(fn, "/.") == 0 || strcmp(fn, "/..") == 0); -// #else -// strcpy(fn, next_fn); + if(entry) { + if(entry->d_type == DT_DIR) sprintf(fn, "/%s", entry->d_name); + else strcpy(fn, entry->d_name); + } else { + strcpy(fn, ""); + } + } while(strcmp(fn, "/.") == 0 || strcmp(fn, "/..") == 0); +#else + strcpy(fn, next_fn); -// strcpy(next_fn, ""); -// WIN32_FIND_DATA fdata; + strcpy(next_fn, ""); + WIN32_FIND_DATA fdata; -// if(FindNextFile(dir_p, &fdata) == false) return LV_FS_RES_OK; -// do { -// if (strcmp(fdata.cFileName, ".") == 0 || strcmp(fdata.cFileName, "..") == 0) { -// continue; -// } else { + if(FindNextFile(dir_p, &fdata) == false) return LV_FS_RES_OK; + do { + if (strcmp(fdata.cFileName, ".") == 0 || strcmp(fdata.cFileName, "..") == 0) { + continue; + } else { + if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + sprintf(next_fn, "/%s", fdata.cFileName); + } else { + sprintf(next_fn, "%s", fdata.cFileName); + } + break; + } + } while(FindNextFile(dir_p, &fdata)); -// if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) -// { -// sprintf(next_fn, "/%s", fdata.cFileName); -// } else { -// sprintf(next_fn, "%s", fdata.cFileName); -// } -// break; -// } -// } while(FindNextFile(dir_p, &fdata)); - -// #endif +#endif return LV_FS_RES_OK; } /** * Close the directory reading * @param drv pointer to a driver where this function belongs - * @param dir_p pointer to an initialized 'fs_read_dir_t' variable + * @param dir_p pointer to an initialized 'DIR' or 'HANDLE' variable * @return LV_FS_RES_OK or any error from lv_fs_res_t enum */ -static lv_fs_res_t fs_dir_close (lv_fs_drv_t * drv, void * dir_p) +static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p) { LV_UNUSED(drv); #ifndef WIN32 diff --git a/src/extra/libs/fsdrv/lv_fs_stdio.c b/src/extra/libs/fsdrv/lv_fs_stdio.c new file mode 100644 index 000000000..e14d2ce1c --- /dev/null +++ b/src/extra/libs/fsdrv/lv_fs_stdio.c @@ -0,0 +1,314 @@ +/** + * @file lv_fs_stdio.c + * + */ + + +/********************* + * INCLUDES + *********************/ +#include "../../../lvgl.h" +#if LV_USE_FS_STDIO != '\0' + +#include +#include +#ifndef WIN32 +#include +#include +#else +#include +#endif + +/********************* + * DEFINES + *********************/ +#ifndef LV_FS_STDIO_PATH +# ifndef WIN32 +# define LV_FS_STDIO_PATH "./" /*Project root*/ +# else +# define LV_FS_STDIO_PATH ".\\" /*Project root*/ +# endif +#endif /*LV_FS_STDIO_PATH*/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ +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_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 * dir_p, char * fn); +static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p); + +/********************** + * STATIC VARIABLES + **********************/ + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +/** + * Register a driver for the File system interface + */ +void lv_fs_stdio_init(void) +{ + /*--------------------------------------------------- + * Register the file system interface in LittlevGL + *--------------------------------------------------*/ + + /* Add a simple drive to open images */ + static lv_fs_drv_t fs_drv; /*A driver descriptor*/ + lv_fs_drv_init(&fs_drv); + + /*Set up fields...*/ + fs_drv.letter = LV_USE_FS_STDIO; + 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 = fs_tell; + + fs_drv.dir_close_cb = fs_dir_close; + fs_drv.dir_open_cb = fs_dir_open; + fs_drv.dir_read_cb = fs_dir_read; + + lv_fs_drv_register(&fs_drv); +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +/** + * 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 pointer to FIL struct or NULL in case of fail + */ +static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode) +{ + LV_UNUSED(drv); + errno = 0; + + const char * flags = ""; + + if(mode == LV_FS_MODE_WR) flags = "wb"; + else if(mode == LV_FS_MODE_RD) flags = "rb"; + else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) flags = "rb+"; + + /*Make the path relative to the current directory (the projects root folder)*/ + + char buf[256]; + sprintf(buf, LV_FS_STDIO_PATH "%s", path); + + FILE * f = fopen(buf, flags); + if(f == NULL) return NULL; + + /*Be sure we are the beginning of the file*/ + fseek(f, 0, SEEK_SET); + + return f; +} + +/** + * Close an opened file + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a FILE variable. (opened with fs_open) + * @return LV_FS_RES_OK: no error, the file is read + * 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); + fclose(file_p); + 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 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, the file is read + * 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); + *br = fread(buf, 1, btr, file_p); + return 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 variable + * @param buf pointer to a buffer with the bytes to write + * @param btr Bytes To Write + * @param br the number of real written bytes (Bytes Written). NULL if unused. + * @return LV_FS_RES_OK 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); + *bw = fwrite(buf, 1, btw, file_p); + return 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 variable. (opened with fs_open ) + * @param pos the new position of read write pointer + * @return LV_FS_RES_OK: no error, the file is read + * 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); + fseek(file_p, pos, whence); + return LV_FS_RES_OK; +} + +/** + * Give the position of the read write pointer + * @param drv pointer to a driver where this function belongs + * @param file_p pointer to a FILE variable. + * @param pos_p pointer to to store the result + * @return LV_FS_RES_OK: no error, the file is read + * any error from lv_fs_res_t enum + */ +static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) +{ + LV_UNUSED(drv); + *pos_p = ftell(file_p); + return LV_FS_RES_OK; +} + +#ifdef WIN32 +static char next_fn[256]; +#endif + +/** + * Initialize a 'DIR' or 'HANDLE' variable for directory reading + * @param drv pointer to a driver where this function belongs + * @param path path to a directory + * @return pointer to an initialized 'DIR' or 'HANDLE' variable + */ +static void * fs_dir_open(lv_fs_drv_t * drv, const char * path) +{ + LV_UNUSED(drv); +#ifndef WIN32 + /*Make the path relative to the current directory (the projects root folder)*/ + char buf[256]; + sprintf(buf, LV_FS_STDIO_PATH "%s", path); + return opendir(buf); +#else + HANDLE d = INVALID_HANDLE_VALUE; + WIN32_FIND_DATA fdata; + + /*Make the path relative to the current directory (the projects root folder)*/ + char buf[256]; + sprintf(buf, LV_FS_STDIO_PATH "%s\\*", path); + + strcpy(next_fn, ""); + d = FindFirstFile(buf, &fdata); + do { + if (strcmp(fdata.cFileName, ".") == 0 || strcmp(fdata.cFileName, "..") == 0) { + continue; + } else { + if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + sprintf(next_fn, "/%s", fdata.cFileName); + } else { + sprintf(next_fn, "%s", fdata.cFileName); + } + break; + } + } while(FindNextFileA(d, &fdata)); + + return d; +#endif +} + +/** + * Read the next filename form a directory. + * The name of the directories will begin with '/' + * @param drv pointer to a driver where this function belongs + * @param dir_p pointer to an initialized 'DIR' or 'HANDLE' variable + * @param fn pointer to a buffer to store the filename + * @return LV_FS_RES_OK or any error from lv_fs_res_t enum + */ +static lv_fs_res_t fs_dir_read(lv_fs_drv_t * drv, void * dir_p, char * fn) +{ + LV_UNUSED(drv); + +#ifndef WIN32 + struct dirent *entry; + do { + entry = readdir(dir_p); + + if(entry) { + if(entry->d_type == DT_DIR) sprintf(fn, "/%s", entry->d_name); + else strcpy(fn, entry->d_name); + } else { + strcpy(fn, ""); + } + } while(strcmp(fn, "/.") == 0 || strcmp(fn, "/..") == 0); +#else + strcpy(fn, next_fn); + + strcpy(next_fn, ""); + WIN32_FIND_DATA fdata; + + if(FindNextFile(dir_p, &fdata) == false) return LV_FS_RES_OK; + do { + if (strcmp(fdata.cFileName, ".") == 0 || strcmp(fdata.cFileName, "..") == 0) { + continue; + } else { + if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + sprintf(next_fn, "/%s", fdata.cFileName); + } else { + sprintf(next_fn, "%s", fdata.cFileName); + } + break; + } + } while(FindNextFile(dir_p, &fdata)); + +#endif + return LV_FS_RES_OK; +} + +/** + * Close the directory reading + * @param drv pointer to a driver where this function belongs + * @param dir_p pointer to an initialized 'DIR' or 'HANDLE' variable + * @return LV_FS_RES_OK or any error from lv_fs_res_t enum + */ +static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p) +{ + LV_UNUSED(drv); +#ifndef WIN32 + closedir(dir_p); +#else + FindClose(dir_p); +#endif + return LV_FS_RES_OK; +} + +#endif /*LV_USE_FS_STDIO*/ diff --git a/src/extra/libs/fs/lv_fs_libs.h b/src/extra/libs/fsdrv/lv_fsdrv.h similarity index 87% rename from src/extra/libs/fs/lv_fs_libs.h rename to src/extra/libs/fsdrv/lv_fsdrv.h index d01aa68f2..c8740a4c8 100644 --- a/src/extra/libs/fs/lv_fs_libs.h +++ b/src/extra/libs/fsdrv/lv_fsdrv.h @@ -1,10 +1,10 @@ /** - * @file lv_fs_libs.h + * @file lv_fsdrv.h * */ -#ifndef LV_FS_LIBS_H -#define LV_FS_LIBS_H +#ifndef LV_FSDRV_H +#define LV_FSDRV_H #ifdef __cplusplus extern "C" { @@ -46,5 +46,5 @@ extern "C" { } /* extern "C" */ #endif -#endif /*LV_FS_LIBS_H*/ +#endif /*LV_FSDRV_H*/ diff --git a/src/extra/libs/lv_libs.h b/src/extra/libs/lv_libs.h index a3f3909a4..7288aa51d 100644 --- a/src/extra/libs/lv_libs.h +++ b/src/extra/libs/lv_libs.h @@ -14,7 +14,7 @@ extern "C" { * INCLUDES *********************/ #include "bmp/lv_bmp.h" -#include "fs/lv_fs_libs.h" +#include "fsdrv/lv_fsdrv.h" #include "png/lv_png.h" #include "gif/lv_gif.h" #include "qrcode/lv_qrcode.h" diff --git a/src/extra/lv_extra.c b/src/extra/lv_extra.c index 703b71985..8bb45f589 100644 --- a/src/extra/lv_extra.c +++ b/src/extra/lv_extra.c @@ -70,9 +70,7 @@ void lv_extra_init(void) /*Init freetype library *Cache max 64 faces and 1 size*/ lv_freetype_init(0, 0, LV_FREETYPE_CACHE_SIZE); - #endif - } /**********************