@@ -21,24 +21,11 @@ sd_sdfat2_esp32.cpp - ESP3D sd support class
21
21
#if defined(ARDUINO_ARCH_ESP32) && defined(SD_DEVICE)
22
22
#if (SD_DEVICE == ESP_SDFAT2)
23
23
24
- #include < SdFat.h>
25
24
#include < sdios.h>
26
-
27
25
#include < stack>
28
-
29
26
#include " ../../../core/esp3d_settings.h"
30
27
#include " ../esp_sd.h"
31
28
32
- #if SDFAT_FILE_TYPE == 1
33
- typedef File32 File;
34
- #elif SDFAT_FILE_TYPE == 2
35
- typedef ExFile File;
36
- #elif SDFAT_FILE_TYPE == 3
37
- typedef FsFile File;
38
- #else // SDFAT_FILE_TYPE
39
- #error Invalid SDFAT_FILE_TYPE
40
- #endif // SDFAT_FILE_TYPE
41
-
42
29
// Try to select the best SD card configuration.
43
30
#if HAS_SDIO_CLASS
44
31
#define SD_CONFIG SdioConfig (FIFO_SDIO)
@@ -50,11 +37,11 @@ typedef FsFile File;
50
37
SdSpiConfig ((ESP_SD_CS_PIN == -1 ) ? SS : ESP_SD_CS_PIN, SHARED_SPI)
51
38
#endif // HAS_SDIO_CLASS
52
39
53
- extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
40
+ ESP3D_SD_Class ESP3D_SD_Card;
41
+ ESP3D_File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
54
42
55
43
// Max Freq Working
56
44
#define FREQMZ 40
57
- SdFat SD;
58
45
#undef FILE_WRITE
59
46
#undef FILE_READ
60
47
#undef FILE_APPEND
@@ -72,7 +59,7 @@ void dateTime(uint16_t* date, uint16_t* dtime) {
72
59
*dtime = FAT_TIME (tmstruct.tm_hour , tmstruct.tm_min , tmstruct.tm_sec );
73
60
}
74
61
75
- time_t getDateTimeFile (File & filehandle) {
62
+ time_t getDateTimeFile (ESP3D_File & filehandle) {
76
63
static time_t dt = 0 ;
77
64
#ifdef SD_TIMESTAMP_FEATURE
78
65
struct tm timefile;
@@ -127,7 +114,7 @@ uint8_t ESP_SD::getState(bool refresh) {
127
114
ESP_SD_MOSI_PIN != -1 ? ESP_SD_MOSI_PIN : MOSI,
128
115
ESP_SD_SCK_PIN != -1 ? ESP_SD_SCK_PIN : SCK);
129
116
// refresh content if card was removed
130
- if (SD .begin ((ESP_SD_CS_PIN == -1 ) ? SS : ESP_SD_CS_PIN,
117
+ if (ESP3D_SD_Card .begin ((ESP_SD_CS_PIN == -1 ) ? SS : ESP_SD_CS_PIN,
131
118
SD_SCK_MHZ (FREQMZ / _spi_speed_divider))) {
132
119
_state = ESP_SDCARD_IDLE;
133
120
}
@@ -180,12 +167,12 @@ void ESP_SD::refreshStats(bool force) {
180
167
181
168
uint64_t ESP_SD::totalBytes (bool refresh) {
182
169
static uint64_t _totalBytes = 0 ;
183
- if (!SD .volumeBegin ()) {
170
+ if (!ESP3D_SD_Card .volumeBegin ()) {
184
171
return 0 ;
185
172
}
186
173
if (refresh || _totalBytes == 0 ) {
187
- _totalBytes = SD .clusterCount ();
188
- uint8_t sectors = SD .sectorsPerCluster ();
174
+ _totalBytes = ESP3D_SD_Card .clusterCount ();
175
+ uint8_t sectors = ESP3D_SD_Card .sectorsPerCluster ();
189
176
_totalBytes = _totalBytes * sectors * 512 ;
190
177
}
191
178
return _totalBytes;
@@ -197,12 +184,12 @@ uint64_t ESP_SD::usedBytes(bool refresh) {
197
184
198
185
uint64_t ESP_SD::freeBytes (bool refresh) {
199
186
static uint64_t _freeBytes = 0 ;
200
- if (!SD .volumeBegin ()) {
187
+ if (!ESP3D_SD_Card .volumeBegin ()) {
201
188
return 0 ;
202
189
}
203
190
if (refresh || _freeBytes == 0 ) {
204
- _freeBytes = SD .freeClusterCount ();
205
- uint8_t sectors = SD .sectorsPerCluster ();
191
+ _freeBytes = ESP3D_SD_Card .freeClusterCount ();
192
+ uint8_t sectors = ESP3D_SD_Card .sectorsPerCluster ();
206
193
_freeBytes = _freeBytes * sectors * 512 ;
207
194
}
208
195
return _freeBytes;
@@ -211,7 +198,7 @@ uint64_t ESP_SD::freeBytes(bool refresh) {
211
198
uint ESP_SD::maxPathLength () { return 255 ; }
212
199
213
200
bool ESP_SD::rename (const char * oldpath, const char * newpath) {
214
- return SD .rename (oldpath, newpath);
201
+ return ESP3D_SD_Card .rename (oldpath, newpath);
215
202
}
216
203
217
204
bool ESP_SD::format () {
@@ -305,7 +292,7 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) {
305
292
return ESP_SDFile ();
306
293
}
307
294
}
308
- File tmp = SD .open (path, (mode == ESP_FILE_READ) ? FILE_READ
295
+ ESP3D_File tmp = ESP3D_SD_Card .open (path, (mode == ESP_FILE_READ) ? FILE_READ
309
296
: (mode == ESP_FILE_WRITE) ? FILE_WRITE
310
297
: FILE_WRITE);
311
298
if (tmp) {
@@ -325,7 +312,7 @@ bool ESP_SD::exists(const char* path) {
325
312
if (strcmp (path, " /" ) == 0 ) {
326
313
return _started;
327
314
}
328
- res = SD .exists (path);
315
+ res = ESP3D_SD_Card .exists (path);
329
316
if (!res) {
330
317
ESP_SDFile root = ESP_SD::open (path, ESP_FILE_READ);
331
318
if (root) {
@@ -337,10 +324,10 @@ bool ESP_SD::exists(const char* path) {
337
324
338
325
bool ESP_SD::remove (const char * path) {
339
326
_sizechanged = true ;
340
- return SD .remove (path);
327
+ return ESP3D_SD_Card .remove (path);
341
328
}
342
329
343
- bool ESP_SD::mkdir (const char * path) { return SD .mkdir (path); }
330
+ bool ESP_SD::mkdir (const char * path) { return ESP3D_SD_Card .mkdir (path); }
344
331
345
332
bool ESP_SD::rmdir (const char * path) {
346
333
String p = path;
@@ -357,9 +344,9 @@ bool ESP_SD::rmdir(const char* path) {
357
344
std::stack<String> pathlist;
358
345
pathlist.push (p);
359
346
while (pathlist.size () > 0 && res) {
360
- File dir = SD .open (pathlist.top ().c_str ());
347
+ ESP3D_File dir = ESP3D_SD_Card .open (pathlist.top ().c_str ());
361
348
dir.rewindDirectory ();
362
- File f = dir.openNextFile ();
349
+ ESP3D_File f = dir.openNextFile ();
363
350
bool candelete = true ;
364
351
while (f && res) {
365
352
if (f.isDir ()) {
@@ -371,22 +358,22 @@ bool ESP_SD::rmdir(const char* path) {
371
358
newdir += " /" ;
372
359
pathlist.push (newdir);
373
360
f.close ();
374
- f = File ();
361
+ f = ESP3D_File ();
375
362
} else {
376
363
char tmp[255 ];
377
364
f.getName (tmp, 254 );
378
365
_sizechanged = true ;
379
366
String filepath = pathlist.top () + tmp;
380
367
f.close ();
381
- if (!SD .remove (filepath.c_str ())) {
368
+ if (!ESP3D_SD_Card .remove (filepath.c_str ())) {
382
369
res = false ;
383
370
}
384
371
f = dir.openNextFile ();
385
372
}
386
373
}
387
374
if (candelete) {
388
375
if (pathlist.top () != " /" ) {
389
- res = SD .rmdir (pathlist.top ().c_str ());
376
+ res = ESP3D_SD_Card .rmdir (pathlist.top ().c_str ());
390
377
}
391
378
pathlist.pop ();
392
379
}
@@ -400,7 +387,7 @@ bool ESP_SD::rmdir(const char* path) {
400
387
void ESP_SD::closeAll () {
401
388
for (uint8_t i = 0 ; i < ESP_MAX_SD_OPENHANDLE; i++) {
402
389
tSDFile_handle[i].close ();
403
- tSDFile_handle[i] = File ();
390
+ tSDFile_handle[i] = ESP3D_File ();
404
391
}
405
392
}
406
393
@@ -427,7 +414,7 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode,
427
414
bool set = false ;
428
415
for (uint8_t i = 0 ; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) {
429
416
if (!tSDFile_handle[i]) {
430
- tSDFile_handle[i] = *((File *)handle);
417
+ tSDFile_handle[i] = *((ESP3D_File *)handle);
431
418
// filename
432
419
char tmp[255 ];
433
420
tSDFile_handle[i].getName (tmp, 254 );
@@ -470,7 +457,7 @@ const char* ESP_SDFile::shortname() const {
470
457
return _name.c_str ();
471
458
#else
472
459
static char sname[13 ];
473
- File ftmp = SD .open (_filename.c_str ());
460
+ ESP3D_File ftmp = ESP3D_SD_Card .open (_filename.c_str ());
474
461
if (ftmp) {
475
462
ftmp.getSFN (sname, 12 );
476
463
ftmp.close ();
@@ -491,14 +478,14 @@ void ESP_SDFile::close() {
491
478
// reopen if mode = write
492
479
// udate size + date
493
480
if (_iswritemode && !_isdir) {
494
- File ftmp = SD .open (_filename.c_str ());
481
+ ESP3D_File ftmp = ESP3D_SD_Card .open (_filename.c_str ());
495
482
if (ftmp) {
496
483
_size = ftmp.size ();
497
484
_lastwrite = getDateTimeFile (ftmp);
498
485
ftmp.close ();
499
486
}
500
487
}
501
- tSDFile_handle[_index] = File ();
488
+ tSDFile_handle[_index] = ESP3D_File ();
502
489
// esp3d_log("Closing File at index %d",_index);
503
490
_index = -1 ;
504
491
}
@@ -509,7 +496,7 @@ ESP_SDFile ESP_SDFile::openNextFile() {
509
496
esp3d_log (" openNextFile failed" );
510
497
return ESP_SDFile ();
511
498
}
512
- File tmp = tSDFile_handle[_index].openNextFile ();
499
+ ESP3D_File tmp = tSDFile_handle[_index].openNextFile ();
513
500
if (tmp) {
514
501
char tmps[255 ];
515
502
tmp.getName (tmps, 254 );
0 commit comments