FastLED 3.9.15
Loading...
Searching...
No Matches
wave.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stdint.h"
4
5#include "fl/warn.h"
6
7#include "fl/colorutils.h"
8#include "fl/gradient.h"
9#include "fl/memory.h"
10#include "fl/wave_simulation.h"
11#include "fl/xymap.h"
12#include "fx/fx.h"
13#include "fx/fx2d.h"
14#include "pixelset.h"
15
16namespace fl {
17
22
24 public:
25 virtual ~WaveCrgbMap() = default;
27 CRGB *leds) = 0;
28};
29
30// A great deafult for the wave rendering. It will draw black and then the
31// amplitude of the wave will be more white.
33 public:
35 CRGB *leds) override {
36 const fl::u32 width = waveSim.getWidth();
37 const fl::u32 height = waveSim.getHeight();
38 for (fl::u32 y = 0; y < height; y++) {
39 for (fl::u32 x = 0; x < width; x++) {
40 fl::u32 idx = xymap(x, y);
41 uint8_t value8 = waveSim.getu8(x, y);
42 leds[idx] = CRGB(value8, value8, value8);
43 }
44 }
45 }
46};
47
49 public:
51 WaveCrgbGradientMap(const CRGBPalette16 &palette) : mGradient(palette) {}
53
55 CRGB *leds) override;
56
57 void setGradient(const Gradient &gradient) { mGradient = gradient; }
58
59 private:
61};
62
63struct WaveFxArgs {
64 WaveFxArgs() = default;
69 WaveFxArgs(const WaveFxArgs &) = default;
70 WaveFxArgs &operator=(const WaveFxArgs &) = default;
72 bool half_duplex = true;
73 bool auto_updates = true;
74 float speed = 0.16f;
75 float dampening = 6.0f;
76 bool x_cyclical = false;
77 bool use_change_grid = false; // Whether to use change grid tracking (default: disabled for better visuals)
78 WaveCrgbMapPtr crgbMap;
79};
80
81// Uses bilearn filtering to double the size of the grid.
82class WaveFx : public Fx2d {
83 public:
85
88 args.factor, args.speed, args.dampening) {
89 // Initialize the wave simulation with the given parameters.
90 if (args.crgbMap == nullptr) {
91 // Use the default CRGB mapping function.
92 mCrgbMap = fl::make_shared<WaveCrgbMapDefault>();
93 } else {
94 // Set a custom CRGB mapping function.
95 mCrgbMap = args.crgbMap;
96 }
97 setAutoUpdate(args.auto_updates);
98 setXCylindrical(args.x_cyclical);
99 setUseChangeGrid(args.use_change_grid);
100 }
101
102 void setXCylindrical(bool on) { mWaveSim.setXCylindrical(on); }
103
104 void setSpeed(float speed) {
105 // Set the speed of the wave simulation.
106 mWaveSim.setSpeed(speed);
107 }
108
110 // Set the dampening of the wave simulation.
111 mWaveSim.setDampening(dampening);
112 }
113
114 void setHalfDuplex(bool on) {
115 // Set whether the wave simulation is half duplex.
116 mWaveSim.setHalfDuplex(on);
117 }
118
120 // Set the supersampling factor of the wave simulation.
121 mWaveSim.setSuperSample(factor);
122 }
123
125 // Set the easing mode for the 8-bit value.
126 mWaveSim.setEasingMode(mode);
127 }
128
129 void setUseChangeGrid(bool enabled) {
130 // Set whether to use the change grid tracking optimization.
131 mWaveSim.setUseChangeGrid(enabled);
132 }
133
134 bool getUseChangeGrid() const {
135 // Get the current change grid tracking setting.
136 return mWaveSim.getUseChangeGrid();
137 }
138
139 void setf(size_t x, size_t y, float value) {
140 // Set the value at the given coordinates in the wave simulation.
141 mWaveSim.setf(x, y, value);
142 }
143
144 void addf(size_t x, size_t y, float value) {
145 // Add a value at the given coordinates in the wave simulation.
146 float sum = value + mWaveSim.getf(x, y);
147 mWaveSim.setf(x, y, MIN(1.0f, sum));
148 }
149
150 uint8_t getu8(size_t x, size_t y) const {
151 // Get the 8-bit value at the given coordinates in the wave simulation.
152 return mWaveSim.getu8(x, y);
153 }
154
155 // This will now own the crgbMap.
156 void setCrgbMap(WaveCrgbMapPtr crgbMap) {
157 // Set a custom CRGB mapping function.
158 mCrgbMap = crgbMap;
159 }
160
161 void draw(DrawContext context) override {
162 // Update the wave simulation.
163 if (mAutoUpdates) {
164 mWaveSim.update();
165 }
166 // Map the wave values to the LEDs.
167 mCrgbMap->mapWaveToLEDs(mXyMap, mWaveSim, context.leds);
168 }
169
170 void setAutoUpdate(bool autoUpdate) {
171 // Set whether to automatically update the wave simulation.
172 mAutoUpdates = autoUpdate;
173 }
174
175 void update() {
176 // Called automatically in draw. Only invoke this if you want extra
177 // simulation updates.
178 // Update the wave simulation.
179 mWaveSim.update();
180 }
181
182 fl::string fxName() const override { return "WaveFx"; }
183
185 WaveCrgbMapPtr mCrgbMap;
186 bool mAutoUpdates = true;
187};
188
189} // namespace fl
CRGB leds[NUM_LEDS]
int y
Definition simple.h:93
int x
Definition simple.h:92
XYMap xymap(WIDTH, HEIGHT, SERPENTINE)
UINumberField palette("Palette", 0, 0, 2)
uint16_t speed
Definition Noise.ino:63
XYMap mXyMap
Definition fx2d.h:31
uint16_t getHeight() const
Definition fx2d.h:24
Fx2d(const XYMap &xyMap)
Definition fx2d.h:20
uint16_t getWidth() const
Definition fx2d.h:25
_DrawContext DrawContext
Definition fx.h:21
void setGradient(const Gradient &gradient)
Definition wave.h:57
fl::GradientInlined Gradient
Definition wave.h:50
void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, CRGB *leds) override
Definition wave.cpp:49
WaveCrgbGradientMap(const CRGBPalette16 &palette)
Definition wave.h:51
Gradient mGradient
Definition wave.h:60
virtual void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, CRGB *leds)=0
virtual ~WaveCrgbMap()=default
void mapWaveToLEDs(const XYMap &xymap, WaveSimulation2D &waveSim, CRGB *leds) override
Definition wave.h:34
void update()
Definition wave.h:175
void setAutoUpdate(bool autoUpdate)
Definition wave.h:170
void setHalfDuplex(bool on)
Definition wave.h:114
void setSpeed(float speed)
Definition wave.h:104
uint8_t getu8(size_t x, size_t y) const
Definition wave.h:150
void setf(size_t x, size_t y, float value)
Definition wave.h:139
void setCrgbMap(WaveCrgbMapPtr crgbMap)
Definition wave.h:156
void draw(DrawContext context) override
Definition wave.h:161
void setEasingMode(U8EasingFunction mode)
Definition wave.h:124
WaveFx(const XYMap &xymap, Args args=Args())
Definition wave.h:86
WaveCrgbMapPtr mCrgbMap
Definition wave.h:185
void setSuperSample(SuperSample factor)
Definition wave.h:119
void setUseChangeGrid(bool enabled)
Definition wave.h:129
WaveFxArgs Args
Definition wave.h:84
void setDampening(float dampening)
Definition wave.h:109
bool getUseChangeGrid() const
Definition wave.h:134
void addf(size_t x, size_t y, float value)
Definition wave.h:144
bool mAutoUpdates
Definition wave.h:186
void setXCylindrical(bool on)
Definition wave.h:102
fl::string fxName() const override
Definition wave.h:182
WaveSimulation2D mWaveSim
Definition wave.h:184
WaveSimulation1D waveSim(NUM_LEDS, SuperSample::SUPER_SAMPLE_2X)
UISlider dampening("Dampening", 6.0f, 0.0f, 10.0f, 0.1f)
Utility functions for color fill, palettes, blending, and more.
#define MIN(a, b)
Definition math_macros.h:49
U8EasingFunction
SuperSample
Definition supersample.h:4
@ SUPER_SAMPLE_2X
Definition supersample.h:6
IMPORTANT!
Definition crgb.h:20
corkscrew_args args
Definition old.h:150
Declares classes for managing logical groups of LEDs.
#define FASTLED_SMART_PTR(type)
Definition ptr.h:33
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
float speed
Definition wave.h:74
bool x_cyclical
Definition wave.h:76
WaveCrgbMapPtr crgbMap
Definition wave.h:78
bool auto_updates
Definition wave.h:73
WaveFxArgs(SuperSample factor, bool half_duplex, bool auto_updates, float speed, float dampening, WaveCrgbMapPtr crgbMap)
Definition wave.h:65
SuperSample factor
Definition wave.h:71
float dampening
Definition wave.h:75
WaveFxArgs()=default
bool half_duplex
Definition wave.h:72
bool use_change_grid
Definition wave.h:77
WaveFxArgs & operator=(const WaveFxArgs &)=default
WaveFxArgs(const WaveFxArgs &)=default