FastLED 3.9.15
Loading...
Searching...
No Matches
file_system.h
Go to the documentation of this file.
1#pragma once
2
3// Note, fs.h breaks ESPAsyncWebServer so we use file_system.h instead.
4
5#include "fl/stl/int.h"
6
7#include "fl/stl/shared_ptr.h" // IWYU pragma: export
8#include "fl/stl/fstream.h" // IWYU pragma: export
9#include "fl/stl/flat_map.h" // IWYU pragma: export
10#include "fl/stl/string.h" // IWYU pragma: export
11#include "fl/fx/video.h" // IWYU pragma: export
12#include "fl/codec/jpeg.h" // IWYU pragma: export
13#include "fl/stl/compiler_control.h" // IWYU pragma: export
14#include "fl/stl/noexcept.h"
15
16// Forward declaration — concrete Mp3Decoder lives in fl/codec/mp3.h
17namespace fl {
18class Mp3Decoder;
20}
21
22namespace fl {
23
25// PLATFORM INTERFACE
26// You need to define this for your platform.
27// Otherwise a null filesystem will be used that will do nothing but spew
28// warnings, but otherwise won't crash the system.
29FsImplPtr make_sdcard_filesystem(int cs_pin);
30
31#ifdef FASTLED_TESTING
32// Test-specific functions for setting up filesystem root path
33// These are implemented in platforms/stub/fs_stub.hpp
34void setTestFileSystemRoot(const char* root_path);
35const char* getTestFileSystemRoot();
36#endif
37
38} // namespace fl
39
40namespace fl {
41
42class ScreenMap;
44class Video;
45template <typename Key, typename Value, fl::size N> class unsorted_map_fixed;
46
47namespace json2 {
48class json;
49}
50
52 public:
54 bool beginSd(int cs_pin); // Signal to begin using the filesystem resource.
55 bool begin(FsImplPtr platform_filesystem); // Signal to begin using the
56 // filesystem resource.
57 void end(); // Signal to end use of the file system.
58
60 openRead(const char *path); // Returns closed ifstream if file could not be opened.
61 Video
62 openVideo(const char *path, fl::size pixelsPerFrame, float fps = 30.0f,
63 fl::size nFrameHistory = 0); // Null if video could not be opened.
64 Video
65 openMpeg1Video(const char *path, fl::size pixelsPerFrame, float fps = 30.0f,
66 fl::size nFrameHistory = 0); // Open MPEG1 video file
67 bool readText(const char *path, string *out);
68 bool readJson(const char *path, json *doc);
69 bool readScreenMaps(const char *path, fl::flat_map<string, ScreenMap> *out,
70 string *error = nullptr);
71 bool readScreenMap(const char *path, const char *name, ScreenMap *out,
72 string *error = nullptr);
73 // Load JPEG image from file path directly to Frame
74 FramePtr loadJpeg(const char *path, const JpegConfig &config = JpegConfig(),
75 fl::string *error_message = nullptr);
76
77 // Open MP3 audio file and return streaming decoder
78 fl::Mp3DecoderPtr openMp3(const char *path,
79 fl::string *error_message = nullptr);
80
81 private:
82 FsImplPtr mFs; // System dependent filesystem.
83};
84
85// Platforms will subclass this to implement the filesystem.
86class FsImpl {
87 public:
88 struct Visitor {
89 virtual ~Visitor() FL_NOEXCEPT {}
90 virtual void accept(const char *path) = 0;
91 };
92 FsImpl() FL_NOEXCEPT = default;
93 virtual ~FsImpl() FL_NOEXCEPT {} // Use default pins for spi.
94 virtual bool begin() = 0;
95 // End use of card
96 virtual void end() = 0;
97 virtual filebuf_ptr openRead(const char *path) = 0;
98
99 virtual bool ls(Visitor &visitor) {
100 // todo: implement.
101 (void)visitor;
102 return false;
103 }
104};
105
106// Standalone helper function to load JPEG from SD card
107// Combines SD card initialization and JPEG loading in one convenient function
108inline FramePtr loadJpegFromSD(int cs_pin, const char *filepath,
109 const JpegConfig &config = JpegConfig(),
110 fl::string *error_message = nullptr) {
111 FileSystem fs;
112 if (!fs.beginSd(cs_pin)) {
113 if (error_message) {
114 *error_message = "Failed to initialize SD card on CS pin ";
115 error_message->append(static_cast<fl::u32>(cs_pin));
116 }
117 return FramePtr();
118 }
119 return fs.loadJpeg(filepath, config, error_message);
120}
121
122} // namespace fl
bool readText(const char *path, string *out)
Video openMpeg1Video(const char *path, fl::size pixelsPerFrame, float fps=30.0f, fl::size nFrameHistory=0)
bool beginSd(int cs_pin)
bool readJson(const char *path, json *doc)
fl::ifstream openRead(const char *path)
bool readScreenMap(const char *path, const char *name, ScreenMap *out, string *error=nullptr)
FileSystem() FL_NOEXCEPT
fl::Mp3DecoderPtr openMp3(const char *path, fl::string *error_message=nullptr)
bool begin(FsImplPtr platform_filesystem)
Video openVideo(const char *path, fl::size pixelsPerFrame, float fps=30.0f, fl::size nFrameHistory=0)
FramePtr loadJpeg(const char *path, const JpegConfig &config=JpegConfig(), fl::string *error_message=nullptr)
FsImplPtr mFs
Definition file_system.h:82
bool readScreenMaps(const char *path, fl::flat_map< string, ScreenMap > *out, string *error=nullptr)
virtual bool begin()=0
FsImpl() FL_NOEXCEPT=default
virtual void end()=0
virtual filebuf_ptr openRead(const char *path)=0
virtual bool ls(Visitor &visitor)
Definition file_system.h:99
fl::shared_ptr< filebuf > filebuf_ptr
Definition idecoder.h:15
FsImplPtr make_sdcard_filesystem(int cs_pin)
fl::shared_ptr< Mp3Decoder > Mp3DecoderPtr
Definition file_system.h:19
FramePtr loadJpegFromSD(int cs_pin, const char *filepath, const JpegConfig &config=JpegConfig(), fl::string *error_message=nullptr)
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:535
virtual ~Visitor() FL_NOEXCEPT
Definition file_system.h:89
virtual void accept(const char *path)=0