FastLED 3.9.15
Loading...
Searching...
No Matches
wave.h
Go to the documentation of this file.
1
6
7#pragma once
8
9#include "fl/stl/stdint.h"
10
11#include "fl/gfx/gradient.h"
12#include "fl/stl/shared_ptr.h" // For FASTLED_SHARED_PTR macros and shared_ptr
14#include "fl/math/xymap.h"
15#include "fl/fx/fx2d.h"
16#include "fl/stl/noexcept.h"
17
18namespace fl {
19
24
30 public:
31 virtual ~WaveCrgbMap() FL_NOEXCEPT = default;
32
38 fl::span<CRGB> leds) = 0;
39};
40
48 public:
54 fl::span<CRGB> leds) override {
55 const fl::u32 width = waveSim.getWidth();
56 const fl::u32 height = waveSim.getHeight();
57 for (fl::u32 y = 0; y < height; y++) {
58 for (fl::u32 x = 0; x < width; x++) {
59 fl::u32 idx = xymap(x, y);
60 u8 value8 = waveSim.getu8(x, y);
61 leds[idx] = CRGB(value8, value8, value8);
62 }
63 }
64 }
65};
66
73 public:
75
78 WaveCrgbGradientMap(const CRGBPalette16 &palette) : mGradient(palette) {}
79
82
88 fl::span<CRGB> leds) override;
89
92 void setGradient(const Gradient &gradient) { mGradient = gradient; }
93
94 private:
96};
97
136
166class WaveFx : public Fx2d {
167 public:
169
175 args.factor, args.speed, args.dampening) {
176 // Initialize the wave simulation with the given parameters.
177 if (args.crgbMap == nullptr) {
178 // Use the default CRGB mapping function.
179 mCrgbMap = fl::make_shared<WaveCrgbMapDefault>();
180 } else {
181 // Set a custom CRGB mapping function.
182 mCrgbMap = args.crgbMap;
183 }
184 setAutoUpdate(args.auto_updates);
185 setXCylindrical(args.x_cyclical);
186 setUseChangeGrid(args.use_change_grid);
187 }
188
194 void setXCylindrical(bool on) { mWaveSim.setXCylindrical(on); }
195
201 void setSpeed(float speed) {
202 // Set the speed of the wave simulation.
203 mWaveSim.setSpeed(speed);
204 }
205
213 // Set the dampening of the wave simulation.
214 mWaveSim.setDampening(dampening);
215 }
216
224 void setHalfDuplex(bool on) {
225 // Set whether the wave simulation is half duplex.
226 mWaveSim.setHalfDuplex(on);
227 }
228
236 // Set the supersampling factor of the wave simulation.
237 mWaveSim.setSuperSample(factor);
238 }
239
246 // Set the easing mode for the 8-bit value.
247 mWaveSim.setEasingMode(mode);
248 }
249
262 mWaveSim.setStencil(s);
263 }
264
267 return mWaveSim.getStencil();
268 }
269
276 void setUseChangeGrid(bool enabled) {
277 // Set whether to use the change grid tracking optimization.
278 mWaveSim.setUseChangeGrid(enabled);
279 }
280
283 bool getUseChangeGrid() const {
284 // Get the current change grid tracking setting.
285 return mWaveSim.getUseChangeGrid();
286 }
287
295 void setf(size_t x, size_t y, float value) {
296 // Set the value at the given coordinates in the wave simulation.
297 mWaveSim.setf(x, y, value);
298 }
299
307 void addf(size_t x, size_t y, float value) {
308 // Add a value at the given coordinates in the wave simulation.
309 float sum = value + mWaveSim.getf(x, y);
310 mWaveSim.setf(x, y, fl::min(1.0f, sum));
311 }
312
320 u8 getu8(size_t x, size_t y) const {
321 // Get the 8-bit value at the given coordinates in the wave simulation.
322 return mWaveSim.getu8(x, y);
323 }
324
331 void setCrgbMap(WaveCrgbMapPtr crgbMap) {
332 // Set a custom CRGB mapping function.
333 mCrgbMap = crgbMap;
334 }
335
341 void draw(DrawContext context) override {
342 // Update the wave simulation.
343 if (mAutoUpdates) {
344 mWaveSim.update();
345 }
346 // Map the wave values to the LEDs.
347 mCrgbMap->mapWaveToLEDs(mXyMap, mWaveSim, context.leds);
348 }
349
355 void setAutoUpdate(bool autoUpdate) {
356 // Set whether to automatically update the wave simulation.
357 mAutoUpdates = autoUpdate;
358 }
359
365 void update() {
366 // Called automatically in draw. Only invoke this if you want extra
367 // simulation updates.
368 // Update the wave simulation.
369 mWaveSim.update();
370 }
371
374 fl::string fxName() const override { return "WaveFx"; }
375
377 WaveCrgbMapPtr mCrgbMap;
378 bool mAutoUpdates = true;
379};
380
381} // namespace fl
fl::CRGB leds[NUM_LEDS]
XYMap xymap
UINumberField palette("Palette", 0, 0, 2)
uint16_t speed
Definition Noise.ino:66
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
void setGradient(const Gradient &gradient)
Set or update the color gradient.
Definition wave.h:92
fl::GradientInlined Gradient
Definition wave.h:74
WaveCrgbGradientMap(const CRGBPalette16 &palette)
Construct with a color palette.
Definition wave.h:78
Gradient mGradient
Definition wave.h:95
WaveCrgbGradientMap() FL_NOEXCEPT=default
Default constructor with no initial gradient.
void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, fl::span< CRGB > leds) override
Map wave values to gradient-colored LEDs.
Definition wave.cpp.hpp:71
Wave-to-color mapper using gradient/palette coloring.
Definition wave.h:72
virtual ~WaveCrgbMap() FL_NOEXCEPT=default
virtual void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, fl::span< CRGB > leds)=0
Convert wave simulation values to LED colors.
void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, fl::span< CRGB > leds) override
Map wave values to grayscale LED colors.
Definition wave.h:53
Default wave-to-color mapper producing grayscale output.
Definition wave.h:47
Abstract base class for mapping wave simulation values to LED colors.
Definition wave.h:29
void update()
Manually advance wave simulation by one step.
Definition wave.h:365
void setAutoUpdate(bool autoUpdate)
Enable/disable automatic simulation updates.
Definition wave.h:355
void setHalfDuplex(bool on)
Enable/disable half-duplex mode.
Definition wave.h:224
void setStencil(LaplacianStencil s) FL_NOEXCEPT
Select the discrete Laplacian stencil.
Definition wave.h:261
void setSpeed(float speed)
Set wave propagation speed.
Definition wave.h:201
void setf(size_t x, size_t y, float value)
Set wave amplitude at a specific grid position.
Definition wave.h:295
void setCrgbMap(WaveCrgbMapPtr crgbMap)
Set custom color mapping function.
Definition wave.h:331
void draw(DrawContext context) override
Render wave to LED array.
Definition wave.h:341
void setEasingMode(U8EasingFunction mode)
Set easing function for wave amplitude calculation.
Definition wave.h:245
LaplacianStencil getStencil() const FL_NOEXCEPT
Get the currently active Laplacian stencil.
Definition wave.h:266
WaveFx(const XYMap &xymap, Args args=Args())
Construct wave effect with given coordinate mapping and parameters.
Definition wave.h:173
WaveCrgbMapPtr mCrgbMap
Definition wave.h:377
void setSuperSample(SuperSample factor)
Set supersampling quality level.
Definition wave.h:235
void setUseChangeGrid(bool enabled)
Enable/disable change grid tracking optimization.
Definition wave.h:276
u8 getu8(size_t x, size_t y) const
Get wave amplitude as 8-bit value.
Definition wave.h:320
WaveFxArgs Args
Definition wave.h:168
void setDampening(float dampening)
Set wave energy dampening.
Definition wave.h:212
bool getUseChangeGrid() const
Get current change grid tracking setting.
Definition wave.h:283
void addf(size_t x, size_t y, float value)
Add wave amplitude to existing value at a position.
Definition wave.h:307
bool mAutoUpdates
Definition wave.h:378
void setXCylindrical(bool on)
Enable/disable cylindrical topology on x-axis.
Definition wave.h:194
fl::string fxName() const override
Get effect name.
Definition wave.h:374
WaveSimulation2D mWaveSim
Definition wave.h:376
2D wave simulation effect with supersampling and gradient coloring
Definition wave.h:166
fl::WaveSimulation1D waveSim(NUM_LEDS, fl::SuperSample::SUPER_SAMPLE_2X)
fl::UISlider dampening("Dampening", 6.0f, 0.0f, 10.0f, 0.1f)
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
unsigned char u8
Definition stdint.h:131
fl::CRGB CRGB
Definition video.h:15
constexpr int type_rank< T >::value
u8 u8 height
Definition blur.h:186
U8EasingFunction
u8 width
Definition blur.h:186
SuperSample
Definition supersample.h:4
Base definition for an LED controller.
Definition crgb.hpp:179
corkscrew_args args
Definition old.h:149
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:535
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
fl::span< CRGB > leds
float speed
Wave propagation speed (0.0-1.0, typical: 0.1-0.3)
Definition wave.h:126
bool x_cyclical
If true, waves wrap around the x-axis (cylindrical topology)
Definition wave.h:130
WaveCrgbMapPtr crgbMap
Custom color mapper (nullptr uses default grayscale)
Definition wave.h:134
bool auto_updates
If true, simulation advances automatically in draw()
Definition wave.h:124
WaveFxArgs() FL_NOEXCEPT=default
SuperSample factor
Supersampling quality (SUPER_SAMPLE_2X recommended for balance)
Definition wave.h:120
float dampening
Energy dampening factor (higher = faster decay, typical: 3-10)
Definition wave.h:128
bool half_duplex
If true, constrains waves to positive values only.
Definition wave.h:122
WaveFxArgs & operator=(const WaveFxArgs &) FL_NOEXCEPT=default
WaveFxArgs(const WaveFxArgs &) FL_NOEXCEPT=default
bool use_change_grid
Use change grid tracking for optimization (may reduce visual quality)
Definition wave.h:132
Configuration parameters for WaveFx effect.
Definition wave.h:102