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
41 static void custom_ramp_update(auto& ctx, uint8_t rampValue)
42 {
43 auto& state = ctx.state;
44
45 const uint8_t rampIndex =
46 std::min<uint8_t>(floorf(rampValue / 255.0f * state.maxPalettesCount), state.maxPalettesCount - 1.0f);
47
48 state.selectedPalette = state._palettes[rampIndex];
49 }
50
51 static void loop(auto& ctx)
52 {
53 auto& state = ctx.state;
54 if (!state.selectedPalette)
55 return;
56
57 const uint8_t paletteWrap = ctx.lamp.tick % UINT8_MAX;
58
59 state.imuEvent.update(ctx);
60
61 auto& particleSystem = state.imuEvent.particuleSystem;
62
63 particleSystem.iterate_with_collisions(state.imuEvent.lastReading.accel, ctx.lamp.frameDurationMs / 1000.0);
64
65 ctx.lamp.fadeToBlackBy(255 - ctx.state.persistance);
66
67 auto colorFunction = [&](int16_t n, const Particle& particle) {
68 const auto wrapIndex = (n + paletteWrap) % particleCount;
69 return colors::from_palette(static_cast<uint8_t>(wrapIndex / static_cast<float>(particleCount) * UINT8_MAX),
70 *state.selectedPalette);
71 };
72 particleSystem.show(colorFunction, ctx.lamp);
73 }
74
75 struct StateTy
76 {
78 uint8_t persistance;
81
83 static constexpr uint8_t maxPalettesCount = 3;
87
90 };
91
92private:
94
96 static int16_t generate_random_particule_position(size_t) { return random16(LampTy::ledCount); }
98 static int16_t generate_particule_at_top_random_position(size_t)
99 {
100 return -static_cast<float>(random16(2.0 * LampTy::maxWidth));
101 }
103 static int16_t generate_particule_at_bottom_random_position(size_t)
104 {
105 return LampTy::ledCount + static_cast<float>(random16(2.0 * LampTy::maxWidth));
106 }
107
109 static int16_t generate_particule_at_extremes(size_t i)
110 {
111 return (i % 2 == 0) ? generate_particule_at_top_random_position(i) :
112 generate_particule_at_bottom_random_position(i);
113 }
114
116 static bool recycle_particules_if_too_far(const Particle& p)
117 {
118 return p.z_mm > LampTy::maxWidth * 3 or p.z_mm < -(LampTy::lampHeight_mm + LampTy::maxWidth * 3);
119 }
120};
121
122} // namespace lampda::modes::default_modes
123
124#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
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:78
const colors::PaletteTy * _palettes[maxPalettesCount]
store references to palettes
Definition: gravity_mode.hpp:85
imu::ImuEventTy imuEvent
handle IMU events
Definition: gravity_mode.hpp:80
static constexpr uint8_t maxPalettesCount
Usable palettes count.
Definition: gravity_mode.hpp:83
colors::PaletteTy const * selectedPalette
store selected palette
Definition: gravity_mode.hpp:89
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:41
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: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