some code cleaning

This commit is contained in:
Luc 2019-10-11 12:01:25 +08:00
parent 57a965dcee
commit 3694e3ca4b
10 changed files with 149 additions and 54 deletions

View File

@ -102,8 +102,18 @@
#if defined (PIN_RESET_FEATURE) #if defined (PIN_RESET_FEATURE)
#define ESP3D_RESET_PIN 2 #define ESP3D_RESET_PIN 2
#endif //PIN_RESET_FEATURE #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 //FILESYSTEM_FEATURE: to host some files on flash
//ESP_SPIFFS_FILESYSTEM 0 //ESP_SPIFFS_FILESYSTEM 0
@ -209,16 +219,6 @@
//Serial rx buffer size is 256 but can be extended //Serial rx buffer size is 256 but can be extended
#define SERIAL_RX_BUFFER_SIZE 512 #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 * Settings

View File

@ -62,11 +62,16 @@
#define ESP_FAT_FILESYSTEM 2 #define ESP_FAT_FILESYSTEM 2
#define ESP_LITTLEFS_FILESYSTEM 3 #define ESP_LITTLEFS_FILESYSTEM 3
//SD //SD READER FS type supported
#define ESP_SD_NATIVE 1 #define ESP_SD_NATIVE 1
#define ESP_SDIO 2 #define ESP_SDIO 2
#define ESP_SDFAT 3 #define ESP_SDFAT 3
//SD state
#define ESP_SDCARD_IDLE 0
#define ESP_SDCARD_NOT_PRESENT 1
#define ESP_SDCARD_BUSY 2
//Notifications //Notifications
#define ESP_PUSHOVER_NOTIFICATION 1 #define ESP_PUSHOVER_NOTIFICATION 1
#define ESP_EMAIL_NOTIFICATION 2 #define ESP_EMAIL_NOTIFICATION 2

View File

@ -40,8 +40,8 @@
#define RECOVERY_FEATURE #define RECOVERY_FEATURE
#endif //PIN_RESET_FEATURE || SD_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 #define CONNECTED_DEVICES_FEATURE
#endif //DISPLAY_DEVICE || DHT_DEVICE #endif //DISPLAY_DEVICE || DHT_DEVICE , etc...
#endif //_ESP3D_CONFIG_H #endif //_ESP3D_CONFIG_H

View File

@ -18,8 +18,14 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 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 #if CAMERA_DEVICE == CAMERA_MODEL_CUSTOM
#define CAM_LED_PIN 4 #define CAM_LED_PIN 4
#define CAM_PULLUP1 -1 #define CAM_PULLUP1 -1
@ -157,3 +163,10 @@
#define HREF_GPIO_NUM 23 #define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22 #define PCLK_GPIO_NUM 22
#endif //CAMERA_MODEL_AI_THINKER #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

View File

@ -39,6 +39,9 @@
#ifdef CAMERA_DEVICE #ifdef CAMERA_DEVICE
#include "../camera/camera.h" #include "../camera/camera.h"
#endif //CAMERA_DEVICE #endif //CAMERA_DEVICE
#ifdef SD_DEVICE
#include "../filesystem/esp_sd.h"
#endif //SD_DEVICE
bool DevicesServices::_started = false; bool DevicesServices::_started = false;
@ -84,6 +87,12 @@ bool DevicesServices::begin()
res = false; res = false;
} }
#endif //CAMERA_DEVICE #endif //CAMERA_DEVICE
#ifdef SD_DEVICE
if (!ESP_SD::begin()) {
log_esp3d("Error sd intialization failed");
res = false;
}
#endif //SD_DEVICE
if (!res) { if (!res) {
end(); end();
} }
@ -96,6 +105,9 @@ void DevicesServices::end()
return; return;
} }
_started = false; _started = false;
#ifdef SD_DEVICE
ESP_SD::end();
#endif //SD_DEVICE
#ifdef CAMERA_DEVICE #ifdef CAMERA_DEVICE
esp3d_camera.stopHardware(); esp3d_camera.stopHardware();
#endif //CAMERA_DEVICE #endif //CAMERA_DEVICE
@ -128,6 +140,9 @@ void DevicesServices::handle()
#ifdef RECOVERY_FEATURE #ifdef RECOVERY_FEATURE
recovery_service.handle(); recovery_service.handle();
#endif //RECOVERY_FEATURE #endif //RECOVERY_FEATURE
#ifdef SD_DEVICE
ESP_SD::handle();
#endif //SD_DEVICE
} }
} }

