FastLED 3.9.15
Loading...
Searching...
No Matches
file_system.cpp
Go to the documentation of this file.
1#include "fl/file_system.h"
2#include "fl/unused.h"
3#include "fl/warn.h"
4
5#ifdef __EMSCRIPTEN__
6#include "platforms/wasm/fs_wasm.h"
7#elif __has_include(<SD.h>)
8// work in progress.
9// #include "platforms/fs_sdcard_arduino.hpp"
10#endif
11
12#include "fl/json.h"
13#include "fl/namespace.h"
14#include "fl/screenmap.h"
15#include "fl/unused.h"
16
17namespace fl {
18
19class NullFileHandle : public FileHandle {
20 public:
21 NullFileHandle() = default;
22 ~NullFileHandle() override {}
23
24 bool available() const override { return false; }
25 size_t size() const override { return 0; }
26 size_t read(uint8_t *dst, size_t bytesToRead) override {
27 FASTLED_UNUSED(dst);
28 FASTLED_UNUSED(bytesToRead);
29 return 0;
30 }
31 size_t pos() const override { return 0; }
32 const char *path() const override { return "NULL FILE HANDLE"; }
33 bool seek(size_t pos) override {
35 return false;
36 }
37 void close() override {}
38 bool valid() const override {
39 FASTLED_WARN("NullFileHandle is not valid");
40 return false;
41 }
42};
43
44using namespace fl;
45
46class NullFileSystem : public FsImpl {
47 public:
49 FASTLED_WARN("NullFileSystem instantiated as a placeholder, please "
50 "implement a file system for your platform.");
51 }
52 ~NullFileSystem() override {}
53
54 bool begin() override { return true; }
55 void end() override {}
56
57 void close(FileHandlePtr file) override {
58 // No need to do anything for in-memory files
59 FASTLED_UNUSED(file);
60 FASTLED_WARN("NullFileSystem::close");
61 }
62
63 FileHandlePtr openRead(const char *_path) override {
64 FASTLED_UNUSED(_path);
65 FileHandlePtr out = FileHandlePtr::TakeOwnership(new NullFileHandle());
66 return out;
67 }
68};
69
70bool FileSystem::beginSd(int cs_pin) {
72 if (!mFs) {
73 return false;
74 }
75 mFs->begin();
76 return true;
77}
78
79bool FileSystem::begin(FsImplPtr platform_filesystem) {
80 mFs = platform_filesystem;
81 if (!mFs) {
82 return false;
83 }
84 mFs->begin();
85 return true;
86}
87
88size_t FileHandle::bytesLeft() const { return size() - pos(); }
89
91
93 if (mFs) {
94 mFs->end();
95 }
96}
97
98bool FileSystem::readJson(const char *path, JsonDocument *doc) {
99 Str text;
100 if (!readText(path, &text)) {
101 return false;
102 }
103 return parseJson(text.c_str(), doc);
104}
105
106bool FileSystem::readScreenMaps(const char *path,
107 FixedMap<Str, ScreenMap, 16> *out, Str *error) {
108 Str text;
109 if (!readText(path, &text)) {
110 FASTLED_WARN("Failed to read file: " << path);
111 if (error) {
112 *error = "Failed to read file: ";
113 error->append(path);
114 }
115 return false;
116 }
117 Str err;
118 bool ok = ScreenMap::ParseJson(text.c_str(), out, &err);
119 if (!ok) {
120 FASTLED_WARN("Failed to parse screen map: " << err.c_str());
121 *error = err;
122 return false;
123 }
124 return true;
125}
126
127bool FileSystem::readScreenMap(const char *path, const char *name,
128 ScreenMap *out, Str *error) {
129 Str text;
130 if (!readText(path, &text)) {
131 FASTLED_WARN("Failed to read file: " << path);
132 if (error) {
133 *error = "Failed to read file: ";
134 error->append(path);
135 }
136 return false;
137 }
138 Str err;
139 bool ok = ScreenMap::ParseJson(text.c_str(), name, out, &err);
140 if (!ok) {
141 FASTLED_WARN("Failed to parse screen map: " << err.c_str());
142 *error = err;
143 return false;
144 }
145 return true;
146}
147
148void FileSystem::close(FileHandlePtr file) { mFs->close(file); }
149
150FileHandlePtr FileSystem::openRead(const char *path) {
151 return mFs->openRead(path);
152}
153Video FileSystem::openVideo(const char *path, size_t pixelsPerFrame, float fps,
154 size_t nFrameHistory) {
155 Video video(pixelsPerFrame, fps, nFrameHistory);
156 FileHandlePtr file = openRead(path);
157 if (!file) {
158 video.setError(fl::Str("Could not open file: ").append(path));
159 return video;
160 }
161 video.begin(file);
162 return video;
163}
164
165bool FileSystem::readText(const char *path, fl::Str *out) {
166 FileHandlePtr file = openRead(path);
167 if (!file) {
168 FASTLED_WARN("Failed to open file: " << path);
169 return false;
170 }
171 size_t size = file->size();
172 out->reserve(size + out->size());
173 bool wrote = false;
174 while (file->available()) {
175 uint8_t buf[64];
176 size_t n = file->read(buf, sizeof(buf));
177 // out->append(buf, n);
178 out->append((const char *)buf, n);
179 wrote = true;
180 }
181 file->close();
182 FASTLED_DBG_IF(!wrote, "Failed to write any data to the output string.");
183 return wrote;
184}
185} // namespace fl
186
187namespace fl {
188__attribute__((weak)) FsImplPtr make_sdcard_filesystem(int cs_pin) {
189 FASTLED_UNUSED(cs_pin);
190 FsImplPtr out = FsImplPtr::TakeOwnership(new NullFileSystem());
191 return out;
192}
193
194} // namespace fl
Video video
Definition FxSdCard.ino:63
virtual size_t size() const =0
virtual size_t bytesLeft() const
virtual size_t pos() const =0
FileHandlePtr openRead(const char *path)
bool beginSd(int cs_pin)
Video openVideo(const char *path, size_t pixelsPerFrame, float fps=30.0f, size_t nFrameHistory=0)
bool readScreenMap(const char *path, const char *name, ScreenMap *out, Str *error=nullptr)
bool readScreenMaps(const char *path, FixedMap< Str, ScreenMap, 16 > *out, Str *error=nullptr)
bool readText(const char *path, Str *out)
bool readJson(const char *path, JsonDocument *doc)
bool begin(FsImplPtr platform_filesystem)
FsImplPtr mFs
Definition file_system.h:59
void close(FileHandlePtr file)
FsImpl()=default
bool available() const override
size_t read(uint8_t *dst, size_t bytesToRead) override
const char * path() const override
size_t pos() const override
size_t size() const override
void close() override
NullFileHandle()=default
bool valid() const override
bool seek(size_t pos) override
~NullFileHandle() override
void end() override
bool begin() override
~NullFileSystem() override
void close(FileHandlePtr file) override
FileHandlePtr openRead(const char *_path) override
static bool ParseJson(const char *jsonStrScreenMap, FixedMap< Str, ScreenMap, 16 > *segmentMaps, Str *err=nullptr)
Definition screenmap.cpp:51
Str & append(const T &val)
Definition str.h:439
Definition str.h:388
const char * c_str() const
Definition str.h:272
size_t size() const
Definition str.h:270
void reserve(size_t newCapacity)
Definition str.h:308
#define FASTLED_DBG_IF(COND, MSG)
Definition dbg.h:66
Implements the FastLED namespace macros.
bool parseJson(const char *json, fl::JsonDocument *doc, Str *_error)
Definition json.cpp:6
FsImplPtr make_sdcard_filesystem(int cs_pin)
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
#define FASTLED_UNUSED(x)
Definition unused.h:3
#define FASTLED_WARN
Definition warn.h:7