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