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