1 /**
2  * @file lv_fs_fatfs.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "../../../lvgl.h"
10 
11 #if LV_USE_FS_FATFS
12 #include "ff.h"
13 
14 #include "../../core/lv_global.h"
15 /*********************
16  *      DEFINES
17  *********************/
18 
19 #ifdef ESP_PLATFORM
20     #define DIR FF_DIR  /* ESP IDF typedefs `DIR` as `FF_DIR` in its version of ff.h. Use `FF_DIR` in LVGL too */
21 #endif
22 
23 #if !LV_FS_IS_VALID_LETTER(LV_FS_FATFS_LETTER)
24     #error "Invalid drive letter"
25 #endif
26 
27 /**********************
28  *      TYPEDEFS
29  **********************/
30 
31 /**********************
32  *  STATIC PROTOTYPES
33  **********************/
34 static void fs_init(void);
35 
36 static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode);
37 static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p);
38 static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br);
39 static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw);
40 static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence);
41 static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p);
42 static void * fs_dir_open(lv_fs_drv_t * drv, const char * path);
43 static lv_fs_res_t fs_dir_read(lv_fs_drv_t * drv, void * dir_p, char * fn, uint32_t fn_len);
44 static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p);
45 
46 /**********************
47  *  STATIC VARIABLES
48  **********************/
49 
50 /**********************
51  *      MACROS
52  **********************/
53 
54 /**********************
55  *   GLOBAL FUNCTIONS
56  **********************/
57 
lv_fs_fatfs_init(void)58 void lv_fs_fatfs_init(void)
59 {
60     /*----------------------------------------------------
61      * Initialize your storage device and File System
62      * -------------------------------------------------*/
63     fs_init();
64 
65     /*---------------------------------------------------
66      * Register the file system interface in LVGL
67      *--------------------------------------------------*/
68 
69     lv_fs_drv_t * fs_drv_p = &(LV_GLOBAL_DEFAULT()->fatfs_fs_drv);
70     lv_fs_drv_init(fs_drv_p);
71 
72     /*Set up fields...*/
73     fs_drv_p->letter = LV_FS_FATFS_LETTER;
74     fs_drv_p->cache_size = LV_FS_FATFS_CACHE_SIZE;
75 
76     fs_drv_p->open_cb = fs_open;
77     fs_drv_p->close_cb = fs_close;
78     fs_drv_p->read_cb = fs_read;
79     fs_drv_p->write_cb = fs_write;
80     fs_drv_p->seek_cb = fs_seek;
81     fs_drv_p->tell_cb = fs_tell;
82 
83     fs_drv_p->dir_close_cb = fs_dir_close;
84     fs_drv_p->dir_open_cb = fs_dir_open;
85     fs_drv_p->dir_read_cb = fs_dir_read;
86 
87     lv_fs_drv_register(fs_drv_p);
88 }
89 
90 /**********************
91  *   STATIC FUNCTIONS
92  **********************/
93 
94 /*Initialize your Storage device and File system.*/
fs_init(void)95 static void fs_init(void)
96 {
97     /*Initialize the SD card and FatFS itself.
98      *Better to do it in your code to keep this library untouched for easy updating*/
99 }
100 
101 /**
102  * Open a file
103  * @param drv   pointer to a driver where this function belongs
104  * @param path  path to the file beginning with the driver letter (e.g. S:/folder/file.txt)
105  * @param mode  read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR
106  * @return pointer to FIL struct or NULL in case of fail
107  */
fs_open(lv_fs_drv_t * drv,const char * path,lv_fs_mode_t mode)108 static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
109 {
110     LV_UNUSED(drv);
111     uint8_t flags = 0;
112 
113     if(mode == LV_FS_MODE_WR) flags = FA_WRITE | FA_OPEN_ALWAYS;
114     else if(mode == LV_FS_MODE_RD) flags = FA_READ;
115     else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) flags = FA_READ | FA_WRITE | FA_OPEN_ALWAYS;
116 
117     FIL * f = lv_malloc(sizeof(FIL));
118     if(f == NULL) return NULL;
119 
120     char buf[LV_FS_MAX_PATH_LEN];
121     lv_snprintf(buf, sizeof(buf), LV_FS_FATFS_PATH "%s", path);
122 
123     FRESULT res = f_open(f, buf, flags);
124     if(res == FR_OK) {
125         return f;
126     }
127     else {
128         lv_free(f);
129         return NULL;
130     }
131 }
132 
133 /**
134  * Close an opened file
135  * @param drv       pointer to a driver where this function belongs
136  * @param file_p    pointer to a FIL variable. (opened with fs_open)
137  * @return LV_FS_RES_OK: no error, the file is read
138  *         any error from lv_fs_res_t enum
139  */
fs_close(lv_fs_drv_t * drv,void * file_p)140 static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p)
141 {
142     LV_UNUSED(drv);
143     f_close(file_p);
144     lv_free(file_p);
145     return LV_FS_RES_OK;
146 }
147 
148 /**
149  * Read data from an opened file
150  * @param drv       pointer to a driver where this function belongs
151  * @param file_p    pointer to a FIL variable.
152  * @param buf       pointer to a memory block where to store the read data
153  * @param btr       number of Bytes To Read
154  * @param br        the real number of read bytes (Byte Read)
155  * @return LV_FS_RES_OK: no error, the file is read
156  *         any error from lv_fs_res_t enum
157  */
fs_read(lv_fs_drv_t * drv,void * file_p,void * buf,uint32_t btr,uint32_t * br)158 static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br)
159 {
160     LV_UNUSED(drv);
161     FRESULT res = f_read(file_p, buf, btr, (UINT *)br);
162     if(res == FR_OK) return LV_FS_RES_OK;
163     else return LV_FS_RES_UNKNOWN;
164 }
165 
166 /**
167  * Write into a file
168  * @param drv       pointer to a driver where this function belongs
169  * @param file_p    pointer to a FIL variable
170  * @param buf       pointer to a buffer with the bytes to write
171  * @param btw       Bytes To Write
172  * @param bw        the number of real written bytes (Bytes Written). NULL if unused.
173  * @return LV_FS_RES_OK or any error from lv_fs_res_t enum
174  */
fs_write(lv_fs_drv_t * drv,void * file_p,const void * buf,uint32_t btw,uint32_t * bw)175 static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw)
176 {
177     LV_UNUSED(drv);
178     FRESULT res = f_write(file_p, buf, btw, (UINT *)bw);
179     if(res == FR_OK) return LV_FS_RES_OK;
180     else return LV_FS_RES_UNKNOWN;
181 }
182 
183 /**
184  * Set the read write pointer. Also expand the file size if necessary.
185  * @param drv       pointer to a driver where this function belongs
186  * @param file_p    pointer to a FIL variable. (opened with fs_open )
187  * @param pos       the new position of read write pointer
188  * @param whence    only LV_SEEK_SET is supported
189  * @return LV_FS_RES_OK: no error, the file is read
190  *         any error from lv_fs_res_t enum
191  */
fs_seek(lv_fs_drv_t * drv,void * file_p,uint32_t pos,lv_fs_whence_t whence)192 static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence)
193 {
194     LV_UNUSED(drv);
195     switch(whence) {
196         case LV_FS_SEEK_SET:
197             f_lseek(file_p, pos);
198             break;
199         case LV_FS_SEEK_CUR:
200             f_lseek(file_p, f_tell((FIL *)file_p) + pos);
201             break;
202         case LV_FS_SEEK_END:
203             f_lseek(file_p, f_size((FIL *)file_p) + pos);
204             break;
205         default:
206             break;
207     }
208     return LV_FS_RES_OK;
209 }
210 
211 /**
212  * Give the position of the read write pointer
213  * @param drv       pointer to a driver where this function belongs
214  * @param file_p    pointer to a FIL variable
215  * @param pos_p     pointer to store the result
216  * @return LV_FS_RES_OK: no error, the file is read
217  *         any error from lv_fs_res_t enum
218  */
fs_tell(lv_fs_drv_t * drv,void * file_p,uint32_t * pos_p)219 static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p)
220 {
221     LV_UNUSED(drv);
222     *pos_p = f_tell((FIL *)file_p);
223     return LV_FS_RES_OK;
224 }
225 
226 /**
227  * Initialize a 'DIR' variable for directory reading
228  * @param drv   pointer to a driver where this function belongs
229  * @param path  path to a directory
230  * @return pointer to an initialized 'DIR' variable
231  */
fs_dir_open(lv_fs_drv_t * drv,const char * path)232 static void * fs_dir_open(lv_fs_drv_t * drv, const char * path)
233 {
234     LV_UNUSED(drv);
235     DIR * d = lv_malloc(sizeof(DIR));
236     if(d == NULL) return NULL;
237 
238     char buf[LV_FS_MAX_PATH_LEN];
239     lv_snprintf(buf, sizeof(buf), LV_FS_FATFS_PATH "%s", path);
240 
241     FRESULT res = f_opendir(d, buf);
242     if(res != FR_OK) {
243         lv_free(d);
244         d = NULL;
245     }
246     return d;
247 }
248 
249 /**
250  * Read the next filename from a directory.
251  * The name of the directories will begin with '/'
252  * @param drv       pointer to a driver where this function belongs
253  * @param dir_p     pointer to an initialized 'DIR' variable
254  * @param fn        pointer to a buffer to store the filename
255  * @param fn_len    length of the buffer to store the filename
256  * @return LV_FS_RES_OK or any error from lv_fs_res_t enum
257  */
fs_dir_read(lv_fs_drv_t * drv,void * dir_p,char * fn,uint32_t fn_len)258 static lv_fs_res_t fs_dir_read(lv_fs_drv_t * drv, void * dir_p, char * fn, uint32_t fn_len)
259 {
260     LV_UNUSED(drv);
261     if(fn_len == 0) return LV_FS_RES_INV_PARAM;
262 
263     FRESULT res;
264     FILINFO fno;
265     fn[0] = '\0';
266 
267     do {
268         res = f_readdir(dir_p, &fno);
269         if(res != FR_OK) return LV_FS_RES_UNKNOWN;
270 
271         if(fno.fname[0] == 0) break; /* End of the directory */
272 
273         if(fno.fattrib & AM_DIR) {
274             lv_snprintf(fn, fn_len, "/%s", fno.fname);
275         }
276         else lv_strlcpy(fn, fno.fname, fn_len);
277 
278     } while(lv_strcmp(fn, "/.") == 0 || lv_strcmp(fn, "/..") == 0);
279 
280     return LV_FS_RES_OK;
281 }
282 
283 /**
284  * Close the directory reading
285  * @param drv   pointer to a driver where this function belongs
286  * @param dir_p pointer to an initialized 'DIR' variable
287  * @return LV_FS_RES_OK or any error from lv_fs_res_t enum
288  */
fs_dir_close(lv_fs_drv_t * drv,void * dir_p)289 static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p)
290 {
291     LV_UNUSED(drv);
292     f_closedir(dir_p);
293     lv_free(dir_p);
294     return LV_FS_RES_OK;
295 }
296 
297 #else /*LV_USE_FS_FATFS == 0*/
298 
299 #if defined(LV_FS_FATFS_LETTER) && LV_FS_FATFS_LETTER != '\0'
300     #warning "LV_USE_FS_FATFS is not enabled but LV_FS_FATFS_LETTER is set"
301 #endif
302 
303 #endif /*LV_USE_FS_FATFS*/
304