FastLED 3.9.15
Loading...
Searching...
No Matches
wave.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#include "fl/warn.h"
6
7#include "fl/colorutils.h"
8#include "fl/gradient.h"
9#include "fl/ptr.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
23class WaveCrgbMap : public Referent {
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 uint32_t width = waveSim.getWidth();
37 const uint32_t height = waveSim.getHeight();
38 for (uint32_t y = 0; y < height; y++) {
39 for (uint32_t x = 0; x < width; x++) {
40 uint32_t 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) {}
52
54 CRGB *leds) override;
55
56 private:
58};
59
60struct WaveFxArgs {
61 WaveFxArgs() = default;
66 WaveFxArgs(const WaveFxArgs &) = default;
67 WaveFxArgs &operator=(const WaveFxArgs &) = default;
69 bool half_duplex = true;
70 bool auto_updates = true;
71 float speed = 0.16f;
72 float dampening = 6.0f;
73 WaveCrgbMapPtr crgbMap;
74};
75
76// Uses bilearn filtering to double the size of the grid.
77class WaveFx : public Fx2d {
78 public:
80
83 args.factor, args.speed, args.dampening) {
84 // Initialize the wave simulation with the given parameters.
85 if (args.crgbMap == nullptr) {
86 // Use the default CRGB mapping function.
87 mCrgbMap = WaveCrgbMapDefaultPtr::New();
88 } else {
89 // Set a custom CRGB mapping function.
90 mCrgbMap = args.crgbMap;
91 }
92 setAutoUpdate(args.auto_updates);
93 }
94
95 void setSpeed(float speed) {
96 // Set the speed of the wave simulation.
97 mWaveSim.setSpeed(speed);
98 }
99
101 // Set the dampening of the wave simulation.
102 mWaveSim.setDampening(dampening);
103 }
104
105 void setHalfDuplex(bool on) {
106 // Set whether the wave simulation is half duplex.
107 mWaveSim.setHalfDuplex(on);
108 }
109
111 // Set the supersampling factor of the wave simulation.
112 mWaveSim.setSuperSample(factor);
113 }
114
116 // Set the easing mode for the 8-bit value.
117 mWaveSim.setEasingMode(mode);
118 }
119
120 void setf(size_t x, size_t y, float value) {
121 // Set the value at the given coordinates in the wave simulation.
122 mWaveSim.setf(x, y, value);
123 }
124
125 void addf(size_t x, size_t y, float value) {
126 // Add a value at the given coordinates in the wave simulation.
127 float sum = value + mWaveSim.getf(x, y);
128 mWaveSim.setf(x, y, MIN(1.0f, sum));
129 }
130
131 uint8_t getu8(size_t x, size_t y) const {
132 // Get the 8-bit value at the given coordinates in the wave simulation.
133 return mWaveSim.getu8(x, y);
134 }
135
136 // This will now own the crgbMap.
137 void setCrgbMap(WaveCrgbMapPtr crgbMap) {
138 // Set a custom CRGB mapping function.
139 mCrgbMap.reset(crgbMap);
140 }
141
142 void draw(DrawContext context) override {
143 // Update the wave simulation.
144 if (mAutoUpdates) {
145 mWaveSim.update();
146 }
147 // Map the wave values to the LEDs.
148 mCrgbMap->mapWaveToLEDs(mXyMap, mWaveSim, context.leds);
149 }
150
151 void setAutoUpdate(bool autoUpdate) {
152 // Set whether to automatically update the wave simulation.
153 mAutoUpdates = autoUpdate;
154 }
155
156 void update() {
157 // Called automatically in draw. Only invoke this if you want extra
158 // simulation updates.
159 // Update the wave simulation.
160 mWaveSim.update();
161 }
162
163 fl::Str fxName() const override { return "WaveFx"; }
164
166 WaveCrgbMapPtr mCrgbMap;
167 bool mAutoUpdates = true;
168};
169
170} // namespace fl
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
UISlider speed("Speed", 1.0f, -20.0f, 20.0f, 0.01f)
XYMap xymap(WIDTH, HEIGHT, SERPENTINE)
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
UINumberField palette("Palette", 0, 0, 2)
UISlider dampening("Dampening", 6.0f, 0.0f, 10.0f, 0.1f)
WaveSimulation1D waveSim(NUM_LEDS, SuperSample::SUPER_SAMPLE_2X)
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
Referent()
Definition ptr.cpp:7
Definition str.h:388
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:57
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:156
void setAutoUpdate(bool autoUpdate)
Definition wave.h:151
void setHalfDuplex(bool on)
Definition wave.h:105
WaveFx(XYMap xymap, Args args=Args())
Definition wave.h:81
void setSpeed(float speed)
Definition wave.h:95
uint8_t getu8(size_t x, size_t y) const
Definition wave.h:131
void setf(size_t x, size_t y, float value)
Definition wave.h:120
void setCrgbMap(WaveCrgbMapPtr crgbMap)
Definition wave.h:137
void draw(DrawContext context) override
Definition wave.h:142
void setEasingMode(U8EasingFunction mode)
Definition wave.h:115
WaveCrgbMapPtr mCrgbMap
Definition wave.h:166
void setSuperSample(SuperSample factor)
Definition wave.h:110
WaveFxArgs Args
Definition wave.h:79
void setDampening(float dampening)
Definition wave.h:100
void addf(size_t x, size_t y, float value)
Definition wave.h:125
bool mAutoUpdates
Definition wave.h:167
WaveSimulation2D mWaveSim
Definition wave.h:165
fl::Str fxName() const override
Definition wave.h:163
Utility functions for color fill, palettes, blending, and more.
#define MIN(a, b)
Definition math_macros.h:15
U8EasingFunction
SuperSample
Definition supersample.h:4
@ SUPER_SAMPLE_2X
Definition supersample.h:6
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Declares classes for managing logical groups of LEDs.
#define FASTLED_SMART_PTR(type)
Definition ptr.h:31
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55
float speed
Definition wave.h:71
WaveCrgbMapPtr crgbMap
Definition wave.h:73
bool auto_updates
Definition wave.h:70
WaveFxArgs(SuperSample factor, bool half_duplex, bool auto_updates, float speed, float dampening, WaveCrgbMapPtr crgbMap)
Definition wave.h:62
SuperSample factor
Definition wave.h:68
float dampening
Definition wave.h:72
WaveFxArgs()=default
bool half_duplex
Definition wave.h:69
WaveFxArgs & operator=(const WaveFxArgs &)=default
WaveFxArgs(const WaveFxArgs &)=default