View File

@ -20,7 +20,6 @@
#include "../../include/esp3d_config.h" #include "../../include/esp3d_config.h"
#ifdef FILESYSTEM_FEATURE #ifdef FILESYSTEM_FEATURE
#include "esp_filesystem.h" #include "esp_filesystem.h"
#include "../../core/genLinkedList.h"
#ifdef FILESYSTEM_TIMESTAMP_FEATURE #ifdef FILESYSTEM_TIMESTAMP_FEATURE
#include <time.h> #include <time.h>
#endif //FILESYSTEM_TIMESTAMP_FEATURE #endif //FILESYSTEM_TIMESTAMP_FEATURE

View File

@ -18,7 +18,7 @@
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"
#ifdef SD_FEATURE #ifdef SD_DEVICE
#include "esp_sd.h" #include "esp_sd.h"
#include "../../core/genLinkedList.h" #include "../../core/genLinkedList.h"
#ifdef SD_TIMESTAMP_FEATURE #ifdef SD_TIMESTAMP_FEATURE
@ -31,6 +31,14 @@
File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
bool ESP_SD::_started = false; 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 //constructor
ESP_SD::ESP_SD() ESP_SD::ESP_SD()
@ -42,6 +50,11 @@ ESP_SD::~ESP_SD()
{ {
} }
void ESP_SD::handle()
{
}
//helper to format size to readable string //helper to format size to readable string
String & ESP_SD::formatBytes (uint64_t bytes) String & ESP_SD::formatBytes (uint64_t bytes)
{ {
@ -185,4 +198,4 @@ ESP_SDFile& ESP_SDFile::operator=(const ESP_SDFile & other)
return *this; return *this;
} }
#endif //SD_FEATURE #endif //SD_DEVICE

View File

@ -77,16 +77,16 @@ public:
ESP_SD(); ESP_SD();
~ESP_SD(); ~ESP_SD();
static bool begin(); static bool begin();
static void handle();
static void end(); static void end();
static size_t totalBytes(); static uint8_t getState(bool refresh);
static size_t usedBytes(); static uint8_t setState(uint8_t state);
static size_t freeBytes() static uint64_t totalBytes();
{ static uint64_t usedBytes();
return totalBytes()-usedBytes(); static uint64_t freeBytes();
};
static const char * FilesystemName(); static const char * FilesystemName();
static bool format(); 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 exists(const char* path);
static bool remove(const char *path); static bool remove(const char *path);
static bool mkdir(const char *path); static bool mkdir(const char *path);
@ -94,6 +94,7 @@ public:
static void closeAll(); static void closeAll();
private: private:
static bool _started; static bool _started;
static uint8_t _state;
}; };

View File

