FastLED 3.9.7
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 effect = VideoFxWrapperPtr::New(effect);
22 }
23 bool auto_set = mEffects.empty();
24 bool ok = mEffects.insert(mCounter, effect).first;
25 if (!ok) {
26 return -1;
27 }
28 if (auto_set) {
29 mCurrId = mCounter;
30 mCompositor.startTransition(0, 0, effect);
31 }
32 return mCounter++;
33}
34
35bool FxEngine::nextFx(uint16_t duration) {
36 bool ok = mEffects.next(mCurrId, &mCurrId, true);
37 if (!ok) {
38 return false;
39 }
40 setNextFx(mCurrId, duration);
41 return true;
42}
43
44bool FxEngine::setNextFx(int index, uint16_t duration) {
45 if (!mEffects.has(index)) {
46 return false;
47 }
48 mCurrId = index;
49 mDuration = duration;
50 mDurationSet = true;
51 return true;
52}
53
54FxPtr FxEngine::removeFx(int index) {
55 if (!mEffects.has(index)) {
56 return FxPtr();
57 }
58
59 FxPtr removedFx;
60 bool ok = mEffects.get(index, &removedFx);
61 if (!ok) {
62 return FxPtr();
63 }
64
65 if (mCurrId == index) {
66 // If we're removing the current effect, switch to the next one
67 mEffects.next(mCurrId, &mCurrId, true);
68 mDurationSet = true;
69 mDuration = 0; // Instant transition
70 }
71
72 return removedFx;
73}
74
75FxPtr FxEngine::getFx(int id) {
76 if (mEffects.has(id)) {
77 FxPtr fx;
78 mEffects.get(id, &fx);
79 return fx;
80 }
81 return FxPtr();
82}
83
84bool FxEngine::draw(uint32_t now, CRGB *finalBuffer) {
85 mTimeFunction.update(now);
86 uint32_t warpedTime = mTimeFunction.time();
87
88 if (mEffects.empty()) {
89 return false;
90 }
91 if (mDurationSet) {
92 FxPtr fx;
93 bool ok = mEffects.get(mCurrId, &fx);
94 if (!ok) {
95 // something went wrong.
96 return false;
97 }
98 mCompositor.startTransition(now, mDuration, fx);
99 mDurationSet = false;
100 }
101 if (!mEffects.empty()) {
102 mCompositor.draw(now, warpedTime, finalBuffer);
103 }
104 return true;
105}
106
107} // 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:35
bool setNextFx(int index, uint16_t duration)
Sets the next effect to transition to.
Definition fx_engine.cpp:44
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:84
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:54
FxPtr getFx(int index)
Retrieves an effect from the engine without removing it.
Definition fx_engine.cpp:75
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