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 WaveCrgbMapPtr crgbMap;
78};
79
80// Uses bilearn filtering to double the size of the grid.
81class WaveFx : public Fx2d {
82 public:
84
87 args.factor, args.speed, args.dampening) {
88 // Initialize the wave simulation with the given parameters.
89 if (args.crgbMap == nullptr) {
90 // Use the default CRGB mapping function.
91 mCrgbMap = fl::make_shared<WaveCrgbMapDefault>();
92 } else {
93 // Set a custom CRGB mapping function.
94 mCrgbMap = args.crgbMap;
95 }
96 setAutoUpdate(args.auto_updates);
97 setXCylindrical(args.x_cyclical);
98 }
99
100 void setXCylindrical(bool on) { mWaveSim.setXCylindrical(on); }
101
102 void setSpeed(float speed) {
103 // Set the speed of the wave simulation.
104 mWaveSim.setSpeed(speed);
105 }
106
108 // Set the dampening of the wave simulation.
109 mWaveSim.setDampening(dampening);
110 }
111
112 void setHalfDuplex(bool on) {
113 // Set whether the wave simulation is half duplex.
114 mWaveSim.setHalfDuplex(on);
115 }
116
118 // Set the supersampling factor of the wave simulation.
119 mWaveSim.setSuperSample(factor);
120 }
121
123 // Set the easing mode for the 8-bit value.
124 mWaveSim.setEasingMode(mode);
125 }
126
127 void setf(size_t x, size_t y, float value) {
128 // Set the value at the given coordinates in the wave simulation.
129 mWaveSim.setf(x, y, value);
130 }
131
132 void addf(size_t x, size_t y, float value) {
133 // Add a value at the given coordinates in the wave simulation.
134 float sum = value + mWaveSim.getf(x, y);
135 mWaveSim.setf(x, y, MIN(1.0f, sum));
136 }
137
138 uint8_t getu8(size_t x, size_t y) const {
139 // Get the 8-bit value at the given coordinates in the wave simulation.
140 return mWaveSim.getu8(x, y);
141 }
142
143 // This will now own the crgbMap.
144 void setCrgbMap(WaveCrgbMapPtr crgbMap) {
145 // Set a custom CRGB mapping function.
146 mCrgbMap = crgbMap;
147 }
148
149 void draw(DrawContext context) override {
150 // Update the wave simulation.
151 if (mAutoUpdates) {
152 mWaveSim.update();
153 }
154 // Map the wave values to the LEDs.
155 mCrgbMap->mapWaveToLEDs(mXyMap, mWaveSim, context.leds);
156 }
157
158 void setAutoUpdate(bool autoUpdate) {
159 // Set whether to automatically update the wave simulation.
160 mAutoUpdates = autoUpdate;
161 }
162
163 void update() {
164 // Called automatically in draw. Only invoke this if you want extra
165 // simulation updates.
166 // Update the wave simulation.
167 mWaveSim.update();
168 }
169
170 fl::string fxName() const override { return "WaveFx"; }
171
173 WaveCrgbMapPtr mCrgbMap;
174 bool mAutoUpdates = true;
175};
176
177} // 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:163
void setAutoUpdate(bool autoUpdate)
Definition wave.h:158
void setHalfDuplex(bool on)
Definition wave.h:112
void setSpeed(float speed)
Definition wave.h:102
uint8_t getu8(size_t x, size_t y) const
Definition wave.h:138
void setf(size_t x, size_t y, float value)
Definition wave.h:127
void setCrgbMap(WaveCrgbMapPtr crgbMap)
Definition wave.h:144
void draw(DrawContext context) override
Definition wave.h:149
void setEasingMode(U8EasingFunction mode)
Definition wave.h:122
WaveFx(const XYMap &xymap, Args args=Args())
Definition wave.h:85
WaveCrgbMapPtr mCrgbMap
Definition wave.h:173
void setSuperSample(SuperSample factor)
Definition wave.h:117
WaveFxArgs Args
Definition wave.h:83
void setDampening(float dampening)
Definition wave.h:107
void addf(size_t x, size_t y, float value)
Definition wave.h:132
bool mAutoUpdates
Definition wave.h:174
void setXCylindrical(bool on)
Definition wave.h:100
fl::string fxName() const override
Definition wave.h:170
WaveSimulation2D mWaveSim
Definition wave.h:172
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:41
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:77
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
WaveFxArgs & operator=(const WaveFxArgs &)=default
WaveFxArgs(const WaveFxArgs &)=default