FastLED 3.10.6
Loading...
Searching...
No Matches
fled.h
Go to the documentation of this file.
1#pragma once
2
3// fl::Fled - value-type handle to a parsed .fled v1 container.
4// PIMPL; cheap to copy. Default-constructed instances are in a null state
5// and evaluate to false. See FLED_FORMAT.md for the on-disk format.
6//
7// What Fled exposes:
8// - Header info: version(), pixelFormat().
9// - Raw JSON envelope: json(), sectionCount().
10// - Raw byte ranges: blob(name, &outLen) for the post-JSON frame payload.
11// - Fully constructed objects: screenMap(), channels().
12//
13// What Fled deliberately does NOT expose (purged from v1):
14// - No video() accessor. Video access is via blob("frame_payload", &n) plus
15// json()["video"] for metadata. No Video object is reconstructed here.
16// - No script() accessor. Scripting is a future-v2 surface (docs only).
17
18#include "fl/fled/color.h"
19#include "fl/stl/int.h"
20#include "fl/stl/noexcept.h"
21#include "fl/stl/shared_ptr.h"
22#include "fl/stl/span.h"
23#include "fl/stl/vector.h"
24
25namespace fl {
26
27class FileSystem;
28class json;
29class ScreenMap;
31
32namespace fled {
33class FledImpl;
34
35// A typed, zero-copy video frame. The payload owner keeps Fled's owned bytes
36// alive; static-input callers still retain the lifetime obligation documented
37// by loadFromStatic(). Rgb16 components are decoded from little-endian bytes,
38// never reinterpreted as native-endian words.
45 fl::size mPayloadBytes = 0;
46 fl::size mStride = 0;
47
48 fl::u16 component16(fl::size led, fl::u8 component) const FL_NO_EXCEPT {
49 if (!mPayload || mStorage.mFormat != fl::PixelFormat::Rgb16 ||
50 mStorage.mComponentByteOrder != ComponentByteOrder::LittleEndian ||
51 component >= 3 || led >= mPayloadBytes / 6) {
52 return 0;
53 }
54 const fl::size offset = led * 6 + static_cast<fl::size>(component) * 2;
55 if (offset >= mPayloadBytes - 1) {
56 return 0;
57 }
58 return static_cast<fl::u16>(mPayload.get()[offset]) |
59 (static_cast<fl::u16>(mPayload.get()[offset + 1]) << 8);
60 }
61};
62}
63
64class Fled {
65 public:
66 // Load from filesystem. Returns a null Fled on any failure (open fail,
67 // truncated, bad magic/version, oversized json_length, parse error).
68 static Fled load(FileSystem &fs, const char *path) FL_NO_EXCEPT;
69
70 // Zero-copy load from a static byte span. Span must outlive the Fled
71 // and any of its copies (this constructor does NOT copy the bytes).
73
74 // Move-load from a caller-owned vector. The vector's storage is moved
75 // into the impl; no extra copy.
77
78 // Default ctor produces a null Fled (operator bool == false).
80
81 // True iff load succeeded and produced a valid v1 envelope.
82 explicit operator bool() const FL_NO_EXCEPT;
83
84 // Header version byte. Returns 0 for null Fled, 1 for valid v1.
86
87 // Header pixel-format byte (offset 5). 0x00 = rgb8. Returns 0 for null.
89
90 // Bytes-per-LED for the configured pixel_format, per FLED_FORMAT.md.
91 // Returns 0 for unknown / reserved pixel formats (0x06 - 0xff) - the
92 // FLED_FORMAT.md spec says consumers should reject before reading
93 // frame bytes in that case.
95
96 // Length of the frame_payload byte range (everything after the JSON
97 // envelope). Returns 0 for null Fled.
98 fl::size payloadBytes() const FL_NO_EXCEPT;
99
100 // Derived frame count per FLED_FORMAT.md:
101 // frame_count = payload_bytes / (led_count * bytes_per_led)
102 // The caller supplies led_count (typically from screenMap()->getLength()).
103 // Returns 0 if led_count is 0, bytes-per-LED is 0, or the bundle is null.
104 fl::size frameCount(fl::size ledCount) const FL_NO_EXCEPT;
105
106 // Reads video.fps from the JSON envelope, falling back to defaultFps
107 // (30 by default) if the key is absent or not a number. FLED_FORMAT.md:
108 // "If video.fps is absent, consumers may use an application default,
109 // sketch parameter, or external playback setting."
110 float videoFps(float defaultFps = 30.0f) const FL_NO_EXCEPT;
111
112 // Resolves video.color against the header's pixel_format per
113 // FLED_FORMAT.md "Source Color Metadata": applies the format's default
114 // tuple for absent keys and enforces every validation rule.
115 //
116 // Returns ColorStatus::Ok with *out populated, or a status naming the
117 // rejection. A null Fled resolves as if the envelope were empty.
118 //
119 // This only reports what the file declares its numbers to mean. FastLED
120 // does not yet transform pixels according to that declaration.
122
123 // Opens a typed zero-copy view of one video frame. The view preserves
124 // storage precision, component layout, and the resolved FLED source color
125 // tuple. Returns false for unsupported storage, invalid color metadata,
126 // incomplete frames, or an out-of-range frame index.
127 bool videoFrame(fl::size frameIndex, fl::size ledCount,
129
130 // Parsed JSON envelope. For null Fled, returns a reference to a static
131 // empty json (safe to chain into).
132 const fl::json &json() const FL_NO_EXCEPT;
133
134 // Number of top-level keys in the parsed JSON envelope. 0 for null.
135 fl::size sectionCount() const FL_NO_EXCEPT;
136
137 // Returns a shared pointer aliased to the underlying byte storage for
138 // the named section. Recognizes "frame_payload" (alias "payload"),
139 // which maps to the raw bytes after the JSON envelope. Any other name
140 // returns nullptr and writes 0 to *outLen (if non-null).
141 //
142 // Lifetime: the returned shared_ptr extends FledImpl's lifetime, but
143 // does NOT extend the lifetime of bytes loaded via loadFromStatic -
144 // those still depend on the caller's span outliving every use.
145 fl::shared_ptr<const fl::u8> blob(const char *sectionName,
146 fl::size *outLen) const FL_NO_EXCEPT;
147
148 // Typed section accessors. Each returns nullptr if the bundle has no
149 // section of that type. Construction touches NO global state (no
150 // controller registration, no EngineEvents broadcast, no scheduler).
153
154 private:
157};
158
159} // namespace fl
fl::u8 pixelFormat() const FL_NO_EXCEPT
Definition fled.cpp.hpp:85
static Fled loadFromStatic(fl::span< const fl::u8 > bytes) FL_NO_EXCEPT
Definition fled.cpp.hpp:48
fl::size sectionCount() const FL_NO_EXCEPT
Definition fled.cpp.hpp:115
fl::size payloadBytes() const FL_NO_EXCEPT
Definition fled.cpp.hpp:93
fl::shared_ptr< const fl::u8 > blob(const char *sectionName, fl::size *outLen) const FL_NO_EXCEPT
Definition fled.cpp.hpp:188
const fl::json & json() const FL_NO_EXCEPT
Definition fled.cpp.hpp:108
fl::size frameCount(fl::size ledCount) const FL_NO_EXCEPT
Definition fled.cpp.hpp:97
fled::ColorStatus videoColor(fled::VideoColor *out) const FL_NO_EXCEPT
Definition fled.cpp.hpp:135
fl::u8 bytesPerLed() const FL_NO_EXCEPT
Definition fled.cpp.hpp:89
Fled() FL_NO_EXCEPT
Definition fled.cpp.hpp:72
bool videoFrame(fl::size frameIndex, fl::size ledCount, fled::VideoFrameView *out) const FL_NO_EXCEPT
Definition fled.cpp.hpp:147
fl::shared_ptr< ScreenMap > screenMap() const FL_NO_EXCEPT
Definition fled.cpp.hpp:211
fl::shared_ptr< MultiChannelConfig > channels() const FL_NO_EXCEPT
Definition fled.cpp.hpp:237
fl::shared_ptr< fled::FledImpl > mImpl
Definition fled.h:156
float videoFps(float defaultFps=30.0f) const FL_NO_EXCEPT
Definition fled.cpp.hpp:120
static Fled loadFromVector(fl::vector< fl::u8 > &&bytes) FL_NO_EXCEPT
Definition fled.cpp.hpp:59
fl::u8 version() const FL_NO_EXCEPT
Definition fled.cpp.hpp:81
static Fled load(FileSystem &fs, const char *path) FL_NO_EXCEPT
Definition fled.cpp.hpp:31
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
ColorStatus
Definition color.h:54
unsigned char u8
Definition stdint.h:131
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
Base definition for an LED controller.
Definition crgb.hpp:179
PixelStorage mStorage
Definition fled.h:40
fl::shared_ptr< const fl::u8 > mPayload
Definition fled.h:44
fl::u16 component16(fl::size led, fl::u8 component) const FL_NO_EXCEPT
Definition fled.h:48
VideoColor mColor
Definition fled.h:42
fl::size mPayloadBytes
Definition fled.h:45