FastLED 3.9.15
Loading...
Searching...
No Matches
FxWave2d.ino
Go to the documentation of this file.
1
2
3/*
4This demo is best viewed using the FastLED compiler.
5Install: pip install fastled
6Run: fastled <this sketch directory>
7This will compile and preview the sketch in the browser, and enable
8all the UI elements you see below.
9*/
10
11#include <Arduino.h>
12#include <FastLED.h>
13
14#include "fl/math_macros.h"
15#include "fl/time_alpha.h"
16#include "fl/ui.h"
17#include "fx/2d/blend.h"
18#include "fx/2d/wave.h"
19
20using namespace fl;
21
22#define HEIGHT 64
23#define WIDTH 64
24#define NUM_LEDS ((WIDTH) * (HEIGHT))
25#define IS_SERPINTINE true
26
28
29UITitle title("FxWave2D Demo");
30UIDescription description("Advanced layered and blended wave effects.");
31
32UIButton button("Trigger");
33UIButton buttonFancy("Trigger Fancy");
34UICheckbox autoTrigger("Auto Trigger", true);
35UISlider triggerSpeed("Trigger Speed", .5f, 0.0f, 1.0f, 0.01f);
36UICheckbox easeModeSqrt("Ease Mode Sqrt", false);
37UISlider blurAmount("Global Blur Amount", 0, 0, 172, 1);
38UISlider blurPasses("Global Blur Passes", 1, 1, 10, 1);
39UISlider superSample("SuperSampleExponent", 1.f, 0.f, 3.f, 1.f);
40
41UISlider speedUpper("Wave Upper: Speed", 0.12f, 0.0f, 1.0f);
42UISlider dampeningUpper("Wave Upper: Dampening", 8.9f, 0.0f, 20.0f, 0.1f);
43UICheckbox halfDuplexUpper("Wave Upper: Half Duplex", true);
44UISlider blurAmountUpper("Wave Upper: Blur Amount", 95, 0, 172, 1);
45UISlider blurPassesUpper("Wave Upper: Blur Passes", 1, 1, 10, 1);
46
47UISlider speedLower("Wave Lower: Speed", 0.26f, 0.0f, 1.0f);
48UISlider dampeningLower("Wave Lower: Dampening", 9.0f, 0.0f, 20.0f, 0.1f);
49UICheckbox halfDuplexLower("Wave Lower: Half Duplex", true);
50UISlider blurAmountLower("Wave Lower: Blur Amount", 0, 0, 172, 1);
51UISlider blurPassesLower("Wave Lower: Blur Passes", 1, 1, 10, 1);
52
53UISlider fancySpeed("Fancy Speed", 796, 0, 1000, 1);
54UISlider fancyIntensity("Fancy Intensity", 32, 1, 255, 1);
55UISlider fancyParticleSpan("Fancy Particle Span", 0.06f, 0.01f, 0.2f, 0.01f);
56
57DEFINE_GRADIENT_PALETTE(electricBlueFirePal){
58 0, 0, 0, 0, // Black
59 32, 0, 0, 70, // Dark blue
60 128, 20, 57, 255, // Electric blue
61 255, 255, 255, 255 // White
62};
63
64DEFINE_GRADIENT_PALETTE(electricGreenFirePal){
65 0, 0, 0, 0, // black
66 8, 128, 64, 64, // green
67 16, 255, 222, 222, // red
68 64, 255, 255, 255, // white
69 255, 255, 255, 255 // white
70};
71
74
76 WaveFx::Args out;
78 out.half_duplex = true;
79 out.auto_updates = true;
80 out.speed = 0.18f;
81 out.dampening = 9.0f;
82 out.crgbMap = WaveCrgbGradientMapPtr::New(electricBlueFirePal);
83 return out;
84}
85
87 WaveFx::Args out;
89 out.half_duplex = true;
90 out.auto_updates = true;
91 out.speed = 0.25f;
92 out.dampening = 3.0f;
93 out.crgbMap = WaveCrgbGradientMapPtr::New(electricGreenFirePal);
94 return out;
95}
96
98
100
102
103void setup() {
104 Serial.begin(115200);
105 auto screenmap = xyMap.toScreenMap();
106 screenmap.setDiameter(.2);
107 FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS).setScreenMap(screenmap);
108 fxBlend.add(waveFxLower);
109 fxBlend.add(waveFxUpper);
110}
111
113 switch (int(superSample)) {
114 case 0:
116 case 1:
118 case 2:
120 case 3:
122 default:
124 }
125}
126
128 float perc = .15f;
129 uint8_t min_x = perc * WIDTH;
130 uint8_t max_x = (1 - perc) * WIDTH;
131 uint8_t min_y = perc * HEIGHT;
132 uint8_t max_y = (1 - perc) * HEIGHT;
133 int x = random(min_x, max_x);
134 int y = random(min_y, max_y);
135 waveFxLower.setf(x, y, 1);
136 waveFxUpper.setf(x, y, 1);
137}
138
139void applyFancyEffect(uint32_t now, bool button_active) {
140 uint32_t total =
141 map(fancySpeed.as<uint32_t>(), 0, fancySpeed.getMax(), 1000, 100);
142 static TimeRamp pointTransition = TimeRamp(total, 0, 0);
143
144 if (button_active) {
145 pointTransition.trigger(now, total, 0, 0);
146 }
147
148 if (!pointTransition.isActive(now)) {
149 // no need to draw
150 return;
151 }
152 int mid_x = WIDTH / 2;
153 int mid_y = HEIGHT / 2;
154 // now make a cross
155 int amount = WIDTH / 2;
156 int start_x = mid_x - amount;
157 int end_x = mid_x + amount;
158 int start_y = mid_y - amount;
159 int end_y = mid_y + amount;
160 int curr_alpha = pointTransition.update8(now);
161 int left_x = map(curr_alpha, 0, 255, mid_x, start_x);
162 int down_y = map(curr_alpha, 0, 255, mid_y, start_y);
163 int right_x = map(curr_alpha, 0, 255, mid_x, end_x);
164 int up_y = map(curr_alpha, 0, 255, mid_y, end_y);
165
166 float curr_alpha_f = curr_alpha / 255.0f;
167
168 float valuef = (1.0f - curr_alpha_f) * fancyIntensity.value() / 255.0f;
169
170 int span = fancyParticleSpan.value() * WIDTH;
171 for (int x = left_x - span; x < left_x + span; x++) {
172 waveFxLower.addf(x, mid_y, valuef);
173 waveFxUpper.addf(x, mid_y, valuef);
174 }
175
176 for (int x = right_x - span; x < right_x + span; x++) {
177 waveFxLower.addf(x, mid_y, valuef);
178 waveFxUpper.addf(x, mid_y, valuef);
179 }
180
181 for (int y = down_y - span; y < down_y + span; y++) {
182 waveFxLower.addf(mid_x, y, valuef);
183 waveFxUpper.addf(mid_x, y, valuef);
184 }
185
186 for (int y = up_y - span; y < up_y + span; y++) {
187 waveFxLower.addf(mid_x, y, valuef);
188 waveFxUpper.addf(mid_x, y, valuef);
189 }
190}
191
192struct ui_state {
193 bool button = false;
194 bool bigButton = false;
195};
196
201 waveFxLower.setSpeed(speedLower);
202 waveFxLower.setDampening(dampeningLower);
203 waveFxLower.setHalfDuplex(halfDuplexLower);
204 waveFxLower.setSuperSample(getSuperSample());
205 waveFxLower.setEasingMode(easeMode);
206
207 waveFxUpper.setSpeed(speedUpper);
208 waveFxUpper.setDampening(dampeningUpper);
209 waveFxUpper.setHalfDuplex(halfDuplexUpper);
210 waveFxUpper.setSuperSample(getSuperSample());
211 waveFxUpper.setEasingMode(easeMode);
212 fxBlend.setGlobalBlurAmount(blurAmount);
213 fxBlend.setGlobalBlurPasses(blurPasses);
214
215 Blend2dParams lower_params = {
216 .blur_amount = blurAmountLower,
217 .blur_passes = blurPassesLower,
218 };
219
220 Blend2dParams upper_params = {
221 .blur_amount = blurAmountUpper,
222 .blur_passes = blurPassesUpper,
223 };
224
225 fxBlend.setParams(waveFxLower, lower_params);
226 fxBlend.setParams(waveFxUpper, upper_params);
227 ui_state state{
228 .button = button,
229 .bigButton = buttonFancy,
230 };
231 return state;
232}
233
234void processAutoTrigger(uint32_t now) {
235 static uint32_t nextTrigger = 0;
236 uint32_t trigger_delta = nextTrigger - now;
237 if (trigger_delta > 10000) {
238 // rolled over!
239 trigger_delta = 0;
240 }
241 if (autoTrigger) {
242 if (now >= nextTrigger) {
244 float speed = 1.0f - triggerSpeed.value();
245 uint32_t min_rand = 400 * speed;
246 uint32_t max_rand = 2000 * speed;
247
248 uint32_t min = MIN(min_rand, max_rand);
249 uint32_t max = MAX(min_rand, max_rand);
250 if (min == max) {
251 max += 1;
252 }
253 nextTrigger = now + random(min, max);
254 }
255 }
256}
257
258void loop() {
259 // Your code here
260 uint32_t now = millis();
261 ui_state state = ui();
262 if (state.button) {
264 }
265 applyFancyEffect(now, state.bigButton);
267 Fx::DrawContext ctx(now, leds);
268 fxBlend.draw(ctx);
269 FastLED.show();
270}
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
#define NUM_LEDS
Definition Apa102.ino:6
int y
Definition Audio.ino:72
#define WIDTH
Definition Audio.ino:33
int x
Definition Audio.ino:71
#define IS_SERPINTINE
Definition Audio.ino:35
#define HEIGHT
Definition Audio.ino:32
UISlider speed("Speed", 1.0f, -20.0f, 20.0f, 0.01f)
XYMap xyMap(WIDTH, HEIGHT, false)
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:58
central include file for FastLED, defines the CFastLED class/object
UISlider blurAmount("Global Blur Amount", 0, 0, 172, 1)
XYMap xyMap(WIDTH, HEIGHT, IS_SERPINTINE)
UISlider blurPassesLower("Wave Lower: Blur Passes", 1, 1, 10, 1)
UISlider blurPasses("Global Blur Passes", 1, 1, 10, 1)
Blend2d fxBlend(xyMap)
ui_state ui()
Definition FxWave2d.ino:197
UISlider blurAmountLower("Wave Lower: Blur Amount", 0, 0, 172, 1)
UISlider speedLower("Wave Lower: Speed", 0.26f, 0.0f, 1.0f)
UISlider blurPassesUpper("Wave Upper: Blur Passes", 1, 1, 10, 1)
UISlider fancyParticleSpan("Fancy Particle Span", 0.06f, 0.01f, 0.2f, 0.01f)
UICheckbox autoTrigger("Auto Trigger", true)
WaveFx waveFxLower(xyRect, CreateArgsLower())
void setup()
Definition FxWave2d.ino:103
UICheckbox halfDuplexUpper("Wave Upper: Half Duplex", true)
UISlider speedUpper("Wave Upper: Speed", 0.12f, 0.0f, 1.0f)
UITitle title("FxWave2D Demo")
void processAutoTrigger(uint32_t now)
Definition FxWave2d.ino:234
UIButton button("Trigger")
UISlider fancySpeed("Fancy Speed", 796, 0, 1000, 1)
WaveFx::Args CreateArgsLower()
Definition FxWave2d.ino:75
XYMap xyRect(WIDTH, HEIGHT, false)
UISlider dampeningUpper("Wave Upper: Dampening", 8.9f, 0.0f, 20.0f, 0.1f)
UICheckbox easeModeSqrt("Ease Mode Sqrt", false)
UIButton buttonFancy("Trigger Fancy")
UISlider fancyIntensity("Fancy Intensity", 32, 1, 255, 1)
UISlider triggerSpeed("Trigger Speed",.5f, 0.0f, 1.0f, 0.01f)
UIDescription description("Advanced layered and blended wave effects.")
UISlider blurAmountUpper("Wave Upper: Blur Amount", 95, 0, 172, 1)
SuperSample getSuperSample()
Definition FxWave2d.ino:112
void triggerRipple()
Definition FxWave2d.ino:127
UISlider superSample("SuperSampleExponent", 1.f, 0.f, 3.f, 1.f)
void applyFancyEffect(uint32_t now, bool button_active)
Definition FxWave2d.ino:139
UISlider dampeningLower("Wave Lower: Dampening", 9.0f, 0.0f, 20.0f, 0.1f)
UICheckbox halfDuplexLower("Wave Lower: Half Duplex", true)
WaveFx::Args CreateArgsUpper()
Definition FxWave2d.ino:86
void loop()
Definition FxWave2d.ino:258
WaveFx waveFxUpper(xyRect, CreateArgsUpper())
LED controller for WS2812 LEDs with GRB color order.
Definition FastLED.h:155
_DrawContext DrawContext
Definition fx.h:21
uint8_t update8(uint32_t now) override
Compute current 0–255 output based on how much time has elapsed since trigger().
bool isActive(uint32_t now) const override
void trigger(uint32_t now) override
Call this when you want to (re)start the ramp cycle.
WaveFxArgs Args
Definition wave.h:79
#define DEFINE_GRADIENT_PALETTE(X)
Defines a static RGB palette very compactly using a series of connected color gradients.
Definition colorutils.h:102
#define MIN(a, b)
Definition math_macros.h:15
#define MAX(a, b)
Definition math_macros.h:11
U8EasingFunction
@ WAVE_U8_MODE_LINEAR
@ WAVE_U8_MODE_SQRT
SuperSample
Definition supersample.h:4
@ SUPER_SAMPLE_8X
Definition supersample.h:8
@ SUPER_SAMPLE_2X
Definition supersample.h:6
@ SUPER_SAMPLE_4X
Definition supersample.h:7
@ SUPER_SAMPLE_NONE
Definition supersample.h:5
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
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
SuperSample factor
Definition wave.h:68
float dampening
Definition wave.h:72
bool half_duplex
Definition wave.h:69
bool bigButton
Definition FxWave2d.ino:194
bool button
Definition FxWave2d.ino:193
ui_state()
Definition ui_state.h:12