FastLED 3.9.15
Loading...
Searching...
No Matches
Downscale.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/downscale.h"
20#include "fl/draw_visitor.h"
21#include "fl/math_macros.h"
22#include "fl/raster.h"
23#include "fl/time_alpha.h"
24#include "fl/ui.h"
25#include "fl/xypath.h"
26#include "fx/time.h"
27
28// Sketch.
29#include "src/wave.h"
30#include "src/xypaths.h"
31
32using namespace fl;
33
34#define HEIGHT 64
35#define WIDTH 64
36#define NUM_LEDS ((WIDTH) * (HEIGHT))
37#define TIME_ANIMATION 1000 // ms
38
40CRGB leds_downscaled[NUM_LEDS / 4]; // Downscaled buffer
41
44 false); // Framebuffer is regular rectangle LED matrix.
45// XYPathPtr shape = XYPath::NewRosePath(WIDTH, HEIGHT);
46
47// Speed up writing to the super sampled waveFx by writing
48// to a raster. This will allow duplicate writes to be removed.
49
50WaveEffect wave_fx; // init in setup().
52
55
56XYPathPtr getShape(int which) {
57 int len = shapes.size();
58 which = which % len;
59 if (which < 0) {
60 which += len;
61 }
62 return shapes[which];
63}
64
66UITitle title("XYPath Demo");
67UIDescription description("Use a path on the WaveFx");
68UIButton trigger("Trigger");
69UISlider whichShape("Which Shape", 0.0f, 0.0f, shapes.size() - 1, 1.0f);
70UICheckbox useWaveFx("Use WaveFX", true);
71UISlider transition("Transition", 0.0f, 0.0f, 1.0f, 0.01f);
72
73UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f);
74UISlider speed("Speed", 1.0f, -20.0f, 20.0f, 0.01f);
75UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f);
76UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f);
77
79
81 speed.onChanged([](UISlider& slider) {
82 time_warp.setSpeed(slider.value());
83 });
84 maxAnimation.onChanged(
85 [](UISlider& slider) {
86 shapeProgress.set_max_clamp(slider.value());
87 });
88
89 trigger.onClicked([]() {
90 // shapeProgress.trigger(millis());
91 FASTLED_WARN("Trigger pressed");
92 });
93 useWaveFx.onChanged([](fl::UICheckbox &checkbox) {
94 if (checkbox.value()) {
95 FASTLED_WARN("WaveFX enabled");
96 } else {
97 FASTLED_WARN("WaveFX disabled");
98 }
99 });
100}
101
102void setup() {
103 Serial.begin(115200);
104 auto screenmap = xyMap.toScreenMap();
105 screenmap.setDiameter(.2);
106 FastLED.addLeds<NEOPIXEL, 2>(leds, xyMap.getTotal())
107 .setScreenMap(screenmap);
108 auto screenmap2 = xyMap_Dst.toScreenMap();
109 screenmap.setDiameter(.5);
110 screenmap2.addOffsetY(-HEIGHT / 2);
111 FastLED.addLeds<NEOPIXEL, 3>(leds_downscaled, xyMap_Dst.getTotal())
112 .setScreenMap(screenmap2);
114 // Initialize wave simulation. Please don't use static constructors, keep it
115 // in setup().
116 trigger.click();
118}
119
121
122float getAnimationTime(uint32_t now) {
123 float pointf = shapeProgress.updatef(now);
124 return pointf + transition.value();
125}
126
131
132void loop() {
133 // Your code here
134 clearLeds();
135 const uint32_t now = millis();
136 uint32_t now_warped = time_warp.update(now);
137
138 auto shape = getShape(whichShape.as<int>());
139 shape->setScale(scale.value());
140
141 float curr_alpha = getAnimationTime(now_warped);
142 static float s_prev_alpha = 0.0f;
143
144 // unconditionally apply the circle.
145 if (trigger) {
146 // trigger the transition
147 time_warp.reset(now);
148 now_warped = time_warp.update(now);
149 shapeProgress.trigger(now_warped);
150 FASTLED_WARN("Transition triggered on " << shape->name());
151 curr_alpha = getAnimationTime(now_warped);
152 s_prev_alpha = curr_alpha;
153 }
154
155 clearLeds();
156 const CRGB purple = CRGB(255, 0, 255);
157 const int number_of_steps = numberOfSteps.value();
158 raster.reset();
159
160 float diff = curr_alpha - s_prev_alpha;
161 diff *= 1.0f;
162 float factor = MAX(s_prev_alpha - diff, 0.f);
163
164 for (int i = 0; i < number_of_steps; ++i) {
165 float a =
166 fl::map_range<float>(i, 0, number_of_steps - 1, factor, curr_alpha);
167 if (a < .04) {
168 // shorter tails at first.
169 a = map_range<float>(a, 0.0f, .04f, 0.0f, .04f);
170 }
171 float diff_max_alpha = maxAnimation.value() - curr_alpha;
172 if (diff_max_alpha < 0.94) {
173 // shorter tails at the end.
174 a = map_range<float>(a, curr_alpha, maxAnimation.value(),
175 curr_alpha, maxAnimation.value());
176 }
177 uint8_t alpha =
178 fl::map_range<uint8_t>(i, 0.0f, number_of_steps - 1, 64, 255);
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 if (useWaveFx) {
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 // downscaleBilinear(leds, WIDTH, HEIGHT, leds_downscaled, WIDTH / 2,
205 // HEIGHT / 2);
206
208
209 // Print out the first 10 pixels of the original and downscaled
210 fl::vector_inlined<CRGB, 10> downscaled_pixels;
211 fl::vector_inlined<CRGB, 10> original_pixels;
212 for (int i = 0; i < 10; ++i) {
213 original_pixels.push_back(leds[i]);
214 downscaled_pixels.push_back(leds_downscaled[i]);
215 }
216
217 FastLED.show();
218}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
#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 push_back(const T &value)
Definition vector.h:1061
void scale(u8 scale)
Definition tile2x2.cpp:43
bool value() const
Definition ui.h:239
UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f)
UIButton trigger("Trigger")
void setup()
Definition Downscale.h:102
void clearLeds()
Definition Downscale.h:127
UITitle title("XYPath Demo")
XYPathPtr getShape(int which)
Definition Downscale.h:56
UIDescription description("Use a path on the WaveFx")
UISlider maxAnimation("Max Animation", 1.0f, 5.0f, 20.0f, 1.f)
WaveEffect wave_fx
Definition Downscale.h:50
void setupUiCallbacks()
Definition Downscale.h:80
XYMap xyMap_Dst(WIDTH/2, HEIGHT/2, false)
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
float getAnimationTime(uint32_t now)
Definition Downscale.h:122
XYRaster raster(WIDTH, HEIGHT)
TimeClampedTransition shapeProgress(TIME_ANIMATION)
UICheckbox useWaveFx("Use WaveFX", true)
CRGB leds_downscaled[NUM_LEDS/4]
Definition Downscale.h:40
void loop()
Definition Downscale.h:132
UISlider whichShape("Which Shape", 0.0f, 0.0f, shapes.size() - 1, 1.0f)
WaveEffect NewWaveSimulation2D(const XYMap &xymap)
Definition wave.cpp:42
UISlider slider("Speed", 0.18f, 0.0f, 1.0f)
#define MAX(a, b)
Definition math_macros.h:37
InlinedVector< T, INLINED_SIZE > vector_inlined
Definition vector.h:1220
XYRasterU8Sparse XYRaster
Definition raster.h:8
void clear(CRGB(&arr)[N])
Definition clear.h:13
void downscaleHalf(const CRGB *src, fl::u16 srcWidth, fl::u16 srcHeight, CRGB *dst)
Definition downscale.cpp:18
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