Skip to content

SD Filesystem compatible with 8266 File, using latest SdFat #5525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4556403
Add a FAT filesystem for SD cards to Arduino FS
earlephilhower Dec 19, 2018
1b95e5d
Add a FSConfig and SDFSConfig param to FS.begin()
Jan 2, 2019
241d0ff
Add FS::setConfig to set FS-specific options
Jan 3, 2019
2b49e7a
Merge branch 'master' into SDFS
earlephilhower Jan 9, 2019
7fcbfdc
Fix platformio.sh test skipping merge error
Jan 9, 2019
a4dadb2
Merge branch 'master' into SDFS
earlephilhower Jan 16, 2019
edd9476
Fix merge error with SPIFFS_MOCK
earlephilhower Jan 17, 2019
9a82f00
Merge branch 'master' into SDFS
earlephilhower Jan 18, 2019
dc977b0
Merge branch 'master' into SDFS
earlephilhower Jan 28, 2019
ef2374c
Update to fix SPI transferBytes issue
earlephilhower Jan 28, 2019
c8acb84
Add ::truncate to File interface
earlephilhower Jan 28, 2019
d215b66
Merge branch 'master' into SDFS
earlephilhower Feb 1, 2019
ce148dd
Merge branch 'master' of https://github.com/esp8266/Arduino into SDFS
earlephilhower Feb 8, 2019
0a0bbc4
Merge branch 'master' into SDFS
earlephilhower Feb 8, 2019
652415d
Merge branch 'master' into SDFS
d-a-v Feb 18, 2019
85946ee
Merge branch 'master' into SDFS
d-a-v Feb 21, 2019
6b0a3f4
Update comments and MAX_PATH to 260
earlephilhower Feb 25, 2019
69bebd7
Merge branch 'master' into SDFS
earlephilhower Feb 25, 2019
1bc7377
Use polledTimeout for formatting yields, cleanup
earlephilhower Feb 27, 2019
cc3cc70
Add assert on illegal file open mode
earlephilhower Feb 27, 2019
e0c26bd
Merge branch 'master' into SDFS
earlephilhower Feb 27, 2019
35ec9ec
Make setConfig take const& ref, cleaner code
earlephilhower Feb 27, 2019
e53f26c
Merge branch 'master' into SDFS
devyte Mar 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "libraries/SoftwareSerial"]
path = libraries/SoftwareSerial
url = https://github.com/plerup/espsoftwareserial.git
[submodule "libraries/ESP8266SdFat"]
path = libraries/ESP8266SdFat
url = https://github.com/earlephilhower/ESP8266SdFat.git
95 changes: 89 additions & 6 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,44 @@ const char* File::name() const {
return _p->name();
}

const char* File::fullName() const {
if (!_p)
return nullptr;

return _p->fullName();
}

bool File::isFile() const {
if (!_p)
return false;

return _p->isFile();
}

bool File::isDirectory() const {
if (!_p)
return false;

return _p->isDirectory();
}

void File::rewindDirectory() {
if (!_fakeDir) {
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
} else {
_fakeDir->rewind();
}
}

File File::openNextFile() {
if (!_fakeDir) {
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
}
_fakeDir->next();
return _fakeDir->openFile("r");
}


