FastLED 3.9.3
Loading...
Searching...
No Matches
fx_compositor.h
1#pragma once
2
3#include <stdint.h>
4#include <string.h>
5
6#include "crgb.h"
7#include "fixed_vector.h"
8#include "fx/fx.h"
9#include "fx/detail/fx_layer.h"
10#include "namespace.h"
11#include "ref.h"
12
13
14
15#ifndef FASTLED_FX_ENGINE_MAX_FX
16#define FASTLED_FX_ENGINE_MAX_FX 64
17#endif
18
19FASTLED_NAMESPACE_BEGIN
20
21// Takes two fx layers and composites them together to a final output buffer.
23public:
24 FxCompositor(uint16_t numLeds) : mNumLeds(numLeds) {
25 mLayers[0] = FxLayerRef::New();
26 mLayers[1] = FxLayerRef::New();
27 }
28
29 void startTransition(uint32_t now, uint32_t duration, Ref<Fx> nextFx) {
30 completeTransition();
31 if (duration == 0) {
32 mLayers[0]->setFx(nextFx);
33 return;
34 }
35 mLayers[1]->setFx(nextFx);
36 mTransition.start(now, duration);
37 }
38
39 void completeTransition() {
40 if (mLayers[1]->getFx()) {
41 swapLayers();
42 mLayers[1]->release();
43 }
44 mTransition.end();
45 }
46
47 void draw(uint32_t now, uint32_t warpedTime, CRGB *finalBuffer);
48
49private:
50 void swapLayers() {
51 FxLayerRef tmp = mLayers[0];
52 mLayers[0] = mLayers[1];
53 mLayers[1] = tmp;
54 }
55
56 FxLayerRef mLayers[2];
57 const uint16_t mNumLeds;
58 Transition mTransition;
59};
60
61inline void FxCompositor::draw(uint32_t now, uint32_t warpedTime, CRGB *finalBuffer) {
62 if (!mLayers[0]->getFx()) {
63 return;
64 }
65 mLayers[0]->draw(warpedTime);
66 uint8_t progress = mTransition.getProgress(now);
67 if (!progress) {
68 memcpy(finalBuffer, mLayers[0]->getSurface(), sizeof(CRGB) * mNumLeds);
69 return;
70 }
71 mLayers[1]->draw(warpedTime);
72 const CRGB* surface0 = mLayers[0]->getSurface();
73 const CRGB* surface1 = mLayers[1]->getSurface();
74
75 for (uint16_t i = 0; i < mNumLeds; i++) {
76 const CRGB& p0 = surface0[i];
77 const CRGB& p1 = surface1[i];
78 CRGB out = CRGB::blend(p0, p1, progress);
79 finalBuffer[i] = out;
80 }
81 if (progress == 255) {
82 completeTransition();
83 }
84}
85
86FASTLED_NAMESPACE_END
Definition ref.h:99
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39