From 3694e3ca4be5a633ca0761dc8bb852571233a0aa Mon Sep 17 00:00:00 2001 From: Luc Date: Fri, 11 Oct 2019 12:01:25 +0800 Subject: [PATCH] some code cleaning --- esp3d/configuration.h | 24 ++--- esp3d/src/include/defines.h | 7 +- esp3d/src/include/esp3d_config.h | 4 +- esp3d/src/include/pins.h | 15 ++- .../src/modules/devices/devices_services.cpp | 15 +++ .../src/modules/filesystem/esp_filesystem.cpp | 1 - esp3d/src/modules/filesystem/esp_sd.cpp | 17 ++- esp3d/src/modules/filesystem/esp_sd.h | 15 +-- esp3d/src/modules/filesystem/sd/sd_esp32.cpp | 102 +++++++++++++----- esp3d/src/modules/serial/serial_service.cpp | 3 + 10 files changed, 149 insertions(+), 54 deletions(-) diff --git a/esp3d/configuration.h b/esp3d/configuration.h index 399ff016..ab2b29be 100644 --- a/esp3d/configuration.h +++ b/esp3d/configuration.h @@ -102,8 +102,18 @@ #if defined (PIN_RESET_FEATURE) #define ESP3D_RESET_PIN 2 #endif //PIN_RESET_FEATURE -//SDCARD_FEATURE: to access SD Card files directly instead of access by serial using printer Board FW -//#define SDCARD_FEATURE + +//SD_DEVICE: to access SD Card files directly instead of access by serial using printer Board FW +//ESP_SD_NATIVE 1 +//ESP_SDIO 2 +//ESP_SDFAT 3 +#define SD_DEVICE ESP_SD_NATIVE + +//pin if reader has insert detection feature +//let -1 or comment if none +#define ESP_SD_DETECT_PIN -1 +//value expected for ESP_SD_DETECT_PIN +#define ESP_SD_DETECT_VALUE 1 //FILESYSTEM_FEATURE: to host some files on flash //ESP_SPIFFS_FILESYSTEM 0 @@ -209,16 +219,6 @@ //Serial rx buffer size is 256 but can be extended #define SERIAL_RX_BUFFER_SIZE 512 -//Serial Parameters -#define ESP_SERIAL_PARAM SERIAL_8N1 - -//Serial Pins -//-1 means use default pins of your board what ever the serial you choose -// * UART 0 possible options are (1, 3), (2, 3) or (15, 13) -// * UART 1 allows only TX on 2 if UART 0 is not (2, 3) -#define ESP_RX_PIN -1 -#define ESP_TX_PIN -1 - /************************************ * * Settings diff --git a/esp3d/src/include/defines.h b/esp3d/src/include/defines.h index 2d984c39..27929dce 100644 --- a/esp3d/src/include/defines.h +++ b/esp3d/src/include/defines.h @@ -62,11 +62,16 @@ #define ESP_FAT_FILESYSTEM 2 #define ESP_LITTLEFS_FILESYSTEM 3 -//SD +//SD READER FS type supported #define ESP_SD_NATIVE 1 #define ESP_SDIO 2 #define ESP_SDFAT 3 +//SD state +#define ESP_SDCARD_IDLE 0 +#define ESP_SDCARD_NOT_PRESENT 1 +#define ESP_SDCARD_BUSY 2 + //Notifications #define ESP_PUSHOVER_NOTIFICATION 1 #define ESP_EMAIL_NOTIFICATION 2 diff --git a/esp3d/src/include/esp3d_config.h b/esp3d/src/include/esp3d_config.h index f0218da8..7388f935 100644 --- a/esp3d/src/include/esp3d_config.h +++ b/esp3d/src/include/esp3d_config.h @@ -40,8 +40,8 @@ #define RECOVERY_FEATURE #endif //PIN_RESET_FEATURE || SD_RECOVERY_FEATURE -#if defined(DISPLAY_DEVICE) || defined(DHT_DEVICE) || defined(RECOVERY_FEATURE) || defined(BUZZER_DEVICE) || defined(CAMERA_DEVICE) +#if defined(DISPLAY_DEVICE) || defined(DHT_DEVICE) || defined(RECOVERY_FEATURE) || defined(BUZZER_DEVICE) || defined(CAMERA_DEVICE) || defined(SD_DEVICE) #define CONNECTED_DEVICES_FEATURE -#endif //DISPLAY_DEVICE || DHT_DEVICE +#endif //DISPLAY_DEVICE || DHT_DEVICE , etc... #endif //_ESP3D_CONFIG_H diff --git a/esp3d/src/include/pins.h b/esp3d/src/include/pins.h index ed0c541a..1751c0b9 100644 --- a/esp3d/src/include/pins.h +++ b/esp3d/src/include/pins.h @@ -18,8 +18,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -//Pins for the support of connected camera +//Serial Pins +//-1 means use default pins of your board what ever the serial you choose +// * UART 0 possible options are (1, 3), (2, 3) or (15, 13) +// * UART 1 allows only TX on 2 if UART 0 is not (2, 3) +#define ESP_RX_PIN -1 +#define ESP_TX_PIN -1 +//Pins for the support of connected camera #if CAMERA_DEVICE == CAMERA_MODEL_CUSTOM #define CAM_LED_PIN 4 #define CAM_PULLUP1 -1 @@ -157,3 +163,10 @@ #define HREF_GPIO_NUM 23 #define PCLK_GPIO_NUM 22 #endif //CAMERA_MODEL_AI_THINKER + +//Pins for the support of SD Card Reader +//-1 means use default pins of your board defined core +#define ESP_SD_CS_PIN 13 //-1 +#define ESP_SD_MISO_PIN 2 //-1 +#define ESP_SD_MOSI_PIN 15 //-1 +#define ESP_SD_SCK_PIN 14 //-1 diff --git a/esp3d/src/modules/devices/devices_services.cpp b/esp3d/src/modules/devices/devices_services.cpp index dcb29261..d19717a1 100644 --- a/esp3d/src/modules/devices/devices_services.cpp +++ b/esp3d/src/modules/devices/devices_services.cpp @@ -39,6 +39,9 @@ #ifdef CAMERA_DEVICE #include "../camera/camera.h" #endif //CAMERA_DEVICE +#ifdef SD_DEVICE +#include "../filesystem/esp_sd.h" +#endif //SD_DEVICE bool DevicesServices::_started = false; @@ -84,6 +87,12 @@ bool DevicesServices::begin() res = false; } #endif //CAMERA_DEVICE +#ifdef SD_DEVICE + if (!ESP_SD::begin()) { + log_esp3d("Error sd intialization failed"); + res = false; + } +#endif //SD_DEVICE if (!res) { end(); } @@ -96,6 +105,9 @@ void DevicesServices::end() return; } _started = false; +#ifdef SD_DEVICE + ESP_SD::end(); +#endif //SD_DEVICE #ifdef CAMERA_DEVICE esp3d_camera.stopHardware(); #endif //CAMERA_DEVICE @@ -128,6 +140,9 @@ void DevicesServices::handle() #ifdef RECOVERY_FEATURE recovery_service.handle(); #endif //RECOVERY_FEATURE +#ifdef SD_DEVICE + ESP_SD::handle(); +#endif //SD_DEVICE } } diff --git a/esp3d/src/modules/filesystem/esp_filesystem.cpp b/esp3d/src/modules/filesystem/esp_filesystem.cpp index aabfb869..2331fa8a 100644 --- a/esp3d/src/modules/filesystem/esp_filesystem.cpp +++ b/esp3d/src/modules/filesystem/esp_filesystem.cpp @@ -20,7 +20,6 @@ #include "../../include/esp3d_config.h" #ifdef FILESYSTEM_FEATURE #include "esp_filesystem.h" -#include "../../core/genLinkedList.h" #ifdef FILESYSTEM_TIMESTAMP_FEATURE #include #endif //FILESYSTEM_TIMESTAMP_FEATURE diff --git a/esp3d/src/modules/filesystem/esp_sd.cpp b/esp3d/src/modules/filesystem/esp_sd.cpp index b86a58f6..0ccec941 100644 --- a/esp3d/src/modules/filesystem/esp_sd.cpp +++ b/esp3d/src/modules/filesystem/esp_sd.cpp @@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "../../include/esp3d_config.h" -#ifdef SD_FEATURE +#ifdef SD_DEVICE #include "esp_sd.h" #include "../../core/genLinkedList.h" #ifdef SD_TIMESTAMP_FEATURE @@ -31,6 +31,14 @@ File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; bool ESP_SD::_started = false; +uint8_t ESP_SD::_state = ESP_SDCARD_IDLE; + +uint8_t ESP_SD::setState(uint8_t flag) +{ + _state = flag; + return _state; +} + //constructor ESP_SD::ESP_SD() @@ -42,6 +50,11 @@ ESP_SD::~ESP_SD() { } +void ESP_SD::handle() +{ + +} + //helper to format size to readable string String & ESP_SD::formatBytes (uint64_t bytes) { @@ -185,4 +198,4 @@ ESP_SDFile& ESP_SDFile::operator=(const ESP_SDFile & other) return *this; } -#endif //SD_FEATURE +#endif //SD_DEVICE diff --git a/esp3d/src/modules/filesystem/esp_sd.h b/esp3d/src/modules/filesystem/esp_sd.h index 5bcf9cf0..01a90ac3 100644 --- a/esp3d/src/modules/filesystem/esp_sd.h +++ b/esp3d/src/modules/filesystem/esp_sd.h @@ -77,16 +77,16 @@ public: ESP_SD(); ~ESP_SD(); static bool begin(); + static void handle(); static void end(); - static size_t totalBytes(); - static size_t usedBytes(); - static size_t freeBytes() - { - return totalBytes()-usedBytes(); - }; + static uint8_t getState(bool refresh); + static uint8_t setState(uint8_t state); + static uint64_t totalBytes(); + static uint64_t usedBytes(); + static uint64_t freeBytes(); static const char * FilesystemName(); static bool format(); - static ESP_File open(const char* path, uint8_t mode = ESP_SD_FILE_READ); + static ESP_SDFile open(const char* path, uint8_t mode = ESP_SD_FILE_READ); static bool exists(const char* path); static bool remove(const char *path); static bool mkdir(const char *path); @@ -94,6 +94,7 @@ public: static void closeAll(); private: static bool _started; + static uint8_t _state; }; diff --git a/esp3d/src/modules/filesystem/sd/sd_esp32.cpp b/esp3d/src/modules/filesystem/sd/sd_esp32.cpp index f053ba26..b0b2d974 100644 --- a/esp3d/src/modules/filesystem/sd/sd_esp32.cpp +++ b/esp3d/src/modules/filesystem/sd/sd_esp32.cpp @@ -18,58 +18,104 @@ sd_native_esp32.cpp - ESP3D sd support class Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "../../../include/esp3d_config.h" -#if defined (ARCH_ESP32) && defined(SD_FEATURE) -#if (SD_FEATURE == ESP_SD_NATIVE) +#if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE) +#if (SD_DEVICE == ESP_SD_NATIVE) #include "../esp_sd.h" +#include "../../../core/genLinkedList.h" #include "FS.h" #include "SD.h" +#define ESP_SPI_FREQ 4000000 + +//TODO read this from EEPROM/Preferences +#define ESP_SD_SPEED_DIVIDER 1 + extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; -bool ESP_SDFileSystem::begin() +uint8_t ESP_SD::getState(bool refresh) { - _started = SD.begin(); +#if defined(ESP_SD_DETECT_PIN) && ESP_SD_DETECT_PIN != -1 + //no need to go further if SD detect is not correct + if (!((digitalRead (ESP_SD_DETECT_PIN) == ESP_SD_DETECT_VALUE) ? true : false)) { + _state = ESP_SDCARD_NOT_PRESENT; + return _state; + } +#endif //ESP_SD_DETECT_PIN + //if busy doing something return state + if (!((_state == ESP_SDCARD_NOT_PRESENT) || _state == ESP_SDCARD_IDLE)) { + return _state; + } + if (!refresh) { + return _state; //to avoid refresh=true + busy to reset SD and waste time + } +//SD is idle or not detected, let see if still the case + + SD.end(); + _state = ESP_SDCARD_NOT_PRESENT; +//using default value for speed ? should be parameter +//refresh content if card was removed + if (SD.begin((ESP_SD_CS_PIN == -1)?SS:ESP_SD_CS_PIN, SPI, ESP_SPI_FREQ / ESP_SD_SPEED_DIVIDER)) { + if ( SD.cardSize() > 0 ) { + _state = ESP_SDCARD_IDLE; + } + } + return _state; +} + +bool ESP_SD::begin() +{ +#if (ESP_SD_CS_PIN != -1) || (ESP_SD_MISO_PIN != -1) || (ESP_SD_MOSI_PIN != -1) || (ESP_SD_SCK_PIN != -1) + SPI.begin(ESP_SD_SCK_PIN, ESP_SD_MISO_PIN, ESP_SD_MOSI_PIN, ESP_SD_CS_PIN); +#endif + _started = true; + _state = ESP_SDCARD_IDLE; return _started; } -void ESP_SDFileSystem::end() +void ESP_SD::end() { SD.end(); + _state = ESP_SDCARD_IDLE; _started = false; } -size_t ESP_SDFileSystem::totalBytes() +uint64_t ESP_SD::totalBytes() { return SD.totalBytes(); } -size_t ESP_SDFileSystem::usedBytes() +uint64_t ESP_SD::usedBytes() { - return (SD.totalBytes() - SD.freeBytes()); + return SD.usedBytes(); } - -const char * ESP_SDFileSystem::FilesystemName() +uint64_t freeBytes() { - return "FAT"; + return (SD.totalBytes() - SD.usedBytes()); +}; + +const char * ESP_SD::FilesystemName() +{ + return "SD Native"; } -bool ESP_SDFileSystem::format() +bool ESP_SD::format() { - return SD.format(); + //not available yet + return false; } -ESP_SDFile ESP_SDFileSystem::open(const char* path, uint8_t mode) +ESP_SDFile ESP_SD::open(const char* path, uint8_t mode) { //do some check - if(((strcmp(path,"/") == 0) && ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) || (strlen(path) == 0)) { + if(((strcmp(path,"/") == 0) && ((mode == ESP_SD_FILE_WRITE) || (mode == ESP_SD_FILE_APPEND))) || (strlen(path) == 0)) { return ESP_SDFile(); } // path must start by '/' if (path[0] != '/') { return ESP_SDFile(); } - if (mode != ESP_FILE_READ) { + if (mode != ESP_SD_FILE_READ) { //check container exists String p = path; p.remove(p.lastIndexOf('/') +1); @@ -78,12 +124,12 @@ ESP_SDFile ESP_SDFileSystem::open(const char* path, uint8_t mode) return ESP_SDFile(); } } - File tmp = SD.open(path, (mode == ESP_FILE_READ)?FILE_READ:(mode == ESP_FILE_WRITE)?FILE_WRITE:FILE_APPEND); - ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_FILE_READ)?false:true, path); + File tmp = SD.open(path, (mode == ESP_SD_FILE_READ)?FILE_READ:(mode == ESP_SD_FILE_WRITE)?FILE_WRITE:FILE_APPEND); + ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_SD_FILE_READ)?false:true, path); return esptmp; } -bool ESP_SDFileSystem::exists(const char* path) +bool ESP_SD::exists(const char* path) { bool res = false; //root should always be there if started @@ -92,7 +138,7 @@ bool ESP_SDFileSystem::exists(const char* path) } res = SD.exists(path); if (!res) { - ESP_SDFile root = ESP_SDFileSystem::open(path, ESP_FILE_READ); + ESP_SDFile root = ESP_SD::open(path, ESP_SD_FILE_READ); if (root) { res = root.isDirectory(); } @@ -100,17 +146,17 @@ bool ESP_SDFileSystem::exists(const char* path) return res; } -bool ESP_SDFileSystem::remove(const char *path) +bool ESP_SD::remove(const char *path) { return SD.remove(path); } -bool ESP_SDFileSystem::mkdir(const char *path) +bool ESP_SD::mkdir(const char *path) { return SD.mkdir(path); } -bool ESP_SDFileSystem::rmdir(const char *path) +bool ESP_SD::rmdir(const char *path) { if (!exists(path)) { return false; @@ -149,9 +195,9 @@ bool ESP_SDFileSystem::rmdir(const char *path) return res; } -void ESP_SDFileSystem::closeAll() +void ESP_SD::closeAll() { - for (uint8_t i = 0; i < ESP_MAX_OPENHANDLE; i++) { + for (uint8_t i = 0; i < ESP_MAX_SD_OPENHANDLE; i++) { tSDFile_handle[i].close(); tSDFile_handle[i] = File(); } @@ -174,7 +220,7 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char * return ; } bool set =false; - for (uint8_t i=0; (i < ESP_MAX_OPENHANDLE) && !set; i++) { + for (uint8_t i=0; (i < ESP_MAX_SD_OPENHANDLE) && !set; i++) { if (!tSDFile_handle[i]) { tSDFile_handle[i] = *((File*)handle); //filename @@ -291,5 +337,5 @@ ESP_SDFile ESP_SDFile::openNextFile() } -#endif //SD_FEATURE == ESP_SD_NATIVE -#endif //ARCH_ESP32 && SD_FEATURE +#endif //SD_DEVICE == ESP_SD_NATIVE +#endif //ARCH_ESP32 && SD_DEVICE diff --git a/esp3d/src/modules/serial/serial_service.cpp b/esp3d/src/modules/serial/serial_service.cpp index 4f2d45e7..23fca9fd 100644 --- a/esp3d/src/modules/serial/serial_service.cpp +++ b/esp3d/src/modules/serial/serial_service.cpp @@ -24,6 +24,9 @@ #include "../../core/esp3doutput.h" #include "../../core/commands.h" +//Serial Parameters +#define ESP_SERIAL_PARAM SERIAL_8N1 + #if ESP_SERIAL_OUTPUT == USE_SERIAL_0 #define ESP3D_SERIAL Serial #endif //USE_SERIAL_0