Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
control_fixed_modes.hpp
Go to the documentation of this file.
1#ifndef MODES_BLUETOOTH_CONTROLFIXEDMODES_H
2#define MODES_BLUETOOTH_CONTROLFIXEDMODES_H
3
5
6#include "src/system/ext/math8.h"
7#include "src/system/ext/noise.h"
8
12
13#include <cstdint>
14
15namespace lampda::modes {
17namespace bluetooth {
18
20{
22 static constexpr bool hasSunsetAnimation = true;
23
24 static constexpr uint8_t dissolveBufferId = 0;
25
26 struct StateTy
27 {
30 };
31
32 static void on_enter_mode(auto& ctx) { ctx.state.sunsetAnimation.reset(ctx); }
33
34 static void loop(auto& ctx)
35 {
36 // Decompress the color
37 const uint8_t customRamp = ctx.get_active_custom_ramp();
38 const uint8_t customIndex = ctx.get_active_custom_index();
39 const uint16_t compressedColor = customRamp << 8 | customIndex;
40 const uint32_t color = colors::get_decompressed_32b_color(compressedColor);
41
42 ctx.lamp.fill(color, ctx.lamp.template getTempBuffer<dissolveBufferId>());
43
44 // update animation
45 ctx.state.sunsetAnimation.loop(ctx, color);
46 }
47
49 static void sunset_update(auto& ctx, float progress) { ctx.state.sunsetAnimation.update_depop_rate(ctx, progress); }
50};
51
52namespace __private {
53
54uint32_t get_color_at(size_t index)
55{
56 std::ignore = index;
57 assert(false and "Invalid color index reached");
58 return 0;
59}
60
61template<typename Head, typename... Tail> uint32_t get_color_at(size_t index, Head head, Tail... tail)
62{
63 if (index == 0)
64 return head;
65 return get_color_at(index - 1, tail...);
66}
67} // namespace __private
68
69template<bool fastMode, uint8_t fadeRateUint, uint32_t... fadeColors> struct ColorSequenceMode : public BasicMode
70{
71 static constexpr float fadeRate = fadeRateUint / (2.0 * UINT8_MAX); // limit to range 0-0.5
72 static constexpr size_t colorCount = sizeof...(fadeColors);
73 static_assert((fadeRate == 0 and colorCount != 0) or (fadeRate > 0 and colorCount >= 2),
74 "Cannot fade colors without at least two colors");
75
77 static constexpr bool hasCustomRamp = true;
79 static constexpr bool hasSunsetAnimation = true;
80
81 static constexpr uint8_t dissolveBufferId = 0;
82
83 struct StateTy
84 {
85 uint32_t colorStartTime_ms;
86 uint8_t colorIndex;
87
90 };
91
92 static uint32_t next_color_index(auto& ctx)
93 {
94 const uint32_t nextIndex = ctx.state.colorIndex + 1;
95 if (nextIndex >= colorCount)
96 return 0;
97 return nextIndex;
98 }
99
100 static void on_enter_mode(auto& ctx)
101 {
102 ctx.template set_config_bool<ConfigKeys::rampSaturates>(true);
103
104 ctx.state.colorStartTime_ms = ctx.lamp.now;
105 ctx.state.colorIndex = 0;
106
107 ctx.state.sunsetAnimation.reset(ctx);
108 }
109
110 static void loop(auto& ctx)
111 {
112 // ramp controls the speed
113 static constexpr uint32_t lowestTiming = fastMode ? 100 : 1000;
114 static constexpr uint32_t highestTiming = fastMode ? 15 : 100;
115 // map ramp to period
116 const uint32_t wholePaletteLoopTiming =
117 lmpd_map<float>(ctx.get_active_custom_ramp(), 0, 255, lowestTiming, highestTiming);
118
119 uint32_t timeSinceStart = ctx.lamp.now - ctx.state.colorStartTime_ms;
120 if (timeSinceStart >= wholePaletteLoopTiming)
121 {
122 ctx.state.colorStartTime_ms = ctx.lamp.now;
123 ctx.state.colorIndex = next_color_index(ctx);
124 // Update time to prevent flashes
125 timeSinceStart = ctx.lamp.now - ctx.state.colorStartTime_ms;
126 }
127
128 uint32_t color = __private::get_color_at(ctx.state.colorIndex, fadeColors...);
129
130 if constexpr (fadeRateUint != 0)
131 {
132 const uint32_t fixedTime_ms = wholePaletteLoopTiming * (1.0 - fadeRate);
133 if (timeSinceStart >= fixedTime_ms)
134 {
135 const uint32_t fadeTime_ms = wholePaletteLoopTiming * fadeRate;
136 const float fadeProgress =
137 lmpd_constrain((timeSinceStart - fixedTime_ms) / static_cast<float>(fadeTime_ms), 0.0f, 0.95f);
138 const uint32_t colorEnd = __private::get_color_at(next_color_index(ctx), fadeColors...);
139 color = utils::get_gradient(color, colorEnd, fadeProgress);
140 }
141 }
142
143 ctx.lamp.fill(color, ctx.lamp.template getTempBuffer<dissolveBufferId>());
144
145 // update animation
146 ctx.state.sunsetAnimation.loop(ctx, color);
147 }
148
150 static void sunset_update(auto& ctx, float progress) { ctx.state.sunsetAnimation.update_depop_rate(ctx, progress); }
151};
152
154template<uint32_t color> struct FixedColorMode : public ColorSequenceMode<false, 0, color>
155{
156};
157
159template<uint32_t... Colors> struct JumpColorMode : public ColorSequenceMode<false, 0, Colors...>
160{
161};
162
163// fade mode
164template<uint8_t fadeRate, uint32_t... Colors> struct FadeColorMode :
165 public ColorSequenceMode<false, fadeRate, Colors...>
166{
167};
168
169// flash mode
170template<uint32_t... Colors> struct FlashColorMode : public ColorSequenceMode<true, 0, Colors...>
171{
172};
173
174} // namespace bluetooth
175} // namespace lampda::modes
176
177#endif
Manipulate color representations.
Define some animations to fade out modes.
Contains basic interface types to implement custom user modes.
Definition: control_fixed_modes.hpp:15
uint32_t get_gradient(const uint32_t colorStart, const uint32_t colorEnd, const float level)
Return the color gradient between colorStart to colorEnd.
Definition: utils.cpp:60
static constexpr T lmpd_constrain(const T &a, const T &mini, const T &maxi)
Constrain a number between two numbers.
Definition: utils.h:57
Define some useful color palettes, and tools to use them.
Parent object for all custom user modes.
Definition: mode_type.hpp:53
Make an animation disapear with gravity.
Definition: fadeout.hpp:196
Definition: control_fixed_modes.hpp:27
Definition: control_fixed_modes.hpp:20
static void sunset_update(auto &ctx, float progress)
Sunset timer will drop pixels downward, and never display them again.
Definition: control_fixed_modes.hpp:49
static constexpr bool hasSunsetAnimation
sunset animation on the fixed modes
Definition: control_fixed_modes.hpp:22
Definition: control_fixed_modes.hpp:84
Definition: control_fixed_modes.hpp:70
static constexpr bool hasSunsetAnimation
sunset animation on the fixed modes
Definition: control_fixed_modes.hpp:79
static constexpr bool hasCustomRamp
hint manager to save our custom ramp
Definition: control_fixed_modes.hpp:77
static void sunset_update(auto &ctx, float progress)
Sunset timer will drop pixels downward, and never display them again.
Definition: control_fixed_modes.hpp:150
Definition: control_fixed_modes.hpp:166
fixed modes
Definition: control_fixed_modes.hpp:155
Definition: control_fixed_modes.hpp:171
jump color
Definition: control_fixed_modes.hpp:160