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