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#define FASTLED_HAS_SDCARD 1
8#elif __has_include(<SD.h>) && __has_include(<fs.h>)
9// Include Arduino SD card implementation when SD library is available
10#include "platforms/fs_sdcard_arduino.hpp"
11#define FASTLED_HAS_SDCARD 1
12#else
13#define FASTLED_HAS_SDCARD 0
14#endif
15
16#include "fl/json.h"
17#include "fl/namespace.h"
18#include "fl/screenmap.h"
19#include "fl/unused.h"
20
21namespace fl {
22
23class NullFileHandle : public FileHandle {
24 public:
25 NullFileHandle() = default;
26 ~NullFileHandle() override {}
27
28 bool available() const override { return false; }
29 fl::size size() const override { return 0; }
30 fl::size read(u8 *dst, fl::size bytesToRead) override {
31 FASTLED_UNUSED(dst);
32 FASTLED_UNUSED(bytesToRead);
33 return 0;
34 }
35 fl::size pos() const override { return 0; }
36 const char *path() const override { return "NULL FILE HANDLE"; }
37 bool seek(fl::size pos) override {
39 return false;
40 }
41 void close() override {}
42 bool valid() const override {
43 FASTLED_WARN("NullFileHandle is not valid");
44 return false;
45 }
46};
47
48class NullFileSystem : public FsImpl {
49 public:
51 FASTLED_WARN("NullFileSystem instantiated as a placeholder, please "
52 "implement a file system for your platform.");
53 }
54 ~NullFileSystem() override {}
55
56 bool begin() override { return true; }
57 void end() override {}
58
59 void close(FileHandlePtr file) override {
60 // No need to do anything for in-memory files
61 FASTLED_UNUSED(file);
62 FASTLED_WARN("NullFileSystem::close");
63 }
64
65 FileHandlePtr openRead(const char *_path) override {
66 FASTLED_UNUSED(_path);
68 FileHandlePtr out = ptr;
69 return out;
70 }
71};
72
73bool FileSystem::beginSd(int cs_pin) {
75 if (!mFs) {
76 return false;
77 }
78 mFs->begin();
79 return true;
80}
81
82bool FileSystem::begin(FsImplPtr platform_filesystem) {
83 mFs = platform_filesystem;
84 if (!mFs) {
85 return false;
86 }
87 mFs->begin();
88 return true;
89}
90
91fl::size FileHandle::bytesLeft() const { return size() - pos(); }
92
94
96 if (mFs) {
97 mFs->end();
98 }
99}
100
101bool FileSystem::readJson(const char *path, Json *doc) {
102 string text;
103 if (!readText(path, &text)) {
104 return false;
105 }
106
107 // Parse using the new Json class
108 *doc = fl::Json::parse(text);
109 return !doc->is_null();
110}
111
112bool FileSystem::readScreenMaps(const char *path,
113 fl::fl_map<string, ScreenMap> *out, string *error) {
114 string 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 string 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,
134 ScreenMap *out, string *error) {
135 string text;
136 if (!readText(path, &text)) {
137 FASTLED_WARN("Failed to read file: " << path);
138 if (error) {
139 *error = "Failed to read file: ";
140 error->append(path);
141 }
142 return false;
143 }
144 string err;
145 bool ok = ScreenMap::ParseJson(text.c_str(), name, out, &err);
146 if (!ok) {
147 FASTLED_WARN("Failed to parse screen map: " << err.c_str());
148 *error = err;
149 return false;
150 }
151 return true;
152}
153
154void FileSystem::close(FileHandlePtr file) { mFs->close(file); }
155
156FileHandlePtr FileSystem::openRead(const char *path) {
157 return mFs->openRead(path);
158}
159Video FileSystem::openVideo(const char *path, fl::size pixelsPerFrame, float fps,
160 fl::size nFrameHistory) {
161 Video video(pixelsPerFrame, fps, nFrameHistory);
162 FileHandlePtr file = openRead(path);
163 if (!file) {
164 video.setError(fl::string("Could not open file: ").append(path));
165 return video;
166 }
167 video.begin(file);
168 return video;
169}
170
171bool FileSystem::readText(const char *path, fl::string *out) {
172 FileHandlePtr file = openRead(path);
173 if (!file) {
174 FASTLED_WARN("Failed to open file: " << path);
175 return false;
176 }
177 fl::size size = file->size();
178 out->reserve(size + out->size());
179 bool wrote = false;
180 while (file->available()) {
181 u8 buf[64];
182 fl::size n = file->read(buf, sizeof(buf));
183 // out->append(buf, n);
184 out->append((const char *)buf, n);
185 wrote = true;
186 }
187 file->close();
188 FASTLED_DBG_IF(!wrote, "Failed to write any data to the output string.");
189 return wrote;
190}
191} // namespace fl
192
193namespace fl {
194#if !FASTLED_HAS_SDCARD
195// Weak fallback implementation when SD library is not available
196__attribute__((weak)) FsImplPtr make_sdcard_filesystem(int cs_pin) {
197 FASTLED_UNUSED(cs_pin);
199 FsImplPtr out = ptr;
200 return out;
201}
202#endif
203
204} // 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 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
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:7