FastLED 3.9.15
Loading...
Searching...
No Matches
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
31#include "fl/memfill.h"
32using namespace fl;
33
34#define HEIGHT 64
35#define WIDTH 64
36#define NUM_LEDS ((WIDTH) * (HEIGHT))
37#define IS_SERPINTINE true
38#define TIME_ANIMATION 1000 // ms
39
41
43// XYPathPtr shape = XYPath::NewRosePath(WIDTH, HEIGHT);
44
45// Speed up writing to the super sampled waveFx by writing
46// to a raster. This will allow duplicate writes to be removed.
47
48WaveEffect wave_fx; // init in setup().
50
51
54
55XYPathPtr getShape(int which) {
56 int len = shapes.size();
57 which = which % len;
58 if (which < 0) {
59 which += len;
60 }
61 return shapes[which];
62}
63
65UITitle title("XYPath Demo");
66UIDescription description("Use a path on the WaveFx");
67UIButton trigger("Trigger");
68UISlider whichShape("Which Shape", 0.0f, 0.0f, shapes.size() - 1, 1.0f);
69UICheckbox useWaveFx("Use WaveFX", true);
70UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f);
71
72UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f);
73UISlider speed("Speed", 1.0f, -20.0f, 20.0f, 0.01f);
74UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f);
75UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f);
76
78
80 speed.onChanged([](float value) { time_warp.setSpeed(speed.value()); });
81 maxAnimation.onChanged(
82 [](float value) { shapeProgress.set_max_clamp(maxAnimation.value()); });
83
84 trigger.onChanged([]() {
85 // shapeProgress.trigger(millis());
86 FASTLED_WARN("Trigger pressed");
87 });
88 useWaveFx.onChanged([](bool on) {
89 if (on) {
90 FASTLED_WARN("WaveFX enabled");
91 } else {
92 FASTLED_WARN("WaveFX disabled");
93 }
94 });
95}
96
97void setup() {
98 Serial.begin(115200);
99 auto screenmap = xyMap.toScreenMap();
100 screenmap.setDiameter(.2);
101 FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS).setScreenMap(screenmap);
103 // Initialize wave simulation. Please don't use static constructors, keep it
104 // in setup().
105 trigger.click();
107}
108
110
111float getAnimationTime(uint32_t now) {
112 float pointf = shapeProgress.updatef(now);
113 return pointf + transition.value();
114}
115
116void clearLeds() { fl::memfill(leds, 0, NUM_LEDS * sizeof(CRGB)); }
117
118void loop() {
119 // Your code here
120 clearLeds();
121 const uint32_t now = millis();
122 uint32_t now_warped = time_warp.update(now);
123
124 auto shape = getShape(whichShape.as<int>());
125 shape->setScale(scale.value());
126
127 float curr_alpha = getAnimationTime(now_warped);
128 static float s_prev_alpha = 0.0f;
129
130 // unconditionally apply the circle.
131 if (trigger) {
132 // trigger the transition
133 time_warp.reset(now);
134 now_warped = time_warp.update(now);
135 shapeProgress.trigger(now_warped);
136 FASTLED_WARN("Transition triggered on " << shape->name());
137 curr_alpha = getAnimationTime(now_warped);
138 s_prev_alpha = curr_alpha;
139 }
140
141 // FASTLED_WARN("Current alpha: " << curr_alpha);
142 // FASTLED_WARN("maxAnimation: " << maxAnimation.value());
143
144 const bool is_active =
145 true || curr_alpha < maxAnimation.value() && curr_alpha > 0.0f;
146
147 // if (shapeProgress.isActive(now)) {
148 static uint32_t frame = 0;
149 frame++;
150 clearLeds();
151 const CRGB purple = CRGB(255, 0, 255);
152 const int number_of_steps = numberOfSteps.value();
153 raster.reset();
154 // float factor = s_prev_alpha; // 0->1.f
155 // factor = MIN(factor/4.0f, 0.05f);
156
157 float diff = curr_alpha - s_prev_alpha;
158 diff *= 1.0f;
159 float factor = MAX(s_prev_alpha - diff, 0.f);
160
161 for (int i = 0; i < number_of_steps; ++i) {
162 float a =
163 fl::map_range<float>(i, 0, number_of_steps - 1, factor, curr_alpha);
164 if (a < .04) {
165 // shorter tails at first.
166 a = map_range<float>(a, 0.0f, .04f, 0.0f, .04f);
167 }
168 float diff_max_alpha = maxAnimation.value() - curr_alpha;
169 if (diff_max_alpha < 0.94) {
170 // shorter tails at the end.
171 a = map_range<float>(a, curr_alpha, maxAnimation.value(),
172 curr_alpha, maxAnimation.value());
173 }
174 uint8_t alpha =
175 fl::map_range<float>(i, 0.0f, number_of_steps - 1, 64, 255);
176 if (!is_active) {
177 alpha = 0;
178 }
179 Tile2x2_u8 subpixel = shape->at_subpixel(a);
180 subpixel.scale(alpha);
181 // subpixels.push_back(subpixel);
182 raster.rasterize(subpixel);
183 }
184
185 s_prev_alpha = curr_alpha;
186
187
188 if (useWaveFx && is_active) {
189 DrawRasterToWaveSimulator draw_wave_fx(&wave_fx);
190 raster.draw(xyMap, draw_wave_fx);
191 } else {
192 raster.draw(purple, xyMap, leds);
193 }
194
195 int first = xyMap(1, 1);
196 int last = xyMap(WIDTH - 2, HEIGHT - 2);
197
198 leds[first] = CRGB(255, 0, 0);
199 leds[last] = CRGB(0, 255, 0);
200 if (useWaveFx) {
201 // fxBlend.draw(Fx::DrawContext(now, leds));
202 wave_fx.draw(Fx::DrawContext(now, leds));
203 }
204
205 EVERY_N_SECONDS(1) {
206 uint32_t frame_time = millis() - now;
207 FASTLED_WARN("Frame time: " << frame_time << "ms");
208 }
209
210 FastLED.show();
211}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
#define IS_SERPINTINE
Definition simple.h:50
#define TIME_ANIMATION
Definition simple.h:51
fl::XYMap xyMap
Definition ColorBoost.h:61
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:74
central include file for FastLED, defines the CFastLED class/object
uint16_t speed
Definition Noise.ino:63
uint16_t scale
Definition Noise.ino:74
#define WIDTH
Definition advanced.h:36
#define HEIGHT
Definition advanced.h:37
LED controller for WS2812 LEDs with GRB color order.
Definition FastLED.h:158
_DrawContext DrawContext
Definition fx.h:21
void scale(u8 scale)
Definition tile2x2.cpp:43
UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f)
UIButton trigger("Trigger")
void setup()
Definition complex.h:97
void clearLeds()
Definition complex.h:116
UITitle title("XYPath Demo")
XYPathPtr getShape(int which)
Definition complex.h:55
UIDescription description("Use a path on the WaveFx")
UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f)
void setupUiCallbacks()
Definition complex.h:79
UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f)
float getAnimationTime(uint32_t now)
Definition complex.h:111
XYRaster raster(WIDTH, HEIGHT)
TimeClampedTransition shapeProgress(TIME_ANIMATION)
UICheckbox useWaveFx("Use WaveFX", true)
void loop()
Definition complex.h:118
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
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
#define EVERY_N_SECONDS(N)
Checks whether to execute a block of code every N seconds.
Definition lib8tion.h:1186
#define MAX(a, b)
Definition math_macros.h:37
XYRasterU8Sparse XYRaster
Definition raster.h:8
void * memfill(void *ptr, int value, fl::size num)
Definition memfill.h:11
HeapVector< T, Allocator > vector
Definition vector.h:1214
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
#define FASTLED_WARN
Definition warn.h:7