ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
esp_sd.cpp
Go to the documentation of this file.
1 /*
2  esp_sd.cpp - ESP3D SD support class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #include "../../include/esp3d_config.h"
22 #ifdef SD_DEVICE
23 #include "esp_sd.h"
24 #include "../../core/genLinkedList.h"
25 #include <time.h>
26 
27 #define ESP_MAX_SD_OPENHANDLE 4
28 #if ((SD_DEVICE == ESP_SD_NATIVE) || (SD_DEVICE == ESP_SDFAT)) && defined (ARDUINO_ARCH_ESP8266)
29 #define FS_NO_GLOBALS
30 #define NO_GLOBAL_SD
31 #include <SdFat.h>
33 #elif (SD_DEVICE == ESP_SDFAT) && defined (ARDUINO_ARCH_ESP32)
34 #include <SdFat.h>
36 #else
37 #include <FS.h>
39 #endif
40 
41 bool ESP_SD::_started = false;
42 uint8_t ESP_SD::_state = ESP_SDCARD_NOT_PRESENT;
43 uint8_t ESP_SD::_spi_speed_divider = 1;
44 bool ESP_SD::_sizechanged = true;
45 uint8_t ESP_SD::setState(uint8_t flag)
46 {
47  _state = flag;
48  return _state;
49 }
50 
51 
52 //constructor
54 {
55 }
56 
57 //destructor
59 {
60 }
61 
63 {
64 
65 }
66 
67 //helper to format size to readable string
68 String & ESP_SD::formatBytes (uint64_t bytes)
69 {
70  static String res;
71  if (bytes < 1024) {
72  res = String ((uint16_t)bytes) + " B";
73  } else if (bytes < (1024 * 1024) ) {
74  res = String ((float)(bytes / 1024.0),2) + " KB";
75  } else if (bytes < (1024 * 1024 * 1024) ) {
76  res = String ((float)(bytes / 1024.0 / 1024.0),2) + " MB";
77  } else {
78  res = String ((float)(bytes / 1024.0 / 1024.0 / 1024.0),2) + " GB";
79  }
80  return res;
81 }
82 
83 ESP_SDFile::ESP_SDFile(const char * name, const char * filename, bool isdir, size_t size)
84 {
85  _isdir = isdir;
86  _dirlist = "";
87  _index = -1;
88  _filename = filename;
89  _name = name;
90  _lastwrite = 0;
91  _iswritemode = false;
92  _size = size;
93 }
94 
96 {
97  //log_esp3d("Destructor %s index %d",(_isdir)?"Dir":"File", _index);
98 }
99 
100 ESP_SDFile::operator bool() const
101 {
102  if ((_index != -1) || (_filename.length() > 0)) {
103  //log_esp3d("Bool yes %d %d",_index, _filename.length());
104  return true;
105  } else {
106  return false;
107  }
108 }
109 
111 {
112  return !(_index == -1);
113 }
114 
115 const char* ESP_SDFile::name() const
116 {
117  return _name.c_str();
118 }
119 
120 const char* ESP_SDFile::filename() const
121 {
122  return _filename.c_str();
123 }
124 
126 {
127  return _isdir;
128 }
129 
131 {
132  return _size;
133 }
134 
136 {
137  return _lastwrite;
138 }
139 
141 {
142  if (_index == -1 || _isdir) {
143  return 0;
144  }
145  return tSDFile_handle[_index].available();
146 }
147 
148 size_t ESP_SDFile::write(uint8_t i)
149 {
150  if ((_index == -1) || _isdir) {
151  return 0;
152  }
153  return tSDFile_handle[_index].write (i);
154 }
155 
156 size_t ESP_SDFile::write(const uint8_t *buf, size_t size)
157 {
158  if ((_index == -1) || _isdir) {
159  return 0;
160  }
161  return tSDFile_handle[_index].write (buf, size);
162 }
163 
165 {
166  if ((_index == -1) || _isdir) {
167  return -1;
168  }
169  return tSDFile_handle[_index].read();
170 }
171 
172 size_t ESP_SDFile::read(uint8_t* buf, size_t size)
173 {
174  if ((_index == -1) || _isdir) {
175  return -1;
176  }
177  return tSDFile_handle[_index].read(buf, size);
178 }
179 
181 {
182  if ((_index == -1) || _isdir) {
183  return;
184  }
185  tSDFile_handle[_index].flush();
186 }
187 
189 {
190  //log_esp3d("Copy %s", other._filename.c_str());
191  _isdir = other._isdir;
192  _index = other._index;
193  _filename = other._filename;
194  _name = other._name;
195  _size = other._size;
196  _iswritemode = other._iswritemode;
197  _dirlist = other._dirlist;
198  _lastwrite = other._lastwrite;
199  return *this;
200 }
201 
202 bool ESP_SD::setSPISpeedDivider(uint8_t speeddivider)
203 {
204  if (speeddivider > 0) {
205  _spi_speed_divider = speeddivider;
206  return true;
207  }
208  return false;
209 }
210 
211 #endif //SD_DEVICE
ESP_SDCARD_NOT_PRESENT
#define ESP_SDCARD_NOT_PRESENT
Definition: defines.h:81
ESP_SDFile::isDirectory
bool isDirectory()
Definition: esp_sd.cpp:125
ESP_SDFile::name
const char * name() const
Definition: esp_sd.cpp:115
ESP_SDFile::available
int available()
Definition: esp_sd.cpp:140
ESP_SDFile
Definition: esp_sd.h:31
ESP_SD::ESP_SD
ESP_SD()
Definition: esp_sd.cpp:53
ESP_SDFile::write
size_t write(uint8_t i)
Definition: esp_sd.cpp:148
ESP_SDFile::size
size_t size()
Definition: esp_sd.cpp:130
ESP_SD::setState
static uint8_t setState(uint8_t state)
Definition: esp_sd.cpp:45
ESP_SD::handle
static void handle()
Definition: esp_sd.cpp:62
ESP_SDFile::operator=
ESP_SDFile & operator=(const ESP_SDFile &other)
Definition: esp_sd.cpp:188
ESP_SDFile::getLastWrite
time_t getLastWrite()
Definition: esp_sd.cpp:135
ESP_SD::formatBytes
static String & formatBytes(uint64_t bytes)
Definition: esp_sd.cpp:68
ESP_SDFile::read
int read()
Definition: esp_sd.cpp:164
ESP_SDFile::isOpen
bool isOpen()
Definition: esp_sd.cpp:110
ESP_SDFile::ESP_SDFile
ESP_SDFile(void *handle=nullptr, bool isdir=false, bool iswritemode=false, const char *path=nullptr)
ESP_SDFile::flush
void flush()
Definition: esp_sd.cpp:180
ESP_MAX_SD_OPENHANDLE
#define ESP_MAX_SD_OPENHANDLE
Definition: esp_sd.cpp:27
tSDFile_handle
File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]
Definition: esp_sd.cpp:38
ESP_SD::~ESP_SD
~ESP_SD()
Definition: esp_sd.cpp:58
ESP_SDFile::~ESP_SDFile
~ESP_SDFile()
Definition: esp_sd.cpp:95
esp_sd.h
ESP_SDFile::filename
const char * filename() const
Definition: esp_sd.cpp:120
ESP_SD::setSPISpeedDivider
static bool setSPISpeedDivider(uint8_t speeddivider)
Definition: esp_sd.cpp:202