xref: /OK3568_Linux_fs/app/lvgl_demo/lvgl/lv_port_file.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include <stdio.h>
2 #include "lv_port_file.h"
3 
4 #if 1//LV_USE_FILESYSTEM
5 
6 /**
7  * Open a file
8  * @param drv pointer to a driver where this function belongs
9  * @param file_p pointer to a file_t variable
10  * @param path path to the file beginning with the driver letter (e.g. S:/folder/file.txt)
11  * @param mode read: FS_MODE_RD, write: FS_MODE_WR, both: FS_MODE_RD | FS_MODE_WR
12  * @return LV_FS_RES_OK or any error from lv_fs_res_t enum
13  */
fs_open(lv_fs_drv_t * drv,const char * path,lv_fs_mode_t mode)14 static void *fs_open(lv_fs_drv_t *drv, const char *path, lv_fs_mode_t mode)
15 {
16     FILE *file_p;
17 
18     if (mode == LV_FS_MODE_WR)
19         file_p = fopen(path, "wb+");
20     else if (mode == LV_FS_MODE_RD)
21         file_p = fopen(path, "rb");
22     else if (mode == (LV_FS_MODE_WR | LV_FS_MODE_RD))
23         file_p = fopen(path, "wb+");
24 
25     return (void *)file_p;
26 }
27 
28 
29 /**
30  * Close an opened file
31  * @param drv pointer to a driver where this function belongs
32  * @param file_p pointer to a file_t variable. (opened with lv_ufs_open)
33  * @return LV_FS_RES_OK: no error, the file is read
34  *         any error from lv_fs_res_t enum
35  */
fs_close(lv_fs_drv_t * drv,void * file_p)36 static lv_fs_res_t fs_close(lv_fs_drv_t *drv, void *file_p)
37 {
38     lv_fs_res_t res = LV_FS_RES_OK;
39 
40     /* Add your code here*/
41     fclose((file_p));
42 
43     return res;
44 }
45 
46 /**
47  * Read data from an opened file
48  * @param drv pointer to a driver where this function belongs
49  * @param file_p pointer to a file_t variable.
50  * @param buf pointer to a memory block where to store the read data
51  * @param btr number of Bytes To Read
52  * @param br the real number of read bytes (Byte Read)
53  * @return LV_FS_RES_OK: no error, the file is read
54  *         any error from lv_fs_res_t enum
55  */
fs_read(lv_fs_drv_t * drv,void * file_p,void * buf,uint32_t btr,uint32_t * br)56 static lv_fs_res_t fs_read(lv_fs_drv_t *drv, void *file_p, void *buf, uint32_t btr, uint32_t *br)
57 {
58     lv_fs_res_t res = LV_FS_RES_OK;
59 
60     /* Add your code here*/
61     *br = fread((char *)buf, 1, btr, file_p);
62     if (*br == 0)
63         printf("ftell %d %p %d\n", ftell(file_p), buf, btr);
64 
65     return res;
66 }
67 
fs_write(lv_fs_drv_t * drv,void * file_p,const void * buf,uint32_t btw,uint32_t * bw)68 static lv_fs_res_t fs_write(lv_fs_drv_t *drv, void *file_p, const void *buf, uint32_t btw, uint32_t *bw)
69 {
70     lv_fs_res_t res = LV_FS_RES_OK;
71 
72     /* Add your code here*/
73     *bw = fwrite((char *)buf, 1, btw, file_p);
74 
75     return res;
76 }
77 
78 /**
79  * Set the read write pointer. Also expand the file size if necessary.
80  * @param drv pointer to a driver where this function belongs
81  * @param file_p pointer to a file_t variable. (opened with lv_ufs_open )
82  * @param pos the new position of read write pointer
83  * @return LV_FS_RES_OK: no error, the file is read
84  *         any error from lv_fs_res_t enum
85  */
fs_seek(lv_fs_drv_t * drv,void * file_p,uint32_t pos,lv_fs_whence_t whence)86 static lv_fs_res_t fs_seek(lv_fs_drv_t *drv, void *file_p, uint32_t pos, lv_fs_whence_t whence)
87 {
88     lv_fs_res_t res = LV_FS_RES_OK;
89 
90     /* Add your code here*/
91     fseek(file_p, pos, whence);
92 
93     return res;
94 }
95 
fs_tell(struct _lv_fs_drv_t * drv,void * file_p,uint32_t * pos_p)96 static lv_fs_res_t fs_tell(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p)
97 {
98     lv_fs_res_t res = LV_FS_RES_OK;
99 
100     *pos_p = ftell(file_p);
101 
102     return res;
103 }
104 
lv_port_fs_init(void)105 void lv_port_fs_init(void)
106 {
107     static lv_fs_drv_t fs_drv = {0};
108 
109     lv_fs_drv_init(&fs_drv);
110 
111     /*Set up fields...*/
112     fs_drv.letter = 'A';
113     fs_drv.open_cb = fs_open;
114     fs_drv.close_cb = fs_close;
115     fs_drv.read_cb = fs_read;
116     fs_drv.write_cb = fs_write;
117     fs_drv.seek_cb = fs_seek;
118     fs_drv.tell_cb = fs_tell;
119 
120     lv_fs_drv_register(&fs_drv);
121 }
122 #endif
123