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"
5#include "fl/has_include.h"
6
7#ifdef __EMSCRIPTEN__
8#include "platforms/wasm/fs_wasm.h"
9#define FASTLED_HAS_SDCARD 1
10#elif FL_HAS_INCLUDE(<SD.h>) && FL_HAS_INCLUDE(<fs.h>)
11// Include Arduino SD card implementation when SD library is available
12#include "platforms/fs_sdcard_arduino.hpp"
13#define FASTLED_HAS_SDCARD 1
14#else
15#define FASTLED_HAS_SDCARD 0
16#endif
17
18#include "fl/json.h"
19#include "fl/namespace.h"
20#include "fl/screenmap.h"
21#include "fl/unused.h"
22
23namespace fl {
24
25class NullFileHandle : public FileHandle {
26 public:
27 NullFileHandle() = default;
28 ~NullFileHandle() override {}
29
30 bool available() const override { return false; }
31 fl::size size() const override { return 0; }
32 fl::size read(u8 *dst, fl::size bytesToRead) override {
33 FASTLED_UNUSED(dst);
34 FASTLED_UNUSED(bytesToRead);
35 return 0;
36 }
37 fl::size pos() const override { return 0; }
38 const char *path() const override { return "NULL FILE HANDLE"; }
39 bool seek(fl::size pos) override {
41 return false;
42 }
43 void close() override {}
44 bool valid() const override {
45 FASTLED_WARN("NullFileHandle is not valid");
46 return false;
47 }
48};
49
50class NullFileSystem : public FsImpl {
51 public:
53 FASTLED_WARN("NullFileSystem instantiated as a placeholder, please "
54 "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);
70 FileHandlePtr out = ptr;
71 return out;
72 }
73};
74
75bool FileSystem::beginSd(int cs_pin) {
77 if (!mFs) {
78 return false;
79 }
80 mFs->begin();
81 return true;
82}
83
84bool FileSystem::begin(FsImplPtr platform_filesystem) {
85 mFs = platform_filesystem;
86 if (!mFs) {
87 return false;
88 }
89 mFs->begin();
90 return true;
91}
92
93fl::size FileHandle::bytesLeft() const { return size() - pos(); }
94
96
98 if (mFs) {
99 mFs->end();
100 }
101}
102
103bool FileSystem::readJson(const char *path, Json *doc) {
104 string text;
105 if (!readText(path, &text)) {
106 return false;
107 }
108
109 // Parse using the new Json class
110 *doc = fl::Json::parse(text);
111 return !doc->is_null();
112}
113
114bool FileSystem::readScreenMaps(const char *path,
115 fl::fl_map<string, ScreenMap> *out, string *error) {
116 string text;
117 if (!readText(path, &text)) {
118 FASTLED_WARN("Failed to read file: " << path);
119 if (error) {
120 *error = "Failed to read file: ";
121 error->append(path);
122 }
123 return false;
124 }
125 string err;
126 bool ok = ScreenMap::ParseJson(text.c_str(), out, &err);
127 if (!ok) {
128 FASTLED_WARN("Failed to parse screen map: " << err.c_str());
129 *error = err;
130 return false;
131 }
132 return true;
133}
134
135bool FileSystem::readScreenMap(const char *path, const char *name,
136 ScreenMap *out, string *error) {
137 string text;
138 if (!readText(path, &text)) {
139 FASTLED_WARN("Failed to read file: " << path);
140 if (error) {
141 *error = "Failed to read file: ";
142 error->append(path);
143 }
144 return false;
145 }
146 string err;
147 bool ok = ScreenMap::ParseJson(text.c_str(), name, out, &err);
148 if (!ok) {
149 FASTLED_WARN("Failed to parse screen map: " << err.c_str());
150 *error = err;
151 return false;
152 }
153 return true;
154}
155
156void FileSystem::close(FileHandlePtr file) { mFs->close(file); }
157
158FileHandlePtr FileSystem::openRead(const char *path) {
159 return mFs->openRead(path);
160}
161Video FileSystem::openVideo(const char *path, fl::size pixelsPerFrame, float fps,
162 fl::size nFrameHistory) {
163 Video video(pixelsPerFrame, fps, nFrameHistory);
164 FileHandlePtr file = openRead(path);
165 if (!file) {
166 video.setError(fl::string("Could not open file: ").append(path));
167 return video;
168 }
169 video.begin(file);
170 return video;
171}
172
173bool FileSystem::readText(const char *path, fl::string *out) {
174 FileHandlePtr file = openRead(path);
175 if (!file) {
176 FASTLED_WARN("Failed to open file: " << path);
177 return false;
178 }
179 fl::size size = file->size();
180 out->reserve(size + out->size());
181 bool wrote = false;
182 while (file->available()) {
183 u8 buf[64];
184 fl::size n = file->read(buf, sizeof(buf));
185 // out->append(buf, n);
186 out->append((const char *)buf, n);
187 wrote = true;
188 }
189 file->close();
190 FASTLED_DBG_IF(!wrote, "Failed to write any data to the output string.");
191 return wrote;
192}
193} // namespace fl
194
195namespace fl {
196#if !FASTLED_HAS_SDCARD
197// Weak fallback implementation when SD library is not available
199 FASTLED_UNUSED(cs_pin);
201 FsImplPtr out = ptr;
202 return out;
203}
204#endif
205
206} // namespace fl
virtual fl::size size() const =0
virtual fl::size bytesLeft() const
virtual fl::size pos() const =0
bool readText(const char *path, string *out)
FileHandlePtr openRead(const char *path)
bool beginSd(int cs_pin)
bool readJson(const char *path, Json *doc)
bool readScreenMaps(const char *path, fl::fl_map< string, ScreenMap > *out, string *error=nullptr)
bool readScreenMap(const char *path, const char *name, ScreenMap *out, string *error=nullptr)
bool begin(FsImplPtr platform_filesystem)
Video openVideo(const char *path, fl::size pixelsPerFrame, float fps=30.0f, fl::size nFrameHistory=0)
FsImplPtr mFs
Definition file_system.h:62
void close(FileHandlePtr file)
FsImpl()=default
bool is_null() const
Definition json.h:1730
static Json parse(const fl::string &txt)
Definition json.h:2126
bool available() const override
const char * path() const override
bool seek(fl::size pos) override
fl::size read(u8 *dst, fl::size bytesToRead) override
fl::size size() const override
fl::size pos() const override
void close() override
NullFileHandle()=default
bool valid() const 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, fl::fl_map< string, ScreenMap > *segmentMaps, string *err=nullptr)
Definition screenmap.cpp:82
fl::size size() const
Definition str.h:324
void reserve(fl::size newCapacity)
Definition str.h:370
const char * c_str() const
Definition str.h:326
bool begin(fl::FileHandlePtr h)
void setError(const Str &error)
Definition video.h:67
string & append(const BitsetFixed< N > &bs)
Definition str.h:675
#define FL_LINK_WEAK
#define FASTLED_DBG_IF(COND, MSG)
Definition dbg.h:64
FastLED's Elegant JSON Library: fl::Json
Implements the FastLED namespace macros.
unsigned char u8
Definition int.h:17
FL_LINK_WEAK FsImplPtr make_sdcard_filesystem(int cs_pin)
shared_ptr< T > make_shared(Args &&... args)
Definition shared_ptr.h:348
MapRedBlackTree< Key, T, Compare, fl::allocator_slab< char > > fl_map
Definition map.h:540
IMPORTANT!
Definition crgb.h:20
#define FASTLED_UNUSED(x)
Definition unused.h:4
#define FASTLED_WARN
Definition warn.h:8