Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
gravity_mode.hpp
Go to the documentation of this file.
1#ifndef GRAVITY_MODE_H
2#define GRAVITY_MODE_H
3
5
7
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12
14
18struct GravityMode : public BasicMode
19{
21 static constexpr bool hasCustomRamp = true;
23 static constexpr uint16_t particleCount = 255;
24
25 static void on_enter_mode(auto& ctx)
26 {
27 auto& state = ctx.state;
28
29 state.imuEvent.reset(ctx);
30 // set particle count
31 state.imuEvent.particuleSystem.set_max_particle_count(particleCount);
32 state.imuEvent.particuleSystem.init_particules(generate_random_particule_position);
33
34 state.persistance = 210;
35
36 // set default value
37 custom_ramp_update(ctx, ctx.get_active_custom_ramp());
38 }
39
40 static uint8_t get_custom_ramp_default_value(auto& ctx)
41 {
42 // start with aurora palette
43 return 0;
44 }
45
47 static void custom_ramp_update(auto& ctx, uint8_t rampValue)
48 {
49 auto& state = ctx.state;
50
51 const uint8_t rampIndex =
52 std::min<uint8_t>(floorf(rampValue / 255.0f * state.maxPalettesCount), state.maxPalettesCount - 1.0f);
53
54 state.selectedPalette = state._palettes[rampIndex];
55 }
56
57 static void loop(auto& ctx)
58 {
59 auto& state = ctx.state;
60 if (!state.selectedPalette)
61 return;
62
63 const uint8_t paletteWrap = ctx.lamp.tick % UINT8_MAX;
64
65 state.imuEvent.update(ctx);
66
67 auto& particleSystem = state.imuEvent.particuleSystem;
68
69 particleSystem.iterate_with_collisions(state.imuEvent.lastReading.accel, ctx.lamp.frameDurationMs / 1000.0f);
70
71 ctx.lamp.fadeToBlackBy(255 - ctx.state.persistance);
72
73 auto colorFunction = [&](int16_t n, const Particle& particle) {
74 const auto wrapIndex = (n + paletteWrap) % particleCount;
75 return colors::from_palette(static_cast<uint8_t>(wrapIndex / static_cast<float>(particleCount) * UINT8_MAX),
76 *state.selectedPalette);
77 };
78 particleSystem.show(colorFunction, ctx.lamp);
79 }
80
81 struct StateTy
82 {
84 uint8_t persistance;
87
89 static constexpr uint8_t maxPalettesCount = 3;
93
96 };
97
98private:
100
102 static int16_t generate_random_particule_position(size_t) { return random16(LampTy::ledCount); }
104 static int16_t generate_particule_at_top_random_position(size_t)
105 {
106 return -static_cast<float>(random16(2.0 * LampTy::maxWidth));
107 }
109 static int16_t generate_particule_at_bottom_random_position(size_t)
110 {
111 return LampTy::ledCount + static_cast<float>(random16(2.0 * LampTy::maxWidth));
112 }
113
115 static int16_t generate_particule_at_extremes(size_t i)
116 {
117 return (i % 2 == 0) ? generate_particule_at_top_random_position(i) :
118 generate_particule_at_bottom_random_position(i);
119 }
120
122 static bool recycle_particules_if_too_far(const Particle& p)
123 {
124 return p.z_mm > LampTy::maxWidth * 3 or p.z_mm < -(LampTy::lampHeight_mm + LampTy::maxWidth * 3);
125 }
126};
127
128} // namespace lampda::modes::default_modes
129
130#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
static constexpr PaletteTy PaletteOceanColors
Ocean colors, blues and whites.
Definition: palettes.hpp:306
std::array< uint32_t, 16 > PaletteTy
Palette types.
Definition: palettes.hpp:18
static constexpr PaletteTy PaletteAuroraColors
Palette of green yellow colors.
Definition: palettes.hpp:432
static constexpr PaletteTy PaletteForestColors
Forest colors, greens.
Definition: palettes.hpp:342
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 persistance
make particles leave a trace
Definition: gravity_mode.hpp:84
const colors::PaletteTy * _palettes[maxPalettesCount]
store references to palettes
Definition: gravity_mode.hpp:91
imu::ImuEventTy imuEvent
handle IMU events
Definition: gravity_mode.hpp:86
static constexpr uint8_t maxPalettesCount
Usable palettes count.
Definition: gravity_mode.hpp:89
colors::PaletteTy const * selectedPalette
store selected palette
Definition: gravity_mode.hpp:95
Sand particle simulation, synched with the board IMU.
Definition: gravity_mode.hpp:19
static constexpr uint16_t particleCount
set the maximum particle count
Definition: gravity_mode.hpp:23
static void custom_ramp_update(auto &ctx, uint8_t rampValue)
User ramp changes the color palette.
Definition: gravity_mode.hpp:47
static constexpr bool hasCustomRamp
hint manager to save our custom ramp
Definition: gravity_mode.hpp:21
Main interface between the user and the hardware of the lamp.
Definition: lamp_type.hpp:118
static constexpr float lampHeight_mm
Computation of the lamp height.
Definition: lamp_type.hpp:441
static constexpr uint16_t maxWidth
(indexable) Width of "pixel space" w/ lamp taken as a LED matrix
Definition: lamp_type.hpp:370
static constexpr uint16_t ledCount
(indexable) Count of indexable LEDs on the lamp
Definition: lamp_type.hpp:357
Definition: utils.hpp:20