FastLED 3.10.8
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/blur.h"
12#include "fl/gfx/gradient.h"
13#include "fl/stl/shared_ptr.h" // For FASTLED_SHARED_PTR macros and shared_ptr
15#include "fl/math/xymap.h"
16#include "fl/fx/fx2d.h"
17#include "fl/stl/noexcept.h"
18
19namespace fl {
20
25
31 public:
32 virtual ~WaveCrgbMap() FL_NO_EXCEPT = default;
33
40};
41
49 public:
56 const fl::u32 width = waveSim.getWidth();
57 const fl::u32 height = waveSim.getHeight();
58 for (fl::u32 y = 0; y < height; y++) {
59 for (fl::u32 x = 0; x < width; x++) {
60 fl::u32 idx = xymap(x, y);
61 u8 value8 = waveSim.getu8(x, y);
62 leds[idx] = CRGB(value8, value8, value8);
63 }
64 }
65 }
66};
67
74 public:
76
80
83
89 fl::span<CRGB> leds) FL_NO_EXCEPT override;
90
93 void setGradient(const Gradient &gradient) FL_NO_EXCEPT { mGradient = gradient; }
94
95 private:
97};
98
147
177class WaveFx : public Fx2d {
178 public:
180
186 args.factor, args.speed, args.dampening) {
187 // Initialize the wave simulation with the given parameters.
188 if (args.crgbMap == nullptr) {
189 // Use the default CRGB mapping function.
190 mCrgbMap = fl::make_shared<WaveCrgbMapDefault>();
191 } else {
192 // Set a custom CRGB mapping function.
193 mCrgbMap = args.crgbMap;
194 }
195 setAutoUpdate(args.auto_updates);
196 setXCylindrical(args.x_cyclical);
197 setUseChangeGrid(args.use_change_grid);
198 setOutputBlur(args.output_blur_amount, args.output_blur_passes);
199 }
200
211 void setOutputBlur(fl::u8 amount, fl::u8 passes = 1) FL_NO_EXCEPT {
212 mOutputBlurAmount = amount;
213 mOutputBlurPasses = (passes < 1) ? 1 : passes;
214 }
217
223 void setXCylindrical(bool on) FL_NO_EXCEPT { mWaveSim.setXCylindrical(on); }
224
231 // Set the speed of the wave simulation.
232 mWaveSim.setSpeed(speed);
233 }
234
242 // Set the dampening of the wave simulation.
243 mWaveSim.setDampening(dampening);
244 }
245
254 // Set whether the wave simulation is half duplex.
255 mWaveSim.setHalfDuplex(on);
256 }
257
265 // Set the supersampling factor of the wave simulation.
266 mWaveSim.setSuperSample(factor);
267 }
268
275 // Set the easing mode for the 8-bit value.
276 mWaveSim.setEasingMode(mode);
277 }
278
291 mWaveSim.setStencil(s);
292 }
293
296 return mWaveSim.getStencil();
297 }
298
305 void setUseChangeGrid(bool enabled) FL_NO_EXCEPT {
306 // Set whether to use the change grid tracking optimization.
307 mWaveSim.setUseChangeGrid(enabled);
308 }
309
313 // Get the current change grid tracking setting.
314 return mWaveSim.getUseChangeGrid();
315 }
316
324 void setf(size_t x, size_t y, float value) FL_NO_EXCEPT {
325 // Set the value at the given coordinates in the wave simulation.
326 mWaveSim.setf(x, y, value);
327 }
328
336 void addf(size_t x, size_t y, float value) FL_NO_EXCEPT {
337 // Add a value at the given coordinates in the wave simulation.
338 float sum = value + mWaveSim.getf(x, y);
339 mWaveSim.setf(x, y, fl::min(1.0f, sum));
340 }
341
349 u8 getu8(size_t x, size_t y) const FL_NO_EXCEPT {
350 // Get the 8-bit value at the given coordinates in the wave simulation.
351 return mWaveSim.getu8(x, y);
352 }
353
360 void setCrgbMap(WaveCrgbMapPtr crgbMap) FL_NO_EXCEPT {
361 // Set a custom CRGB mapping function.
362 mCrgbMap = crgbMap;
363 }
364
370 void draw(DrawContext context) FL_NO_EXCEPT override {
371 // Update the wave simulation.
372 if (mAutoUpdates) {
373 mWaveSim.update();
374 }
375 // Map the wave values to the LEDs.
376 mCrgbMap->mapWaveToLEDs(mXyMap, mWaveSim, context.leds);
377 // #3122 A4 — post-process output blur as a cheaper alternative
378 // to SuperSample. Skipped entirely when amount == 0.
379 if (mOutputBlurAmount > 0) {
380 const fl::u32 w = mWaveSim.getWidth();
381 const fl::u32 h = mWaveSim.getHeight();
382 for (fl::u8 pass = 0; pass < mOutputBlurPasses; ++pass) {
383 fl::blur2d(context.leds, static_cast<fl::u8>(w),
384 static_cast<fl::u8>(h), mOutputBlurAmount,
385 mXyMap);
386 }
387 }
388 }
389
395 void setAutoUpdate(bool autoUpdate) FL_NO_EXCEPT {
396 // Set whether to automatically update the wave simulation.
397 mAutoUpdates = autoUpdate;
398 }
399
406 // Called automatically in draw. Only invoke this if you want extra
407 // simulation updates.
408 // Update the wave simulation.
409 mWaveSim.update();
410 }
411
414 fl::string fxName() const FL_NO_EXCEPT override { return "WaveFx"; }
415
417 WaveCrgbMapPtr mCrgbMap;
418 bool mAutoUpdates = true;
419 fl::u8 mOutputBlurAmount = 0; // #3122 A4 — 0 disables
421};
422
423} // namespace fl
CRGB leds[1]
XYMap xymap
UINumberField palette("Palette", 0, 0, 2)
uint16_t speed
Definition Noise.ino:72
XYMap mXyMap
Definition fx2d.h:31
u16 getWidth() const FL_NO_EXCEPT
Definition fx2d.h:25
Fx2d(const XYMap &xyMap) FL_NO_EXCEPT
Definition fx2d.h:20
u16 getHeight() const FL_NO_EXCEPT
Definition fx2d.h:24
fl::GradientInlined Gradient
Definition wave.h:75
Gradient mGradient
Definition wave.h:96
void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, fl::span< CRGB > leds) FL_NO_EXCEPT override
Map wave values to gradient-colored LEDs.
Definition wave.cpp.hpp:72
WaveCrgbGradientMap() FL_NO_EXCEPT=default
Default constructor with no initial gradient.
WaveCrgbGradientMap(const CRGBPalette16 &palette) FL_NO_EXCEPT
Construct with a color palette.
Definition wave.h:79
void setGradient(const Gradient &gradient) FL_NO_EXCEPT
Set or update the color gradient.
Definition wave.h:93
Wave-to-color mapper using gradient/palette coloring.
Definition wave.h:73
virtual ~WaveCrgbMap() FL_NO_EXCEPT=default
virtual void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, fl::span< CRGB > leds) FL_NO_EXCEPT=0
Convert wave simulation values to LED colors.
void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, fl::span< CRGB > leds) FL_NO_EXCEPT override
Map wave values to grayscale LED colors.
Definition wave.h:54
Default wave-to-color mapper producing grayscale output.
Definition wave.h:48
Abstract base class for mapping wave simulation values to LED colors.
Definition wave.h:30
void addf(size_t x, size_t y, float value) FL_NO_EXCEPT
Add wave amplitude to existing value at a position.
Definition wave.h:336
void setf(size_t x, size_t y, float value) FL_NO_EXCEPT
Set wave amplitude at a specific grid position.
Definition wave.h:324
bool getUseChangeGrid() const FL_NO_EXCEPT
Get current change grid tracking setting.
Definition wave.h:312
LaplacianStencil getStencil() const FL_NO_EXCEPT
Get the currently active Laplacian stencil.
Definition wave.h:295
fl::u8 getOutputBlurPasses() const FL_NO_EXCEPT
Definition wave.h:216
void setXCylindrical(bool on) FL_NO_EXCEPT
Enable/disable cylindrical topology on x-axis.
Definition wave.h:223
void setStencil(LaplacianStencil s) FL_NO_EXCEPT
Select the discrete Laplacian stencil.
Definition wave.h:290
void setCrgbMap(WaveCrgbMapPtr crgbMap) FL_NO_EXCEPT
Set custom color mapping function.
Definition wave.h:360
WaveCrgbMapPtr mCrgbMap
Definition wave.h:417
fl::u8 getOutputBlurAmount() const FL_NO_EXCEPT
Definition wave.h:215
void draw(DrawContext context) FL_NO_EXCEPT override
Render wave to LED array.
Definition wave.h:370
WaveFxArgs Args
Definition wave.h:179
void setHalfDuplex(bool on) FL_NO_EXCEPT
Enable/disable half-duplex mode.
Definition wave.h:253
void setDampening(float dampening) FL_NO_EXCEPT
Set wave energy dampening.
Definition wave.h:241
fl::u8 mOutputBlurPasses
Definition wave.h:420
void setAutoUpdate(bool autoUpdate) FL_NO_EXCEPT
Enable/disable automatic simulation updates.
Definition wave.h:395
WaveFx(const XYMap &xymap, Args args=Args()) FL_NO_EXCEPT
Construct wave effect with given coordinate mapping and parameters.
Definition wave.h:184
void setOutputBlur(fl::u8 amount, fl::u8 passes=1) FL_NO_EXCEPT
Configure the post-process output blur (#3122 A4)
Definition wave.h:211
void setEasingMode(U8EasingFunction mode) FL_NO_EXCEPT
Set easing function for wave amplitude calculation.
Definition wave.h:274
void setSuperSample(SuperSample factor) FL_NO_EXCEPT
Set supersampling quality level.
Definition wave.h:264
fl::u8 mOutputBlurAmount
Definition wave.h:419
bool mAutoUpdates
Definition wave.h:418
void setSpeed(float speed) FL_NO_EXCEPT
Set wave propagation speed.
Definition wave.h:230
void setUseChangeGrid(bool enabled) FL_NO_EXCEPT
Enable/disable change grid tracking optimization.
Definition wave.h:305
void update() FL_NO_EXCEPT
Manually advance wave simulation by one step.
Definition wave.h:405
u8 getu8(size_t x, size_t y) const FL_NO_EXCEPT
Get wave amplitude as 8-bit value.
Definition wave.h:349
WaveSimulation2D mWaveSim
Definition wave.h:416
fl::string fxName() const FL_NO_EXCEPT override
Get effect name.
Definition wave.h:414
2D wave simulation effect with supersampling and gradient coloring
Definition wave.h:177
fl::WaveSimulation1D waveSim(NUM_LEDS, fl::SuperSample::SUPER_SAMPLE_2X)
fl::UISlider dampening("Dampening", 6.0f, 0.0f, 10.0f, 0.1f)
unsigned char u8
Definition stdint.h:131
fl::CRGB CRGB
Definition video.h:18
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
constexpr int type_rank< T >::value
void blur2d(fl::span< CRGB > leds, u8 width, u8 height, fract8 blur_amount, const XYMap &xymap) FL_NO_EXCEPT
Definition blur.h:153
u8 u8 height
Definition blur.h:186
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
Definition math.h:71
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 FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:577
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
float speed
Wave propagation speed (0.0-1.0, typical: 0.1-0.3)
Definition wave.h:127
bool x_cyclical
If true, waves wrap around the x-axis (cylindrical topology)
Definition wave.h:131
fl::u8 output_blur_passes
Number of blur passes (1 = single pass, 2+ = repeated smoothing).
Definition wave.h:143
WaveFxArgs & operator=(const WaveFxArgs &) FL_NO_EXCEPT=default
WaveCrgbMapPtr crgbMap
Custom color mapper (nullptr uses default grayscale)
Definition wave.h:145
bool auto_updates
If true, simulation advances automatically in draw()
Definition wave.h:125
SuperSample factor
Supersampling quality (SUPER_SAMPLE_2X recommended for balance)
Definition wave.h:121
WaveFxArgs(const WaveFxArgs &) FL_NO_EXCEPT=default
float dampening
Energy dampening factor (higher = faster decay, typical: 3-10)
Definition wave.h:129
WaveFxArgs() FL_NO_EXCEPT=default
bool half_duplex
If true, constrains waves to positive values only.
Definition wave.h:123
bool use_change_grid
Use change grid tracking for optimization (may reduce visual quality)
Definition wave.h:133
fl::u8 output_blur_amount
Post-process Gaussian-style blur (#3122 A4 / #3112 research §A1).
Definition wave.h:139
Configuration parameters for WaveFx effect.
Definition wave.h:103