FastLED 3.10.8
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#include "fl/stl/noexcept.h"
13
14#ifndef FASTLED_FX_ENGINE_MAX_FX
15#define FASTLED_FX_ENGINE_MAX_FX 64
16#endif
17
18namespace fl {
19
20// Takes two fx layers and composites them together to a final output buffer.
22 public:
23 FxCompositor(fl::u32 numLeds) FL_NO_EXCEPT : mNumLeds(numLeds) {
26 }
27
28 void startTransition(fl::u32 now, fl::u32 duration, fl::shared_ptr<Fx> nextFx) FL_NO_EXCEPT {
30 if (duration == 0) {
31 mLayers[0]->setFx(nextFx);
32 return;
33 }
34 mLayers[1]->setFx(nextFx);
35 mTransition.start(now, duration);
36 }
37
39 if (mLayers[1]->getFx()) {
40 swapLayers();
41 mLayers[1]->release();
42 }
43 mTransition.end();
44 }
45
46 void draw(fl::u32 now, fl::u32 warpedTime, fl::span<CRGB> finalBuffer,
47 float speed = 1.0f, const AudioBatch *audio = nullptr) FL_NO_EXCEPT;
48
49 private:
51 FxLayerPtr tmp = mLayers[0];
52 mLayers[0] = mLayers[1];
53 mLayers[1] = tmp;
54 }
55
56 FxLayerPtr mLayers[2];
57 const fl::u32 mNumLeds;
59};
60
61inline void FxCompositor::draw(fl::u32 now, fl::u32 warpedTime,
62 fl::span<CRGB> finalBuffer,
63 float speed, const AudioBatch *audio) FL_NO_EXCEPT {
64 if (!mLayers[0]->getFx()) {
65 return;
66 }
67 mLayers[0]->draw(warpedTime, speed, audio);
68 u8 progress = mTransition.getProgress(now);
69 if (!progress) {
70 fl::span<CRGB> surface0 = mLayers[0]->getSurface();
71 fl::memcpy(finalBuffer.data(), surface0.data(), sizeof(CRGB) * mNumLeds);
72 return;
73 }
74 mLayers[1]->draw(warpedTime, speed, audio);
75 fl::span<CRGB> surface0 = mLayers[0]->getSurface();
76 fl::span<CRGB> surface1 = mLayers[1]->getSurface();
77
78 for (fl::u32 i = 0; i < mNumLeds; i++) {
79 const CRGB &p0 = surface0[i];
80 const CRGB &p1 = surface1[i];
81 CRGB out = CRGB::blend(p0, p1, progress);
82 finalBuffer[i] = out;
83 }
84 if (progress == 255) {
86 }
87}
88
89} // namespace fl
uint16_t speed
Definition Noise.ino:72
void startTransition(fl::u32 now, fl::u32 duration, fl::shared_ptr< Fx > nextFx) FL_NO_EXCEPT
void completeTransition() FL_NO_EXCEPT
void swapLayers() FL_NO_EXCEPT
FxLayerPtr mLayers[2]
const fl::u32 mNumLeds
FxCompositor(fl::u32 numLeds) FL_NO_EXCEPT
Transition mTransition
void draw(fl::u32 now, fl::u32 warpedTime, fl::span< CRGB > finalBuffer, float speed=1.0f, const AudioBatch *audio=nullptr) FL_NO_EXCEPT
const T * data() const FL_NO_EXCEPT
Definition span.h:486
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
FASTLED_FORCE_INLINE void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
Definition cstring.h:136
Base definition for an LED controller.
Definition crgb.hpp:179
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) FL_NO_EXCEPT
Definition crgb.cpp.hpp:55
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38