Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
group_type.hpp
Go to the documentation of this file.
1#ifndef MODE_GROUP_H
2#define MODE_GROUP_H
3
9#include <cstdint>
10#include <utility>
11#include <optional>
12#include <tuple>
13#include <array>
14
16
21
22namespace lampda::modes {
23
25template<typename AllModes> static constexpr bool verifyGroup()
26{
27 constexpr bool inheritsFromBasicMode = details::allOf<AllModes>::inheritsFromBasicMode;
28 static_assert(inheritsFromBasicMode, "All modes must inherit from modes::BasicMode!");
29
30 constexpr bool stateDefaultConstructible = details::allOf<AllModes>::stateDefaultConstructible;
31 static_assert(stateDefaultConstructible, "All states must be default constructible!");
32
33 bool acc = false;
34 acc |= !inheritsFromBasicMode;
35 acc |= !stateDefaultConstructible;
36 return acc;
37}
38
40template<typename AllModes, bool earlyFail = verifyGroup<AllModes>()> struct GroupTy
41{
42 // tuple helper
43 using SelfTy = GroupTy<AllModes>;
44 using AllModesTy = AllModes;
45 using AllStatesTy = details::StateTyFrom<AllModes>;
46 static constexpr uint8_t nbModes {std::tuple_size_v<AllModesTy>};
47
48 // last mode index must not collide with modes::store::noModeIndex
49 static_assert(nbModes < 32, "Maximum of 31 modes has been exceeded.");
50
51 template<uint8_t Idx> using ModeAtRaw = std::tuple_element_t<Idx, AllModesTy>;
52 template<uint8_t Idx> using ModeAt = std::conditional_t<!earlyFail, ModeAtRaw<Idx>, BasicMode>;
53
54 // required to support group-level context
55 using HasAnyMode = details::anyOf<AllModesTy, earlyFail>;
56 static constexpr bool hasSunsetAnimation = HasAnyMode::hasSunsetAnimation;
57 static constexpr bool hasSunriseAnimation = HasAnyMode::hasSunriseAnimation;
58 static constexpr bool hasBrightCallback = HasAnyMode::hasBrightCallback;
59 static constexpr bool hasSystemCallbacks = HasAnyMode::hasSystemCallbacks;
60 static constexpr bool requireUserThread = HasAnyMode::requireUserThread;
61 static constexpr bool hasCustomRamp = HasAnyMode::hasCustomRamp;
62 static constexpr bool hasButtonCustomUI = HasAnyMode::hasButtonCustomUI;
63
64 // useful for runtime tests of mode properties
65 static constexpr auto everySunsetCallback = HasAnyMode::everySunsetCallback;
66 static constexpr auto everySunriseCallback = HasAnyMode::everySunriseCallback;
67 static constexpr auto everyBrightCallback = HasAnyMode::everyBrightCallback;
68 static constexpr auto everySystemCallbacks = HasAnyMode::everySystemCallbacks;
69 static constexpr auto everyRequireUserThread = HasAnyMode::everyRequireUserThread;
70 static constexpr auto everyCustomRamp = HasAnyMode::everyCustomRamp;
71 static constexpr auto everyButtonCustomUI = HasAnyMode::everyButtonCustomUI;
72
73 // constructors
74 GroupTy() = delete;
75 GroupTy(const GroupTy&) = delete;
76 GroupTy& operator=(const GroupTy&) = delete;
77
79 template<typename CallBack> static void LMBD_INLINE dispatch_mode(auto& ctx, CallBack&& cb)
80 {
81 uint8_t modeId = ctx.get_active_mode(nbModes);
82
83 details::unroll<nbModes>([&](auto Idx) LMBD_INLINE {
84 if (Idx == modeId)
85 {
86 cb(context_as<ModeAt<Idx>>(ctx));
87 }
88 });
89 }
90
92 template<bool systemCallbacksOnly, typename CallBack> static void LMBD_INLINE foreach_mode(auto& ctx, CallBack&& cb)
93 {
94 if constexpr (systemCallbacksOnly)
95 {
96 details::unroll<nbModes>([&](auto Idx) LMBD_INLINE {
97 if constexpr (ModeAt<Idx>::hasSystemCallbacks)
98 {
99 cb(context_as<ModeAt<Idx>>(ctx));
100 }
101 });
102 }
103 else
104 {
105 details::unroll<nbModes>([&](auto Idx) LMBD_INLINE {
106 cb(context_as<ModeAt<Idx>>(ctx));
107 });
108 }
109 }
110
112 template<bool systemCallbacksOnly, typename CallBack> static void LMBD_INLINE iterate_mode(auto& ctx, CallBack&& cb)
113 {
114 if constexpr (systemCallbacksOnly)
115 {
116 details::unroll<nbModes>([&](auto Idx) LMBD_INLINE {
117 if constexpr (ModeAt<Idx>::hasSystemCallbacks)
118 {
119 cb(context_as<ModeAt<Idx>>(ctx), Idx);
120 }
121 });
122 }
123 else
124 {
125 details::unroll<nbModes>([&](auto Idx) LMBD_INLINE {
126 cb(context_as<ModeAt<Idx>>(ctx), Idx);
127 });
128 }
129 }
130
131 //
132 // store
133 //
134
135 // persistent values
136 enum class Store : uint16_t
137 {
138 rampMemory,
139 indexMemory
140 };
141
142 static constexpr uint32_t storeId = modes::store::derivateStoreId<modes::store::hash("GroupTyStoreId"), AllModesTy>;
143
144 //
145 // state
146 //
147
148 struct StateTy
149 {
150 AllStatesTy modeStates;
151 std::array<uint8_t, nbModes> customRampMemory;
152 std::array<uint8_t, nbModes> customIndexMemory;
153
155 void LMBD_INLINE save_ramps(auto& ctx, uint8_t modeId)
156 {
157 assert(modeId < nbModes);
158 if constexpr (ctx.hasCustomRamp)
159 {
160 ctx.state.customRampMemory[modeId] = ctx.get_active_custom_ramp();
161 ctx.state.customIndexMemory[modeId] = ctx.get_active_custom_index();
162 }
163 }
164
166 void LMBD_INLINE load_ramps(auto& ctx, uint8_t modeId)
167 {
168 assert(modeId < nbModes);
169 if constexpr (ctx.hasCustomRamp)
170 {
171 ctx.set_active_custom_ramp(ctx.state.customRampMemory[modeId]);
172 ctx.set_active_custom_index(ctx.state.customIndexMemory[modeId]);
173 }
174 }
175 };
176
178 template<typename Mode> static auto* LMBD_INLINE getStateOf(auto& manager)
179 {
180 using StateTy = typename Mode::StateTy;
181 using OptionalTy = std::optional<StateTy>;
182
183 StateTy* substate = nullptr;
184 details::unroll<nbModes>([&](auto Idx) LMBD_INLINE {
185 using ModeHere = ModeAt<Idx>;
186 constexpr bool isHere = std::is_same_v<ModeHere, Mode>;
187
188 if constexpr (isHere)
189 {
190 auto* state = manager.template getStateGroupOf<SelfTy>();
191 if (state)
192 {
193 OptionalTy& opt = std::get<OptionalTy>(state->modeStates);
194 if (!opt.has_value())
195 {
196 opt.emplace(); // all StateTy must be default-contructible :)
197 }
198
199 StateTy& stateHere = *opt;
200 substate = &stateHere;
201 }
202 }
203 });
204
205 assert(substate != nullptr && "this should not have happened!");
206 return substate;
207 }
208
209 //
210 // navigation
211 //
212
214 static constexpr bool isGroupManager = false;
216 static constexpr bool isModeManager = true;
217
219 static void next_mode(auto& ctx)
220 {
221 uint8_t modeIdBefore = ctx.get_active_mode(nbModes);
222 ctx.set_active_mode(modeIdBefore + 1, nbModes);
223 }
224
226 static void enter_mode(auto& ctx)
227 {
228 // restore brightness before entering a mode
229 // align the internal references first, so enforcement does not add the restored step again
230 ctx.lamp.align_internal_to_system_brightness();
231 ctx.lamp.setBrightness(logic::brightness::get_scaled_saved_brightness(), false, false, true);
232
233 // set ramps if they exist
234 uint8_t modeIdAfter = ctx.get_active_mode(nbModes);
235 ctx.state.load_ramps(ctx, modeIdAfter);
236
237 // start new mode we switched to
238 dispatch_mode(ctx, [](auto mode) {
239 mode.on_enter_mode();
240 });
241 }
242
244 static void quit_mode(auto& ctx)
245 {
246 // Do not save presets in favorite group
247 if (not ctx.is_in_favorite_group())
248 {
249 // save ramps if they exist
250 uint8_t modeIdBefore = ctx.get_active_mode(nbModes);
251
252 // save ramps if they exist
253 ctx.state.save_ramps(ctx, modeIdBefore);
254 }
255
256 // start new mode we switched to
257 dispatch_mode(ctx, [](auto mode) {
258 mode.on_exit_mode();
259 });
260 }
261
262 //
263 // all the callbacks
264 //
265
267 static void loop(auto& ctx)
268 {
269 dispatch_mode(ctx, [](auto mode) {
270 mode.loop();
271 });
272 }
273
275 static void sunset_update(auto& ctx, float progress)
276 {
277 dispatch_mode(ctx, [&](auto mode) {
278 mode.sunset_update(progress);
279 });
280 }
281
283 static void sunrise_update(auto& ctx, float progress)
284 {
285 dispatch_mode(ctx, [&](auto mode) {
286 mode.sunrise_update(progress);
287 });
288 }
289
291 static void brightness_update(auto& ctx, brightness_t brightness)
292 {
293 dispatch_mode(ctx, [&](auto mode) {
294 mode.brightness_update(brightness);
295 });
296 }
297
299 static void custom_ramp_update(auto& ctx, uint8_t rampValue)
300 {
301 dispatch_mode(ctx, [&](auto mode) {
302 mode.custom_ramp_update(rampValue);
303 });
304 }
305
307 static bool custom_click(auto& ctx, uint8_t nbClick)
308 {
309 bool retVal = false;
310 dispatch_mode(ctx, [&](auto mode) {
311 retVal = mode.custom_click(nbClick);
312 });
313 return retVal;
314 }
315
317 static bool custom_hold(auto& ctx, uint8_t nbClickAndHold, bool isEndOfHoldEvent, uint32_t holdDuration)
318 {
319 bool retVal = false;
320 dispatch_mode(ctx, [&](auto mode) {
321 retVal = mode.custom_hold(nbClickAndHold, isEndOfHoldEvent, holdDuration);
322 });
323 return retVal;
324 }
325
326 static std::array<uint8_t, nbModes> get_custom_ramp_default_value(auto& ctx)
327 {
328 std::array<uint8_t, nbModes> defaultValues;
329 defaultValues.fill(0);
330
331 iterate_mode<false>(ctx, [&defaultValues](auto mode, const size_t index) {
332 if constexpr (mode.hasCustomRamp)
333 {
334 defaultValues[index] = mode.get_custom_ramp_default_value();
335 }
336 });
337 return defaultValues;
338 }
339
341 static void power_on_sequence(auto& ctx)
342 {
343 foreach_mode<true>(ctx, [](auto mode) {
344 mode.power_on_sequence();
345 });
346 }
347
349 static void power_off_sequence(auto& ctx)
350 {
351 foreach_mode<true>(ctx, [](auto mode) {
352 mode.power_off_sequence();
353 });
354 }
355
357 static void write_parameters(auto& ctx)
358 {
359 foreach_mode<true>(ctx, [](auto mode) {
360 mode.write_parameters();
361 });
362 }
363
365 static void read_parameters(auto& ctx)
366 {
367 foreach_mode<true>(ctx, [](auto mode) {
368 mode.read_parameters();
369 });
370 }
371
373 static void user_thread(auto& ctx)
374 {
375 dispatch_mode(ctx, [](auto mode) {
376 mode.user_thread();
377 });
378 }
379};
380
386template<typename... Modes> using GroupFor = GroupTy<std::tuple<Modes...>>;
387
388} // namespace lampda::modes
389
390#endif
Define assertions helpers.
ContextTy and associated definitions.
modes::ManagerFor and associated definitions
Basic interface types to implement custom user modes.
brightness_t get_scaled_saved_brightness()
Return the saved brightness level, with the user brightness scale applied. This is the level that the...
Definition: brightness_handle.cpp:71
Contains basic interface types to implement custom user modes.
Definition: control_fixed_modes.hpp:14
GroupTy< std::tuple< Modes... > > GroupFor
Group together many different modes::BasicMode.
Definition: group_type.hpp:386
static auto context_as(auto &ctx)
Bind provided context to another modes::BasicMode.
Definition: context_type.hpp:25
void brightness_update(const brightness_t brightness)
Called when the system changes the LED strip brightness.
Definition: default_behavior.hpp:64
void user_thread()
Called at each tick of the secondary thread.
Definition: default_behavior.hpp:199
void read_parameters()
Called when system wants to read parameters from filesystem.
Definition: default_behavior.hpp:94
void power_on_sequence()
Called when the system powers on (must be non blocking function!)
Definition: default_behavior.hpp:39
void write_parameters()
Called when system wants to write parameters to filesystem.
Definition: default_behavior.hpp:88
void power_off_sequence()
Called when the system powers off (must be non blocking function!)
Definition: default_behavior.hpp:50
void loop()
Called at each tick of the main loop.
Definition: default_behavior.hpp:179
uint16_t brightness_t
Define the type of the brightness parameters.
Definition: constants.h:151
Definition: group_type.hpp:149
std::array< uint8_t, nbModes > customIndexMemory
Store the active index for each mode.
Definition: group_type.hpp:152
std::array< uint8_t, nbModes > customRampMemory
Store the ramp value for each mode.
Definition: group_type.hpp:151
AllStatesTy modeStates
Store the current group state.
Definition: group_type.hpp:150
void LMBD_INLINE save_ramps(auto &ctx, uint8_t modeId)
If a mode signaled that it has a ramp, save it.
Definition: group_type.hpp:155
void LMBD_INLINE load_ramps(auto &ctx, uint8_t modeId)
If a mode signaled that it has a ramp, load it.
Definition: group_type.hpp:166
Define templated tools to analyze the manager objects.