5#ifndef MODES_ANIM_FADEIN_HPP
6#define MODES_ANIM_FADEIN_HPP
10#include <etl/bitset.h>
14namespace lampda::modes::anims {
32 particuleSystem.reset();
33 particuleSystem.set_max_particle_count(MaxParticles);
38 latestProgress = -1.0f;
39 latestProgressTime = 0;
53 for (uint16_t i = 0; i < maskedLeds.size(); i++)
56 ctx.lamp.mask_led_at_index(i,
true);
59 if (particuleSystem.get_number_of_active() > 0)
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);
66 const uint16_t left = particuleSystem.depop_particules_indexed([&](
size_t slot,
const Particle& p) {
71 static constexpr bool forceDisplayParticles =
true;
72 particuleSystem.show(ctx.lamp, forceDisplayParticles);
74 if (left == 0 and spawned >= ctx.lamp.ledCount)
88 const float progress = lmpd_constrain<float>(expectedProgress, 0.0f, 1.0f);
97 if (progress == latestProgress)
100 if (latestProgress >= 0.0f and ctx.lamp.now > latestProgressTime)
102 const float elapsedMs =
static_cast<float>(ctx.lamp.now - latestProgressTime);
103 progressPerMs = lmpd_constrain<float>((progress - latestProgress) / elapsedMs, 0.0f, 1.0f);
107 progressPerMs = 0.0f;
109 latestProgress = progress;
110 latestProgressTime = ctx.lamp.now;
117 if (latestProgress < 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);
123 void complete(
auto& ctx)
127 particuleSystem.reset();
129 spawned = ctx.lamp.ledCount;
136 const uint16_t total = ctx.lamp.ledCount;
137 const uint16_t wanted =
static_cast<uint16_t
>(
estimated_progress(ctx) * total + 0.5f);
140 for (uint8_t i = 0; i < maxSpawnPerLoop and spawned < wanted; i++)
142 if (not spawn_one(ctx))
147 bool spawn_one(
auto& ctx)
149 if (particuleSystem.get_number_of_active() >= MaxParticles)
158 const float theta = atan2(helix.y, helix.x);
160 const float targetZ = helix.z;
161 const uint32_t color = ctx.lamp.getPixelColor(targetId);
163 const bool notAllCreated = particuleSystem.init_deferred_particules(
165 [
this, targetId, targetZ, spawnIndex](
size_t slot) -> int16_t {
166 this->targets[slot] = targetId;
167 this->targetsZ[slot] = targetZ;
176 reserved.set(targetId);
185 if (id < 0 or id >= ctx.lamp.ledCount)
187 return maskedLeds[id] and not reserved[id];
193 for (int16_t y = ctx.lamp.maxHeight; y >= 0; y--)
196 for (uint16_t x = 0; x <= ctx.lamp.maxWidth; x++)
204 uint16_t selected =
static_cast<uint16_t
>(rand() % count);
205 for (uint16_t x = 0; x <= ctx.lamp.maxWidth; x++)
224 if (p.
z_mm > targetsZ[slot])
227 const uint32_t target = targets[slot];
228 maskedLeds.reset(target);
229 reserved.reset(target);
234 static constexpr float fallLeadMs = 300.0f;
235 static constexpr uint8_t maxSpawnPerLoop = 2;
237 float latestProgress = -1.0f;
238 uint32_t latestProgressTime = 0;
239 float progressPerMs = 0.0f;
241 bool isComplete =
false;
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;
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