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