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
11
12#include <cstdint>
13
14namespace lampda::modes {
16namespace bluetooth {
17
19{
21 static constexpr bool hasSunsetAnimation = true;
22 static constexpr bool hasSunriseAnimation = true;
23 // Use the two internal gravity modes, so no need to define the callbacks
24 static constexpr bool usesSunsetGravityDissolveMode = true;
25 static constexpr bool usesSunriseGravityAppearMode = true;
26
27 static void loop(auto& ctx)
28 {
29 // Decompress the color
30 const uint8_t customRamp = ctx.get_active_custom_ramp();
31 const uint8_t customIndex = ctx.get_active_custom_index();
32 const uint16_t compressedColor = customRamp << 8 | customIndex;
33 const uint32_t color = colors::get_decompressed_32b_color(compressedColor);
34
35 ctx.lamp.fill(color);
36 }
37};
38
39namespace __private {
40
41uint32_t get_color_at(size_t index)
42{
43 std::ignore = index;
44 assert(false and "Invalid color index reached");
45 return 0;
46}
47
48template<typename Head, typename... Tail> uint32_t get_color_at(size_t index, Head head, Tail... tail)
49{
50 if (index == 0)
51 return head;
52 return get_color_at(index - 1, tail...);
53}
54} // namespace __private
55
56template<bool fastMode, uint8_t fadeRateUint, uint32_t... fadeColors> struct ColorSequenceMode : public BasicMode
57{
58 static constexpr float fadeRate = fadeRateUint / (2.0 * UINT8_MAX); // limit to range 0-0.5
59 static constexpr size_t colorCount = sizeof...(fadeColors);
60 static_assert((fadeRate == 0 and colorCount != 0) or (fadeRate > 0 and colorCount >= 2),
61 "Cannot fade colors without at least two colors");
62
64 static constexpr bool hasCustomRamp = true;
66 static constexpr bool hasSunsetAnimation = true;
67 static constexpr bool hasSunriseAnimation = true;
68 // Use the two internal gravity modes, so no need to define the callbacks
69 static constexpr bool usesSunsetGravityDissolveMode = true;
70 static constexpr bool usesSunriseGravityAppearMode = true;
71
72 struct StateTy
73 {
74 uint32_t colorStartTime_ms;
75 uint8_t colorIndex;
76 };
77
78 static uint32_t next_color_index(auto& ctx)
79 {
80 const uint32_t nextIndex = ctx.state.colorIndex + 1;
81 if (nextIndex >= colorCount)
82 return 0;
83 return nextIndex;
84 }
85
86 static void on_enter_mode(auto& ctx)
87 {
88 ctx.template set_config_bool<ConfigKeys::rampSaturates>(true);
89
90 ctx.state.colorStartTime_ms = ctx.lamp.now;
91 ctx.state.colorIndex = 0;
92 }
93
94 static void loop(auto& ctx)
95 {
96 // ramp controls the speed
97 static constexpr uint32_t lowestTiming = fastMode ? 100 : 1000;
98 static constexpr uint32_t highestTiming = fastMode ? 15 : 100;
99 // map ramp to period
100 const uint32_t wholePaletteLoopTiming =
101 lmpd_map<float>(ctx.get_active_custom_ramp(), 0, 255, lowestTiming, highestTiming);
102
103 uint32_t timeSinceStart = ctx.lamp.now - ctx.state.colorStartTime_ms;
104 if (timeSinceStart >= wholePaletteLoopTiming)
105 {
106 ctx.state.colorStartTime_ms = ctx.lamp.now;
107 ctx.state.colorIndex = next_color_index(ctx);
108 // Update time to prevent flashes
109 timeSinceStart = ctx.lamp.now - ctx.state.colorStartTime_ms;
110 }
111
112 uint32_t color = __private::get_color_at(ctx.state.colorIndex, fadeColors...);
113
114 if constexpr (fadeRateUint != 0)
115 {
116 const uint32_t fixedTime_ms = wholePaletteLoopTiming * (1.0 - fadeRate);
117 if (timeSinceStart >= fixedTime_ms)
118 {
119 const uint32_t fadeTime_ms = wholePaletteLoopTiming * fadeRate;
120 const float fadeProgress =
121 lmpd_constrain((timeSinceStart - fixedTime_ms) / static_cast<float>(fadeTime_ms), 0.0f, 0.95f);
122 const uint32_t colorEnd = __private::get_color_at(next_color_index(ctx), fadeColors...);
123 color = utils::get_gradient(color, colorEnd, fadeProgress);
124 }
125 }
126
127 ctx.lamp.fill(color);
128 }
129};
130
132template<uint32_t color> struct FixedColorMode : public ColorSequenceMode<false, 0, color>
133{
134};
135
137template<uint32_t... Colors> struct JumpColorMode : public ColorSequenceMode<false, 0, Colors...>
138{
139};
140
141// fade mode
142template<uint8_t fadeRate, uint32_t... Colors> struct FadeColorMode :
143 public ColorSequenceMode<false, fadeRate, Colors...>
144{
145};
146
147// flash mode
148template<uint32_t... Colors> struct FlashColorMode : public ColorSequenceMode<true, 0, Colors...>
149{
150};
151
152} // namespace bluetooth
153} // namespace lampda::modes
154
155#endif
Manipulate color representations.
Contains basic interface types to implement custom user modes.
Definition: control_fixed_modes.hpp:14
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
Definition: control_fixed_modes.hpp:19
static constexpr bool hasSunsetAnimation
sunset & sunrise animation on the fixed modes
Definition: control_fixed_modes.hpp:21
Definition: control_fixed_modes.hpp:73
Definition: control_fixed_modes.hpp:57
static constexpr bool hasSunsetAnimation
sunset & sunrise animation on the fixed modes
Definition: control_fixed_modes.hpp:66
static constexpr bool hasCustomRamp
hint manager to save our custom ramp
Definition: control_fixed_modes.hpp:64
Definition: control_fixed_modes.hpp:144
fixed modes
Definition: control_fixed_modes.hpp:133
Definition: control_fixed_modes.hpp:149
jump color
Definition: control_fixed_modes.hpp:138