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