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.
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/draw_visitor.h"
15#include "fl/math_macros.h"
16#include "fl/raster.h"
17#include "fl/time_alpha.h"
18#include "fl/ui.h"
19#include "fl/xypath.h"
20#include "fx/time.h"
21
22// Sketch.
23#include "src/wave.h"
24#include "src/xypaths.h"
25
26using namespace fl;
27
28#define HEIGHT 64
29#define WIDTH 64
30#define NUM_LEDS ((WIDTH) * (HEIGHT))
31#define IS_SERPINTINE true
32#define TIME_ANIMATION 1000 // ms
33
35
37// XYPathPtr shape = XYPath::NewRosePath(WIDTH, HEIGHT);
38
39// Speed up writing to the super sampled waveFx by writing
40// to a raster. This will allow duplicate writes to be removed.
41
42WaveEffect wave_fx; // init in setup().
44
45
48
49XYPathPtr getShape(int which) {
50 int len = shapes.size();
51 which = which % len;
52 if (which < 0) {
53 which += len;
54 }
55 return shapes[which];
56}
57
59UITitle title("XYPath Demo");
60UIDescription description("Use a path on the WaveFx");
61UIButton trigger("Trigger");
62UISlider whichShape("Which Shape", 0.0f, 0.0f, shapes.size() - 1, 1.0f);
63UICheckbox useWaveFx("Use WaveFX", true);
64UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f);
65
66UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f);
67UISlider speed("Speed", 1.0f, -20.0f, 20.0f, 0.01f);
68UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f);
69UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f);
70
72
74 speed.onChanged([](float value) { time_warp.setSpeed(speed.value()); });
75 maxAnimation.onChanged(
76 [](float value) { shapeProgress.set_max_clamp(maxAnimation.value()); });
77
78 trigger.onChanged([]() {
79 // shapeProgress.trigger(millis());
80 FASTLED_WARN("Trigger pressed");
81 });
82 useWaveFx.onChanged([](bool on) {
83 if (on) {
84 FASTLED_WARN("WaveFX enabled");
85 } else {
86 FASTLED_WARN("WaveFX disabled");
87 }
88 });
89}
90
91void setup() {
92 Serial.begin(115200);
93 auto screenmap = xyMap.toScreenMap();
94 screenmap.setDiameter(.2);
95 FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS).setScreenMap(screenmap);
97 // Initialize wave simulation. Please don't use static constructors, keep it
98 // in setup().
99 trigger.click();
101}
102
104
105float getAnimationTime(uint32_t now) {
106 float pointf = shapeProgress.updatef(now);
107 return pointf + transition.value();
108}
109
110void clearLeds() { memset(leds, 0, NUM_LEDS * sizeof(CRGB)); }
111
112void loop() {
113 // Your code here
114 clearLeds();
115 const uint32_t now = millis();
116 uint32_t now_warped = time_warp.update(now);
117
118 auto shape = getShape(whichShape.as<int>());
119 shape->setScale(scale.value());
120
121 float curr_alpha = getAnimationTime(now_warped);
122 static float s_prev_alpha = 0.0f;
123
124 // unconditionally apply the circle.
125 if (trigger) {
126 // trigger the transition
127 time_warp.reset(now);
128 now_warped = time_warp.update(now);
129 shapeProgress.trigger(now_warped);
130 FASTLED_WARN("Transition triggered on " << shape->name());
131 curr_alpha = getAnimationTime(now_warped);
132 s_prev_alpha = curr_alpha;
133 }
134
135 // FASTLED_WARN("Current alpha: " << curr_alpha);
136 // FASTLED_WARN("maxAnimation: " << maxAnimation.value());
137
138 const bool is_active =
139 true || curr_alpha < maxAnimation.value() && curr_alpha > 0.0f;
140
141 // if (shapeProgress.isActive(now)) {
142 static uint32_t frame = 0;
143 frame++;
144 clearLeds();
145 const CRGB purple = CRGB(255, 0, 255);
146 const int number_of_steps = numberOfSteps.value();
147 raster.reset();
148 // float factor = s_prev_alpha; // 0->1.f
149 // factor = MIN(factor/4.0f, 0.05f);
150
151 float diff = curr_alpha - s_prev_alpha;
152 diff *= 1.0f;
153 float factor = MAX(s_prev_alpha - diff, 0.f);
154
155 for (int i = 0; i < number_of_steps; ++i) {
156 float a =
157 fl::map_range<float>(i, 0, number_of_steps - 1, factor, curr_alpha);
158 if (a < .04) {
159 // shorter tails at first.
160 a = map_range<float>(a, 0.0f, .04f, 0.0f, .04f);
161 }
162 float diff_max_alpha = maxAnimation.value() - curr_alpha;
163 if (diff_max_alpha < 0.94) {
164 // shorter tails at the end.
165 a = map_range<float>(a, curr_alpha, maxAnimation.value(),
166 curr_alpha, maxAnimation.value());
167 }
168 uint8_t alpha =
169 fl::map_range<float>(i, 0.0f, number_of_steps - 1, 64, 255);
170 if (!is_active) {
171 alpha = 0;
172 }
173 Tile2x2_u8 subpixel = shape->at_subpixel(a);
174 subpixel.scale(alpha);
175 // subpixels.push_back(subpixel);
176 raster.rasterize(subpixel);
177 }
178
179 s_prev_alpha = curr_alpha;
180
181
182 if (useWaveFx && is_active) {
183 DrawRasterToWaveSimulator draw_wave_fx(&wave_fx);
184 raster.draw(xyMap, draw_wave_fx);
185 } else {
186 raster.draw(purple, xyMap, leds);
187 }
188
189 int first = xyMap(1, 1);
190 int last = xyMap(WIDTH - 2, HEIGHT - 2);
191
192 leds[first] = CRGB(255, 0, 0);
193 leds[last] = CRGB(0, 255, 0);
194 if (useWaveFx) {
195 // fxBlend.draw(Fx::DrawContext(now, leds));
196 wave_fx.draw(Fx::DrawContext(now, leds));
197 }
198
199 EVERY_N_SECONDS(1) {
200 uint32_t frame_time = millis() - now;
201 FASTLED_WARN("Frame time: " << frame_time << "ms");
202 }
203
204 FastLED.show();
205}
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
#define NUM_LEDS
Definition Apa102.ino:6
UITitle title("Simple control of an xy path")
#define WIDTH
Definition Audio.ino:33
#define IS_SERPINTINE
Definition Audio.ino:35
#define TIME_ANIMATION
Definition Audio.ino:36
UIDescription description("This is more of a test for new features.")
#define HEIGHT
Definition Audio.ino:32
fl::vector< XYPathPtr > CreateXYPaths(int width, int height)
Definition xypaths.cpp:31
UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f)
UISlider speed("Speed", 1.0f, -20.0f, 20.0f, 0.01f)
TimeClampedTransition shapeProgress(TIME_ANIMATION)
XYMap xyMap(WIDTH, HEIGHT, false)
UICheckbox useWaveFx("Use WaveFX", true)
WaveEffect wave_fx
UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f)
UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f)
XYRaster raster(WIDTH, HEIGHT)
fl::vector< XYPathPtr > shapes
TimeWarp time_warp
UISlider whichShape("Which Shape", 0.0f, 0.0f, shapes.size() - 1, 1.0f)
UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f)
UIButton trigger("Trigger")
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:58
central include file for FastLED, defines the CFastLED class/object
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:24
void setup()
Definition complex.h:91
void clearLeds()
Definition complex.h:110
XYPathPtr getShape(int which)
Definition complex.h:49
void setupUiCallbacks()
Definition complex.h:73
float getAnimationTime(uint32_t now)
Definition complex.h:105
void loop()
Definition complex.h:112
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:1306
#define MAX(a, b)
Definition math_macros.h:11
XYRasterU8Sparse XYRaster
Definition raster.h:8
HeapVector< T > vector
Definition vector.h:1028
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