File Dir::openFile(const char* mode) {
if (!_impl) {
return File();
Expand All @@ -133,7 +171,7 @@ File Dir::openFile(const char* mode) {
return File();
}

return File(_impl->openFile(om, am));
return File(_impl->openFile(om, am), _baseFS);
}

String Dir::fileName() {
Expand All @@ -152,6 +190,20 @@ size_t Dir::fileSize() {
return _impl->fileSize();
}

bool Dir::isFile() const {
if (!_impl)
return false;

return _impl->isFile();
}

bool Dir::isDirectory() const {
if (!_impl)
return false;

return _impl->isDirectory();
}

bool Dir::next() {
if (!_impl) {
return false;
Expand All @@ -160,11 +212,19 @@ bool Dir::next() {
return _impl->next();
}

bool FS::begin() {
bool Dir::rewind() {
if (!_impl) {
return false;
}
return _impl->begin();

return _impl->rewind();
}

bool FS::begin(const FSConfig *cfg) {
if (!_impl) {
return false;
}
return _impl->begin(cfg);
}

void FS::end() {
Expand Down Expand Up @@ -202,8 +262,7 @@ File FS::open(const char* path, const char* mode) {
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
return File();
}

return File(_impl->open(path, om, am));
return File(_impl->open(path, om, am), this);
}

bool FS::exists(const char* path) {
Expand All @@ -221,7 +280,8 @@ Dir FS::openDir(const char* path) {
if (!_impl) {
return Dir();
}
return Dir(_impl->openDir(path));
DirImplPtr p = _impl->openDir(path);
return Dir(p, this);
}

Dir FS::openDir(const String& path) {
Expand All @@ -239,6 +299,28 @@ bool FS::remove(const String& path) {
return remove(path.c_str());
}

bool FS::rmdir(const char* path) {
if (!_impl) {
return false;
}
return _impl->rmdir(path);
}

bool FS::rmdir(const String& path) {
return rmdir(path.c_str());
}

bool FS::mkdir(const char* path) {
if (!_impl) {
return false;
}
return _impl->mkdir(path);
}

bool FS::mkdir(const String& path) {
return mkdir(path.c_str());
}

bool FS::rename(const char* pathFrom, const char* pathTo) {
if (!_impl) {
return false;
Expand All @@ -251,6 +333,7 @@ bool FS::rename(const String& pathFrom, const String& pathTo) {
}



static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
switch (mode[0]) {
case 'r':
Expand Down
43 changes: 40 additions & 3 deletions cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace fs {

class File;
class Dir;
class FS;

class FileImpl;
typedef std::shared_ptr<FileImpl> FileImplPtr;
Expand All @@ -48,7 +49,7 @@ enum SeekMode {
class File : public Stream
{
public:
File(FileImplPtr p = FileImplPtr()) : _p(p) {}
File(FileImplPtr p = FileImplPtr(), FS *baseFS = nullptr) : _p(p), _fakeDir(nullptr), _baseFS(baseFS) { }

// Print methods:
size_t write(uint8_t) override;
Expand All @@ -72,22 +73,41 @@ class File : public Stream
void close();
operator bool() const;
const char* name() const;
const char* fullName() const; // Includes path

bool isFile() const;
bool isDirectory() const;

// Arduino "class SD" methods for compatibility
size_t write(const char *str) { return write((const uint8_t*)str, strlen(str)); }
void rewindDirectory();
File openNextFile();

protected:
FileImplPtr _p;

// Arduino SD class emulation
std::shared_ptr<Dir> _fakeDir;
FS *_baseFS;
};

class Dir {
public:
Dir(DirImplPtr impl = DirImplPtr()): _impl(impl) { }
Dir(DirImplPtr impl = DirImplPtr(), FS *baseFS = nullptr): _impl(impl), _baseFS(baseFS) { }

File openFile(const char* mode);

String fileName();
size_t fileSize();
bool isFile() const;
bool isDirectory() const;

bool next();
bool rewind();

protected:
DirImplPtr _impl;
FS *_baseFS;
};

struct FSInfo {
Expand All @@ -99,12 +119,22 @@ struct FSInfo {
size_t maxPathLength;
};

class FSConfig
{
public:
FSConfig(bool autoFormat = true) {
_autoFormat = autoFormat;
}

bool _autoFormat;
};

class FS
{
public:
FS(FSImplPtr impl) : _impl(impl) { }

bool begin();
bool begin(const FSConfig *cfg = nullptr);
void end();

bool format();
Expand All @@ -125,6 +155,12 @@ class FS
bool rename(const char* pathFrom, const char* pathTo);
bool rename(const String& pathFrom, const String& pathTo);

bool mkdir(const char* path);
bool mkdir(const String& path);

bool rmdir(const char* path);
bool rmdir(const String& path);

protected:
FSImplPtr _impl;
};
Expand All @@ -140,6 +176,7 @@ using fs::SeekSet;
using fs::SeekCur;
using fs::SeekEnd;
using fs::FSInfo;
using fs::FSConfig;
#endif //FS_NO_GLOBALS

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)
Expand Down
11 changes: 9 additions & 2 deletions cores/esp8266/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class FileImpl {
virtual size_t size() const = 0;
virtual void close() = 0;
virtual const char* name() const = 0;
virtual const char* fullName() const = 0;
virtual bool isFile() const = 0;
virtual bool isDirectory() const = 0;
};

enum OpenMode {
Expand All @@ -57,13 +60,16 @@ class DirImpl {
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
virtual const char* fileName() = 0;
virtual size_t fileSize() = 0;
virtual bool isFile() const = 0;
virtual bool isDirectory() const = 0;
virtual bool next() = 0;
virtual bool rewind() = 0;
};

class FSImpl {
public:
virtual ~FSImpl () { }
virtual bool begin() = 0;
virtual bool begin(const FSConfig *cfg) = 0;
virtual void end() = 0;
virtual bool format() = 0;
virtual bool info(FSInfo& info) = 0;
Expand All @@ -72,7 +78,8 @@ class FSImpl {
virtual DirImplPtr openDir(const char* path) = 0;
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
virtual bool remove(const char* path) = 0;

virtual bool mkdir(const char* path) = 0;
virtual bool rmdir(const char* path) = 0;
};

} // namespace fs
Expand Down
Loading