Skip to content

Commit e625b3b

Browse files
authored
Add FS, SD and SD_MMC (espressif#256)
1 parent 56ffec8 commit e625b3b

19 files changed

+2555
-0
lines changed

Diff for: libraries/FS/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=FS
2+
version=1.0
3+
author=Hristo Gochkov, Ivan Grokhtkov
4+
maintainer=Hristo Gochkov <hristo@espressif.com>
5+
sentence=ESP32 File System
6+
paragraph=
7+
category=Data Storage
8+
url=
9+
architectures=esp32

Diff for: libraries/FS/src/FS.cpp

+260
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
/*
2+
FS.cpp - file system wrapper
3+
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
4+
This file is part of the esp8266 core for Arduino environment.
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 "FS.h"
22+
#include "FSImpl.h"
23+
24+
using namespace fs;
25+
26+
size_t File::write(uint8_t c)
27+
{
28+
if (!_p) {
29+
return 0;
30+
}
31+
32+
return _p->write(&c, 1);
33+
}
34+
35+
size_t File::write(const uint8_t *buf, size_t size)
36+
{
37+
if (!_p) {
38+
return 0;
39+
}
40+
41+
return _p->write(buf, size);
42+
}
43+
44+
int File::available()
45+
{
46+
if (!_p) {
47+
return false;
48+
}
49+
50+
return _p->size() - _p->position();
51+
}
52+
53+
int File::read()
54+
{
55+
if (!_p) {
56+
return -1;
57+
}
58+
59+
uint8_t result;
60+
if (_p->read(&result, 1) != 1) {
61+
return -1;
62+
}
63+
64+
return result;
65+
}
66+
67+
size_t File::read(uint8_t* buf, size_t size)
68+
{
69+
if (!_p) {
70+
return -1;
71+
}
72+
73+
return _p->read(buf, size);
74+
}
75+
76+
int File::peek()
77+
{
78+
if (!_p) {
79+
return -1;
80+
}
81+
82+
size_t curPos = _p->position();
83+
int result = read();
84+
seek(curPos, SeekSet);
85+
return result;
86+
}
87+
88+
void File::flush()
89+
{
90+
if (!_p) {
91+
return;
92+
}
93+
94+
_p->flush();
95+
}
96+
97+
bool File::seek(uint32_t pos, SeekMode mode)
98+
{
99+
if (!_p) {
100+
return false;
101+
}
102+
103+
return _p->seek(pos, mode);
104+
}
105+
106+
size_t File::position() const
107+
{
108+
if (!_p) {
109+
return 0;
110+
}
111+
112+
return _p->position();
113+
}
114+
115+
size_t File::size() const
116+
{
117+
if (!_p) {
118+
return 0;
119+
}
120+
121+
return _p->size();
122+
}
123+
124+
void File::close()
125+
{
126+
if (_p) {
127+
_p->close();
128+
_p = nullptr;
129+
}
130+
}
131+
132+
File::operator bool() const
133+
{
134+
return !!_p;
135+
}
136+
137+
const char* File::name() const
138+
{
139+
if (!_p) {
140+
return nullptr;
141+
}
142+
143+
return _p->name();
144+
}
145+
146+
//to implement
147+
boolean File::isDirectory(void)
148+
{
149+
if (!_p) {
150+
return false;
151+
}
152+
return _p->isDirectory();
153+
}
154+
155+
File File::openNextFile(const char* mode)
156+
{
157+
if (!_p) {
158+
return File();
159+
}
160+
return _p->openNextFile(mode);
161+
}
162+
163+
void File::rewindDirectory(void)
164+
{
165+
if (!_p) {
166+
return;
167+
}
168+
_p->rewindDirectory();
169+
}
170+
171+
File FS::open(const String& path, const char* mode)
172+
{
173+
return open(path.c_str(), mode);
174+
}
175+
176+
File FS::open(const char* path, const char* mode)
177+
{
178+
if (!_impl) {
179+
return File();
180+
}
181+
182+
return File(_impl->open(path, mode));
183+
}
184+
185+
bool FS::exists(const char* path)
186+
{
187+
if (!_impl) {
188+
return false;
189+
}
190+
return _impl->exists(path);
191+
}
192+
193+
bool FS::exists(const String& path)
194+
{
195+
return exists(path.c_str());
196+
}
197+
198+
bool FS::remove(const char* path)
199+
{
200+
if (!_impl) {
201+
return false;
202+
}
203+
return _impl->remove(path);
204+
}
205+
206+
bool FS::remove(const String& path)
207+
{
208+
return remove(path.c_str());
209+
}
210+
211+
bool FS::rename(const char* pathFrom, const char* pathTo)
212+
{
213+
if (!_impl) {
214+
return false;
215+
}
216+
return _impl->rename(pathFrom, pathTo);
217+
}
218+
219+
bool FS::rename(const String& pathFrom, const String& pathTo)
220+
{
221+
return rename(pathFrom.c_str(), pathTo.c_str());
222+
}
223+
224+
225+
bool FS::mkdir(const char *path)
226+
{
227+
if (!_impl) {
228+
return false;
229+
}
230+
return _impl->mkdir(path);
231+
}
232+
233+
bool FS::mkdir(const String &path)
234+
{
235+
return mkdir(path.c_str());
236+
}
237+
238+
bool FS::rmdir(const char *path)
239+
{
240+
if (!_impl) {
241+
return false;
242+
}
243+
return _impl->rmdir(path);
244+
}
245+
246+
bool FS::rmdir(const String &path)
247+
{
248+
return rmdir(path.c_str());
249+
}
250+
251+
252+
void FSImpl::mountpoint(const char * mp)
253+
{
254+
_mountpoint = mp;
255+
}
256+
257+
const char * FSImpl::mountpoint()
258+
{
259+
return _mountpoint;
260+
}

Diff for: libraries/FS/src/FS.h

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
FS.h - file system wrapper
3+
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
4+
This file is part of the esp8266 core for Arduino environment.
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+
#ifndef FS_H
22+
#define FS_H
23+
24+
#include <memory>
25+
#include <Arduino.h>
26+
27+
namespace fs
28+
{
29+
30+
#define FILE_READ "r"
31+
#define FILE_WRITE "w"
32+
#define FILE_APPEND "a"
33+
34+
class File;
35+
36+
class FileImpl;
37+
typedef std::shared_ptr<FileImpl> FileImplPtr;
38+
class FSImpl;
39+
typedef std::shared_ptr<FSImpl> FSImplPtr;
40+
41+
enum SeekMode {
42+
SeekSet = 0,
43+
SeekCur = 1,
44+
SeekEnd = 2
45+
};
46+
47+
class File : public Stream
48+
{
49+
public:
50+
File(FileImplPtr p = FileImplPtr()) : _p(p) {}
51+
52+
size_t write(uint8_t) override;
53+
size_t write(const uint8_t *buf, size_t size) override;
54+
int available() override;
55+
int read() override;
56+
int peek() override;
57+
void flush() override;
58+
size_t read(uint8_t* buf, size_t size);
59+
size_t readBytes(char *buffer, size_t length)
60+
{
61+
return read((uint8_t*)buffer, length);
62+
}
63+
64+
bool seek(uint32_t pos, SeekMode mode);
65+
bool seek(uint32_t pos)
66+
{
67+
return seek(pos, SeekSet);
68+
}
69+
size_t position() const;
70+
size_t size() const;
71+
void close();
72+
operator bool() const;
73+
const char* name() const;
74+
75+
boolean isDirectory(void);
76+
File openNextFile(const char* mode = FILE_READ);
77+
void rewindDirectory(void);
78+
79+
protected:
80+
FileImplPtr _p;
81+
};
82+
83+
class FS
84+
{
85+
public:
86+
FS(FSImplPtr impl) : _impl(impl) { }
87+
88+
File open(const char* path, const char* mode = FILE_READ);
89+
File open(const String& path, const char* mode = FILE_READ);
90+
91+
bool exists(const char* path);
92+
bool exists(const String& path);
93+
94+
bool remove(const char* path);
95+
bool remove(const String& path);
96+
97+
bool rename(const char* pathFrom, const char* pathTo);
98+
bool rename(const String& pathFrom, const String& pathTo);
99+
100+
bool mkdir(const char *path);
101+
bool mkdir(const String &path);
102+
103+
bool rmdir(const char *path);
104+
bool rmdir(const String &path);
105+
106+
107+
protected:
108+
FSImplPtr _impl;
109+
};
110+
111+
} // namespace fs
112+
113+
#ifndef FS_NO_GLOBALS
114+
using fs::FS;
115+
using fs::File;
116+
using fs::SeekMode;
117+
using fs::SeekSet;
118+
using fs::SeekCur;
119+
using fs::SeekEnd;
120+
#endif //FS_NO_GLOBALS
121+
122+
#endif //FS_H

0 commit comments

Comments
 (0)