Apply CLANG style

This commit is contained in:
Luc 2023-10-16 16:36:30 +08:00
parent 606cc3f0ff
commit 3bc8c874ae
2 changed files with 484 additions and 530 deletions

View File

@ -17,23 +17,27 @@
License along with This code; if not, write to the Free Software License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
//#define ESP_DEBUG_FEATURE DEBUG_OUTPUT_SERIAL0 // #define ESP_DEBUG_FEATURE DEBUG_OUTPUT_SERIAL0
#include "../../include/esp3d_config.h" #include "../../include/esp3d_config.h"
#ifdef SD_DEVICE #ifdef SD_DEVICE
#include "esp_sd.h"
#include <time.h> #include <time.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
#include <SD.h> #include <SD.h>
File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
#elif ((SD_DEVICE == ESP_SDFAT) || (SD_DEVICE == ESP_SDFAT2)) && defined (ARDUINO_ARCH_ESP8266) #elif ((SD_DEVICE == ESP_SDFAT) || (SD_DEVICE == ESP_SDFAT2)) && \
defined(ARDUINO_ARCH_ESP8266)
#define FS_NO_GLOBALS #define FS_NO_GLOBALS
#define NO_GLOBAL_SD #define NO_GLOBAL_SD
#include <SdFat.h> #include <SdFat.h>
sdfat::File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; sdfat::File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
#elif ((SD_DEVICE == ESP_SDFAT) || (SD_DEVICE == ESP_SDFAT2)) && defined (ARDUINO_ARCH_ESP32) #elif ((SD_DEVICE == ESP_SDFAT) || (SD_DEVICE == ESP_SDFAT2)) && \
defined(ARDUINO_ARCH_ESP32)
#include <SdFat.h> #include <SdFat.h>
#if (SD_DEVICE == ESP_SDFAT2) #if (SD_DEVICE == ESP_SDFAT2)
#if SDFAT_FILE_TYPE == 1 #if SDFAT_FILE_TYPE == 1
@ -52,30 +56,31 @@ File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
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"
#endif // ESP3DLIB_ENV #endif // ESP3DLIB_ENV
#if SD_DEVICE_CONNECTION == ESP_SHARED_SD #if SD_DEVICE_CONNECTION == ESP_SHARED_SD
bool ESP_SD::_enabled = false; bool ESP_SD::_enabled = false;
bool ESP_SD::enableSharedSD() bool ESP_SD::enableSharedSD() {
{ if (_enabled) {
if(_enabled) {
return false; return false;
} }
_enabled = true; _enabled = true;
#if defined (ESP_FLAG_SHARED_SD_PIN) && ESP_FLAG_SHARED_SD_PIN != -1 #if defined(ESP_FLAG_SHARED_SD_PIN) && ESP_FLAG_SHARED_SD_PIN != -1
//need to check if SD is in use ? // need to check if SD is in use ?
//Method : TBD // Method : TBD
//1 - check sd cs state ? what about SDIO then ? // 1 - check sd cs state ? what about SDIO then ?
//2 - check M27 status ? // 2 - check M27 status ?
log_esp3d("SD shared enabled PIN %d with %d", ESP_FLAG_SHARED_SD_PIN, ESP_FLAG_SHARED_SD_VALUE); log_esp3d("SD shared enabled PIN %d with %d", ESP_FLAG_SHARED_SD_PIN,
ESP_FLAG_SHARED_SD_VALUE);
digitalWrite(ESP_FLAG_SHARED_SD_PIN, ESP_FLAG_SHARED_SD_VALUE); digitalWrite(ESP_FLAG_SHARED_SD_PIN, ESP_FLAG_SHARED_SD_VALUE);
#endif // ESP_FLAG_SHARED_SD_PIN #endif // ESP_FLAG_SHARED_SD_PIN
#if defined (ESP3DLIB_ENV) #if defined(ESP3DLIB_ENV)
//check if card is not currently in use // check if card is not currently in use
if (card.isMounted() && (IS_SD_PRINTING() ||IS_SD_FETCHING() ||IS_SD_PAUSED() || IS_SD_FILE_OPEN())) { if (card.isMounted() && (IS_SD_PRINTING() || IS_SD_FETCHING() ||
IS_SD_PAUSED() || IS_SD_FILE_OPEN())) {
_enabled = false; _enabled = false;
} else { } else {
card.release(); card.release();
@ -89,22 +94,19 @@ 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;
return _state; return _state;
} }
uint8_t ESP_SD::getFSType(const char * path) uint8_t ESP_SD::getFSType(const char* path) {
{
(void)path; (void)path;
return FS_SD; return FS_SD;
} }
bool ESP_SD::accessFS(uint8_t FS) bool ESP_SD::accessFS(uint8_t FS) {
{
(void)FS; (void)FS;
//if card is busy do not let another task access SD and so prevent a release // if card is busy do not let another task access SD and so prevent a release
if (_state == ESP_SDCARD_BUSY) { if (_state == ESP_SDCARD_BUSY) {
log_esp3d("SD Busy"); log_esp3d("SD Busy");
return false; return false;
@ -122,49 +124,43 @@ bool ESP_SD::accessFS(uint8_t FS)
return true; return true;
#endif // SD_DEVICE_CONNECTION == ESP_SHARED_SD #endif // SD_DEVICE_CONNECTION == ESP_SHARED_SD
} }
void ESP_SD::releaseFS(uint8_t FS) void ESP_SD::releaseFS(uint8_t FS) {
{
(void)FS; (void)FS;
log_esp3d("Release SD"); log_esp3d("Release SD");
setState(ESP_SDCARD_IDLE); setState(ESP_SDCARD_IDLE);
#if SD_DEVICE_CONNECTION == ESP_SHARED_SD #if SD_DEVICE_CONNECTION == ESP_SHARED_SD
_enabled = false; _enabled = false;
#if defined (ESP_FLAG_SHARED_SD_PIN) && ESP_FLAG_SHARED_SD_PIN != -1 #if defined(ESP_FLAG_SHARED_SD_PIN) && ESP_FLAG_SHARED_SD_PIN != -1
log_esp3d("SD shared disabled PIN %d with %d", ESP_FLAG_SHARED_SD_PIN, ESP_FLAG_SHARED_SD_VALUE); log_esp3d("SD shared disabled PIN %d with %d", ESP_FLAG_SHARED_SD_PIN,
ESP_FLAG_SHARED_SD_VALUE);
digitalWrite(ESP_FLAG_SHARED_SD_PIN, !ESP_FLAG_SHARED_SD_VALUE); digitalWrite(ESP_FLAG_SHARED_SD_PIN, !ESP_FLAG_SHARED_SD_VALUE);
#endif // ESP_FLAG_SHARED_SD_PIN #endif // ESP_FLAG_SHARED_SD_PIN
#if defined (ESP3DLIB_ENV) #if defined(ESP3DLIB_ENV)
log_esp3d("Mount SD in Marlin"); log_esp3d("Mount SD in Marlin");
card.mount(); card.mount();
#endif // ESP3DLIB_ENV #endif // ESP3DLIB_ENV
#endif //SD_DEVICE_CONNECTION == ESP_SHARED_SD #endif // SD_DEVICE_CONNECTION == ESP_SHARED_SD
} }
void ESP_SD::handle() {}
void ESP_SD::handle() // helper to format size to readable string
{ String& ESP_SD::formatBytes(uint64_t bytes) {
}
//helper to format size to readable string
String & ESP_SD::formatBytes (uint64_t bytes)
{
static String res; static String res;
if (bytes < 1024) { if (bytes < 1024) {
res = String ((uint16_t)bytes) + " B"; res = String((uint16_t)bytes) + " B";
} else if (bytes < (1024 * 1024) ) { } else if (bytes < (1024 * 1024)) {
res = String ((float)(bytes / 1024.0),2) + " KB"; res = String((float)(bytes / 1024.0), 2) + " KB";
} else if (bytes < (1024 * 1024 * 1024) ) { } else if (bytes < (1024 * 1024 * 1024)) {
res = String ((float)(bytes / 1024.0 / 1024.0),2) + " MB"; res = String((float)(bytes / 1024.0 / 1024.0), 2) + " MB";
} else { } else {
res = String ((float)(bytes / 1024.0 / 1024.0 / 1024.0),2) + " GB"; res = String((float)(bytes / 1024.0 / 1024.0 / 1024.0), 2) + " GB";
} }
return res; return res;
} }
ESP_SDFile::ESP_SDFile(const char * name, const char * filename, bool isdir, size_t size) ESP_SDFile::ESP_SDFile(const char* name, const char* filename, bool isdir,
{ size_t size) {
_isdir = isdir; _isdir = isdir;
_dirlist = ""; _dirlist = "";
_index = -1; _index = -1;
@ -175,102 +171,75 @@ ESP_SDFile::ESP_SDFile(const char * name, const char * filename, bool isdir, siz
_size = size; _size = size;
} }
ESP_SDFile::~ESP_SDFile() ESP_SDFile::~ESP_SDFile() {
{ // log_esp3d("Destructor %s index %d",(_isdir)?"Dir":"File", _index);
//log_esp3d("Destructor %s index %d",(_isdir)?"Dir":"File", _index);
} }
ESP_SDFile::operator bool() const ESP_SDFile::operator bool() const {
{
if ((_index != -1) || (_filename.length() > 0)) { if ((_index != -1) || (_filename.length() > 0)) {
//log_esp3d("Bool yes %d %d",_index, _filename.length()); // log_esp3d("Bool yes %d %d",_index, _filename.length());
return true; return true;
} else { } else {
return false; return false;
} }
} }
bool ESP_SDFile::isOpen() bool ESP_SDFile::isOpen() { return !(_index == -1); }
{
return !(_index == -1);
}
const char* ESP_SDFile::name() const const char* ESP_SDFile::name() const { return _name.c_str(); }
{
return _name.c_str();
}
const char* ESP_SDFile::filename() const const char* ESP_SDFile::filename() const { return _filename.c_str(); }
{
return _filename.c_str();
}
bool ESP_SDFile::isDirectory() bool ESP_SDFile::isDirectory() { return _isdir; }
{
return _isdir;
}
size_t ESP_SDFile::size() size_t ESP_SDFile::size() { return _size; }
{
return _size;
}
time_t ESP_SDFile::getLastWrite() time_t ESP_SDFile::getLastWrite() { return _lastwrite; }
{
return _lastwrite;
}
int ESP_SDFile::available() int ESP_SDFile::available() {
{
if (_index == -1 || _isdir) { if (_index == -1 || _isdir) {
return 0; return 0;
} }
return tSDFile_handle[_index].available(); return tSDFile_handle[_index].available();
} }
size_t ESP_SDFile::write(uint8_t i) size_t ESP_SDFile::write(uint8_t i) {
{
if ((_index == -1) || _isdir) { if ((_index == -1) || _isdir) {
return 0; return 0;
} }
return tSDFile_handle[_index].write (i); return tSDFile_handle[_index].write(i);
} }
size_t ESP_SDFile::write(const uint8_t *buf, size_t size) size_t ESP_SDFile::write(const uint8_t* buf, size_t size) {
{
if ((_index == -1) || _isdir) { if ((_index == -1) || _isdir) {
return 0; return 0;
} }
return tSDFile_handle[_index].write (buf, size); return tSDFile_handle[_index].write(buf, size);
} }
int ESP_SDFile::read() int ESP_SDFile::read() {
{
if ((_index == -1) || _isdir) { if ((_index == -1) || _isdir) {
return -1; return -1;
} }
return tSDFile_handle[_index].read(); return tSDFile_handle[_index].read();
} }
size_t ESP_SDFile::read(uint8_t* buf, size_t size) size_t ESP_SDFile::read(uint8_t* buf, size_t size) {
{
if ((_index == -1) || _isdir) { if ((_index == -1) || _isdir) {
return -1; return -1;
} }
return tSDFile_handle[_index].read(buf, size); return tSDFile_handle[_index].read(buf, size);
} }
void ESP_SDFile::flush() void ESP_SDFile::flush() {
{
if ((_index == -1) || _isdir) { if ((_index == -1) || _isdir) {
return; return;
} }
tSDFile_handle[_index].flush(); tSDFile_handle[_index].flush();
} }
ESP_SDFile& ESP_SDFile::operator=(const ESP_SDFile & other) ESP_SDFile& ESP_SDFile::operator=(const ESP_SDFile& other) {
{ // log_esp3d("Copy %s", other._filename.c_str());
//log_esp3d("Copy %s", other._filename.c_str());
_isdir = other._isdir; _isdir = other._isdir;
_index = other._index; _index = other._index;
_filename = other._filename; _filename = other._filename;
@ -282,8 +251,7 @@ ESP_SDFile& ESP_SDFile::operator=(const ESP_SDFile & other)
return *this; return *this;
} }
bool ESP_SD::setSPISpeedDivider(uint8_t speeddivider) bool ESP_SD::setSPISpeedDivider(uint8_t speeddivider) {
{
if (speeddivider > 0) { if (speeddivider > 0) {
_spi_speed_divider = speeddivider; _spi_speed_divider = speeddivider;
return true; return true;
@ -291,4 +259,4 @@ bool ESP_SD::setSPISpeedDivider(uint8_t speeddivider)
return false; return false;
} }
#endif //SD_DEVICE #endif // SD_DEVICE

View File

@ -18,140 +18,140 @@ sd_native_esp32.cpp - ESP3D sd support class
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "../../../include/esp3d_config.h" #include "../../../include/esp3d_config.h"
#if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE) #if defined(ARDUINO_ARCH_ESP32) && defined(SD_DEVICE)
#if (SD_DEVICE == ESP_SD_NATIVE) #if (SD_DEVICE == ESP_SD_NATIVE)
#include "../esp_sd.h"
#include <stack> #include <stack>
#include "../../../core/settings_esp3d.h" #include "../../../core/settings_esp3d.h"
#include "../esp_sd.h"
#include "FS.h" #include "FS.h"
#include "SD.h" #include "SD.h"
//base frequency
//or use (1000000 * ESP.getCpuFreqMHz()) TBC // TBC: base frequency
// or use (1000000 * ESP.getCpuFreqMHz())
#define ESP_SPI_FREQ 4000000 #define ESP_SPI_FREQ 4000000
extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; extern 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
//no need to go further if SD detect is not correct // no need to go further if SD detect is not correct
if (!((digitalRead (ESP_SD_DETECT_PIN) == ESP_SD_DETECT_VALUE) ? true : false)) { if (!((digitalRead(ESP_SD_DETECT_PIN) == ESP_SD_DETECT_VALUE) ? true
: false)) {
_state = ESP_SDCARD_NOT_PRESENT; _state = ESP_SDCARD_NOT_PRESENT;
return _state; return _state;
} }
#endif //ESP_SD_DETECT_PIN #endif // ESP_SD_DETECT_PIN
//if busy doing something return state // if busy doing something return state
if (!((_state == ESP_SDCARD_NOT_PRESENT) || _state == ESP_SDCARD_IDLE)) { if (!((_state == ESP_SDCARD_NOT_PRESENT) || _state == ESP_SDCARD_IDLE)) {
return _state; return _state;
} }
if (!refresh) { if (!refresh) {
return _state; //to avoid refresh=true + busy to reset SD and waste time return _state; // to avoid refresh=true + busy to reset SD and waste time
} else { } else {
_sizechanged = true; _sizechanged = true;
} }
//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(); SD.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
log_esp3d("Spi : CS: %d, Miso: %d, Mosi: %d, SCK: %d",ESP_SD_CS_PIN!=-1?ESP_SD_CS_PIN:SS, ESP_SD_MISO_PIN!=-1?ESP_SD_MISO_PIN:MISO, ESP_SD_MOSI_PIN!=-1?ESP_SD_MOSI_PIN:MOSI, ESP_SD_SCK_PIN!=-1?ESP_SD_SCK_PIN:SCK); log_esp3d("Spi : CS: %d, Miso: %d, Mosi: %d, SCK: %d",
if (SD.begin((ESP_SD_CS_PIN == -1)?SS:ESP_SD_CS_PIN, SPI, ESP_SPI_FREQ / _spi_speed_divider)) { ESP_SD_CS_PIN != -1 ? ESP_SD_CS_PIN : SS,
if ( SD.cardSize() > 0 ) { ESP_SD_MISO_PIN != -1 ? ESP_SD_MISO_PIN : MISO,
ESP_SD_MOSI_PIN != -1 ? ESP_SD_MOSI_PIN : MOSI,
ESP_SD_SCK_PIN != -1 ? ESP_SD_SCK_PIN : SCK);
if (SD.begin((ESP_SD_CS_PIN == -1) ? SS : ESP_SD_CS_PIN, SPI,
ESP_SPI_FREQ / _spi_speed_divider)) {
if (SD.cardSize() > 0) {
_state = ESP_SDCARD_IDLE; _state = ESP_SDCARD_IDLE;
} }
} }
return _state; return _state;
} }
bool ESP_SD::begin() bool ESP_SD::begin() {
{ #if (ESP_SD_CS_PIN != -1) || (ESP_SD_MISO_PIN != -1) || \
#if (ESP_SD_CS_PIN != -1) || (ESP_SD_MISO_PIN != -1) || (ESP_SD_MOSI_PIN != -1) || (ESP_SD_SCK_PIN != -1) (ESP_SD_MOSI_PIN != -1) || (ESP_SD_SCK_PIN != -1)
log_esp3d("Custom spi : CS: %d, Miso: %d, Mosi: %d, SCK: %d",ESP_SD_CS_PIN, ESP_SD_MISO_PIN, ESP_SD_MOSI_PIN, ESP_SD_SCK_PIN); log_esp3d("Custom spi : CS: %d, Miso: %d, Mosi: %d, SCK: %d", ESP_SD_CS_PIN,
ESP_SD_MISO_PIN, ESP_SD_MOSI_PIN, ESP_SD_SCK_PIN);
SPI.begin(ESP_SD_SCK_PIN, ESP_SD_MISO_PIN, ESP_SD_MOSI_PIN, ESP_SD_CS_PIN); SPI.begin(ESP_SD_SCK_PIN, ESP_SD_MISO_PIN, ESP_SD_MOSI_PIN, ESP_SD_CS_PIN);
#endif #endif
_started = true; _started = true;
_state = ESP_SDCARD_NOT_PRESENT; _state = ESP_SDCARD_NOT_PRESENT;
_spi_speed_divider = Settings_ESP3D::read_byte(ESP_SD_SPEED_DIV); _spi_speed_divider = Settings_ESP3D::read_byte(ESP_SD_SPEED_DIV);
//sanity check // sanity check
if (_spi_speed_divider <= 0) { if (_spi_speed_divider <= 0) {
_spi_speed_divider = 1; _spi_speed_divider = 1;
} }
//Setup pins // Setup pins
#if defined(ESP_SD_DETECT_PIN) && ESP_SD_DETECT_PIN != -1 #if defined(ESP_SD_DETECT_PIN) && ESP_SD_DETECT_PIN != -1
pinMode (ESP_SD_DETECT_PIN, INPUT); pinMode(ESP_SD_DETECT_PIN, INPUT);
#endif //ESP_SD_DETECT_PIN #endif // ESP_SD_DETECT_PIN
#if SD_DEVICE_CONNECTION == ESP_SHARED_SD #if SD_DEVICE_CONNECTION == ESP_SHARED_SD
#if defined(ESP_FLAG_SHARED_SD_PIN) && ESP_FLAG_SHARED_SD_PIN != -1 #if defined(ESP_FLAG_SHARED_SD_PIN) && ESP_FLAG_SHARED_SD_PIN != -1
pinMode (ESP_FLAG_SHARED_SD_PIN, OUTPUT); pinMode(ESP_FLAG_SHARED_SD_PIN, OUTPUT);
digitalWrite(ESP_FLAG_SHARED_SD_PIN, !ESP_FLAG_SHARED_SD_VALUE); digitalWrite(ESP_FLAG_SHARED_SD_PIN, !ESP_FLAG_SHARED_SD_VALUE);
#endif //ESP_FLAG_SHARED_SD_PIN #endif // ESP_FLAG_SHARED_SD_PIN
#endif //SD_DEVICE_CONNECTION == ESP_SHARED_SD #endif // SD_DEVICE_CONNECTION == ESP_SHARED_SD
return _started; return _started;
} }
void ESP_SD::end() void ESP_SD::end() {
{
SD.end(); SD.end();
_state = ESP_SDCARD_NOT_PRESENT; _state = ESP_SDCARD_NOT_PRESENT;
_started = false; _started = false;
} }
void ESP_SD::refreshStats(bool force) void ESP_SD::refreshStats(bool force) {
{
if (force || _sizechanged) { if (force || _sizechanged) {
freeBytes(true); freeBytes(true);
} }
_sizechanged = false; _sizechanged = false;
} }
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 = SD.totalBytes();
;
} }
return _totalBytes; return _totalBytes;
} }
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 = SD.usedBytes();
} }
return _usedBytes; return _usedBytes;
} }
uint64_t ESP_SD::freeBytes(bool refresh) uint64_t ESP_SD::freeBytes(bool refresh) {
{
return (totalBytes(refresh) - usedBytes(refresh)); return (totalBytes(refresh) - usedBytes(refresh));
} }
uint ESP_SD::maxPathLength() uint ESP_SD::maxPathLength() { return 255; }
{
return 255; bool ESP_SD::rename(const char *oldpath, const char *newpath) {
return SD.rename(oldpath, newpath);
} }
bool ESP_SD::rename(const char *oldpath, const char *newpath) bool ESP_SD::format(ESP3DOutput *output) {
{ // not available yet
return SD.rename(oldpath,newpath);
}
bool ESP_SD::format(ESP3DOutput * output)
{
//not available yet
if (output) { if (output) {
output->printERROR ("Not implemented!"); output->printERROR("Not implemented!");
} }
return false; return false;
} }
ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) ESP_SDFile ESP_SD::open(const char *path, uint8_t mode) {
{ // do some check
//do some check if (((strcmp(path, "/") == 0) &&
if(((strcmp(path,"/") == 0) && ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) || (strlen(path) == 0)) { ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) ||
(strlen(path) == 0)) {
return ESP_SDFile(); return ESP_SDFile();
} }
// path must start by '/' // path must start by '/'
@ -159,29 +159,31 @@ ESP_SDFile ESP_SD::open(const char* path, uint8_t mode)
return ESP_SDFile(); return ESP_SDFile();
} }
if (mode != ESP_FILE_READ) { if (mode != ESP_FILE_READ) {
//check container exists // check container exists
String p = path; String p = path;
p.remove(p.lastIndexOf('/') +1); p.remove(p.lastIndexOf('/') + 1);
if (!exists(p.c_str())) { if (!exists(p.c_str())) {
log_esp3d("Error opening: %s", path); log_esp3d("Error opening: %s", path);
return ESP_SDFile(); return ESP_SDFile();
} }
} }
File tmp = SD.open(path, (mode == ESP_FILE_READ)?FILE_READ:(mode == ESP_FILE_WRITE)?FILE_WRITE:FILE_APPEND); File tmp = SD.open(path, (mode == ESP_FILE_READ) ? FILE_READ
ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_FILE_READ)?false:true, path); : (mode == ESP_FILE_WRITE) ? FILE_WRITE
: FILE_APPEND);
ESP_SDFile esptmp(&tmp, tmp.isDirectory(),
(mode == ESP_FILE_READ) ? false : true, path);
return esptmp; return esptmp;
} }
bool ESP_SD::exists(const char* path) bool ESP_SD::exists(const char *path) {
{
bool res = false; bool res = false;
String p = path; String p = path;
//root should always be there if started // root should always be there if started
if (p == "/") { if (p == "/") {
return _started; return _started;
} }
if (p.endsWith("/")) { if (p.endsWith("/")) {
p.remove( p.length() - 1,1); p.remove(p.length() - 1, 1);
} }
res = SD.exists(p); res = SD.exists(p);
if (!res) { if (!res) {
@ -193,37 +195,31 @@ bool ESP_SD::exists(const char* path)
return res; return res;
} }
bool ESP_SD::remove(const char *path) bool ESP_SD::remove(const char *path) { return SD.remove(path); }
{
return SD.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 SD.mkdir(p.c_str());
} }
bool ESP_SD::rmdir(const char *path) bool ESP_SD::rmdir(const char *path) {
{
String p = path; String p = path;
if (!p.startsWith("/")) { if (!p.startsWith("/")) {
p = '/'+p; p = '/' + p;
} }
if (p!= "/") { if (p != "/") {
if (p.endsWith("/")) { if (p.endsWith("/")) {
p.remove(p.length()-1); p.remove(p.length() - 1);
} }
} }
if (!exists(p.c_str())) { if (!exists(p.c_str())) {
return false; return false;
} }
bool res = true; bool res = true;
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()); File dir = SD.open(pathlist.top().c_str());
@ -232,24 +228,23 @@ bool ESP_SD::rmdir(const char *path)
while (f && res) { while (f && res) {
if (f.isDirectory()) { if (f.isDirectory()) {
candelete = false; candelete = false;
String newdir = pathlist.top()+ '/'; String newdir = pathlist.top() + '/';
newdir+= f.name(); newdir += f.name();
pathlist.push(newdir); pathlist.push(newdir);
f.close(); f.close();
f = File(); f = 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 (!SD.remove(filepath.c_str())) {
res = false; res = false;
} }
f = dir.openNextFile(); f = dir.openNextFile();
} }
} }
if (candelete) { if (candelete) {
if (pathlist.top() !="/") { if (pathlist.top() != "/") {
res = SD.rmdir(pathlist.top().c_str()); res = SD.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
@ -261,16 +256,15 @@ bool ESP_SD::rmdir(const char *path)
return res; return res;
} }
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] = File();
} }
} }
ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char * path) ESP_SDFile::ESP_SDFile(void *handle, bool isdir, bool iswritemode,
{ const char *path) {
_isdir = isdir; _isdir = isdir;
_dirlist = ""; _dirlist = "";
_index = -1; _index = -1;
@ -280,52 +274,50 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
_iswritemode = iswritemode; _iswritemode = iswritemode;
_size = 0; _size = 0;
if (!handle) { if (!handle) {
return ; return;
} }
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] = *((File *)handle);
//filename // filename
_name = tSDFile_handle[i].name(); _name = tSDFile_handle[i].name();
_filename = path; _filename = path;
if (_name.endsWith("/")) { if (_name.endsWith("/")) {
_name.remove( _name.length() - 1,1); _name.remove(_name.length() - 1, 1);
_isdir = true; _isdir = true;
} }
if (_name[0] == '/') { if (_name[0] == '/') {
_name.remove( 0, 1); _name.remove(0, 1);
} }
int pos = _name.lastIndexOf('/'); int pos = _name.lastIndexOf('/');
if (pos != -1) { if (pos != -1) {
_name.remove( 0, pos+1); _name.remove(0, pos + 1);
} }
if (_name.length() == 0) { if (_name.length() == 0) {
_name = "/"; _name = "/";
} }
//size // size
_size = tSDFile_handle[i].size(); _size = tSDFile_handle[i].size();
//time // time
_lastwrite = tSDFile_handle[i].getLastWrite(); _lastwrite = tSDFile_handle[i].getLastWrite();
_index = i; _index = i;
//log_esp3d("Opening File at index %d",_index); // log_esp3d("Opening File at index %d",_index);
set = true; set = true;
} }
} }
} }
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, (SeekMode)mode);
} }
void ESP_SDFile::close() void ESP_SDFile::close() {
{
if (_index != -1) { if (_index != -1) {
//log_esp3d("Closing File at index %d", _index); // log_esp3d("Closing File at index %d", _index);
tSDFile_handle[_index].close(); tSDFile_handle[_index].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()); File ftmp = SD.open(_filename.c_str());
if (ftmp) { if (ftmp) {
@ -335,40 +327,34 @@ void ESP_SDFile::close()
} }
} }
tSDFile_handle[_index] = File(); tSDFile_handle[_index] = File();
//log_esp3d("Closing File at index %d",_index); // log_esp3d("Closing File at index %d",_index);
_index = -1; _index = -1;
} }
} }
ESP_SDFile ESP_SDFile::openNextFile() ESP_SDFile ESP_SDFile::openNextFile() {
{
if ((_index == -1) || !_isdir) { if ((_index == -1) || !_isdir) {
log_esp3d("openNextFile failed"); log_esp3d("openNextFile failed");
return ESP_SDFile(); return ESP_SDFile();
} }
File tmp = tSDFile_handle[_index].openNextFile(); File tmp = tSDFile_handle[_index].openNextFile();
if (tmp) { if (tmp) {
log_esp3d("tmp name :%s %s %s", tmp.name(), (tmp.isDirectory())?"isDir":"isFile", _filename.c_str()); log_esp3d("tmp name :%s %s %s", tmp.name(),
String s = tmp.name() ; (tmp.isDirectory()) ? "isDir" : "isFile", _filename.c_str());
//if (s!="/")s+="/"; String s = tmp.name();
//s += tmp.name(); // if (s!="/")s+="/";
ESP_SDFile esptmp(&tmp, tmp.isDirectory(),false, s.c_str()); // s += tmp.name();
ESP_SDFile esptmp(&tmp, tmp.isDirectory(), false, s.c_str());
esptmp.close(); esptmp.close();
return esptmp; return esptmp;
} }
return ESP_SDFile(); return ESP_SDFile();
} }
//TODO need to find reliable way // TODO need to find reliable way
const char* ESP_SDFile::shortname() const const char *ESP_SDFile::shortname() const { return _name.c_str(); }
{
return _name.c_str();
}
const char * ESP_SD::FilesystemName() const char *ESP_SD::FilesystemName() { return "SD native"; }
{
return "SD native";
}
#endif //SD_DEVICE == ESP_SD_NATIVE #endif // SD_DEVICE == ESP_SD_NATIVE
#endif //ARCH_ESP32 && SD_DEVICE #endif // ARCH_ESP32 && SD_DEVICE