Skip to content

Commit 143dbae

Browse files
committed
Merge branch 'ficeto-esp8266' into esp8266
* ficeto-esp8266: add template methods for stream to stream writes to SD and FS alignment not needed. we use fixed addresses Rework SPIFFS API to be more Arduino like fix missed edits disable automount fix SPIFFS to work pull get/set NoDelay for WiFiClient Add SPIFFS Support export sketch data folder to the build config Revert "Revert "Edit SD Server example to use the new Write(Stream) method"" add template client write Revert "Add WiFiClient.write for Stream" Revert "Edit SD Server example to use the new Write(Stream) method" Edit SD Server example to use the new Write(Stream) method Add WiFiClient.write for Stream make upload callback packets aligned to defined size Conflicts: hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h
2 parents 8c1a40b + fa9ee11 commit 143dbae

File tree

29 files changed

+7392
-85
lines changed

29 files changed

+7392
-85
lines changed

arduino-core/src/processing/app/debug/Compiler.java

+7
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,13 @@ private PreferencesMap createBuildPreferences(String _buildPath,
460460
p.put("build.path", _buildPath);
461461
p.put("build.project_name", _primaryClassName);
462462
p.put("build.arch", targetPlatform.getId().toUpperCase());
463+
File sketch_data = sketch.getDataFolder();
464+
if(sketch_data.exists()){
465+
p.put("build.sketch_data", sketch_data.getAbsolutePath());
466+
} else {
467+
p.put("build.sketch_data", "");
468+
}
469+
463470

464471
// Platform.txt should define its own compiler.path. For
465472
// compatibility with earlier 1.5 versions, we define a (ugly,

hardware/esp8266com/esp8266/cores/esp8266/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C" {
3838
#include "pgmspace.h"
3939
#include "esp8266_peri.h"
4040
#include "twi.h"
41+
#include "spiffs/spiffs.h"
4142

4243
void yield(void);
4344

@@ -211,6 +212,7 @@ void loop(void);
211212
#include "WString.h"
212213

213214
#include "HardwareSerial.h"
215+
#include "FileSystem.h"
214216
#include "Esp.h"
215217
#include "debug.h"
216218

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
FileSystem.cpp - SPIFS implementation for esp8266
3+
4+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
#include "FileSystem.h"
22+
#include "Arduino.h"
23+
24+
boolean FSClass::mount(){
25+
if(_mounted) return true;
26+
_mounted = spiffs_mount();
27+
return _mounted;
28+
}
29+
30+
void FSClass::unmount(){
31+
if(!_mounted) return;
32+
spiffs_unmount();
33+
_mounted = false;
34+
}
35+
36+
boolean FSClass::format(){
37+
return spiffs_format();
38+
}
39+
40+
boolean FSClass::exists(const char *filename){
41+
spiffs_stat stat = {0};
42+
if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0) return false;
43+
return stat.name[0] != '\0';
44+
}
45+
46+
boolean FSClass::create(const char *filepath){
47+
return SPIFFS_creat(&_filesystemStorageHandle, filepath, 0) == 0;
48+
}
49+
50+
boolean FSClass::remove(const char *filepath){
51+
return SPIFFS_remove(&_filesystemStorageHandle, filepath) == 0;
52+
}
53+
54+
boolean FSClass::rename(const char *filename, const char *newname){
55+
return SPIFFS_rename(&_filesystemStorageHandle, filename, newname) == 0;
56+
}
57+
58+
FSFile FSClass::open(const char *filename, uint8_t mode){
59+
int repeats = 0;
60+
bool notExist;
61+
bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT;
62+
int res;
63+
64+
do{
65+
notExist = false;
66+
res = SPIFFS_open(&_filesystemStorageHandle, filename, (spiffs_flags)mode, 0);
67+
int code = SPIFFS_errno(&_filesystemStorageHandle);
68+
if (res < 0){
69+
debugf("open errno %d\n", code);
70+
notExist = (code == SPIFFS_ERR_NOT_FOUND || code == SPIFFS_ERR_DELETED || code == SPIFFS_ERR_FILE_DELETED || code == SPIFFS_ERR_IS_FREE);
71+
if (notExist && canRecreate)
72+
remove(filename); // fix for deleted files
73+
}
74+
} while (notExist && canRecreate && repeats++ < 3);
75+
76+
if(res){
77+
return FSFile(res);
78+
}
79+
return FSFile();
80+
}
81+
82+
FSClass FS;
83+
84+
FSFile::FSFile(){
85+
_file = 0;
86+
_stats = {0};
87+
}
88+
89+
FSFile::FSFile(file_t f){
90+
_file = f;
91+
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
92+
debugf("mount errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
93+
}
94+
}
95+
96+
void FSFile::close(){
97+
if (! _file) return;
98+
SPIFFS_close(&_filesystemStorageHandle, _file);
99+
_file = 0;
100+
}
101+
102+
uint32_t FSFile::size(){
103+
if(! _file) return 0;
104+
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
105+
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
106+
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
107+
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
108+
return size;
109+
}
110+
111+
uint32_t FSFile::seek(uint32_t pos){
112+
if (! _file) return 0;
113+
return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
114+
}
115+
116+
uint32_t FSFile::position(){
117+
if (! _file) return 0;
118+
return SPIFFS_tell(&_filesystemStorageHandle, _file);
119+
}
120+
121+
boolean FSFile::eof(){
122+
if (! _file) return 0;
123+
return SPIFFS_eof(&_filesystemStorageHandle, _file);
124+
}
125+
126+
boolean FSFile::isDirectory(void){
127+
return false;
128+
}
129+
130+
int FSFile::read(void *buf, uint16_t nbyte){
131+
if (! _file) return -1;
132+
return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte);
133+
}
134+
135+
int FSFile::read(){
136+
if (! _file) return -1;
137+
int val;
138+
if(SPIFFS_read(&_filesystemStorageHandle, _file, &val, 1) != 1) return -1;
139+
return val;
140+
}
141+
142+
int FSFile::peek() {
143+
if (! _file) return 0;
144+
int c = read();
145+
SPIFFS_lseek(&_filesystemStorageHandle, _file, -1, SPIFFS_SEEK_CUR);
146+
return c;
147+
}
148+
149+
int FSFile::available() {
150+
if (! _file) return 0;
151+
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
152+
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
153+
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
154+
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
155+
return size - pos;
156+
}
157+
158+
size_t FSFile::write(const uint8_t *buf, size_t size){
159+
if (! _file) return 0;
160+
return SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
161+
}
162+
163+
size_t FSFile::write(uint8_t val) {
164+
if (! _file) return 0;
165+
return write(&val, 1);
166+
}
167+
168+
void FSFile::flush(){
169+
if (! _file) return;
170+
SPIFFS_fflush(&_filesystemStorageHandle, _file);
171+
}
172+
173+
uint32_t FSFile::remove(){
174+
if (! _file) return 0;
175+
return SPIFFS_fremove(&_filesystemStorageHandle, _file);
176+
_file = 0;
177+
}
178+
179+
int FSFile::lastError(){
180+
return SPIFFS_errno(&_filesystemStorageHandle);
181+
}
182+
183+
void FSFile::clearError(){
184+
_filesystemStorageHandle.errno = SPIFFS_OK;
185+
}
186+
187+
char * FSFile::name(){
188+
return 0;
189+
}
190+
191+
192+
193+
194+
195+
196+
/*
197+
spiffs_DIR *dirOpen(spiffs_DIR *d){
198+
return SPIFFS_opendir(&_filesystemStorageHandle, 0, d);
199+
}
200+
201+
int dirClose(spiffs_DIR *d){
202+
return SPIFFS_closedir(d);
203+
}
204+
205+
file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){
206+
return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0);
207+
}
208+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
FileSystem.h - SPIFS implementation for esp8266
3+
4+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
#ifndef _SPIFFS_CORE_FILESYSTEM_H_
22+
#define _SPIFFS_CORE_FILESYSTEM_H_
23+
24+
#include "spiffs/spiffs.h"
25+
#include "Arduino.h"
26+
class String;
27+
28+
#define FSFILE_READ SPIFFS_RDONLY
29+
#define FSFILE_WRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC)
30+
31+
class FSFile : public Stream {
32+
private:
33+
spiffs_stat _stats;
34+
file_t _file;
35+
36+
public:
37+
FSFile(file_t f);
38+
FSFile(void);
39+
virtual size_t write(uint8_t);
40+
virtual size_t write(const uint8_t *buf, size_t size);
41+
virtual int read();
42+
virtual int peek();
43+
virtual int available();
44+
virtual void flush();
45+
int read(void *buf, uint16_t nbyte);
46+
uint32_t seek(uint32_t pos);
47+
uint32_t remove();
48+
uint32_t position();
49+
uint32_t size();
50+
boolean eof();
51+
void close();
52+
int lastError();
53+
void clearError();
54+
operator bool(){ return _file > 0; }
55+
char * name();
56+
boolean isDirectory(void);
57+
58+
template<typename T> size_t write(T &src){
59+
uint8_t obuf[64];
60+
size_t doneLen = 0;
61+
size_t sentLen;
62+
int i;
63+
64+
while (src.available() > 64){
65+
src.read(obuf, 64);
66+
sentLen = write(obuf, 64);
67+
doneLen = doneLen + sentLen;
68+
if(sentLen != 64){
69+
return doneLen;
70+
}
71+
}
72+
73+
size_t leftLen = src.available();
74+
src.read(obuf, leftLen);
75+
sentLen = write(obuf, leftLen);
76+
doneLen = doneLen + sentLen;
77+
return doneLen;
78+
}
79+
80+
using Print::write;
81+
};
82+
83+
class FSClass {
84+
85+
private:
86+
boolean _mounted;
87+
88+
public:
89+
boolean mount();
90+
void unmount();
91+
boolean format();
92+
boolean exists(const char *filename);
93+
boolean create(const char *filepath);
94+
boolean remove(const char *filepath);
95+
boolean rename(const char *filename, const char *newname);
96+
97+
FSFile open(const char *filename, uint8_t mode = FSFILE_READ);
98+
99+
private:
100+
friend class FSFile;
101+
};
102+
103+
extern FSClass FS;
104+
105+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013-2015 Peter Andersson (pelleplutt1976<at>gmail.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)