Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
rain_mode.hpp
Go to the documentation of this file.
1#ifndef RAIN_MODE_H
2#define RAIN_MODE_H
3
5
7
9#include <cstddef>
10#include <cstdint>
11
13
18struct RainMode : public BasicMode
19{
21 static constexpr bool hasCustomRamp = true;
23 static constexpr bool hasSunsetAnimation = true;
24 static constexpr bool hasSunriseAnimation = true;
26 static constexpr float lightRainDropsPerSecond = 1;
28 static constexpr float heavyRainDropsPerSecond = 700;
29
30 static void on_enter_mode(auto& ctx)
31 {
32 auto& state = ctx.state;
33
34 state.imuEvent.reset(ctx);
35 // set particle count
36 state.imuEvent.particuleSystem.set_max_particle_count(512);
37
38 // ramp saturates
39 ctx.template set_config_bool<ConfigKeys::rampSaturates>(true);
40
41 state.rainDropSpawn = 0.0f;
42 state.persistance = 100;
43
44 // set default value
45 custom_ramp_update(ctx, ctx.get_active_custom_ramp());
46 }
47
49 static void custom_ramp_update(auto& ctx, uint8_t rampValue)
50 {
51 ctx.state.rainDensityCommand = rampValue;
52 ctx.state.rainDensity = ctx.state.rainDensityCommand;
53 }
54
56 static void sunset_update(auto& ctx, float progress)
57 {
58 // progress goes from 0 to 1
59 // lamp turns off when 1 is reached
60
61 // rain will turn off slowly
62 ctx.state.rainDensity = ctx.state.rainDensityCommand * (1.0 - progress);
63 }
64
66 static void sunrise_update(auto& ctx, float progress)
67 {
68 // progress goes from 0 to 1
69 // rain will turn on slowly
70 ctx.state.rainDensity = ctx.state.rainDensityCommand * progress;
71 }
72
73 static void loop(auto& ctx)
74 {
75 auto& state = ctx.state;
76 state.imuEvent.update(ctx);
77
78 auto& particleSystem = state.imuEvent.particuleSystem;
79
80 // multiply by 2 because we rain on both sides of the lamp, half of the drops are not seen
81 const float expectedDropPerSecond =
82 ctx.state.rainDensity / 255.0 * heavyRainDropsPerSecond + lightRainDropsPerSecond;
83 ctx.state.rainDropSpawn += expectedDropPerSecond * ctx.lamp.frameDurationMs / 1000.0;
84
85 if (ctx.state.rainDropSpawn > 1.0)
86 { // break palette size to display more colors per drops
87 const uint8_t particlesToSpawn = 2 * ctx.state.rainDropSpawn;
88 const auto activeParticles = particleSystem.get_number_of_active() + particlesToSpawn;
89 auto colorFunction = [&](size_t n) {
90 const uint16_t wrapIndex = (activeParticles <= 0) ? 0 : (n * 5) / static_cast<float>(activeParticles) * 255;
91 return colors::from_palette(static_cast<uint8_t>(wrapIndex % 255), ctx.state.palette);
92 };
93
94 // initialize particules in a deffered way, when free spots are available
95 particleSystem.init_deferred_particules(particlesToSpawn, generate_particule_at_extremes, colorFunction);
96 ctx.state.rainDropSpawn = 0.0;
97 }
98
99 // no collisions between particles, and with no lamp limits
100 static constexpr bool shouldKeepInLampBounds = false;
101 particleSystem.iterate_no_collisions(
102 state.imuEvent.lastReading.accel, ctx.lamp.frameDurationMs / 1000.0, shouldKeepInLampBounds);
103 // depop particules that fell too far
104 particleSystem.depop_particules(recycle_particules_if_too_far);
105
106 ctx.lamp.fadeToBlackBy(255 - ctx.state.persistance);
107
108 particleSystem.show(ctx.lamp);
109 }
110
111 struct StateTy
112 {
115
118
120 uint8_t rainDensity;
122 uint8_t persistance;
125
128 };
129
130private:
131 using LampTy = hardware::LampTy;
132
134 static int16_t generate_random_particule_position(size_t) { return random16(LampTy::ledCount); }
136 static int16_t generate_particule_at_top_random_position(size_t)
137 {
138 return -static_cast<float>(random16(2.0 * LampTy::maxWidth));
139 }
141 static int16_t generate_particule_at_bottom_random_position(size_t)
142 {
143 return LampTy::ledCount + static_cast<float>(random16(2.0 * LampTy::maxWidth));
144 }
145
147 static int16_t generate_particule_at_extremes(size_t i)
148 {
149 return (i % 2 == 0) ? generate_particule_at_top_random_position(i) :
150 generate_particule_at_bottom_random_position(i);
151 }
152
154 static bool recycle_particules_if_too_far(const Particle& p)
155 {
156 return p.z_mm > LampTy::maxWidth * 3 or p.z_mm < -(LampTy::lampHeight_mm + LampTy::maxWidth * 3);
157 }
158};
159
160} // namespace lampda::modes::default_modes
161
162#endif // BEATSYNC_MODE_H
User mode IMU utilities.
static constexpr uint32_t from_palette(const UIntTy index, const PaletteTy &palette, const uint8_t brightness=255)
Return a color from a palette.
Definition: palettes.hpp:504
std::array< uint32_t, 16 > PaletteTy
Palette types.
Definition: palettes.hpp:18
static constexpr PaletteTy PaletteWaterColors
Water colors, blues.
Definition: palettes.hpp:324
Basic "default" modes included with the hardware.
Definition: aurora.hpp:12
Define some useful color palettes, and tools to use them.
Parent object for all custom user modes.
Definition: mode_type.hpp:53
uint8_t rainDensity
effective rain density
Definition: rain_mode.hpp:120
const colors::PaletteTy & palette
color palette to display
Definition: rain_mode.hpp:127
uint8_t rainDensityCommand
save the requested rain density by the user
Definition: rain_mode.hpp:117
imu::ImuEventTy imuEvent
track imu events
Definition: rain_mode.hpp:114
uint8_t persistance
trails of drops
Definition: rain_mode.hpp:122
float rainDropSpawn
rate of rain drops, per frame
Definition: rain_mode.hpp:124
Emulate falling rain using the IMU. Interaction with the susnet timer to fade out the rain and make i...
Definition: rain_mode.hpp:19
static void sunrise_update(auto &ctx, float progress)
Sunrise timer will fade in the rain from 0 to saved limit.
Definition: rain_mode.hpp:66
static void sunset_update(auto &ctx, float progress)
Sunset timer will fade out the rain to zero.
Definition: rain_mode.hpp:56
static constexpr bool hasCustomRamp
hint manager to save our custom ramp
Definition: rain_mode.hpp:21
static constexpr bool hasSunsetAnimation
sunset & sunrise animation on the rain
Definition: rain_mode.hpp:23
static void custom_ramp_update(auto &ctx, uint8_t rampValue)
Custom ramp controls the rain density.
Definition: rain_mode.hpp:49
static constexpr float heavyRainDropsPerSecond
average drops per seconds for heavy rain
Definition: rain_mode.hpp:28
static constexpr float lightRainDropsPerSecond
average drops per seconds for a light rain
Definition: rain_mode.hpp:26
Main interface between the user and the hardware of the lamp.
Definition: lamp_type.hpp:120
static constexpr float lampHeight_mm
Computation of the lamp height.
Definition: lamp_type.hpp:447
static constexpr uint16_t maxWidth
(indexable) Width of "pixel space" w/ lamp taken as a LED matrix
Definition: lamp_type.hpp:376
static constexpr uint16_t ledCount
(indexable) Count of indexable LEDs on the lamp
Definition: lamp_type.hpp:363
Definition: utils.hpp:20