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([](float value) { time_warp.setSpeed(speed.value()); });
82 maxAnimation.onChanged(
83 [](float value) { shapeProgress.set_max_clamp(maxAnimation.value()); });
84
85 trigger.onClicked([]() {
86 // shapeProgress.trigger(millis());
87 FASTLED_WARN("Trigger pressed");
88 });
89 useWaveFx.onChanged([](fl::UICheckbox &checkbox) {
90 if (checkbox.value()) {
91 FASTLED_WARN("WaveFX enabled");
92 } else {
93 FASTLED_WARN("WaveFX disabled");
94 }
95 });
96}
97
98void setup() {
99 Serial.begin(115200);
100 auto screenmap = xyMap.toScreenMap();
101 screenmap.setDiameter(.2);
102 FastLED.addLeds<NEOPIXEL, 2>(leds, xyMap.getTotal())
103 .setScreenMap(screenmap);
104 auto screenmap2 = xyMap_Dst.toScreenMap();
105 screenmap.setDiameter(.5);
106 screenmap2.addOffsetY(-HEIGHT / 2);
107 FastLED.addLeds<NEOPIXEL, 3>(leds_downscaled, xyMap_Dst.getTotal())
108 .setScreenMap(screenmap2);
110 // Initialize wave simulation. Please don't use static constructors, keep it
111 // in setup().
112 trigger.click();
114}
115
117
118float getAnimationTime(uint32_t now) {
119 float pointf = shapeProgress.updatef(now);
120 return pointf + transition.value();
121}
122
127
128void loop() {
129 // Your code here
130 clearLeds();
131 const uint32_t now = millis();
132 uint32_t now_warped = time_warp.update(now);
133
134 auto shape = getShape(whichShape.as<int>());
135 shape->setScale(scale.value());
136
137 float curr_alpha = getAnimationTime(now_warped);
138 static float s_prev_alpha = 0.0f;
139
140 // unconditionally apply the circle.
141 if (trigger) {
142 // trigger the transition
143 time_warp.reset(now);
144 now_warped = time_warp.update(now);
145 shapeProgress.trigger(now_warped);
146 FASTLED_WARN("Transition triggered on " << shape->name());
147 curr_alpha = getAnimationTime(now_warped);
148 s_prev_alpha = curr_alpha;
149 }
150
151 static uint32_t frame = 0;
152 frame++;
153 clearLeds();
154 const CRGB purple = CRGB(255, 0, 255);
155 const int number_of_steps = numberOfSteps.value();
156 raster.reset();
157
158 float diff = curr_alpha - s_prev_alpha;
159 diff *= 1.0f;
160 float factor = MAX(s_prev_alpha - diff, 0.f);
161
162 for (int i = 0; i < number_of_steps; ++i) {
163 float a =
164 fl::map_range<float>(i, 0, number_of_steps - 1, factor, curr_alpha);
165 if (a < .04) {
166 // shorter tails at first.
167 a = map_range<float>(a, 0.0f, .04f, 0.0f, .04f);
168 }
169 float diff_max_alpha = maxAnimation.value() - curr_alpha;
170 if (diff_max_alpha < 0.94) {
171 // shorter tails at the end.
172 a = map_range<float>(a, curr_alpha, maxAnimation.value(),
173 curr_alpha, maxAnimation.value());
174 }
175 uint8_t alpha =
176 fl::map_range<uint8_t>(i, 0.0f, number_of_steps - 1, 64, 255);
177 Tile2x2_u8 subpixel = shape->at_subpixel(a);
178 subpixel.scale(alpha);
179 // subpixels.push_back(subpixel);
180 raster.rasterize(subpixel);
181 }
182
183 s_prev_alpha = curr_alpha;
184
185 if (useWaveFx) {
186 DrawRasterToWaveSimulator draw_wave_fx(&wave_fx);
187 raster.draw(xyMap, draw_wave_fx);
188 } else {
189 raster.draw(purple, xyMap, leds);
190 }
191
192 int first = xyMap(1, 1);
193 int last = xyMap(WIDTH - 2, HEIGHT - 2);
194
195 leds[first] = CRGB(255, 0, 0);
196 leds[last] = CRGB(0, 255, 0);
197 if (useWaveFx) {
198 // fxBlend.draw(Fx::DrawContext(now, leds));
199 wave_fx.draw(Fx::DrawContext(now, leds));
200 }
201
202 // downscaleBilinear(leds, WIDTH, HEIGHT, leds_downscaled, WIDTH / 2,
203 // HEIGHT / 2);
204
206
207 // Print out the first 10 pixels of the original and downscaled
208 fl::vector_inlined<CRGB, 10> downscaled_pixels;
209 fl::vector_inlined<CRGB, 10> original_pixels;
210 for (int i = 0; i < 10; ++i) {
211 original_pixels.push_back(leds[i]);
212 downscaled_pixels.push_back(leds_downscaled[i]);
213 }
214
215 FastLED.show();
216}
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
LED controller for WS2812 LEDs with GRB color order.
Definition FastLED.h:155
_DrawContext DrawContext
Definition fx.h:21
void push_back(const T &value)
Definition vector.h:965
void scale(uint8_t scale)
Definition tile2x2.cpp:35
bool value() const
Definition ui_impl.h:132
UISlider numberOfSteps("Number of Steps", 32.0f, 1.0f, 100.0f, 1.0f)
UIButton trigger("Trigger")
void setup()
Definition Downscale.h:98
void clearLeds()
Definition Downscale.h:123
XYPathPtr getShape(int which)
Definition Downscale.h:56
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:118
#define TIME_ANIMATION
Definition Downscale.h:37
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:128
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 MAX(a, b)
Definition math_macros.h:11
InlinedVector< T, INLINED_SIZE > vector_inlined
Definition vector.h:1080
XYRasterU8Sparse XYRaster
Definition raster.h:8
void clear(CRGB(&arr)[N])
Definition clear.h:13
void downscaleHalf(const CRGB *src, uint16_t srcWidth, uint16_t srcHeight, CRGB *dst)
Definition downscale.cpp:17
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.")