FastLED 3.9.15
Loading...
Searching...
No Matches
fx_compositor.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/stdint.h"
4#include "fl/stl/cstring.h"
5#include "fl/stl/span.h"
6
7#include "crgb.h" // IWYU pragma: keep
8#include "fl/stl/shared_ptr.h" // For shared_ptr
9#include "fl/stl/vector.h" // IWYU pragma: keep
11#include "fl/fx/fx.h" // IWYU pragma: keep
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(fl::u32 numLeds) : mNumLeds(numLeds) {
25 }
26
27 void startTransition(fl::u32 now, fl::u32 duration, fl::shared_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(fl::u32 now, fl::u32 warpedTime, fl::span<CRGB> finalBuffer,
46 float speed = 1.0f, const AudioBatch *audio = nullptr);
47
48 private:
49 void swapLayers() {
50 FxLayerPtr tmp = mLayers[0];
51 mLayers[0] = mLayers[1];
52 mLayers[1] = tmp;
53 }
54
55 FxLayerPtr mLayers[2];
56 const fl::u32 mNumLeds;
58};
59
60inline void FxCompositor::draw(fl::u32 now, fl::u32 warpedTime,
61 fl::span<CRGB> finalBuffer,
62 float speed, const AudioBatch *audio) {
63 if (!mLayers[0]->getFx()) {
64 return;
65 }
66 mLayers[0]->draw(warpedTime, speed, audio);
67 u8 progress = mTransition.getProgress(now);
68 if (!progress) {
69 fl::span<CRGB> surface0 = mLayers[0]->getSurface();
70 fl::memcpy(finalBuffer.data(), surface0.data(), sizeof(CRGB) * mNumLeds);
71 return;
72 }
73 mLayers[1]->draw(warpedTime, speed, audio);
74 fl::span<CRGB> surface0 = mLayers[0]->getSurface();
75 fl::span<CRGB> surface1 = mLayers[1]->getSurface();
76
77 for (fl::u32 i = 0; i < mNumLeds; i++) {
78 const CRGB &p0 = surface0[i];
79 const CRGB &p1 = surface1[i];
80 CRGB out = CRGB::blend(p0, p1, progress);
81 finalBuffer[i] = out;
82 }
83 if (progress == 255) {
85 }
86}
87
88} // namespace fl
uint16_t speed
Definition Noise.ino:66
FxLayerPtr mLayers[2]
const fl::u32 mNumLeds
void draw(fl::u32 now, fl::u32 warpedTime, fl::span< CRGB > finalBuffer, float speed=1.0f, const AudioBatch *audio=nullptr)
void startTransition(fl::u32 now, fl::u32 duration, fl::shared_ptr< Fx > nextFx)
void completeTransition()
Transition mTransition
FxCompositor(fl::u32 numLeds)
const T * data() const FL_NOEXCEPT
Definition span.h:461
void * memcpy(void *dest, const void *src, size_t n) FL_NOEXCEPT
unsigned char u8
Definition stdint.h:131
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
Base definition for an LED controller.
Definition crgb.hpp:179
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) FL_NOEXCEPT
Definition crgb.cpp.hpp:54
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38