Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
fadein.hpp
Go to the documentation of this file.
1
5#ifndef MODES_ANIM_FADEIN_HPP
6#define MODES_ANIM_FADEIN_HPP
7
8#include <array>
9#include <cstdlib>
10#include <etl/bitset.h>
11
13
14namespace lampda::modes::anims {
15namespace fadein {
16
26template<size_t MaxParticles = 20> struct GravityFadeIn
27{
28 void reset(auto& ctx)
29 {
30 maskedLeds.set(); // everything hidden
31
32 particuleSystem.reset();
33 particuleSystem.set_max_particle_count(MaxParticles);
34 reserved.reset();
35 targets.fill(0);
36 targetsZ.fill(0.0f);
37
38 latestProgress = -1.0f;
39 latestProgressTime = 0;
40 progressPerMs = 0.0f;
41 spawned = 0;
42 isComplete = false;
43 }
44
45 void loop(auto& ctx)
46 {
47 if (isComplete)
48 return;
49
50 spawn_pending(ctx);
51
52 // mask all the leds that needs masking
53 for (uint16_t i = 0; i < maskedLeds.size(); i++)
54 {
55 if (maskedLeds[i])
56 ctx.lamp.mask_led_at_index(i, true);
57 }
58
59 if (particuleSystem.get_number_of_active() > 0)
60 {
61 static constexpr bool shouldKeepInLampBounds = false;
62 particuleSystem.iterate_no_collisions(
63 utils::vec3d(0.0, 0.0, -9.81 / 4.0), ctx.lamp.frameDurationMs / 1000.0, shouldKeepInLampBounds);
64
65 // unmask and remove the particles that reached their target
66 const uint16_t left = particuleSystem.depop_particules_indexed([&](size_t slot, const Particle& p) {
67 return land_if_arrived(ctx, slot, p);
68 });
69
70 // Force display because the pixels below are masked
71 static constexpr bool forceDisplayParticles = true;
72 particuleSystem.show(ctx.lamp, forceDisplayParticles);
73
74 if (left == 0 and spawned >= ctx.lamp.ledCount)
75 isComplete = true;
76 }
77 }
78
83 void update_progress_rate(auto& ctx, const float expectedProgress)
84 {
85 if (isComplete)
86 return;
87
88 const float progress = lmpd_constrain<float>(expectedProgress, 0.0f, 1.0f);
89
90 // done: reveal everything, whatever is still falling
91 if (progress >= 1.0f)
92 {
93 complete(ctx);
94 return;
95 }
96
97 if (progress == latestProgress)
98 return;
99
100 if (latestProgress >= 0.0f and ctx.lamp.now > latestProgressTime)
101 {
102 const float elapsedMs = static_cast<float>(ctx.lamp.now - latestProgressTime);
103 progressPerMs = lmpd_constrain<float>((progress - latestProgress) / elapsedMs, 0.0f, 1.0f);
104 }
105 else
106 {
107 progressPerMs = 0.0f; // first call: rate unknown, only catch up to the current progress
108 }
109 latestProgress = progress;
110 latestProgressTime = ctx.lamp.now;
111 }
112
113protected:
115 float estimated_progress(auto& ctx) const
116 {
117 if (latestProgress < 0.0f)
118 return 0.0f;
119 const float elapsedMs = static_cast<float>(ctx.lamp.now - latestProgressTime) + fallLeadMs;
120 return lmpd_constrain<float>(latestProgress + progressPerMs * elapsedMs, latestProgress, 1.0f);
121 }
122
123 void complete(auto& ctx)
124 {
125 maskedLeds.reset(); // everything visible
126
127 particuleSystem.reset();
128 reserved.reset();
129 spawned = ctx.lamp.ledCount;
130 isComplete = true;
131 }
132
134 void spawn_pending(auto& ctx)
135 {
136 const uint16_t total = ctx.lamp.ledCount;
137 const uint16_t wanted = static_cast<uint16_t>(estimated_progress(ctx) * total + 0.5f);
138
139 // limit the spawns per frame: no burst, particles are spread over time
140 for (uint8_t i = 0; i < maxSpawnPerLoop and spawned < wanted; i++)
141 {
142 if (not spawn_one(ctx))
143 break; // no free slot or no free target: retry next frame
144 }
145 }
146
147 bool spawn_one(auto& ctx)
148 {
149 if (particuleSystem.get_number_of_active() >= MaxParticles)
150 return false;
151
152 uint32_t targetId;
153 if (not pick_target(ctx, targetId))
154 return false;
155
156 // spawn at the top of the lamp, same angle as the target
157 const auto helix = modes::strip_to_helix_unconstraint(static_cast<int16_t>(targetId));
158 const float theta = atan2(helix.y, helix.x);
159 const int16_t spawnIndex = modes::to_led_index_no_bounds(theta, 0.0f);
160 const float targetZ = helix.z;
161 const uint32_t color = ctx.lamp.getPixelColor(targetId);
162
163 const bool notAllCreated = particuleSystem.init_deferred_particules(
164 1,
165 [this, targetId, targetZ, spawnIndex](size_t slot) -> int16_t {
166 this->targets[slot] = targetId;
167 this->targetsZ[slot] = targetZ;
168 return spawnIndex;
169 },
170 [color](size_t) {
171 return color;
172 });
173 if (notAllCreated)
174 return false; // no free slot
175
176 reserved.set(targetId);
177 spawned += 1;
178 return true;
179 }
180
182 bool is_candidate(auto& ctx, uint16_t x, int16_t y)
183 {
184 const auto id = modes::to_strip(x, y);
185 if (id < 0 or id >= ctx.lamp.ledCount)
186 return false;
187 return maskedLeds[id] and not reserved[id];
188 }
189
191 bool pick_target(auto& ctx, uint32_t& targetId)
192 {
193 for (int16_t y = ctx.lamp.maxHeight; y >= 0; y--)
194 {
195 uint16_t count = 0;
196 for (uint16_t x = 0; x <= ctx.lamp.maxWidth; x++)
197 {
198 if (is_candidate(ctx, x, y))
199 count += 1;
200 }
201 if (count == 0)
202 continue;
203
204 uint16_t selected = static_cast<uint16_t>(rand() % count);
205 for (uint16_t x = 0; x <= ctx.lamp.maxWidth; x++)
206 {
207 if (not is_candidate(ctx, x, y))
208 continue;
209 if (selected == 0)
210 {
211 targetId = static_cast<uint32_t>(modes::to_strip(x, y));
212 return true;
213 }
214 selected -= 1;
215 }
216 }
217 return false; // every pixel is revealed or targeted
218 }
219
221 bool land_if_arrived(auto& ctx, const size_t slot, const Particle& p)
222 {
223 // z goes from 0 (top) to -lampHeight (bottom)
224 if (p.z_mm > targetsZ[slot])
225 return false;
226
227 const uint32_t target = targets[slot];
228 maskedLeds.reset(target); // visible
229 reserved.reset(target);
230 return true;
231 }
232
233private:
234 static constexpr float fallLeadMs = 300.0f;
235 static constexpr uint8_t maxSpawnPerLoop = 2;
236
237 float latestProgress = -1.0f;
238 uint32_t latestProgressTime = 0;
239 float progressPerMs = 0.0f;
240 size_t spawned = 0;
241 bool isComplete = false;
242
243 std::array<uint32_t, MaxParticles> targets {};
244 std::array<float, MaxParticles> targetsZ {};
245 etl::bitset<LampTy::ledCount> reserved;
246 etl::bitset<LampTy::ledCount> maskedLeds;
247
249};
250
251} // namespace fadein
252} // namespace lampda::modes::anims
253
254#endif
Define a particle system ALL PARTICLE SYSTEM SHARE THE SAME PARTICLE SUBSET.
Definition: particle_system.hpp:30
static constexpr uint16_t to_strip(uint16_t, uint16_t)
convert grid coordinates to strip index
Definition: lamp_type.hpp:1224
static constexpr int16_t to_led_index_no_bounds(const float angle_rad, const float z)
convert a lamp coordinate to a led index, the result can be an index out of the lamp body
Definition: lamp_type.hpp:1370
static constexpr HelixXYZTy strip_to_helix_unconstraint(const int16_t n)
convert strip index to 3D coordinates, without checks on led index. This allows to use 3D coordinates...
Definition: lamp_type.hpp:1302
Define a particle system, with particle based logic.
Define a particle in 3D space. it has a position and speed.
Definition: particle.hpp:44
float z_mm
height, in millimeters.
Definition: particle.hpp:267
Reveal an animation with falling pixels.
Definition: fadein.hpp:27
bool land_if_arrived(auto &ctx, const size_t slot, const Particle &p)
Unmask the target and remove the particle once it has reached the target height.
Definition: fadein.hpp:221
void spawn_pending(auto &ctx)
Spawn the particles needed to follow the estimated progress.
Definition: fadein.hpp:134
void update_progress_rate(auto &ctx, const float expectedProgress)
Update the drop rate.
Definition: fadein.hpp:83
bool pick_target(auto &ctx, uint32_t &targetId)
Pick a random target in the lowest line that still has free pixels.
Definition: fadein.hpp:191
bool is_candidate(auto &ctx, uint16_t x, int16_t y)
A masked pixel not already targeted by another particle.
Definition: fadein.hpp:182
float estimated_progress(auto &ctx) const
Progress estimated "now", including the fall duration so that pixels land on time.
Definition: fadein.hpp:115
3d vector in any space
Definition: vector_math.h:54