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;
25 static constexpr float lightRainDropsPerSecond = 1;
27 static constexpr float heavyRainDropsPerSecond = 700;
28
29 static void on_enter_mode(auto& ctx)
30 {
31 auto& state = ctx.state;
32
33 state.imuEvent.reset(ctx);
34 // set particle count
35 state.imuEvent.particuleSystem.set_max_particle_count(512);
36
37 // ramp saturates
38 ctx.template set_config_bool<ConfigKeys::rampSaturates>(true);
39
40 state.rainDropSpawn = 0.0;
41 state.persistance = 100;
42
43 // set default value
44 custom_ramp_update(ctx, ctx.get_active_custom_ramp());
45 }
46
48 static void custom_ramp_update(auto& ctx, uint8_t rampValue)
49 {
50 ctx.state.rainDensityCommand = rampValue;
51 ctx.state.rainDensity = ctx.state.rainDensityCommand;
52 }
53
55 static void sunset_update(auto& ctx, float progress)
56 {
57 // progress goes from 0 to 1
58 // lamp turns off when 1 is reached
59
60 // rain will turn off slowly
61 ctx.state.rainDensity = ctx.state.rainDensityCommand * (1.0 - progress);
62 }
63
64 static void loop(auto& ctx)
65 {
66 auto& state = ctx.state;
67 state.imuEvent.update(ctx);
68
69 auto& particleSystem = state.imuEvent.particuleSystem;
70
71 // multiply by 2 because we rain on both sides of the lamp, half of the drops are not seen
72 const float expectedDropPerSecond =
73 ctx.state.rainDensity / 255.0 * heavyRainDropsPerSecond + lightRainDropsPerSecond;
74 ctx.state.rainDropSpawn += expectedDropPerSecond * ctx.lamp.frameDurationMs / 1000.0;
75
76 if (ctx.state.rainDropSpawn > 1.0)
77 {
78 // initialize particules in a deffered way, when free spots are available
79 particleSystem.init_deferred_particules(2 * ctx.state.rainDropSpawn, generate_particule_at_extremes);
80 ctx.state.rainDropSpawn = 0.0;
81 }
82
83 // no collisions between particles, and with no lamp limits
84 static constexpr bool shouldKeepInLampBounds = false;
85 particleSystem.iterate_no_collisions(
86 state.imuEvent.lastReading.accel, ctx.lamp.frameDurationMs / 1000.0, shouldKeepInLampBounds);
87 // depop particules that fell too far
88 const uint16_t activeParticles = particleSystem.depop_particules(recycle_particules_if_too_far);
89
90 ctx.lamp.fadeToBlackBy(255 - ctx.state.persistance);
91
92 // break palette size to display more colors per drops
93 auto colorFunction = [&](int16_t n, const Particle& particle) {
94 const uint16_t wrapIndex = (n * 5) / static_cast<float>(activeParticles) * 255;
95 return colors::from_palette(static_cast<uint8_t>(wrapIndex % 255), ctx.state.palette);
96 };
97 particleSystem.show(colorFunction, ctx.lamp);
98 }
99
100 struct StateTy
101 {
104
107
109 uint8_t rainDensity;
111 uint8_t persistance;
114
117 };
118
119private:
120 using LampTy = hardware::LampTy;
121
123 static int16_t generate_random_particule_position(size_t) { return random16(LampTy::ledCount); }
125 static int16_t generate_particule_at_top_random_position(size_t)
126 {
127 return -static_cast<float>(random16(2.0 * LampTy::maxWidth));
128 }
130 static int16_t generate_particule_at_bottom_random_position(size_t)
131 {
132 return LampTy::ledCount + static_cast<float>(random16(2.0 * LampTy::maxWidth));
133 }
134
136 static int16_t generate_particule_at_extremes(size_t i)
137 {
138 return (i % 2 == 0) ? generate_particule_at_top_random_position(i) :
139 generate_particule_at_bottom_random_position(i);
140 }
141
143 static bool recycle_particules_if_too_far(const Particle& p)
144 {
145 return p.z_mm > LampTy::maxWidth * 3 or p.z_mm < -(LampTy::lampHeight_mm + LampTy::maxWidth * 3);
146 }
147};
148
149} // namespace lampda::modes::default_modes
150
151#endif // BEATSYNC_MODE_H
User mode IMU utilities.
static constexpr uint32_t from_palette(UIntTy index, const PaletteTy &palette, 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
Define a particle in 3D space. it has a position and speed.
Definition: particle.hpp:37
uint8_t rainDensity
effective rain density
Definition: rain_mode.hpp:109
const colors::PaletteTy & palette
color palette to display
Definition: rain_mode.hpp:116
uint8_t rainDensityCommand
save the requested rain density by the user
Definition: rain_mode.hpp:106
imu::ImuEventTy imuEvent
track imu events
Definition: rain_mode.hpp:103
uint8_t persistance
trails of drops
Definition: rain_mode.hpp:111
float rainDropSpawn
rate of rain drops, per frame
Definition: rain_mode.hpp:113
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 sunset_update(auto &ctx, float progress)
Sunset timer will fade out the rain to zero.
Definition: rain_mode.hpp:55
static constexpr bool hasCustomRamp
hint manager to save our custom ramp
Definition: rain_mode.hpp:21
static constexpr bool hasSunsetAnimation
sunset 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:48
static constexpr float heavyRainDropsPerSecond
average drops per seconds for heavy rain
Definition: rain_mode.hpp:27
static constexpr float lightRainDropsPerSecond
average drops per seconds for a light rain
Definition: rain_mode.hpp:25
Main interface between the user and the hardware of the lamp.
Definition: lamp_type.hpp:114
static constexpr float lampHeight_mm
Computation of the lamp height.
Definition: lamp_type.hpp:437
static constexpr uint16_t maxWidth
(indexable) Width of "pixel space" w/ lamp taken as a LED matrix
Definition: lamp_type.hpp:366
static constexpr uint16_t ledCount
(indexable) Count of indexable LEDs on the lamp
Definition: lamp_type.hpp:353
Definition: utils.hpp:19