ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
esp_filesystem.cpp
Go to the documentation of this file.
1 /*
2  esp_filesystem.cpp - ESP3D filesystem configuration 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 #include "../../include/esp3d_config.h"
21 #ifdef FILESYSTEM_FEATURE
22 #include "esp_filesystem.h"
23 #include <time.h>
24 #include <FS.h>
25 #ifdef ARDUINO_ARCH_ESP32
26 #include <esp_ota_ops.h>
27 #endif //ARDUINO_ARCH_ESP32
28 
29 #define ESP_MAX_OPENHANDLE 4
31 
32 bool ESP_FileSystem::_started = false;
33 
34 //constructor
36 {
37 }
38 
39 //destructor
41 {
42 }
43 
44 //helper to format size to readable string
45 String & ESP_FileSystem::formatBytes (uint64_t bytes)
46 {
47  static String res;
48  if (bytes < 1024) {
49  res = String ((uint16_t)bytes) + " B";
50  } else if (bytes < (1024 * 1024) ) {
51  res = String ((float)(bytes / 1024.0),2) + " KB";
52  } else if (bytes < (1024 * 1024 * 1024) ) {
53  res = String ((float)(bytes / 1024.0 / 1024.0),2) + " MB";
54  } else {
55  res = String ((float)(bytes / 1024.0 / 1024.0 / 1024.0),2) + " GB";
56  }
57  return res;
58 }
59 
61 {
62  size_t flashsize = 0;
63 #if defined (ARDUINO_ARCH_ESP8266)
64  flashsize = ESP.getFlashChipSize();
65  //if higher than 1MB take out SPIFFS
66  if (flashsize <= 1024 * 1024) {
67  flashsize = (1024 * 1024)-ESP.getSketchSize()-1024;
68  } else {
69  flashsize = flashsize - ESP.getSketchSize()-totalBytes()-1024;
70  //max OTA partition is 1019Kb
71  if (flashsize > 1024 * 1024) {
72  flashsize = (1024 * 1024) - 1024;
73  }
74  }
75 #endif //ARDUINO_ARCH_ESP8266
76 #if defined (ARDUINO_ARCH_ESP32)
77  //Is OTA available ?
78  if (esp_ota_get_running_partition()) {
79  const esp_partition_t* partition = esp_ota_get_next_update_partition(NULL);
80  if (partition) {
81  flashsize = partition->size;
82  }
83  } else {
84  flashsize = 0;
85  }
86 #endif //ARDUINO_ARCH_ESP32
87  return flashsize;
88 }
89 
90 ESP_File::ESP_File(const char * name, const char * filename, bool isdir, size_t size)
91 {
92  _isdir = isdir;
93  _dirlist = "";
94  _isfakedir = isdir;
95  _index = -1;
96  _filename = filename;
97  _name = name;
98  _lastwrite = 0;
99  _iswritemode = false;
100  _size = size;
101 }
102 
104 {
105  //log_esp3d("Destructor %s index %d",(_isdir)?"Dir":"File", _index);
106 }
107 
108 ESP_File::operator bool() const
109 {
110  if ((_index != -1) || (_filename.length() > 0)) {
111  //log_esp3d("Bool yes %d %d",_index, _filename.length());
112  return true;
113  } else {
114  return false;
115  }
116 }
117 
119 {
120  return !(_index == -1);
121 }
122 
123 const char* ESP_File::name() const
124 {
125  return _name.c_str();
126 }
127 
128 const char* ESP_File::filename() const
129 {
130  return _filename.c_str();
131 }
132 
134 {
135  return _isdir;
136 }
137 
139 {
140  return _size;
141 }
142 
144 {
145  return _lastwrite;
146 }
147 
149 {
150  if (_index == -1 || _isdir) {
151  return 0;
152  }
153  return tFile_handle[_index].available();
154 }
155 
156 size_t ESP_File::write(uint8_t i)
157 {
158  if ((_index == -1) || _isdir) {
159  return 0;
160  }
161  return tFile_handle[_index].write (i);
162 }
163 
164 size_t ESP_File::write(const uint8_t *buf, size_t size)
165 {
166  if ((_index == -1) || _isdir) {
167  return 0;
168  }
169  return tFile_handle[_index].write (buf, size);
170 }
171 
173 {
174  if ((_index == -1) || _isdir) {
175  return -1;
176  }
177  return tFile_handle[_index].read();
178 }
179 
180 size_t ESP_File::read(uint8_t* buf, size_t size)
181 {
182  if ((_index == -1) || _isdir) {
183  return -1;
184  }
185  return tFile_handle[_index].read(buf, size);
186 }
187 
189 {
190  if ((_index == -1) || _isdir) {
191  return;
192  }
193  tFile_handle[_index].flush();
194 }
195 
197 {
198  //log_esp3d("Copy %s", other._filename.c_str());
199  _isdir = other._isdir;
200  _isfakedir = other._isfakedir;
201  _index = other._index;
202  _filename = other._filename;
203  _name = other._name;
204  _size = other._size;
205  _iswritemode = other._iswritemode;
206  _dirlist = other._dirlist;
207  _lastwrite = other._lastwrite;
208  return *this;
209 }
210 
211 #endif //FILESYSTEM_FEATURE
ESP_File::operator=
ESP_File & operator=(const ESP_File &other)
Definition: esp_filesystem.cpp:196
ESP_File::~ESP_File
~ESP_File()
Definition: esp_filesystem.cpp:103
ESP_File::name
const char * name() const
Definition: esp_filesystem.cpp:123
ESP_MAX_OPENHANDLE
#define ESP_MAX_OPENHANDLE
Definition: esp_filesystem.cpp:29
ESP_FileSystem::totalBytes
static size_t totalBytes()
ESP_File::getLastWrite
time_t getLastWrite()
Definition: esp_filesystem.cpp:143
ESP_FileSystem::ESP_FileSystem
ESP_FileSystem()
Definition: esp_filesystem.cpp:35
ESP_File::isOpen
bool isOpen()
Definition: esp_filesystem.cpp:118
ESP_File::filename
const char * filename() const
Definition: esp_filesystem.cpp:128
ESP_File
Definition: esp_filesystem.h:30
ESP_FileSystem::max_update_size
static size_t max_update_size()
Definition: esp_filesystem.cpp:60
tFile_handle
File tFile_handle[ESP_MAX_OPENHANDLE]
Definition: esp_filesystem.cpp:30
ESP_File::read
int read()
Definition: esp_filesystem.cpp:172
ESP_File::write
size_t write(uint8_t i)
Definition: esp_filesystem.cpp:156
ESP_FileSystem::~ESP_FileSystem
~ESP_FileSystem()
Definition: esp_filesystem.cpp:40
ESP_File::size
size_t size()
Definition: esp_filesystem.cpp:138
ESP_File::ESP_File
ESP_File(void *handle=nullptr, bool isdir=false, bool iswritemode=false, const char *path=nullptr)
esp_filesystem.h
ESP_FileSystem::formatBytes
static String & formatBytes(uint64_t bytes)
Definition: esp_filesystem.cpp:45
ESP_File::available
int available()
Definition: esp_filesystem.cpp:148
ESP_File::flush
void flush()
Definition: esp_filesystem.cpp:188
ESP_File::isDirectory
bool isDirectory()
Definition: esp_filesystem.cpp:133