FastLED 3.9.3
Loading...
Searching...
No Matches
fx_engine.h
1#pragma once
2
3#include <stdint.h>
4#include <string.h>
5
6
7#include "crgb.h"
8#include "fixed_map.h"
9#include "fx/fx.h"
10#include "fx/detail/fx_compositor.h"
11#include "fx/detail/fx_layer.h"
12#include "namespace.h"
13#include "ref.h"
14#include "ui.h"
15#include "fx/detail/time_warp.h"
16#include "fx/video.h"
17
18
19// Forward declaration
20class TimeWarp;
21
22#ifndef FASTLED_FX_ENGINE_MAX_FX
23#define FASTLED_FX_ENGINE_MAX_FX 64
24#endif
25
26FASTLED_NAMESPACE_BEGIN
27
37class FxEngine {
38 public:
44 FxEngine(uint16_t numLeds);
45
49 ~FxEngine();
50
56 int addFx(FxRef effect);
57
58
65 int addVideo(Video video, XYMap xymap);
66
75 int addFx(Fx& effect) { return addFx(Ref<Fx>::NoTracking(effect)); }
76
83 FxRef removeFx(int index);
84
90 FxRef getFx(int index);
91
92 int getCurrentFxId() const { return mCurrId; }
93
99 bool draw(uint32_t now, CRGB *outputBuffer);
100
106 bool nextFx(uint16_t transition_ms = 500);
107
114 bool setNextFx(int index, uint16_t duration);
115
116
117 IntFxMap& _getEffects() { return mEffects; }
118
123 void setTimeScale(float timeScale) { mTimeWarp.setTimeScale(timeScale); }
124
125 private:
126 Slider mTimeBender;
127 int mCounter = 0;
128 TimeWarp mTimeWarp; // FxEngine controls the clock, to allow "time-bending" effects.
129 IntFxMap mEffects;
130 FxCompositor mCompositor;
131 int mCurrId;
132 uint16_t mDuration = 0;
133 bool mDurationSet = false;
134};
135
136inline FxEngine::FxEngine(uint16_t numLeds)
137 : mTimeBender("FxEngineSpeed", 1.0f, -50.0f, 50.0f, 0.01f),
138 mTimeWarp(0),
139 mCompositor(numLeds),
140 mCurrId(0) {
141}
142
144
145inline int FxEngine::addFx(FxRef effect) {
146 bool auto_set = mEffects.empty();
147 bool ok = mEffects.insert(mCounter, effect);
148 if (!ok) {
149 return -1;
150 }
151 if (auto_set) {
152 mCurrId = mCounter;
153 mCompositor.startTransition(0, 0, effect);
154 }
155 return mCounter++;
156}
157
158int FxEngine::addVideo(Video video, XYMap xymap) {
159 auto vidfx = VideoFxRef::New(video, xymap);
160 int id = addFx(vidfx);
161 return id;
162}
163
164inline bool FxEngine::nextFx(uint16_t duration) {
165 bool ok = mEffects.next(mCurrId, &mCurrId, true);
166 if (!ok) {
167 return false;
168 }
169 setNextFx(mCurrId, duration);
170 return true;
171}
172
173inline bool FxEngine::setNextFx(int index, uint16_t duration) {
174 if (!mEffects.has(index)) {
175 return false;
176 }
177 mCurrId = index;
178 mDuration = duration;
179 mDurationSet = true;
180 return true;
181}
182
183inline FxRef FxEngine::removeFx(int index) {
184 if (!mEffects.has(index)) {
185 return FxRef();
186 }
187
188 FxRef removedFx;
189 bool ok = mEffects.get(index, &removedFx);
190 if (!ok) {
191 return FxRef();
192 }
193
194 if (mCurrId == index) {
195 // If we're removing the current effect, switch to the next one
196 mEffects.next(mCurrId, &mCurrId, true);
197 mDurationSet = true;
198 mDuration = 0; // Instant transition
199 }
200
201 return removedFx;
202}
203
204inline FxRef FxEngine::getFx(int id) {
205 if (mEffects.has(id)) {
206 FxRef fx;
207 mEffects.get(id, &fx);
208 return fx;
209 }
210 return FxRef();
211}
212
213inline bool FxEngine::draw(uint32_t now, CRGB *finalBuffer) {
214 mTimeWarp.setTimeScale(mTimeBender);
215 mTimeWarp.update(now);
216 uint32_t warpedTime = mTimeWarp.getTime();
217
218 if (mEffects.empty()) {
219 return false;
220 }
221 if (mDurationSet) {
222 FxRef fx;
223 bool ok = mEffects.get(mCurrId, &fx);
224 if (!ok) {
225 // something went wrong.
226 return false;
227 }
228 mCompositor.startTransition(now, mDuration, fx);
229 mDurationSet = false;
230 }
231 if (!mEffects.empty()) {
232 mCompositor.draw(now, warpedTime, finalBuffer);
233 }
234 return true;
235}
236
237FASTLED_NAMESPACE_END
Manages and renders multiple visual effects (Fx) for LED strips.
Definition fx_engine.h:37
int addFx(Fx &effect)
Adds a new effect to the engine.
Definition fx_engine.h:75
int addFx(FxRef effect)
Adds a new effect to the engine.
Definition fx_engine.h:145
bool setNextFx(int index, uint16_t duration)
Sets the next effect to transition to.
Definition fx_engine.h:173
void setTimeScale(float timeScale)
Sets the time scale for the TimeWarp object.
Definition fx_engine.h:123
bool nextFx(uint16_t transition_ms=500)
Transitions to the next effect in the sequence.
Definition fx_engine.h:164
FxRef removeFx(int index)
Requests removal of an effect from the engine, which might not happen immediately (for example the Fx...
Definition fx_engine.h:183
~FxEngine()
Destructor for FxEngine.
Definition fx_engine.h:143
int addVideo(Video video, XYMap xymap)
Adds a new video effect to the engine.
Definition fx_engine.h:158
FxRef getFx(int index)
Retrieves an effect from the engine without removing it.
Definition fx_engine.h:204
bool draw(uint32_t now, CRGB *outputBuffer)
Renders the current effect or transition to the output buffer.
Definition fx_engine.h:213
FxEngine(uint16_t numLeds)
Constructs an FxEngine with the specified number of LEDs.
Definition fx_engine.h:136
Definition fx.h:16
Definition ref.h:99
Definition ui.h:40
Definition video.h:23
Definition xymap.h:39
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39