Sd refactoring (#1086)

This commit is contained in:
Luc 2025-03-26 14:49:45 +08:00 committed by GitHub
parent 5501e78bcb
commit 8a6334e9e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 221 additions and 189 deletions

View File

@ -22,7 +22,7 @@
#define _VERSION_ESP3D_H #define _VERSION_ESP3D_H
// version and sources location // version and sources location
#define FW_VERSION "3.0.0.4b1" #define FW_VERSION "3.0.0.5b1"
#define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0" #define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0"
#endif //_VERSION_ESP3D_H #endif //_VERSION_ESP3D_H

View File

@ -23,7 +23,7 @@
#include <time.h> #include <time.h>
#include "esp_sd.h" #include "esp_sd.h"
/*
#define ESP_MAX_SD_OPENHANDLE 4 #define ESP_MAX_SD_OPENHANDLE 4
#if (SD_DEVICE == ESP_SD_NATIVE) && defined(ARDUINO_ARCH_ESP8266) #if (SD_DEVICE == ESP_SD_NATIVE) && defined(ARDUINO_ARCH_ESP8266)
#define FS_NO_GLOBALS #define FS_NO_GLOBALS
@ -62,7 +62,7 @@ File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
#else #else
#include <FS.h> #include <FS.h>
File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
#endif #endif*/
#if defined(ESP3DLIB_ENV) #if defined(ESP3DLIB_ENV)
#include "../../include/Marlin/cardreader.h" #include "../../include/Marlin/cardreader.h"
@ -138,6 +138,8 @@ bool ESP_SD::disableSharedSD() {
bool ESP_SD::_started = false; bool ESP_SD::_started = false;
uint8_t ESP_SD::_state = ESP_SDCARD_NOT_PRESENT; uint8_t ESP_SD::_state = ESP_SDCARD_NOT_PRESENT;
uint8_t ESP_SD::_spi_speed_divider = 1; uint8_t ESP_SD::_spi_speed_divider = 1;
bool ESP_SD::_sizechanged = true; bool ESP_SD::_sizechanged = true;
uint8_t ESP_SD::setState(uint8_t flag) { uint8_t ESP_SD::setState(uint8_t flag) {
_state = flag; _state = flag;

View File

@ -20,14 +20,9 @@
#ifndef _ESP_SD_H #ifndef _ESP_SD_H
#define _ESP_SD_H #define _ESP_SD_H
#include <time.h>
#include "../../core/esp3d_message.h"
#include "../../include/esp3d_config.h" #include "../../include/esp3d_config.h"
#include "sd/esp_sd_common.h"
#define ESP_SD_FS_HEADER "/SD"
#define ESP_MAX_SD_OPENHANDLE 4
class ESP_SDFile { class ESP_SDFile {
public: public:

View File

@ -0,0 +1,85 @@
/*
esp_sd_common.h - ESP3D SD support class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include "../../../include/esp3d_config.h"
#define ESP_SD_FS_HEADER "/SD"
#define ESP_MAX_SD_OPENHANDLE 4
#if (SD_DEVICE == ESP_SD_NATIVE) && defined(ARDUINO_ARCH_ESP8266)
#define FS_NO_GLOBALS
#define NO_GLOBAL_SD
#include <SD.h>
using ESP3D_File = fs::File;
using ESP3D_SD_Class = SDClass;
#endif // (SD_DEVICE == ESP_SD_NATIVE) && defined(ARDUINO_ARCH_ESP8266
#if ((SD_DEVICE == ESP_SDFAT) || (SD_DEVICE == ESP_SDFAT2)) && defined(ARDUINO_ARCH_ESP8266)
#define FS_NO_GLOBALS
#define NO_GLOBAL_SD
#define DISABLE_FS_H_WARNING 1
#include <SdFat.h>
#if SDFAT_FILE_TYPE == 1
using ESP3D_File = File32;
#elif SDFAT_FILE_TYPE == 2
using ESP3D_File = ExFile;
#elif SDFAT_FILE_TYPE == 3
using ESP3D_File = FsFile;
#else // SDFAT_FILE_TYPE
#error Invalid SDFAT_FILE_TYPE
#endif // SDFAT_FILE_TYPE
using ESP3D_SD_Class = SdFat;
#endif // ((SD_DEVICE == ESP_SDFAT) || (SD_DEVICE == ESP_SDFAT2)) && defined(ARDUINO_ARCH_ESP8266
#if ((SD_DEVICE == ESP_SDFAT) || (SD_DEVICE == ESP_SDFAT2)) && defined(ARDUINO_ARCH_ESP32)
#define DISABLE_FS_H_WARNING 1
#include <SdFat.h>
#if SDFAT_FILE_TYPE == 1
using ESP3D_File = File32;
#elif SDFAT_FILE_TYPE == 2
using ESP3D_File = ExFile;
#elif SDFAT_FILE_TYPE == 3
using ESP3D_File = FsFile;
#else // SDFAT_FILE_TYPE
#error Invalid SDFAT_FILE_TYPE
#endif // SDFAT_FILE_TYPE
using ESP3D_SD_Class = SdFat;
#endif // ((SD_DEVICE == ESP_SDFAT) || (SD_DEVICE == ESP_SDFAT2)) && defined(ARDUINO_ARCH_ESP32
#if (SD_DEVICE == ESP_SD_NATIVE) && defined(ARDUINO_ARCH_ESP32)
#include <FS.h>
#include <SD.h>
using ESP3D_File = fs::File;
using ESP3D_SD_Class = fs::SDFS;
#endif // (SD_DEVICE == ESP_SD_NATIVE) && defined(ARDUINO_ARCH_ESP32
#if (SD_DEVICE == ESP_SDIO) && defined(ARDUINO_ARCH_ESP32)
#include <FS.h>
#include <SD_MMC.h>
using ESP3D_File = fs::File;
using ESP3D_SD_Class = fs::SDMMCFS;
#endif // (SD_DEVICE == ESP_SDIO) && defined(ARDUINO_ARCH_ESP32
extern ESP3D_SD_Class ESP3D_SD_Card;
extern ESP3D_File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];

View File

@ -24,14 +24,13 @@ sd_native_esp32.cpp - ESP3D sd support class
#include "../../../core/esp3d_settings.h" #include "../../../core/esp3d_settings.h"
#include "../esp_sd.h" #include "../esp_sd.h"
#include "FS.h"
#include "SD.h"
// TBC: base frequency // TBC: base frequency
// or use (1000000 * ESP.getCpuFreqMHz()) // or use (1000000 * ESP.getCpuFreqMHz())
#define ESP_SPI_FREQ 4000000 #define ESP_SPI_FREQ 4000000
extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; ESP3D_SD_Class ESP3D_SD_Card = SD;
ESP3D_File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
uint8_t ESP_SD::getState(bool refresh) { uint8_t ESP_SD::getState(bool refresh) {
#if defined(ESP_SD_DETECT_PIN) && ESP_SD_DETECT_PIN != -1 #if defined(ESP_SD_DETECT_PIN) && ESP_SD_DETECT_PIN != -1
@ -53,7 +52,7 @@ uint8_t ESP_SD::getState(bool refresh) {
} }
// SD is idle or not detected, let see if still the case // SD is idle or not detected, let see if still the case
SD.end(); ESP3D_SD_Card.end();
_state = ESP_SDCARD_NOT_PRESENT; _state = ESP_SDCARD_NOT_PRESENT;
// using default value for speed ? should be parameter // using default value for speed ? should be parameter
// refresh content if card was removed // refresh content if card was removed
@ -62,9 +61,9 @@ uint8_t ESP_SD::getState(bool refresh) {
ESP_SD_MISO_PIN != -1 ? ESP_SD_MISO_PIN : MISO, ESP_SD_MISO_PIN != -1 ? ESP_SD_MISO_PIN : MISO,
ESP_SD_MOSI_PIN != -1 ? ESP_SD_MOSI_PIN : MOSI, ESP_SD_MOSI_PIN != -1 ? ESP_SD_MOSI_PIN : MOSI,
ESP_SD_SCK_PIN != -1 ? ESP_SD_SCK_PIN : SCK); ESP_SD_SCK_PIN != -1 ? ESP_SD_SCK_PIN : SCK);
if (SD.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, SPI, if (ESP3D_SD_Card.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, SPI,
ESP_SPI_FREQ / _spi_speed_divider)) { ESP_SPI_FREQ / _spi_speed_divider)) {
if (SD.cardSize() > 0) { if (ESP3D_SD_Card.cardSize() > 0) {
_state = ESP_SDCARD_IDLE; _state = ESP_SDCARD_IDLE;
} }
} }
@ -114,7 +113,7 @@ bool ESP_SD::begin() {
} }
void ESP_SD::end() { void ESP_SD::end() {
SD.end(); ESP3D_SD_Card.end();
_state = ESP_SDCARD_NOT_PRESENT; _state = ESP_SDCARD_NOT_PRESENT;
_started = false; _started = false;
} }
@ -129,7 +128,7 @@ void ESP_SD::refreshStats(bool force) {
uint64_t ESP_SD::totalBytes(bool refresh) { uint64_t ESP_SD::totalBytes(bool refresh) {
static uint64_t _totalBytes = 0; static uint64_t _totalBytes = 0;
if (refresh || _totalBytes == 0) { if (refresh || _totalBytes == 0) {
_totalBytes = SD.totalBytes(); _totalBytes = ESP3D_SD_Card.totalBytes();
; ;
} }
return _totalBytes; return _totalBytes;
@ -138,7 +137,7 @@ uint64_t ESP_SD::totalBytes(bool refresh) {
uint64_t ESP_SD::usedBytes(bool refresh) { uint64_t ESP_SD::usedBytes(bool refresh) {
static uint64_t _usedBytes = 0; static uint64_t _usedBytes = 0;
if (refresh || _usedBytes == 0) { if (refresh || _usedBytes == 0) {
_usedBytes = SD.usedBytes(); _usedBytes = ESP3D_SD_Card.usedBytes();
} }
return _usedBytes; return _usedBytes;
} }
@ -151,7 +150,7 @@ uint ESP_SD::maxPathLength() { return 255; }
bool ESP_SD::rename(const char *oldpath, const char *newpath) { bool ESP_SD::rename(const char *oldpath, const char *newpath) {
esp3d_log("rename %s to %s", oldpath, newpath); esp3d_log("rename %s to %s", oldpath, newpath);
return SD.rename(oldpath, newpath); return ESP3D_SD_Card.rename(oldpath, newpath);
} }
bool ESP_SD::format() { bool ESP_SD::format() {
@ -180,7 +179,7 @@ ESP_SDFile ESP_SD::open(const char *path, uint8_t mode) {
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD.open(path, (mode == ESP_FILE_READ) ? FILE_READ ESP3D_File tmp = ESP3D_SD_Card.open(path, (mode == ESP_FILE_READ) ? FILE_READ
: (mode == ESP_FILE_WRITE) ? FILE_WRITE : (mode == ESP_FILE_WRITE) ? FILE_WRITE
: FILE_APPEND); : FILE_APPEND);
ESP_SDFile esptmp(&tmp, tmp.isDirectory(), ESP_SDFile esptmp(&tmp, tmp.isDirectory(),
@ -198,23 +197,23 @@ bool ESP_SD::exists(const char *path) {
if (p.endsWith("/")) { if (p.endsWith("/")) {
p.remove(p.length() - 1, 1); p.remove(p.length() - 1, 1);
} }
res = SD.exists(p); res = ESP3D_SD_Card.exists(p);
if (!res) { if (!res) {
// check if it is a directory // check if it is a directory
p += '/'; p += '/';
res = SD.exists(p); res = ESP3D_SD_Card.exists(p);
} }
return res; return res;
} }
bool ESP_SD::remove(const char *path) { return SD.remove(path); } bool ESP_SD::remove(const char *path) { return ESP3D_SD_Card.remove(path); }
bool ESP_SD::mkdir(const char *path) { bool ESP_SD::mkdir(const char *path) {
String p = path; String p = path;
if (p.endsWith("/")) { if (p.endsWith("/")) {
p.remove(p.length() - 1, 1); p.remove(p.length() - 1, 1);
} }
return SD.mkdir(p.c_str()); return ESP3D_SD_Card.mkdir(p.c_str());
} }
bool ESP_SD::rmdir(const char *path) { bool ESP_SD::rmdir(const char *path) {
@ -234,8 +233,8 @@ bool ESP_SD::rmdir(const char *path) {
std::stack<String> pathlist; std::stack<String> pathlist;
pathlist.push(p); pathlist.push(p);
while (pathlist.size() > 0 && res) { while (pathlist.size() > 0 && res) {
File dir = SD.open(pathlist.top().c_str()); ESP3D_File dir = ESP3D_SD_Card.open(pathlist.top().c_str());
File f = dir.openNextFile(); ESP3D_File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f && res) { while (f && res) {
if (f.isDirectory()) { if (f.isDirectory()) {
@ -244,12 +243,12 @@ bool ESP_SD::rmdir(const char *path) {
newdir += f.name(); newdir += f.name();
pathlist.push(newdir); pathlist.push(newdir);
f.close(); f.close();
f = File(); f =ESP3D_File();
} else { } else {
String filepath = pathlist.top() + '/'; String filepath = pathlist.top() + '/';
filepath += f.name(); filepath += f.name();
f.close(); f.close();
if (!SD.remove(filepath.c_str())) { if (!ESP3D_SD_Card.remove(filepath.c_str())) {
res = false; res = false;
} }
f = dir.openNextFile(); f = dir.openNextFile();
@ -257,7 +256,7 @@ bool ESP_SD::rmdir(const char *path) {
} }
if (candelete) { if (candelete) {
if (pathlist.top() != "/") { if (pathlist.top() != "/") {
res = SD.rmdir(pathlist.top().c_str()); res = ESP3D_SD_Card.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
@ -271,7 +270,7 @@ bool ESP_SD::rmdir(const char *path) {
void ESP_SD::closeAll() { void ESP_SD::closeAll() {
for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) { for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) {
tSDFile_handle[i].close(); tSDFile_handle[i].close();
tSDFile_handle[i] = File(); tSDFile_handle[i] =ESP3D_File();
} }
} }
@ -291,7 +290,7 @@ ESP_SDFile::ESP_SDFile(void *handle, bool isdir, bool iswritemode,
bool set = false; bool set = false;
for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) { for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) {
if (!tSDFile_handle[i]) { if (!tSDFile_handle[i]) {
tSDFile_handle[i] = *((File *)handle); tSDFile_handle[i] = *((ESP3D_File *)handle);
// filename // filename
_name = tSDFile_handle[i].name(); _name = tSDFile_handle[i].name();
_filename = path; _filename = path;
@ -331,14 +330,14 @@ void ESP_SDFile::close() {
// reopen if mode = write // reopen if mode = write
// udate size + date // udate size + date
if (_iswritemode && !_isdir) { if (_iswritemode && !_isdir) {
File ftmp = SD.open(_filename.c_str()); ESP3D_File ftmp = ESP3D_SD_Card.open(_filename.c_str());
if (ftmp) { if (ftmp) {
_size = ftmp.size(); _size = ftmp.size();
_lastwrite = ftmp.getLastWrite(); _lastwrite = ftmp.getLastWrite();
ftmp.close(); ftmp.close();
} }
} }
tSDFile_handle[_index] = File(); tSDFile_handle[_index] =ESP3D_File();
// esp3d_log("Closing File at index %d",_index); // esp3d_log("Closing File at index %d",_index);
_index = -1; _index = -1;
} }
@ -349,7 +348,7 @@ ESP_SDFile ESP_SDFile::openNextFile() {
esp3d_log("openNextFile failed"); esp3d_log("openNextFile failed");
return ESP_SDFile(); return ESP_SDFile();
} }
File tmp = tSDFile_handle[_index].openNextFile(); ESP3D_File tmp = tSDFile_handle[_index].openNextFile();
if (tmp) { if (tmp) {
esp3d_log("tmp name :%s %s %s", tmp.name(), esp3d_log("tmp name :%s %s %s", tmp.name(),
(tmp.isDirectory()) ? "isDir" : "isFile", _filename.c_str()); (tmp.isDirectory()) ? "isDir" : "isFile", _filename.c_str());

View File

@ -20,16 +20,13 @@ sd_native_esp8266.cpp - ESP3D sd support class
#include "../../../include/esp3d_config.h" #include "../../../include/esp3d_config.h"
#if defined(ARDUINO_ARCH_ESP8266) && defined(SD_DEVICE) #if defined(ARDUINO_ARCH_ESP8266) && defined(SD_DEVICE)
#if (SD_DEVICE == ESP_SD_NATIVE) #if (SD_DEVICE == ESP_SD_NATIVE)
#define FS_NO_GLOBALS
#include <SD.h>
#include <SDFS.h>
#include <stack> #include <stack>
#include "../../../core/esp3d_settings.h" #include "../../../core/esp3d_settings.h"
#include "../esp_sd.h" #include "../esp_sd.h"
extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; ESP3D_SD_Class ESP3D_SD_Card;
ESP3D_File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
void dateTime(uint16_t* date, uint16_t* dtime) { void dateTime(uint16_t* date, uint16_t* dtime) {
struct tm tmstruct; struct tm tmstruct;
@ -41,30 +38,10 @@ void dateTime(uint16_t* date, uint16_t* dtime) {
*dtime = FAT_TIME(tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec); *dtime = FAT_TIME(tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
} }
time_t getDateTimeFile(File& filehandle) { time_t getDateTimeFile(ESP3D_File& filehandle) {
static time_t dt = 0; static time_t dt = 0;
#ifdef SD_TIMESTAMP_FEATURE #ifdef SD_TIMESTAMP_FEATURE
struct tm timefile; dt = filehandle.getLastWrite();
dir_t d;
if (filehandle) {
if (filehandle.dirEntry(&d)) {
timefile.tm_year = FAT_YEAR(d.lastWriteDate) - 1900;
timefile.tm_mon = FAT_MONTH(d.lastWriteDate) - 1;
timefile.tm_mday = FAT_DAY(d.lastWriteDate);
timefile.tm_hour = FAT_HOUR(d.lastWriteTime);
timefile.tm_min = FAT_MINUTE(d.lastWriteTime);
timefile.tm_sec = FAT_SECOND(d.lastWriteTime);
timefile.tm_isdst = -1;
dt = mktime(&timefile);
if (dt == -1) {
esp3d_log_e("mktime failed");
}
} else {
esp3d_log_e("stat file failed");
}
} else {
esp3d_log("check file for stat failed");
}
#endif // SD_TIMESTAMP_FEATURE #endif // SD_TIMESTAMP_FEATURE
return dt; return dt;
} }
@ -96,10 +73,10 @@ uint8_t ESP_SD::getState(bool refresh) {
// SD is idle or not detected, let see if still the case // SD is idle or not detected, let see if still the case
_state = ESP_SDCARD_NOT_PRESENT; _state = ESP_SDCARD_NOT_PRESENT;
// refresh content if card was removed // refresh content if card was removed
if (SD.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, if (ESP3D_SD_Card.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN,
SD_SCK_HZ(F_CPU / _spi_speed_divider))) { SD_SCK_HZ(F_CPU / _spi_speed_divider))) {
esp3d_log("Init SD State ok"); esp3d_log("Init SD State ok");
if (SD.size64() > 0) { if (ESP3D_SD_Card.size64() > 0) {
esp3d_log("SD available"); esp3d_log("SD available");
_state = ESP_SDCARD_IDLE; _state = ESP_SDCARD_IDLE;
} else { } else {
@ -152,7 +129,7 @@ void ESP_SD::refreshStats(bool force) {
uint64_t ESP_SD::totalBytes(bool refresh) { uint64_t ESP_SD::totalBytes(bool refresh) {
static uint64_t _totalBytes = 0; static uint64_t _totalBytes = 0;
if (refresh || _totalBytes == 0) { if (refresh || _totalBytes == 0) {
_totalBytes = SD.size64(); _totalBytes = ESP3D_SD_Card.size64();
; ;
} }
return _totalBytes; return _totalBytes;
@ -207,7 +184,7 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) {
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD.open(path, (mode == ESP_FILE_READ) ? FILE_READ ESP3D_File tmp = ESP3D_SD_Card.open(path, (mode == ESP_FILE_READ) ? FILE_READ
: (mode == ESP_FILE_WRITE) ? FILE_WRITE : (mode == ESP_FILE_WRITE) ? FILE_WRITE
: FILE_WRITE); : FILE_WRITE);
ESP_SDFile esptmp(&tmp, tmp.isDirectory(), ESP_SDFile esptmp(&tmp, tmp.isDirectory(),
@ -222,7 +199,7 @@ bool ESP_SD::exists(const char* path) {
return _started; return _started;
} }
esp3d_log("%s exists ?", path); esp3d_log("%s exists ?", path);
res = SD.exists(path); res = ESP3D_SD_Card.exists(path);
if (!res) { if (!res) {
esp3d_log("Seems not - trying open it"); esp3d_log("Seems not - trying open it");
ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ); ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ);
@ -236,10 +213,10 @@ bool ESP_SD::exists(const char* path) {
bool ESP_SD::remove(const char* path) { bool ESP_SD::remove(const char* path) {
_sizechanged = true; _sizechanged = true;
return SD.remove(path); return ESP3D_SD_Card.remove(path);
} }
bool ESP_SD::mkdir(const char* path) { return SD.mkdir(path); } bool ESP_SD::mkdir(const char* path) { return ESP3D_SD_Card.mkdir(path); }
bool ESP_SD::rmdir(const char* path) { bool ESP_SD::rmdir(const char* path) {
String p = path; String p = path;
@ -256,9 +233,9 @@ bool ESP_SD::rmdir(const char* path) {
std::stack<String> pathlist; std::stack<String> pathlist;
pathlist.push(p); pathlist.push(p);
while (pathlist.size() > 0 && res) { while (pathlist.size() > 0 && res) {
File dir = SD.open(pathlist.top().c_str()); ESP3D_File dir = ESP3D_SD_Card.open(pathlist.top().c_str());
dir.rewindDirectory(); dir.rewindDirectory();
File f = dir.openNextFile(); ESP3D_File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f && res) { while (f && res) {
if (f.isDirectory()) { if (f.isDirectory()) {
@ -267,12 +244,12 @@ bool ESP_SD::rmdir(const char* path) {
newdir += "/"; newdir += "/";
pathlist.push(newdir); pathlist.push(newdir);
f.close(); f.close();
f = File(); f =ESP3D_File();
} else { } else {
_sizechanged = true; _sizechanged = true;
String filepath = pathlist.top() + f.name(); String filepath = pathlist.top() + f.name();
f.close(); f.close();
if (!SD.remove(filepath.c_str())) { if (!ESP3D_SD_Card.remove(filepath.c_str())) {
res = false; res = false;
} }
f = dir.openNextFile(); f = dir.openNextFile();
@ -280,7 +257,7 @@ bool ESP_SD::rmdir(const char* path) {
} }
if (candelete) { if (candelete) {
if (pathlist.top() != "/") { if (pathlist.top() != "/") {
res = SD.rmdir(pathlist.top().c_str()); res = ESP3D_SD_Card.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
@ -298,7 +275,7 @@ bool ESP_SDFile::seek(uint32_t pos, uint8_t mode) {
void ESP_SD::closeAll() { void ESP_SD::closeAll() {
for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) { for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) {
tSDFile_handle[i].close(); tSDFile_handle[i].close();
tSDFile_handle[i] = File(); tSDFile_handle[i] =ESP3D_File();
} }
} }
@ -318,7 +295,7 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode,
bool set = false; bool set = false;
for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) { for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) {
if (!tSDFile_handle[i]) { if (!tSDFile_handle[i]) {
tSDFile_handle[i] = *((File*)handle); tSDFile_handle[i] = *((ESP3D_File*)handle);
// filename // filename
_filename = path; _filename = path;
// name // name
@ -366,15 +343,15 @@ void ESP_SDFile::close() {
// reopen if mode = write // reopen if mode = write
// udate size + date // udate size + date
if (_iswritemode && !_isdir) { if (_iswritemode && !_isdir) {
File ftmp = SD.open(_filename.c_str()); ESP3D_File ftmp = ESP3D_SD_Card.open(_filename.c_str());
if (ftmp) { if (ftmp) {
_size = ftmp.size(); _size = ftmp.size();
_lastwrite = getDateTimeFile(ftmp); _lastwrite = getDateTimeFile(ftmp);
ftmp.close(); ftmp.close();
} }
} }
tSDFile_handle[_index] = File(); tSDFile_handle[_index] =ESP3D_File();
// esp3d_log("Closing File at index %d",_index); // esp3d_log("ClosingESP3D_File at index %d",_index);
_index = -1; _index = -1;
} }
} }
@ -384,7 +361,7 @@ ESP_SDFile ESP_SDFile::openNextFile() {
esp3d_log_e("openNextFile failed"); esp3d_log_e("openNextFile failed");
return ESP_SDFile(); return ESP_SDFile();
} }
File tmp = tSDFile_handle[_index].openNextFile(); ESP3D_File tmp = tSDFile_handle[_index].openNextFile();
if (tmp) { if (tmp) {
String name = tmp.name(); String name = tmp.name();
esp3d_log("tmp name :%s %s", name.c_str(), esp3d_log("tmp name :%s %s", name.c_str(),

View File

@ -21,24 +21,11 @@ sd_sdfat2_esp32.cpp - ESP3D sd support class
#if defined(ARDUINO_ARCH_ESP32) && defined(SD_DEVICE) #if defined(ARDUINO_ARCH_ESP32) && defined(SD_DEVICE)
#if (SD_DEVICE == ESP_SDFAT2) #if (SD_DEVICE == ESP_SDFAT2)
#include <SdFat.h>
#include <sdios.h> #include <sdios.h>
#include <stack> #include <stack>
#include "../../../core/esp3d_settings.h" #include "../../../core/esp3d_settings.h"
#include "../esp_sd.h" #include "../esp_sd.h"
#if SDFAT_FILE_TYPE == 1
typedef File32 File;
#elif SDFAT_FILE_TYPE == 2
typedef ExFile File;
#elif SDFAT_FILE_TYPE == 3
typedef FsFile File;
#else // SDFAT_FILE_TYPE
#error Invalid SDFAT_FILE_TYPE
#endif // SDFAT_FILE_TYPE
// Try to select the best SD card configuration. // Try to select the best SD card configuration.
#if HAS_SDIO_CLASS #if HAS_SDIO_CLASS
#define SD_CONFIG SdioConfig(FIFO_SDIO) #define SD_CONFIG SdioConfig(FIFO_SDIO)
@ -50,11 +37,11 @@ typedef FsFile File;
SdSpiConfig((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, SHARED_SPI) SdSpiConfig((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, SHARED_SPI)
#endif // HAS_SDIO_CLASS #endif // HAS_SDIO_CLASS
extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; ESP3D_SD_Class ESP3D_SD_Card;
ESP3D_File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
// Max Freq Working // Max Freq Working
#define FREQMZ 40 #define FREQMZ 40
SdFat SD;
#undef FILE_WRITE #undef FILE_WRITE
#undef FILE_READ #undef FILE_READ
#undef FILE_APPEND #undef FILE_APPEND
@ -72,7 +59,7 @@ void dateTime(uint16_t* date, uint16_t* dtime) {
*dtime = FAT_TIME(tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec); *dtime = FAT_TIME(tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
} }
time_t getDateTimeFile(File& filehandle) { time_t getDateTimeFile(ESP3D_File& filehandle) {
static time_t dt = 0; static time_t dt = 0;
#ifdef SD_TIMESTAMP_FEATURE #ifdef SD_TIMESTAMP_FEATURE
struct tm timefile; struct tm timefile;
@ -127,7 +114,7 @@ uint8_t ESP_SD::getState(bool refresh) {
ESP_SD_MOSI_PIN != -1 ? ESP_SD_MOSI_PIN : MOSI, ESP_SD_MOSI_PIN != -1 ? ESP_SD_MOSI_PIN : MOSI,
ESP_SD_SCK_PIN != -1 ? ESP_SD_SCK_PIN : SCK); ESP_SD_SCK_PIN != -1 ? ESP_SD_SCK_PIN : SCK);
// refresh content if card was removed // refresh content if card was removed
if (SD.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, if (ESP3D_SD_Card.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN,
SD_SCK_MHZ(FREQMZ / _spi_speed_divider))) { SD_SCK_MHZ(FREQMZ / _spi_speed_divider))) {
_state = ESP_SDCARD_IDLE; _state = ESP_SDCARD_IDLE;
} }
@ -180,12 +167,12 @@ void ESP_SD::refreshStats(bool force) {
uint64_t ESP_SD::totalBytes(bool refresh) { uint64_t ESP_SD::totalBytes(bool refresh) {
static uint64_t _totalBytes = 0; static uint64_t _totalBytes = 0;
if (!SD.volumeBegin()) { if (!ESP3D_SD_Card.volumeBegin()) {
return 0; return 0;
} }
if (refresh || _totalBytes == 0) { if (refresh || _totalBytes == 0) {
_totalBytes = SD.clusterCount(); _totalBytes = ESP3D_SD_Card.clusterCount();
uint8_t sectors = SD.sectorsPerCluster(); uint8_t sectors = ESP3D_SD_Card.sectorsPerCluster();
_totalBytes = _totalBytes * sectors * 512; _totalBytes = _totalBytes * sectors * 512;
} }
return _totalBytes; return _totalBytes;
@ -197,12 +184,12 @@ uint64_t ESP_SD::usedBytes(bool refresh) {
uint64_t ESP_SD::freeBytes(bool refresh) { uint64_t ESP_SD::freeBytes(bool refresh) {
static uint64_t _freeBytes = 0; static uint64_t _freeBytes = 0;
if (!SD.volumeBegin()) { if (!ESP3D_SD_Card.volumeBegin()) {
return 0; return 0;
} }
if (refresh || _freeBytes == 0) { if (refresh || _freeBytes == 0) {
_freeBytes = SD.freeClusterCount(); _freeBytes = ESP3D_SD_Card.freeClusterCount();
uint8_t sectors = SD.sectorsPerCluster(); uint8_t sectors = ESP3D_SD_Card.sectorsPerCluster();
_freeBytes = _freeBytes * sectors * 512; _freeBytes = _freeBytes * sectors * 512;
} }
return _freeBytes; return _freeBytes;
@ -211,7 +198,7 @@ uint64_t ESP_SD::freeBytes(bool refresh) {
uint ESP_SD::maxPathLength() { return 255; } uint ESP_SD::maxPathLength() { return 255; }
bool ESP_SD::rename(const char* oldpath, const char* newpath) { bool ESP_SD::rename(const char* oldpath, const char* newpath) {
return SD.rename(oldpath, newpath); return ESP3D_SD_Card.rename(oldpath, newpath);
} }
bool ESP_SD::format() { bool ESP_SD::format() {
@ -305,7 +292,7 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) {
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD.open(path, (mode == ESP_FILE_READ) ? FILE_READ ESP3D_File tmp = ESP3D_SD_Card.open(path, (mode == ESP_FILE_READ) ? FILE_READ
: (mode == ESP_FILE_WRITE) ? FILE_WRITE : (mode == ESP_FILE_WRITE) ? FILE_WRITE
: FILE_WRITE); : FILE_WRITE);
if (tmp) { if (tmp) {
@ -325,7 +312,7 @@ bool ESP_SD::exists(const char* path) {
if (strcmp(path, "/") == 0) { if (strcmp(path, "/") == 0) {
return _started; return _started;
} }
res = SD.exists(path); res = ESP3D_SD_Card.exists(path);
if (!res) { if (!res) {
ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ); ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ);
if (root) { if (root) {
@ -337,10 +324,10 @@ bool ESP_SD::exists(const char* path) {
bool ESP_SD::remove(const char* path) { bool ESP_SD::remove(const char* path) {
_sizechanged = true; _sizechanged = true;
return SD.remove(path); return ESP3D_SD_Card.remove(path);
} }
bool ESP_SD::mkdir(const char* path) { return SD.mkdir(path); } bool ESP_SD::mkdir(const char* path) { return ESP3D_SD_Card.mkdir(path); }
bool ESP_SD::rmdir(const char* path) { bool ESP_SD::rmdir(const char* path) {
String p = path; String p = path;
@ -357,9 +344,9 @@ bool ESP_SD::rmdir(const char* path) {
std::stack<String> pathlist; std::stack<String> pathlist;
pathlist.push(p); pathlist.push(p);
while (pathlist.size() > 0 && res) { while (pathlist.size() > 0 && res) {
File dir = SD.open(pathlist.top().c_str()); ESP3D_File dir = ESP3D_SD_Card.open(pathlist.top().c_str());
dir.rewindDirectory(); dir.rewindDirectory();
File f = dir.openNextFile(); ESP3D_File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f && res) { while (f && res) {
if (f.isDir()) { if (f.isDir()) {
@ -371,14 +358,14 @@ bool ESP_SD::rmdir(const char* path) {
newdir += "/"; newdir += "/";
pathlist.push(newdir); pathlist.push(newdir);
f.close(); f.close();
f = File(); f = ESP3D_File();
} else { } else {
char tmp[255]; char tmp[255];
f.getName(tmp, 254); f.getName(tmp, 254);
_sizechanged = true; _sizechanged = true;
String filepath = pathlist.top() + tmp; String filepath = pathlist.top() + tmp;
f.close(); f.close();
if (!SD.remove(filepath.c_str())) { if (!ESP3D_SD_Card.remove(filepath.c_str())) {
res = false; res = false;
} }
f = dir.openNextFile(); f = dir.openNextFile();
@ -386,7 +373,7 @@ bool ESP_SD::rmdir(const char* path) {
} }
if (candelete) { if (candelete) {
if (pathlist.top() != "/") { if (pathlist.top() != "/") {
res = SD.rmdir(pathlist.top().c_str()); res = ESP3D_SD_Card.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
@ -400,7 +387,7 @@ bool ESP_SD::rmdir(const char* path) {
void ESP_SD::closeAll() { void ESP_SD::closeAll() {
for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) { for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) {
tSDFile_handle[i].close(); tSDFile_handle[i].close();
tSDFile_handle[i] = File(); tSDFile_handle[i] = ESP3D_File();
} }
} }
@ -427,7 +414,7 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode,
bool set = false; bool set = false;
for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) { for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) {
if (!tSDFile_handle[i]) { if (!tSDFile_handle[i]) {
tSDFile_handle[i] = *((File*)handle); tSDFile_handle[i] = *((ESP3D_File*)handle);
// filename // filename
char tmp[255]; char tmp[255];
tSDFile_handle[i].getName(tmp, 254); tSDFile_handle[i].getName(tmp, 254);
@ -470,7 +457,7 @@ const char* ESP_SDFile::shortname() const {
return _name.c_str(); return _name.c_str();
#else #else
static char sname[13]; static char sname[13];
File ftmp = SD.open(_filename.c_str()); ESP3D_File ftmp = ESP3D_SD_Card.open(_filename.c_str());
if (ftmp) { if (ftmp) {
ftmp.getSFN(sname, 12); ftmp.getSFN(sname, 12);
ftmp.close(); ftmp.close();
@ -491,14 +478,14 @@ void ESP_SDFile::close() {
// reopen if mode = write // reopen if mode = write
// udate size + date // udate size + date
if (_iswritemode && !_isdir) { if (_iswritemode && !_isdir) {
File ftmp = SD.open(_filename.c_str()); ESP3D_File ftmp = ESP3D_SD_Card.open(_filename.c_str());
if (ftmp) { if (ftmp) {
_size = ftmp.size(); _size = ftmp.size();
_lastwrite = getDateTimeFile(ftmp); _lastwrite = getDateTimeFile(ftmp);
ftmp.close(); ftmp.close();
} }
} }
tSDFile_handle[_index] = File(); tSDFile_handle[_index] = ESP3D_File();
// esp3d_log("Closing File at index %d",_index); // esp3d_log("Closing File at index %d",_index);
_index = -1; _index = -1;
} }
@ -509,7 +496,7 @@ ESP_SDFile ESP_SDFile::openNextFile() {
esp3d_log("openNextFile failed"); esp3d_log("openNextFile failed");
return ESP_SDFile(); return ESP_SDFile();
} }
File tmp = tSDFile_handle[_index].openNextFile(); ESP3D_File tmp = tSDFile_handle[_index].openNextFile();
if (tmp) { if (tmp) {
char tmps[255]; char tmps[255];
tmp.getName(tmps, 254); tmp.getName(tmps, 254);

View File

@ -21,14 +21,11 @@ sd_sdfat2_esp8266.cpp - ESP3D sd support class
#include "../../../include/esp3d_config.h" #include "../../../include/esp3d_config.h"
#if defined(ARDUINO_ARCH_ESP8266) && defined(SD_DEVICE) #if defined(ARDUINO_ARCH_ESP8266) && defined(SD_DEVICE)
#if (SD_DEVICE == ESP_SDFAT2) #if (SD_DEVICE == ESP_SDFAT2)
#define FS_NO_GLOBALS
#include <stack> #include <stack>
#include "../../../core/esp3d_settings.h" #include "../../../core/esp3d_settings.h"
#include "../esp_sd.h" #include "../esp_sd.h"
#define NO_GLOBAL_SD
#include <SdFat.h>
#include <sdios.h> #include <sdios.h>
// Try to select the best SD card configuration. // Try to select the best SD card configuration.
@ -41,18 +38,9 @@ sd_sdfat2_esp8266.cpp - ESP3D sd support class
#define SD_CONFIG \ #define SD_CONFIG \
SdSpiConfig((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, SHARED_SPI) SdSpiConfig((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, SHARED_SPI)
#endif // HAS_SDIO_CLASS #endif // HAS_SDIO_CLASS
#if SDFAT_FILE_TYPE == 1
typedef File32 File;
#elif SDFAT_FILE_TYPE == 2
typedef ExFile File;
#elif SDFAT_FILE_TYPE == 3
typedef FsFile File;
#else // SDFAT_FILE_TYPE
#error Invalid SDFAT_FILE_TYPE
#endif // SDFAT_FILE_TYPE
extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; ESP3D_SD_Class ESP3D_SD_Card;
SdFat SD; ESP3D_File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
void dateTime(uint16_t* date, uint16_t* dtime) { void dateTime(uint16_t* date, uint16_t* dtime) {
struct tm tmstruct; struct tm tmstruct;
@ -64,7 +52,7 @@ void dateTime(uint16_t* date, uint16_t* dtime) {
*dtime = FAT_TIME(tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec); *dtime = FAT_TIME(tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
} }
time_t getDateTimeFile(File& filehandle) { time_t getDateTimeFile(ESP3D_File& filehandle) {
static time_t dt = 0; static time_t dt = 0;
#ifdef SD_TIMESTAMP_FEATURE #ifdef SD_TIMESTAMP_FEATURE
struct tm timefile; struct tm timefile;
@ -120,7 +108,7 @@ uint8_t ESP_SD::getState(bool refresh) {
// SD is idle or not detected, let see if still the case // SD is idle or not detected, let see if still the case
_state = ESP_SDCARD_NOT_PRESENT; _state = ESP_SDCARD_NOT_PRESENT;
// refresh content if card was removed // refresh content if card was removed
if (SD.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, if (ESP3D_SD_Card.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN,
SD_SCK_HZ(F_CPU / _spi_speed_divider))) { SD_SCK_HZ(F_CPU / _spi_speed_divider))) {
esp3d_log("Init SD State ok"); esp3d_log("Init SD State ok");
_state = ESP_SDCARD_IDLE; _state = ESP_SDCARD_IDLE;
@ -170,12 +158,12 @@ void ESP_SD::refreshStats(bool force) {
uint64_t ESP_SD::totalBytes(bool refresh) { uint64_t ESP_SD::totalBytes(bool refresh) {
static uint64_t _totalBytes = 0; static uint64_t _totalBytes = 0;
if (!SD.volumeBegin()) { if (!ESP3D_SD_Card.volumeBegin()) {
return 0; return 0;
} }
if (refresh || _totalBytes == 0) { if (refresh || _totalBytes == 0) {
_totalBytes = SD.clusterCount(); _totalBytes = ESP3D_SD_Card.clusterCount();
uint8_t sectors = SD.sectorsPerCluster(); uint8_t sectors = ESP3D_SD_Card.sectorsPerCluster();
_totalBytes = _totalBytes * sectors * 512; _totalBytes = _totalBytes * sectors * 512;
} }
return _totalBytes; return _totalBytes;
@ -187,12 +175,12 @@ uint64_t ESP_SD::usedBytes(bool refresh) {
uint64_t ESP_SD::freeBytes(bool refresh) { uint64_t ESP_SD::freeBytes(bool refresh) {
static uint64_t _freeBytes = 0; static uint64_t _freeBytes = 0;
if (!SD.volumeBegin()) { if (!ESP3D_SD_Card.volumeBegin()) {
return 0; return 0;
} }
if (refresh || _freeBytes == 0) { if (refresh || _freeBytes == 0) {
_freeBytes = SD.freeClusterCount(); _freeBytes = ESP3D_SD_Card.freeClusterCount();
uint8_t sectors = SD.sectorsPerCluster(); uint8_t sectors = ESP3D_SD_Card.sectorsPerCluster();
_freeBytes = _freeBytes * sectors * 512; _freeBytes = _freeBytes * sectors * 512;
} }
return _freeBytes; return _freeBytes;
@ -201,7 +189,7 @@ uint64_t ESP_SD::freeBytes(bool refresh) {
uint ESP_SD::maxPathLength() { return 255; } uint ESP_SD::maxPathLength() { return 255; }
bool ESP_SD::rename(const char* oldpath, const char* newpath) { bool ESP_SD::rename(const char* oldpath, const char* newpath) {
return SD.rename(oldpath, newpath); return ESP3D_SD_Card.rename(oldpath, newpath);
} }
bool ESP_SD::format() { bool ESP_SD::format() {
@ -296,7 +284,7 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) {
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD.open(path, (mode == ESP_FILE_READ) ? FILE_READ ESP3D_File tmp = ESP3D_SD_Card.open(path, (mode == ESP_FILE_READ) ? FILE_READ
: (mode == ESP_FILE_WRITE) ? FILE_WRITE : (mode == ESP_FILE_WRITE) ? FILE_WRITE
: FILE_WRITE); : FILE_WRITE);
ESP_SDFile esptmp(&tmp, tmp.isDir(), (mode == ESP_FILE_READ) ? false : true, ESP_SDFile esptmp(&tmp, tmp.isDir(), (mode == ESP_FILE_READ) ? false : true,
@ -311,7 +299,7 @@ bool ESP_SD::exists(const char* path) {
return _started; return _started;
} }
esp3d_log("%s exists ?", path); esp3d_log("%s exists ?", path);
res = SD.exists(path); res = ESP3D_SD_Card.exists(path);
if (!res) { if (!res) {
esp3d_log("Seems not - trying open it"); esp3d_log("Seems not - trying open it");
ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ); ESP_SDFile root = ESP_SD::open(path, ESP_FILE_READ);
@ -325,10 +313,10 @@ bool ESP_SD::exists(const char* path) {
bool ESP_SD::remove(const char* path) { bool ESP_SD::remove(const char* path) {
_sizechanged = true; _sizechanged = true;
return SD.remove(path); return ESP3D_SD_Card.remove(path);
} }
bool ESP_SD::mkdir(const char* path) { return SD.mkdir(path); } bool ESP_SD::mkdir(const char* path) { return ESP3D_SD_Card.mkdir(path); }
bool ESP_SD::rmdir(const char* path) { bool ESP_SD::rmdir(const char* path) {
String p = path; String p = path;
@ -345,9 +333,9 @@ bool ESP_SD::rmdir(const char* path) {
std::stack<String> pathlist; std::stack<String> pathlist;
pathlist.push(p); pathlist.push(p);
while (pathlist.size() > 0 && res) { while (pathlist.size() > 0 && res) {
File dir = SD.open(pathlist.top().c_str()); ESP3D_File dir = ESP3D_SD_Card.open(pathlist.top().c_str());
dir.rewindDirectory(); dir.rewindDirectory();
File f = dir.openNextFile(); ESP3D_File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f && res) { while (f && res) {
if (f.isDir()) { if (f.isDir()) {
@ -359,14 +347,14 @@ bool ESP_SD::rmdir(const char* path) {
newdir += "/"; newdir += "/";
pathlist.push(newdir); pathlist.push(newdir);
f.close(); f.close();
f = File(); f = ESP3D_File() ;
} else { } else {
char tmp[255]; char tmp[255];
f.getName(tmp, 254); f.getName(tmp, 254);
_sizechanged = true; _sizechanged = true;
String filepath = pathlist.top() + tmp; String filepath = pathlist.top() + tmp;
f.close(); f.close();
if (!SD.remove(filepath.c_str())) { if (!ESP3D_SD_Card.remove(filepath.c_str())) {
res = false; res = false;
} }
f = dir.openNextFile(); f = dir.openNextFile();
@ -374,7 +362,7 @@ bool ESP_SD::rmdir(const char* path) {
} }
if (candelete) { if (candelete) {
if (pathlist.top() != "/") { if (pathlist.top() != "/") {
res = SD.rmdir(pathlist.top().c_str()); res = ESP3D_SD_Card.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
@ -399,7 +387,7 @@ bool ESP_SDFile::seek(uint32_t pos, uint8_t mode) {
void ESP_SD::closeAll() { void ESP_SD::closeAll() {
for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) { for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) {
tSDFile_handle[i].close(); tSDFile_handle[i].close();
tSDFile_handle[i] = File(); tSDFile_handle[i] = ESP3D_File() ;
} }
} }
@ -419,7 +407,7 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode,
bool set = false; bool set = false;
for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) { for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) {
if (!tSDFile_handle[i]) { if (!tSDFile_handle[i]) {
tSDFile_handle[i] = *((File*)handle); tSDFile_handle[i] = *((ESP3D_File*)handle);
// filename // filename
char tmp[255]; char tmp[255];
tSDFile_handle[i].getName(tmp, 254); tSDFile_handle[i].getName(tmp, 254);
@ -462,7 +450,7 @@ const char* ESP_SDFile::shortname() const {
return _name.c_str(); return _name.c_str();
#else #else
static char sname[13]; static char sname[13];
File ftmp = SD.open(_filename.c_str()); ESP3D_File ftmp = ESP3D_SD_Card.open(_filename.c_str());
if (ftmp) { if (ftmp) {
ftmp.getSFN(sname, 12); ftmp.getSFN(sname, 12);
ftmp.close(); ftmp.close();
@ -483,14 +471,14 @@ void ESP_SDFile::close() {
// reopen if mode = write // reopen if mode = write
// udate size + date // udate size + date
if (_iswritemode && !_isdir) { if (_iswritemode && !_isdir) {
File ftmp = SD.open(_filename.c_str()); ESP3D_File ftmp = ESP3D_SD_Card.open(_filename.c_str());
if (ftmp) { if (ftmp) {
_size = ftmp.size(); _size = ftmp.size();
_lastwrite = getDateTimeFile(ftmp); _lastwrite = getDateTimeFile(ftmp);
ftmp.close(); ftmp.close();
} }
} }
tSDFile_handle[_index] = File(); tSDFile_handle[_index] = ESP3D_File() ;
// esp3d_log("Closing File at index %d",_index); // esp3d_log("Closing File at index %d",_index);
_index = -1; _index = -1;
} }
@ -501,7 +489,7 @@ ESP_SDFile ESP_SDFile::openNextFile() {
esp3d_log("openNextFile failed"); esp3d_log("openNextFile failed");
return ESP_SDFile(); return ESP_SDFile();
} }
File tmp = tSDFile_handle[_index].openNextFile(); ESP3D_File tmp = tSDFile_handle[_index].openNextFile();
if (tmp) { if (tmp) {
char tmps[255]; char tmps[255];
tmp.getName(tmps, 254); tmp.getName(tmps, 254);

View File

@ -24,10 +24,9 @@ sdio_esp32.cpp - ESP3D sd support class
#include "../../../core/esp3d_settings.h" #include "../../../core/esp3d_settings.h"
#include "../esp_sd.h" #include "../esp_sd.h"
#include "FS.h"
#include "SD_MMC.h"
extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; ESP3D_SD_Class ESP3D_SD_Card = SD_MMC;
ESP3D_File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
#define SDMMC_FORCE_BEGIN #define SDMMC_FORCE_BEGIN
@ -60,10 +59,10 @@ uint8_t ESP_SD::getState(bool refresh) {
// refresh content if card was removed // refresh content if card was removed
if (!lastinitok) { if (!lastinitok) {
esp3d_log("last init was failed try sd_mmc begin"); esp3d_log("last init was failed try sd_mmc begin");
SD_MMC.end(); ESP3D_SD_Card.end();
if (SD_MMC.begin("/sdcard", SDIO_BIT_MODE)) { if (ESP3D_SD_Card.begin("/sdcard", SDIO_BIT_MODE)) {
esp3d_log("sd_mmc begin succeed"); esp3d_log("sd_mmc begin succeed");
if (SD_MMC.cardType() != CARD_NONE) { if (ESP3D_SD_Card.cardType() != CARD_NONE) {
_state = ESP_SDCARD_IDLE; _state = ESP_SDCARD_IDLE;
lastinitok = true; lastinitok = true;
esp3d_log("sd_mmc card type succeed"); esp3d_log("sd_mmc card type succeed");
@ -75,16 +74,16 @@ uint8_t ESP_SD::getState(bool refresh) {
} }
} else { } else {
esp3d_log("last init was ok try card type"); esp3d_log("last init was ok try card type");
if (SD_MMC.cardType() != CARD_NONE) { if (ESP3D_SD_Card.cardType() != CARD_NONE) {
esp3d_log("checking sd_mmc card type succeed"); esp3d_log("checking sd_mmc card type succeed");
_state = ESP_SDCARD_IDLE; _state = ESP_SDCARD_IDLE;
} else { } else {
lastinitok = false; lastinitok = false;
esp3d_log_e("Soft sd check failed"); esp3d_log_e("Soft sd check failed");
SD_MMC.end(); ESP3D_SD_Card.end();
if (SD_MMC.begin("/sdcard", SDIO_BIT_MODE)) { if (ESP3D_SD_Card.begin("/sdcard", SDIO_BIT_MODE)) {
esp3d_log("new sd_mmc begin succeed"); esp3d_log("new sd_mmc begin succeed");
if (SD_MMC.cardType() != CARD_NONE) { if (ESP3D_SD_Card.cardType() != CARD_NONE) {
_state = ESP_SDCARD_IDLE; _state = ESP_SDCARD_IDLE;
lastinitok = true; lastinitok = true;
esp3d_log("new sd_mmc card type succeed"); esp3d_log("new sd_mmc card type succeed");
@ -103,13 +102,13 @@ bool ESP_SD::begin() {
#if SDIO_BIT_MODE == SD_ONE_BIT_MODE #if SDIO_BIT_MODE == SD_ONE_BIT_MODE
#if (ESP_SDIO_CLK_PIN != -1) || (ESP_SDIO_CMD_PIN != -1) || \ #if (ESP_SDIO_CLK_PIN != -1) || (ESP_SDIO_CMD_PIN != -1) || \
(ESP_SDIO_D0_PIN != -1) (ESP_SDIO_D0_PIN != -1)
SD_MMC.setPins(ESP_SDIO_CLK_PIN, ESP_SDIO_CMD_PIN, ESP_SDIO_D0_PIN); ESP3D_SD_Card.setPins(ESP_SDIO_CLK_PIN, ESP_SDIO_CMD_PIN, ESP_SDIO_D0_PIN);
#endif //(ESP_SDIO_CLK_PIN != -1) #endif //(ESP_SDIO_CLK_PIN != -1)
#else #else
#if (ESP_SDIO_CLK_PIN != -1) || (ESP_SDIO_CMD_PIN != -1) || \ #if (ESP_SDIO_CLK_PIN != -1) || (ESP_SDIO_CMD_PIN != -1) || \
(ESP_SDIO_D0_PIN != -1) || (ESP_SDIO_D1_PIN != -1) || \ (ESP_SDIO_D0_PIN != -1) || (ESP_SDIO_D1_PIN != -1) || \
(ESP_SDIO_D2_PIN != -1) || (ESP_SDIO_D3_PIN != -1) (ESP_SDIO_D2_PIN != -1) || (ESP_SDIO_D3_PIN != -1)
SD_MMC.setPins(ESP_SDIO_CLK_PIN, ESP_SDIO_CMD_PIN, ESP_SDIO_D0_PIN, ESP3D_SD_Card.setPins(ESP_SDIO_CLK_PIN, ESP_SDIO_CMD_PIN, ESP_SDIO_D0_PIN,
ESP_SDIO_D1_PIN, ESP_SDIO_D2_PIN, ESP_SDIO_D3_PIN); ESP_SDIO_D1_PIN, ESP_SDIO_D2_PIN, ESP_SDIO_D3_PIN);
#endif //(ESP_SDIO_CLK_PIN != -1) #endif //(ESP_SDIO_CLK_PIN != -1)
#endif // SD_ONE_BIT_MODE #endif // SD_ONE_BIT_MODE
@ -125,7 +124,7 @@ bool ESP_SD::begin() {
} }
void ESP_SD::end() { void ESP_SD::end() {
SD_MMC.end(); ESP3D_SD_Card.end();
_state = ESP_SDCARD_NOT_PRESENT; _state = ESP_SDCARD_NOT_PRESENT;
_started = false; _started = false;
} }
@ -140,7 +139,7 @@ void ESP_SD::refreshStats(bool force) {
uint64_t ESP_SD::totalBytes(bool refresh) { uint64_t ESP_SD::totalBytes(bool refresh) {
static uint64_t _totalBytes = 0; static uint64_t _totalBytes = 0;
if (refresh || _totalBytes == 0) { if (refresh || _totalBytes == 0) {
_totalBytes = SD_MMC.totalBytes(); _totalBytes = ESP3D_SD_Card.totalBytes();
; ;
} }
return _totalBytes; return _totalBytes;
@ -149,7 +148,7 @@ uint64_t ESP_SD::totalBytes(bool refresh) {
uint64_t ESP_SD::usedBytes(bool refresh) { uint64_t ESP_SD::usedBytes(bool refresh) {
static uint64_t _usedBytes = 0; static uint64_t _usedBytes = 0;
if (refresh || _usedBytes == 0) { if (refresh || _usedBytes == 0) {
_usedBytes = SD_MMC.usedBytes(); _usedBytes = ESP3D_SD_Card.usedBytes();
} }
return _usedBytes; return _usedBytes;
} }
@ -161,7 +160,7 @@ uint64_t ESP_SD::freeBytes(bool refresh) {
uint ESP_SD::maxPathLength() { return 255; } uint ESP_SD::maxPathLength() { return 255; }
bool ESP_SD::rename(const char *oldpath, const char *newpath) { bool ESP_SD::rename(const char *oldpath, const char *newpath) {
return SD_MMC.rename(oldpath, newpath); return ESP3D_SD_Card.rename(oldpath, newpath);
} }
bool ESP_SD::format() { bool ESP_SD::format() {
@ -193,7 +192,7 @@ ESP_SDFile ESP_SD::open(const char *path, uint8_t mode) {
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD_MMC.open(path, (mode == ESP_FILE_READ) ? FILE_READ ESP3D_File tmp = ESP3D_SD_Card.open(path, (mode == ESP_FILE_READ) ? FILE_READ
: (mode == ESP_FILE_WRITE) ? FILE_WRITE : (mode == ESP_FILE_WRITE) ? FILE_WRITE
: FILE_APPEND); : FILE_APPEND);
ESP_SDFile esptmp(&tmp, tmp.isDirectory(), ESP_SDFile esptmp(&tmp, tmp.isDirectory(),
@ -212,7 +211,7 @@ bool ESP_SD::exists(const char *path) {
if (p.endsWith("/")) { if (p.endsWith("/")) {
p.remove(p.length() - 1, 1); p.remove(p.length() - 1, 1);
} }
res = SD_MMC.exists(p); res = ESP3D_SD_Card.exists(p);
if (!res) { if (!res) {
ESP_SDFile root = ESP_SD::open(p.c_str(), ESP_FILE_READ); ESP_SDFile root = ESP_SD::open(p.c_str(), ESP_FILE_READ);
if (root) { if (root) {
@ -222,14 +221,14 @@ bool ESP_SD::exists(const char *path) {
return res; return res;
} }
bool ESP_SD::remove(const char *path) { return SD_MMC.remove(path); } bool ESP_SD::remove(const char *path) { return ESP3D_SD_Card.remove(path); }
bool ESP_SD::mkdir(const char *path) { bool ESP_SD::mkdir(const char *path) {
String p = path; String p = path;
if (p.endsWith("/")) { if (p.endsWith("/")) {
p.remove(p.length() - 1, 1); p.remove(p.length() - 1, 1);
} }
return SD_MMC.mkdir(p.c_str()); return ESP3D_SD_Card.mkdir(p.c_str());
} }
bool ESP_SD::rmdir(const char *path) { bool ESP_SD::rmdir(const char *path) {
@ -244,8 +243,8 @@ bool ESP_SD::rmdir(const char *path) {
} }
pathlist.push(p); pathlist.push(p);
while (pathlist.size() > 0 && res) { while (pathlist.size() > 0 && res) {
File dir = SD_MMC.open(pathlist.top().c_str()); ESP3D_File dir = ESP3D_SD_Card.open(pathlist.top().c_str());
File f = dir.openNextFile(); ESP3D_File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f && res) { while (f && res) {
if (f.isDirectory()) { if (f.isDirectory()) {
@ -254,12 +253,12 @@ bool ESP_SD::rmdir(const char *path) {
newdir += f.name(); newdir += f.name();
pathlist.push(newdir); pathlist.push(newdir);
f.close(); f.close();
f = File(); f =ESP3D_File();
} else { } else {
String filepath = pathlist.top() + '/'; String filepath = pathlist.top() + '/';
filepath += f.name(); filepath += f.name();
f.close(); f.close();
if (!SD_MMC.remove(filepath.c_str())) { if (!ESP3D_SD_Card.remove(filepath.c_str())) {
res = false; res = false;
} }
f = dir.openNextFile(); f = dir.openNextFile();
@ -267,7 +266,7 @@ bool ESP_SD::rmdir(const char *path) {
} }
if (candelete) { if (candelete) {
if (pathlist.top() != "/") { if (pathlist.top() != "/") {
res = SD_MMC.rmdir(pathlist.top().c_str()); res = ESP3D_SD_Card.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
@ -281,7 +280,7 @@ bool ESP_SD::rmdir(const char *path) {
void ESP_SD::closeAll() { void ESP_SD::closeAll() {
for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) { for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) {
tSDFile_handle[i].close(); tSDFile_handle[i].close();
tSDFile_handle[i] = File(); tSDFile_handle[i] =ESP3D_File();
} }
} }
@ -301,7 +300,7 @@ ESP_SDFile::ESP_SDFile(void *handle, bool isdir, bool iswritemode,
bool set = false; bool set = false;
for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) { for (uint8_t i = 0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) {
if (!tSDFile_handle[i]) { if (!tSDFile_handle[i]) {
tSDFile_handle[i] = *((File *)handle); tSDFile_handle[i] = *((ESP3D_File *)handle);
// filename // filename
_name = tSDFile_handle[i].name(); _name = tSDFile_handle[i].name();
_filename = path; _filename = path;
@ -331,7 +330,7 @@ ESP_SDFile::ESP_SDFile(void *handle, bool isdir, bool iswritemode,
} }
bool ESP_SDFile::seek(uint32_t pos, uint8_t mode) { bool ESP_SDFile::seek(uint32_t pos, uint8_t mode) {
return tSDFile_handle[_index].seek(pos, (SeekMode)mode); return tSDFile_handle[_index].seek(pos, (fs::SeekMode)mode);
} }
void ESP_SDFile::close() { void ESP_SDFile::close() {
@ -341,14 +340,14 @@ void ESP_SDFile::close() {
// reopen if mode = write // reopen if mode = write
// udate size + date // udate size + date
if (_iswritemode && !_isdir) { if (_iswritemode && !_isdir) {
File ftmp = SD_MMC.open(_filename.c_str()); ESP3D_File ftmp = ESP3D_SD_Card.open(_filename.c_str());
if (ftmp) { if (ftmp) {
_size = ftmp.size(); _size = ftmp.size();
_lastwrite = ftmp.getLastWrite(); _lastwrite = ftmp.getLastWrite();
ftmp.close(); ftmp.close();
} }
} }
tSDFile_handle[_index] = File(); tSDFile_handle[_index] =ESP3D_File();
// esp3d_log("Closing File at index %d",_index); // esp3d_log("Closing File at index %d",_index);
_index = -1; _index = -1;
} }
@ -359,7 +358,7 @@ ESP_SDFile ESP_SDFile::openNextFile() {
esp3d_log("openNextFile failed"); esp3d_log("openNextFile failed");
return ESP_SDFile(); return ESP_SDFile();
} }
File tmp = tSDFile_handle[_index].openNextFile(); ESP3D_File tmp = tSDFile_handle[_index].openNextFile();
if (tmp) { if (tmp) {
esp3d_log("tmp name :%s %s %s", tmp.name(), esp3d_log("tmp name :%s %s %s", tmp.name(),
(tmp.isDirectory()) ? "isDir" : "isFile", _filename.c_str()); (tmp.isDirectory()) ? "isDir" : "isFile", _filename.c_str());

View File

@ -290,7 +290,7 @@ void GcodeHost::readNextCommand() {
} else { } else {
_processedSize++; _processedSize++;
_currentPosition++; _currentPosition++;
if (!(char)c == '\n' ) { if ((char)c != '\n' ) {
_currentCommand += (char)c; _currentCommand += (char)c;
} else { } else {
processing = false; processing = false;

View File

@ -24,7 +24,6 @@
*/ */
#ifndef SdFat_h #ifndef SdFat_h
#define SdFat_h #define SdFat_h
#define DISABLE_FS_H_WARNING 1
/** /**
* \file * \file
* \brief main SdFs include file. * \brief main SdFs include file.

View File

@ -289,6 +289,7 @@ lib_ignore =
esp32-usb-serial esp32-usb-serial
[env:esp8266dev] [env:esp8266dev]
;core 3.1.1
platform = espressif8266@4.1.0 platform = espressif8266@4.1.0
board = esp12e board = esp12e
framework = arduino framework = arduino