FastLED 3.9.15
Loading...
Searching...
No Matches
luminova.cpp.hpp
Go to the documentation of this file.
1#ifndef FASTLED_INTERNAL
2#define FASTLED_INTERNAL
3#endif
4
5#include "fl/fx/2d/luminova.h"
6
7// Includes needed for implementation
8#include "fl/system/fastled.h"
9#include "fl/gfx/blur.h"
10#include "fl/math/math.h"
11#include "fl/math/math.h"
12#include "fl/math/xymap.h"
13#include "fl/stl/vector.h"
14#include "crgb.h"
15#include "noise.h"
16
17namespace fl {
18
19Luminova::Luminova(const XYMap &xyMap, const Params &params)
20 : Fx2d(xyMap), mParams(params) {
21 int cap = params.max_particles;
22 if (cap <= 0) {
23 cap = 1;
24 }
25 mParticles.resize(static_cast<size_t>(cap));
26 // Initialize as dead
27 for (size_t i = 0; i < mParticles.size(); ++i) {
28 mParticles[i].alive = false;
29 }
30}
31
32void Luminova::setMaxParticles(int max_particles) {
33 if (max_particles <= 0) {
34 max_particles = 1;
35 }
36 if (static_cast<int>(mParticles.size()) == max_particles) {
37 return;
38 }
39 mParticles.clear();
40 mParticles.resize(static_cast<size_t>(max_particles));
41 for (size_t i = 0; i < mParticles.size(); ++i) {
42 mParticles[i].alive = false;
43 }
44}
45
46void Luminova::resetParticle(Particle &p, fl::u32 tt) {
47 // Position at center
48 const float cx = static_cast<float>(getWidth() - 1) * 0.5f;
49 const float cy = static_cast<float>(getHeight() - 1) * 0.5f;
50 p.x = cx;
51 p.y = cy;
52
53 // Original used noise(I)*W, we approximate with 1D noise scaled to width
54 int I = static_cast<int>(tt / 50);
55 u8 n1 = inoise8(static_cast<u16>(I * 19));
56 float noiseW = (static_cast<float>(n1) / 255.0f) * static_cast<float>(getWidth());
57
58 p.a = static_cast<float>(tt) * 1.25f + noiseW;
59 p.f = (tt & 1u) ? +1 : -1;
60 p.g = I;
61 p.s = 3.0f;
62 p.alive = true;
63}
64
65void Luminova::plotDot(fl::span<CRGB> leds, int x, int y, u8 v) const {
66 if (!mXyMap.has(x, y)) {
67 return;
68 }
69 const u16 idx = mXyMap.mapToIndex(static_cast<u16>(x), static_cast<u16>(y));
70 leds[idx] += CHSV(0, 0, scale8(v, mParams.point_gain));
71}
72
73void Luminova::plotSoftDot(fl::span<CRGB> leds, float fx, float fy, float s) const {
74 // Map s (decays from ~3) to a pixel radius 1..3
75 float r = fl::clamp<float>(s * 0.5f, 1.0f, 3.0f);
76 int R = static_cast<int>(fl::ceil(r));
77 // Avoid roundf() to prevent AVR macro conflicts; do manual symmetric rounding
78 int cx = static_cast<int>(fx + (fx >= 0.0f ? 0.5f : -0.5f));
79 int cy = static_cast<int>(fy + (fy >= 0.0f ? 0.5f : -0.5f));
80 float r2 = r * r;
81 for (int dy = -R; dy <= R; ++dy) {
82 for (int dx = -R; dx <= R; ++dx) {
83 float d2 = static_cast<float>(dx * dx + dy * dy);
84 if (d2 <= r2) {
85 float fall = 1.0f - (d2 / (r2 + 0.0001f));
86 float vf = 255.0f * fall;
87 if (vf < 0.0f) vf = 0.0f;
88 if (vf > 255.0f) vf = 255.0f;
89 u8 v = static_cast<u8>(vf);
90 plotDot(leds, cx + dx, cy + dy, v);
91 }
92 }
93 }
94}
95
97 // Fade + blur trails each frame
98 fadeToBlackBy(context.leds, mParams.fade_amount);
99 blur2d(context.leds, static_cast<fl::u8>(getWidth()), static_cast<fl::u8>(getHeight()),
100 mParams.blur_amount, mXyMap);
101
102 // Spawn/overwrite one particle per frame, round-robin across pool
103 if (!mParticles.empty()) {
104 size_t idx = static_cast<size_t>(mTick % static_cast<fl::u32>(mParticles.size()));
106 }
107
108 // Update and draw all particles
109 for (size_t i = 0; i < mParticles.size(); ++i) {
110 Particle &p = mParticles[i];
111 if (!p.alive) {
112 continue;
113 }
114
115 // s *= 0.997
116 p.s *= 0.997f;
117 if (p.s < 0.5f) {
118 p.alive = false;
119 continue;
120 }
121
122 // angle jitter using 2D noise: (t/99, g)
123 float tOver99 = static_cast<float>(mTick) / 99.0f;
124 u8 n2 = inoise8(static_cast<u16>(tOver99 * 4096.0f), static_cast<u16>(p.g * 37));
125 float n2c = (static_cast<int>(n2) - 128) / 255.0f; // ~ -0.5 .. +0.5
126 p.a += (n2c) / 9.0f;
127
128 float aa = p.a * static_cast<float>(p.f);
129 p.x += fl::cosf(aa);
130 p.y += fl::sinf(aa);
131
132 plotSoftDot(context.leds, p.x, p.y, p.s);
133 }
134
135 ++mTick;
136}
137
138} // namespace fl
fl::CRGB leds[NUM_LEDS]
XYMap mXyMap
Definition fx2d.h:30
Fx2d(const XYMap &xyMap)
Definition fx2d.h:19
u16 getWidth() const
Definition fx2d.h:24
u16 getHeight() const
Definition fx2d.h:23
u16 xyMap(u16 x, u16 y) const
Definition fx2d.h:20
void resetParticle(Particle &p, fl::u32 tick)
fl::u32 mTick
Definition luminova.h:59
void plotDot(fl::span< CRGB > leds, int x, int y, u8 v) const
void plotSoftDot(fl::span< CRGB > leds, float fx, float fy, float s) const
LuminovaParams Params
Definition luminova.h:28
Params mParams
Definition luminova.h:58
Luminova(const XYMap &xyMap, const Params &params=Params())
fl::vector< Particle > mParticles
Definition luminova.h:60
void draw(DrawContext context) override
void setMaxParticles(int max_particles)
Internal FastLED header for implementation files.
fl::hsv8 CHSV
Definition chsv.h:11
fl::u8 inoise8(fl::u16 x, fl::u16 y, fl::u16 z)
unsigned char u8
Definition s16x16x4.h:132
unsigned char u8
Definition stdint.h:131
constexpr enable_if< is_fixed_point< T >::value, T >::type ceil(T x) FL_NOEXCEPT
void fadeToBlackBy(CRGB *leds, fl::u16 num_leds, fl::u8 fadeBy)
float sinf(float value) FL_NOEXCEPT
Definition math.h:352
void blur2d(fl::span< CRGB > leds, u8 width, u8 height, fract8 blur_amount, const XYMap &xymap) FL_NOEXCEPT
Definition blur.h:153
float cosf(float value) FL_NOEXCEPT
Definition math.h:358
constexpr enable_if< is_fixed_point< T >::value, T >::type clamp(T x, T lo, T hi) FL_NOEXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
fl::span< CRGB > leds