ESP3D Lib  1.0
3D Printer WiFi Library
sd_ESP32.cpp
Go to the documentation of this file.
1 
23 #ifdef ARDUINO_ARCH_ESP32
24 #include "esplibconfig.h"
25 #if ENABLED(SDSUPPORT) && ENABLED(ESP3D_WIFISUPPORT)
26 #include MARLIN_PATH(sd/cardreader.h)
27 #include MARLIN_PATH(sd/SdVolume.h)
28 #include MARLIN_PATH(sd/SdFatStructs.h)
29 #include MARLIN_PATH(sd/SdFile.h)
30 #include "sd_ESP32.h"
31 
32 
33 //Cannot move to static variable due to conflict with ESP32 SD library
34 SdFile workDir;
35 dir_t dir_info;
36 SdVolume sd_volume;
37 
38 
40  _size = 0;
41  _pos = 0;
42  _readonly=true;
43  //ugly Workaround to not expose SdFile class which conflict with Native ESP32 SD class
44  _sdfile = new SdFile;
45 }
47 if (((SdFile *)_sdfile)->isOpen())close();
48 if (_sdfile) delete (SdFile *) _sdfile;
49 }
50 
51 bool ESP_SD::isopen(){
52  return (((SdFile *)_sdfile)->isOpen());
53 }
54 
55 int8_t ESP_SD::card_status(){
56 if (!IS_SD_INSERTED() || !card.isMounted()) return 0; //No sd
57 if ( card.isPrinting() || card.isFileOpen() ) return -1; // busy
58 return 1; //ok
59 }
60 
61 bool ESP_SD::open(const char * path, bool readonly ){
62  if (path == NULL) return false;
63  String fullpath=path;
64  String pathname = fullpath.substring(0,fullpath.lastIndexOf("/"));
65  String filename = makeshortname(fullpath.substring(fullpath.lastIndexOf("/")+1));
66  if (pathname.length() == 0)pathname="/";
67  if (!openDir(pathname))return false;
68  _pos = 0;
69  _readonly = readonly;
70  return ((SdFile *)_sdfile)->open(&workDir, filename.c_str(), readonly?O_READ:(O_CREAT | O_APPEND | O_WRITE | O_TRUNC));
71 }
72 
73 uint32_t ESP_SD::size(){
74  if(((SdFile *)_sdfile)->isOpen()) {
75  _size = ((SdFile *)_sdfile)->fileSize();
76  }
77  return _size ;
78 }
79 
80 uint32_t ESP_SD::available(){
81  if(!((SdFile *)_sdfile)->isOpen() || !_readonly) return 0;
82  _size = ((SdFile *)_sdfile)->fileSize();
83  if (_size == 0) return 0;
84 
85  return _size - _pos ;
86 }
87 
88 void ESP_SD::close(){
89  if(((SdFile *)_sdfile)->isOpen()) {
90  ((SdFile *)_sdfile)->sync();
91  _size = ((SdFile *)_sdfile)->fileSize();
92  ((SdFile *)_sdfile)->close();
93  }
94 }
95 
96 int16_t ESP_SD::write(const uint8_t * data, uint16_t len){
97  return ((SdFile *)_sdfile)->write(data, len);
98 }
99 
100 int16_t ESP_SD::write(const uint8_t byte){
101  return ((SdFile *)_sdfile)->write(&byte, 1);
102 }
103 
104 
105 bool ESP_SD::exists(const char * path){
106  bool response = open(path);
107  if (response) close();
108  return response;
109 }
110 
111 bool ESP_SD::remove(const char * path){
112  if (path == NULL) return false;
113  String fullpath=path;
114  String pathname = fullpath.substring(0,fullpath.lastIndexOf("/"));
115  String filename = makeshortname(fullpath.substring(fullpath.lastIndexOf("/")+1));
116  if (pathname.length() == 0)pathname="/";
117  if (!openDir(pathname))return false;
118  SdFile file;
119  return file.remove(&workDir, filename.c_str());
120 }
121 
122 bool ESP_SD::dir_exists(const char * path){
123  return openDir(path);
124 }
125 
126 bool ESP_SD::rmdir(const char * path){
127  if (path == NULL) return false;
128  String fullpath=path;
129  if (fullpath=="/") return false;
130  if (!openDir(fullpath))return false;
131  return workDir.rmRfStar();
132 }
133 
134 bool ESP_SD::mkdir(const char * path){
135  if (path == NULL) return false;
136  String fullpath=path;
137  String pathname = fullpath.substring(0,fullpath.lastIndexOf("/"));
138  String filename = makeshortname(fullpath.substring(fullpath.lastIndexOf("/")+1));
139  if (pathname.length() == 0)pathname="/";
140  if (!openDir(pathname))return false;
141  SdFile file;
142  return file.mkdir(&workDir, filename.c_str());
143 }
144 
145 int16_t ESP_SD::read(){
146  if (!_readonly) return 0;
147  int16_t v = ((SdFile *)_sdfile)->read();
148  if (v!=-1)_pos++;
149  return v;
150 
151 }
152 
153 uint16_t ESP_SD::read(uint8_t * buf, uint16_t nbyte){
154  if (!_readonly) return 0;
155  int16_t v = ((SdFile *)_sdfile)->read(buf, nbyte);
156  if (v!=-1)_pos+=v;
157  return v;
158 }
159 
160 String ESP_SD::get_path_part(String data, int index){
161  int found = 0;
162  int strIndex[] = {0, -1};
163  int maxIndex;
164  String no_res;
165  String s = data;
166  s.trim();
167  if (s.length() == 0) return no_res;
168  maxIndex = s.length()-1;
169  if ((s[0] == '/') && (s.length() > 1)){
170  String s2 = &s[1];
171  s = s2;
172  }
173  for(int i=0; i<=maxIndex && found<=index; i++){
174  if(s.charAt(i)=='/' || i==maxIndex){
175  found++;
176  strIndex[0] = strIndex[1]+1;
177  strIndex[1] = (i == maxIndex) ? i+1 : i;
178  }
179  }
180 
181  return found>index ? s.substring(strIndex[0], strIndex[1]) : no_res;
182 }
183 
184 String ESP_SD::makeshortname(String longname, uint8_t index){
185  String s = longname;
186  String part_name;
187  String part_ext;
188  //Sanity check name is uppercase and no space
189  s.replace(" ","");
190  s.toUpperCase();
191  int pos = s.lastIndexOf(".");
192  //do we have extension ?
193  if (pos != -1) {
194  part_name = s.substring(0,pos);
195  if (part_name.lastIndexOf(".") !=-1) {
196  part_name.replace(".","");
197  //trick for short name but force ~1 at the end
198  part_name+=" ";
199  }
200  part_ext = s.substring(pos+1,pos+4);
201  } else {
202  part_name = s;
203  }
204  //check is under 8 char
205  if (part_name.length() > 8) {
206  //if not cut and use index
207  //check file exists is not part of this function
208  part_name = part_name.substring(0,6);
209  part_name += "~" + String(index);
210  }
211  //remove the possible " " for the trick
212  part_name.replace(" ","");
213  //create full short name
214  if (part_ext.length() > 0) part_name+="." + part_ext;
215  return part_name;
216 }
217 
218 String ESP_SD::makepath83(String longpath){
219  String path;
220  String tmp;
221  int index = 0;
222  tmp = get_path_part(longpath,index);
223  while (tmp.length() > 0) {
224  path += "/";
225  //TODO need to check short name index (~1) match actually the long name...
226  path += makeshortname (tmp);
227  index++;
228  tmp = get_path_part(longpath,index);
229  }
230  return path;
231 }
232 
233 
234 uint32_t ESP_SD::card_total_space(){
235 
236  return (512.00) * (sd_volume.clusterCount()) * (sd_volume.blocksPerCluster());
237 }
238 uint32_t ESP_SD::card_used_space(){
239  return (512.00) * (sd_volume.clusterCount() - sd_volume.freeClusterCount() ) * (sd_volume.blocksPerCluster());
240 }
241 
242 bool ESP_SD::openDir(String path){
243  static SdFile root;
244  static String name;
245  int index = 0;
246  //SdFile *parent;
247  if(root.isOpen())root.close();
248  if (!sd_volume.init(&(card.getSd2Card()))) {
249  return false;
250  }
251  if (!root.openRoot(&sd_volume)){
252  return false;
253  }
254  root.rewind();
255  workDir = root;
256  //parent = &workDir;
257  name = get_path_part(path,index);
258  while ((name.length() > 0) && (name!="/")) {
259  SdFile newDir;
260  if (!newDir.open(&root, name.c_str(), O_READ)) {
261  return false;
262  }
263  workDir=newDir;
264  //parent = &workDir;
265  index++;
266  if (index > MAX_DIR_DEPTH) {
267  return false;
268  }
269  name = get_path_part(path,index);
270  }
271  return true;
272 }
273 //TODO may be add date and use a struct for all info
274 bool ESP_SD::readDir(char name[13], uint32_t * size, bool * isFile){
275  if ((name == NULL) || (size==NULL)) {
276  return false;
277  }
278  * size = 0;
279  name[0]= 0;
280  * isFile = false;
281 
282  if ((workDir.readDir(&dir_info, NULL)) > 0){
283  workDir.dirName(dir_info,name);
284  * size = dir_info.fileSize;
285  if (DIR_IS_FILE(&dir_info))* isFile = true;
286  return true;
287  }
288  return false;
289 }
290 
291 
292 //TODO
293 /*
294 bool SD_file_timestamp(const char * path, uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
295  uint8_t hour, uint8_t minute, uint8_t second){
296 }**/
297 
298 #endif
299 
300 #endif
ESP_SD::read
int16_t read()
sd_ESP32.h
ESP_SD::card_used_space
uint32_t card_used_space()
ESP_SD::makepath83
String makepath83(String longpath)
ESP_SD::available
uint32_t available()
ESP_SD::ESP_SD
ESP_SD()
ESP_SD::makeshortname
String makeshortname(String longname, uint8_t index=1)
ESP_SD::mkdir
bool mkdir(const char *path)
ESP_SD::exists
bool exists(const char *path)
ESP_SD::card_status
int8_t card_status()
ESP_SD::isopen
bool isopen()
ESP_SD::remove
bool remove(const char *path)
ESP_SD::write
int16_t write(const uint8_t *data, uint16_t len)
esplibconfig.h
ESP_SD::close
void close()
ESP_SD::dir_exists
bool dir_exists(const char *path)
ESP_SD::openDir
bool openDir(String path)
ESP_SD::size
uint32_t size()
ESP_SD::rmdir
bool rmdir(const char *path)
ESP_SD::isFile
bool * isFile
Definition: sd_ESP32.h:50
ESP_SD::card_total_space
uint32_t card_total_space()
ESP_SD::~ESP_SD
~ESP_SD()
ESP_SD::readDir
bool readDir(char name[13], uint32_t *size, bool *isFile)
ESP_SD::open
bool open(const char *path, bool readonly=true)