FastLED 3.9.15
Loading...
Searching...
No Matches
fx_engine.cpp
Go to the documentation of this file.
1#include "fx_engine.h"
2#include "video.h"
3
4namespace fl {
5
6FxEngine::FxEngine(uint16_t numLeds, bool interpolate)
7 : mTimeFunction(0), mCompositor(numLeds), mCurrId(0),
8 mInterpolate(interpolate) {}
9
11
12int FxEngine::addFx(FxPtr effect) {
13 float fps = 0;
14 if (mInterpolate && effect->hasFixedFrameRate(&fps)) {
15 // Wrap the effect in a VideoFxWrapper so that we can get
16 // interpolation.
17 VideoFxWrapperPtr vid_fx = VideoFxWrapperPtr::New(effect);
18 vid_fx->setFade(0, 0); // No fade for interpolated effects
19 effect = vid_fx;
20 }
21 bool auto_set = mEffects.empty();
22 bool ok = mEffects.insert(mCounter, effect).first;
23 if (!ok) {
24 return -1;
25 }
26 if (auto_set) {
28 mCompositor.startTransition(0, 0, effect);
29 }
30 return mCounter++;
31}
32
33bool FxEngine::nextFx(uint16_t duration) {
34 bool ok = mEffects.next(mCurrId, &mCurrId, true);
35 if (!ok) {
36 return false;
37 }
38 setNextFx(mCurrId, duration);
39 return true;
40}
41
42bool FxEngine::setNextFx(int index, uint16_t duration) {
43 if (!mEffects.has(index)) {
44 return false;
45 }
46 mCurrId = index;
47 mDuration = duration;
48 mDurationSet = true;
49 return true;
50}
51
52FxPtr FxEngine::removeFx(int index) {
53 if (!mEffects.has(index)) {
54 return FxPtr();
55 }
56
57 FxPtr removedFx;
58 bool ok = mEffects.get(index, &removedFx);
59 if (!ok) {
60 return FxPtr();
61 }
62
63 if (mCurrId == index) {
64 // If we're removing the current effect, switch to the next one
65 mEffects.next(mCurrId, &mCurrId, true);
66 mDurationSet = true;
67 mDuration = 0; // Instant transition
68 }
69
70 return removedFx;
71}
72
73FxPtr FxEngine::getFx(int id) {
74 if (mEffects.has(id)) {
75 FxPtr fx;
76 mEffects.get(id, &fx);
77 return fx;
78 }
79 return FxPtr();
80}
81
82bool FxEngine::draw(uint32_t now, CRGB *finalBuffer) {
83 mTimeFunction.update(now);
84 uint32_t warpedTime = mTimeFunction.time();
85
86 if (mEffects.empty()) {
87 return false;
88 }
89 if (mDurationSet) {
90 FxPtr fx;
91 bool ok = mEffects.get(mCurrId, &fx);
92 if (!ok) {
93 // something went wrong.
94 return false;
95 }
96 mCompositor.startTransition(now, mDuration, fx);
97 mDurationSet = false;
98 }
99 if (!mEffects.empty()) {
100 mCompositor.draw(now, warpedTime, finalBuffer);
101 }
102 return true;
103}
104
105} // namespace fl
int addFx(FxPtr effect)
Adds a new effect to the engine.
Definition fx_engine.cpp:12
~FxEngine()
Destructor for FxEngine.
Definition fx_engine.cpp:10
bool nextFx(uint16_t transition_ms=500)
Transitions to the next effect in the sequence.
Definition fx_engine.cpp:33
bool mDurationSet
Flag indicating if a new transition has been set.
Definition fx_engine.h:125
FxCompositor mCompositor
Handles effect transitions and rendering.
Definition fx_engine.h:122
int mCurrId
Id of the current effect.
Definition fx_engine.h:123
bool setNextFx(int index, uint16_t duration)
Sets the next effect to transition to.
Definition fx_engine.cpp:42
FxEngine(uint16_t numLeds, bool interpolate=true)
Constructs an FxEngine with the specified number of LEDs.
Definition fx_engine.cpp:6
bool draw(uint32_t now, CRGB *outputBuffer)
Renders the current effect or transition to the output buffer.
Definition fx_engine.cpp:82
IntFxMap mEffects
Collection of effects.
Definition fx_engine.h:121
uint16_t mDuration
Duration of the current transition.
Definition fx_engine.h:124
FxPtr removeFx(int index)
Requests removal of an effect from the engine, which might not happen immediately (for example the Fx...
Definition fx_engine.cpp:52
FxPtr getFx(int index)
Retrieves an effect from the engine without removing it.
Definition fx_engine.cpp:73
bool mInterpolate
Definition fx_engine.h:127
TimeWarp mTimeFunction
Definition fx_engine.h:119
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55