Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
ping_pong_mode.hpp
Go to the documentation of this file.
1#ifndef PING_PONG_MODE_HPP
2#define PING_PONG_MODE_HPP
3
5
7
8#include <cstdint>
9
14{
16 static constexpr float randomVariation = 0.3;
18 static constexpr uint32_t animationTiming = 1000;
19
20 struct StateTy
21 {
23 size_t lastIndex;
25 float progress;
27 bool step;
29 uint32_t color;
31 uint8_t persistance;
32 };
33
34 static void on_enter_mode(auto& ctx)
35 {
36 ctx.state.lastIndex = 0;
37 ctx.state.persistance = 128;
38 ctx.state.progress = 0.0;
39 ctx.state.step = true;
40 ctx.state.color = utils::get_random_complementary_color(ctx.state.color, randomVariation);
41 }
42
43 static void loop(auto& ctx)
44 {
45 static constexpr float iteration = ctx.lamp.frameDurationMs / static_cast<float>(animationTiming / 2.0f);
46 static constexpr auto maxLedIndex = ctx.lamp.ledCount - 1;
47 ctx.state.progress += iteration;
48
49 const float prog = std::min<float>(ctx.state.progress, 1.0);
50
51 // fade leds
52 ctx.lamp.fadeToBlackBy(255 - ctx.state.persistance);
53
54 // display new ramps
55 if (ctx.state.step)
56 {
57 const size_t newIndex = prog * maxLedIndex;
58 for (size_t i = ctx.state.lastIndex; i < newIndex; i++)
59 ctx.lamp.setPixelColor(i, ctx.state.color);
60 ctx.state.lastIndex = newIndex;
61 }
62 else
63 {
64 const size_t newIndex = std::max<float>(0.0, (1.0f - prog) * maxLedIndex);
65 for (size_t i = newIndex; i < ctx.state.lastIndex; i++)
66 ctx.lamp.setPixelColor(i, ctx.state.color);
67 ctx.state.lastIndex = newIndex;
68 }
69
70 // end of cycle
71 if (ctx.state.progress >= 1.0)
72 {
73 ctx.state.progress = 0.0;
74 ctx.state.step = not ctx.state.step;
75
76 if (ctx.state.step)
77 ctx.state.color = utils::get_random_complementary_color(ctx.state.color, randomVariation);
78 }
79 }
80};
81
82} // namespace lampda::modes::default_modes
83
84#endif
Basic "default" modes included with the hardware.
Definition: aurora.hpp:12
uint32_t get_random_complementary_color(const uint32_t color, const float tolerance)
Compute the complementary color of the given color, with a random variation.
Definition: utils.cpp:47
Parent object for all custom user modes.
Definition: mode_type.hpp:53
float progress
progress between 0 and 1
Definition: ping_pong_mode.hpp:25
uint8_t persistance
make the dot leave a trail
Definition: ping_pong_mode.hpp:31
uint32_t color
color of the dot
Definition: ping_pong_mode.hpp:29
size_t lastIndex
actual dot index
Definition: ping_pong_mode.hpp:23
bool step
back of forth
Definition: ping_pong_mode.hpp:27
Ping pong of a light dot between the lampe sides.
Definition: ping_pong_mode.hpp:14
static constexpr uint32_t animationTiming
back and forth timing in milliseconds
Definition: ping_pong_mode.hpp:18
static constexpr float randomVariation
color random variation
Definition: ping_pong_mode.hpp:16