ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
lv_flash_drv.cpp
Go to the documentation of this file.
1 /*
2  lv_flash_drv.cpp - ESP3D flash (spiffs/Fat/etc) driver for lvgl
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "../../include/esp3d_config.h"
21 #if defined (DISPLAY_DEVICE) && (DISPLAY_UI_TYPE == UI_TYPE_ADVANCED)
22 #include "lv_flash_drv.h"
23 #ifndef FILESYSTEM_FEATURE
24 #error No FileSystem defined
25 #endif
26 #if FILESYSTEM_FEATURE == ESP_SPIFFS_FILESYSTEM
27 #include "SPIFFS.h"
28 #else
29 #if FILESYSTEM_FEATURE == ESP_FAT_FILESYSTEM
30 #include "FFat.h"
31 #else
32 #error Unknow FileSystem defined
33 #endif
34 #endif
35 
36 //Driver wrote based on lvg 6.0.3 documentation
37 
47 lv_fs_res_t esp_flash_open(lv_fs_drv_t * drv, void * file_p, const char * fn, lv_fs_mode_t mode)
48 {
49  (void) drv; /*Unused*/
50  const char * flags = "";
51  if(mode == LV_FS_MODE_WR) {
52  flags = "w";
53  } else if(mode == LV_FS_MODE_RD) {
54  flags = "r";
55  } else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) {
56  flags = "a";
57  }
58  char buf[256];
59  sprintf(buf, "/%s", fn);
60  File * f= new File();
61  if (f == nullptr) {
62  return LV_FS_RES_OUT_OF_MEM;
63  }
64 #if FILESYSTEM_FEATURE == ESP_SPIFFS_FILESYSTEM
65  *f = SPIFFS.open(buf, flags);
66 #else
67  *f = FFat.open(buf, flags);
68 #endif
69  if(!(*f)) {
70  return LV_FS_RES_UNKNOWN;
71  } else {
72  f->seek(0);
73  file_t * ft = (file_t *)file_p;
74  ft->fileobjecthandle = f;
75  }
76  return LV_FS_RES_OK;
77 }
78 
79 
87 lv_fs_res_t esp_flash_close(lv_fs_drv_t * drv, void * file_p)
88 {
89  (void) drv; /*Unused*/
90  file_t * ft = (file_t *)file_p;
91  if(ft->fileobjecthandle) {
92  (ft->fileobjecthandle)->close();
93  delete(ft->fileobjecthandle);
94  ft->fileobjecthandle = nullptr;
95  }
96  return LV_FS_RES_OK;
97 }
98 
109 lv_fs_res_t esp_flash_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br)
110 {
111  (void) drv; /*Unused*/
112  file_t * ft = (file_t *)file_p;
113  if(ft->fileobjecthandle) {
114  *br = (ft->fileobjecthandle)->read((uint8_t*)buf,btr);
115  return LV_FS_RES_OK;
116  } else {
117  return LV_FS_RES_UNKNOWN;
118  }
119 }
120 
129 lv_fs_res_t esp_flash_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos)
130 {
131  (void) drv; /*Unused*/
132  file_t * ft = (file_t *)file_p;
133  if(ft->fileobjecthandle) {
134  (ft->fileobjecthandle)->seek(pos);
135  return LV_FS_RES_OK;
136  } else {
137  return LV_FS_RES_UNKNOWN;
138  }
139 }
140 
149 lv_fs_res_t esp_flash_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p)
150 {
151  (void) drv; /*Unused*/
152  file_t * ft = (file_t *)file_p;
153  if(ft->fileobjecthandle) {
154  *pos_p = (ft->fileobjecthandle)->position();
155  return LV_FS_RES_OK;
156  } else {
157  return LV_FS_RES_UNKNOWN;
158  }
159 }
160 
161 #endif //(DISPLAY_DEVICE) && (DISPLAY_UI_TYPE == UI_TYPE_ADVANCED)
lv_flash_drv.h
file_t::fileobjecthandle
File * fileobjecthandle
Definition: lv_flash_drv.h:31
file_t
Definition: lv_flash_drv.h:28
esp_flash_tell
lv_fs_res_t esp_flash_tell(lv_fs_drv_t *drv, void *file_p, uint32_t *pos_p)
esp_flash_seek
lv_fs_res_t esp_flash_seek(lv_fs_drv_t *drv, void *file_p, uint32_t pos)
esp_flash_read
lv_fs_res_t esp_flash_read(lv_fs_drv_t *drv, void *file_p, void *buf, uint32_t btr, uint32_t *br)
esp_flash_open
lv_fs_res_t esp_flash_open(lv_fs_drv_t *drv, void *file_p, const char *fn, lv_fs_mode_t mode)
esp_flash_close
lv_fs_res_t esp_flash_close(lv_fs_drv_t *drv, void *file_p)