FastLED 3.9.15
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
complex.h
Go to the documentation of this file.
1
2
3/*
4This demo is best viewed using the FastLED compiler.
5
6Windows/MacOS binaries: https://github.com/FastLED/FastLED/releases
7
8Python
9
10Install: pip install fastled
11Run: fastled <this sketch directory>
12This will compile and preview the sketch in the browser, and enable
13all the UI elements you see below.
14*/
15
16#include <Arduino.h>
17#include <FastLED.h>
18
19#include "fl/draw_visitor.h"
20#include "fl/math_macros.h"
21#include "fl/raster.h"
22#include "fl/time_alpha.h"
23#include "fl/ui.h"
24#include "fl/xypath.h"
25#include "fx/time.h"
26
27// Sketch.
28#include "src/wave.h"
29#include "src/xypaths.h"
30
31using namespace fl;
32
33#define HEIGHT 64
34#define WIDTH 64
35#define NUM_LEDS ((WIDTH) * (HEIGHT))
36#define IS_SERPINTINE true
37#define TIME_ANIMATION 1000 // ms
38
40
42// XYPathPtr shape = XYPath::NewRosePath(WIDTH, HEIGHT);
43
44// Speed up writing to the super sampled waveFx by writing
45// to a raster. This will allow duplicate writes to be removed.
46
47WaveEffect wave_fx; // init in setup().
49
50
53
54XYPathPtr getShape(int which) {
55 int len = shapes.size();
56 which = which % len;
57 if (which < 0) {
58 which += len;
59 }
60 return shapes[which];
61}
62
64UITitle title("XYPath Demo");
65UIDescription description("Use a path on the WaveFx");
66UIButton trigger("Trigger");
67UISlider whichShape("Which Shape", 0.0f, 0.0f, shapes.size() - 1, 1.0f);
68UICheckbox useWaveFx("Use WaveFX", true);
69UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f);
70
71UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f);
72UISlider speed("Speed", 1.0f, -20.0f, 20.0f, 0.01f);
73UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f);
74UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f);
75
77
79 speed.onChanged([](float value) { time_warp.setSpeed(speed.value()); });
80 maxAnimation.onChanged(
81 [](float value) { shapeProgress.set_max_clamp(maxAnimation.value()); });
82
83 trigger.onChanged([]() {
84 // shapeProgress.trigger(millis());
85 FASTLED_WARN("Trigger pressed");
86 });
87 useWaveFx.onChanged([](bool on) {
88 if (on) {
89 FASTLED_WARN("WaveFX enabled");
90 } else {
91 FASTLED_WARN("WaveFX disabled");
92 }
93 });
94}
95
96void setup() {
97 Serial.begin(115200);
98 auto screenmap = xyMap.toScreenMap();
99 screenmap.setDiameter(.2);
100 FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS).setScreenMap(screenmap);
102 // Initialize wave simulation. Please don't use static constructors, keep it
103 // in setup().
104 trigger.click();
106}
107
109
110float getAnimationTime(uint32_t now) {
111 float pointf = shapeProgress.updatef(now);
112 return pointf + transition.value();
113}
114
115void clearLeds() { memset(leds, 0, NUM_LEDS * sizeof(CRGB)); }
116
117void loop() {
118 // Your code here
119 clearLeds();
120 const uint32_t now = millis();
121 uint32_t now_warped = time_warp.update(now);
122
123 auto shape = getShape(whichShape.as<int>());
124 shape->setScale(scale.value());
125
126 float curr_alpha = getAnimationTime(now_warped);
127 static float s_prev_alpha = 0.0f;
128
129 // unconditionally apply the circle.
130 if (trigger) {
131 // trigger the transition
132 time_warp.reset(now);
133 now_warped = time_warp.update(now);
134 shapeProgress.trigger(now_warped);
135 FASTLED_WARN("Transition triggered on " << shape->name());
136 curr_alpha = getAnimationTime(now_warped);
137 s_prev_alpha = curr_alpha;
138 }
139
140 // FASTLED_WARN("Current alpha: " << curr_alpha);
141 // FASTLED_WARN("maxAnimation: " << maxAnimation.value());
142
143 const bool is_active =
144 true || curr_alpha < maxAnimation.value() && curr_alpha > 0.0f;
145
146 // if (shapeProgress.isActive(now)) {
147 static uint32_t frame = 0;
148 frame++;
149 clearLeds();
150 const CRGB purple = CRGB(255, 0, 255);
151 const int number_of_steps = numberOfSteps.value();
152 raster.reset();
153 // float factor = s_prev_alpha; // 0->1.f
154 // factor = MIN(factor/4.0f, 0.05f);
155
156 float diff = curr_alpha - s_prev_alpha;
157 diff *= 1.0f;
158 float factor = MAX(s_prev_alpha - diff, 0.f);
159
160 for (int i = 0; i < number_of_steps; ++i) {
161 float a =
162 fl::map_range<float>(i, 0, number_of_steps - 1, factor, curr_alpha);
163 if (a < .04) {
164 // shorter tails at first.
165 a = map_range<float>(a, 0.0f, .04f, 0.0f, .04f);
166 }
167 float diff_max_alpha = maxAnimation.value() - curr_alpha;
168 if (diff_max_alpha < 0.94) {
169 // shorter tails at the end.
170 a = map_range<float>(a, curr_alpha, maxAnimation.value(),
171 curr_alpha, maxAnimation.value());
172 }
173 uint8_t alpha =
174 fl::map_range<float>(i, 0.0f, number_of_steps - 1, 64, 255);
175 if (!is_active) {
176 alpha = 0;
177 }
178 Tile2x2_u8 subpixel = shape->at_subpixel(a);
179 subpixel.scale(alpha);
180 // subpixels.push_back(subpixel);
181 raster.rasterize(subpixel);
182 }
183
184 s_prev_alpha = curr_alpha;
185
186
187 if (useWaveFx && is_active) {
188 DrawRasterToWaveSimulator draw_wave_fx(&wave_fx);
189 raster.draw(xyMap, draw_wave_fx);
190 } else {
191 raster.draw(purple, xyMap, leds);
192 }
193
194 int first = xyMap(1, 1);
195 int last = xyMap(WIDTH - 2, HEIGHT - 2);
196
197 leds[first] = CRGB(255, 0, 0);
198 leds[last] = CRGB(0, 255, 0);
199 if (useWaveFx) {
200 // fxBlend.draw(Fx::DrawContext(now, leds));
201 wave_fx.draw(Fx::DrawContext(now, leds));
202 }
203
204 EVERY_N_SECONDS(1) {
205 uint32_t frame_time = millis() - now;
206 FASTLED_WARN("Frame time: " << frame_time << "ms");
207 }
208
209 FastLED.show();
210}
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
#define NUM_LEDS
Definition Apa102.ino:6
#define WIDTH
Definition Blur2d.ino:9
#define HEIGHT
Definition Blur2d.ino:10
UITitle title("Chromancer")
fl::vector< XYPathPtr > CreateXYPaths(int width, int height)
Definition xypaths.cpp:31
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:62
central include file for FastLED, defines the CFastLED class/object
#define IS_SERPINTINE
Definition FxEngine.ino:35
LED controller for WS2812 LEDs with GRB color order.
Definition FastLED.h:155
_DrawContext DrawContext
Definition fx.h:21
void scale(uint8_t scale)
Definition tile2x2.cpp:35
UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f)
UIButton trigger("Trigger")
void setup()
Definition complex.h:96
void clearLeds()
Definition complex.h:115
XYPathPtr getShape(int which)
Definition complex.h:54
UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f)
void setupUiCallbacks()
Definition complex.h:78
UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f)
float getAnimationTime(uint32_t now)
Definition complex.h:110
XYRaster raster(WIDTH, HEIGHT)
TimeClampedTransition shapeProgress(TIME_ANIMATION)
UICheckbox useWaveFx("Use WaveFX", true)
void loop()
Definition complex.h:117
UISlider whichShape("Which Shape", 0.0f, 0.0f, shapes.size() - 1, 1.0f)
UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f)
UIButton trigger("Trigger")
UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f)
WaveEffect wave_fx
Definition Downscale.h:50
UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f)
fl::vector< XYPathPtr > shapes
Definition Downscale.h:51
TimeWarp time_warp
Definition Downscale.h:54
#define TIME_ANIMATION
Definition Downscale.h:37
XYRaster raster(WIDTH, HEIGHT)
TimeClampedTransition shapeProgress(TIME_ANIMATION)
UICheckbox useWaveFx("Use WaveFX", true)
UISlider whichShape("Which Shape", 0.0f, 0.0f, shapes.size() - 1, 1.0f)
WaveEffect NewWaveSimulation2D(const XYMap xymap)
Definition wave.cpp:42
uint16_t speed
Definition funky.cpp:82
uint16_t scale
Definition funky.cpp:83
XYMap xyMap
Definition gfx.cpp:8
#define EVERY_N_SECONDS(N)
Checks whether to execute a block of code every N seconds.
Definition lib8tion.h:1306
#define MAX(a, b)
Definition math_macros.h:11
XYRasterU8Sparse XYRaster
Definition raster.h:8
HeapVector< T, Allocator > vector
Definition vector.h:1074
FASTLED_FORCE_INLINE U map_range(T value, T in_min, T in_max, U out_min, U out_max)
Definition map_range.h:26
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
#define FASTLED_WARN
Definition warn.h:7
UIDescription description("Advanced layered and blended wave effects.")