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