@ -18,58 +18,104 @@ 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 (ARCH_ESP32) && defined(SD_FEATURE) #if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE)
#if (SD_FEATURE == ESP_SD_NATIVE) #if (SD_DEVICE == ESP_SD_NATIVE)
#include "../esp_sd.h" #include "../esp_sd.h"
#include "../../../core/genLinkedList.h"
#include "FS.h" #include "FS.h"
#include "SD.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]; 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; return _started;
} }
void ESP_SDFileSystem::end() void ESP_SD::end()
{ {
SD.end(); SD.end();
_state = ESP_SDCARD_IDLE;
_started = false; _started = false;
} }
size_t ESP_SDFileSystem::totalBytes() uint64_t ESP_SD::totalBytes()
{ {
return SD.totalBytes(); return SD.totalBytes();
} }
size_t ESP_SDFileSystem::usedBytes() uint64_t ESP_SD::usedBytes()
{ {
return (SD.totalBytes() - SD.freeBytes()); return SD.usedBytes();
} }
uint64_t freeBytes()
const char * ESP_SDFileSystem::FilesystemName()
{ {
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 //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(); return ESP_SDFile();
} }
// path must start by '/' // path must start by '/'
if (path[0] != '/') { if (path[0] != '/') {
return ESP_SDFile(); return ESP_SDFile();
} }
if (mode != ESP_FILE_READ) { if (mode != ESP_SD_FILE_READ) {
//check container exists //check container exists
String p = path; String p = path;
p.remove(p.lastIndexOf('/') +1); p.remove(p.lastIndexOf('/') +1);
@ -78,12 +124,12 @@ ESP_SDFile ESP_SDFileSystem::open(const char* path, uint8_t mode)
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_SD_FILE_READ)?FILE_READ:(mode == ESP_SD_FILE_WRITE)?FILE_WRITE:FILE_APPEND);
ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_FILE_READ)?false:true, path); ESP_SDFile esptmp(&tmp, tmp.isDirectory(),(mode == ESP_SD_FILE_READ)?false:true, path);
return esptmp; return esptmp;
} }
bool ESP_SDFileSystem::exists(const char* path) bool ESP_SD::exists(const char* path)
{ {
bool res = false; bool res = false;
//root should always be there if started //root should always be there if started
@ -92,7 +138,7 @@ bool ESP_SDFileSystem::exists(const char* path)
} }
res = SD.exists(path); res = SD.exists(path);
if (!res) { 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) { if (root) {
res = root.isDirectory(); res = root.isDirectory();
} }
@ -100,17 +146,17 @@ bool ESP_SDFileSystem::exists(const char* path)
return res; return res;
} }
bool ESP_SDFileSystem::remove(const char *path) bool ESP_SD::remove(const char *path)
{ {
return SD.remove(path); return SD.remove(path);
} }
bool ESP_SDFileSystem::mkdir(const char *path) bool ESP_SD::mkdir(const char *path)
{ {
return SD.mkdir(path); return SD.mkdir(path);
} }
bool ESP_SDFileSystem::rmdir(const char *path) bool ESP_SD::rmdir(const char *path)
{ {
if (!exists(path)) { if (!exists(path)) {
return false; return false;
@ -149,9 +195,9 @@ bool ESP_SDFileSystem::rmdir(const char *path)
return res; 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].close();
tSDFile_handle[i] = File(); tSDFile_handle[i] = File();
} }
@ -174,7 +220,7 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char *
return ; return ;
} }
bool set =false; 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]) { if (!tSDFile_handle[i]) {
tSDFile_handle[i] = *((File*)handle); tSDFile_handle[i] = *((File*)handle);
//filename //filename
@ -291,5 +337,5 @@ ESP_SDFile ESP_SDFile::openNextFile()
} }
#endif //SD_FEATURE == ESP_SD_NATIVE #endif //SD_DEVICE == ESP_SD_NATIVE
#endif //ARCH_ESP32 && SD_FEATURE #endif //ARCH_ESP32 && SD_DEVICE

View File

@ -24,6 +24,9 @@
#include "../../core/esp3doutput.h" #include "../../core/esp3doutput.h"
#include "../../core/commands.h" #include "../../core/commands.h"
//Serial Parameters
#define ESP_SERIAL_PARAM SERIAL_8N1
#if ESP_SERIAL_OUTPUT == USE_SERIAL_0 #if ESP_SERIAL_OUTPUT == USE_SERIAL_0
#define ESP3D_SERIAL Serial #define ESP3D_SERIAL Serial
#endif //USE_SERIAL_0 #endif //USE_SERIAL_0