24#include "src/modes/include/default_config.hpp"
51 static constexpr auto from(
const uint8_t* arr)
53 ActiveIndexTy index = {arr[0], arr[1], arr[2], arr[3]};
56 static constexpr auto from(
const std::array<uint8_t, 4>& arr) {
return ActiveIndexTy::from(arr.data()); }
60template<
typename Config>
struct RampHandlerTy
62 using ConfigTy = Config;
63 static constexpr uint32_t startPeriod = Config::rampStartPeriodMs;
67 RampHandlerTy(uint32_t stepSpeed,
bool rampSaturates = Config::defaultRampSaturates) :
68 stepSpeed {stepSpeed},
70 lastTimeMeasured {1000},
72 animEffect {Config::defaultCustomRampAnimEffect},
73 animChoice {Config::defaultCustomRampAnimChoice}
77 void LMBD_INLINE reset()
80 stepSpeed = Config::defaultCustomRampStepSpeedMs;
82 animEffect = Config::defaultCustomRampAnimEffect;
83 animChoice = Config::defaultCustomRampAnimChoice;
86 void LMBD_INLINE update_ramp(uint8_t rampValue, uint32_t holdTime,
auto callback)
89 if (holdTime < lastTimeMeasured)
91 lastTimeMeasured = holdTime;
93 if (holdTime < startPeriod)
96 isForward = !isForward;
101 if (rampValue == 255)
109 uint32_t lastCounter = lastTimeMeasured / stepSpeed;
110 uint32_t nextCounter = holdTime / stepSpeed;
111 if (nextCounter <= lastCounter)
115 for (uint32_t I = 0; I < nextCounter - lastCounter; ++I)
118 if (isForward && (!rampSaturates || rampValue < 255))
122 if (!isForward && (!rampSaturates || rampValue > 0))
129 lastTimeMeasured = holdTime;
136 uint32_t lastTimeMeasured;
143template<
typename Config,
typename AllGroups, u
int8_t h
iddenGroupsCount>
struct ModeManagerTy
145 using SelfTy = ModeManagerTy<Config, AllGroups, hiddenGroupsCount>;
146 using ConfigTy = Config;
147 using AllGroupsTy = AllGroups;
148 using AllStatesTy = details::StateTyFrom<AllGroups>;
151 static constexpr uint8_t nbGroupsTotal {std::tuple_size_v<AllGroupsTy>};
152 static constexpr uint8_t hiddenGroupCount = hiddenGroupsCount;
154 static_assert(nbGroupsTotal >= hiddenGroupsCount,
"Manager cannot operate without accessible groups");
157 static constexpr uint8_t nbAccessibleGroups = nbGroupsTotal - hiddenGroupsCount;
160 static_assert(nbGroupsTotal <= 15,
"Maximum of 15 groups has been exceeded.");
162 template<u
int8_t Idx>
using GroupAt = std::tuple_element_t<Idx, AllGroupsTy>;
165 using HasAnyGroup = details::anyOf<AllGroupsTy>;
166 static constexpr bool hasSunsetAnimation = HasAnyGroup::hasSunsetAnimation;
167 static constexpr bool hasSunriseAnimation = HasAnyGroup::hasSunriseAnimation;
168 static constexpr bool hasBrightCallback = HasAnyGroup::hasBrightCallback;
169 static constexpr bool requireUserThread = HasAnyGroup::requireUserThread;
170 static constexpr bool hasCustomRamp = HasAnyGroup::hasCustomRamp;
171 static constexpr bool hasSystemCallbacks = hasCustomRamp || HasAnyGroup::hasSystemCallbacks;
172 static constexpr bool hasButtonCustomUI = HasAnyGroup::hasButtonCustomUI;
175 using EveryModeBool = details::asTableFor<AllGroupsTy>;
176 static constexpr auto everySunsetCallback = EveryModeBool::everySunsetCallback;
177 static constexpr auto everySunriseCallback = EveryModeBool::everySunriseCallback;
178 static constexpr auto everyBrightCallback = EveryModeBool::everyBrightCallback;
179 static constexpr auto everyRequireUserThread = EveryModeBool::everyRequireUserThread;
180 static constexpr auto everyCustomRamp = EveryModeBool::everyCustomRamp;
181 static constexpr auto everySystemCallbacks = EveryModeBool::everySystemCallbacks;
182 static constexpr auto everyButtonCustomUI = EveryModeBool::everyButtonCustomUI;
185 ModeManagerTy(hardware::LampTy& lamp) : activeIndex {ActiveIndexTy::from(Config::initialActiveIndex)}, lamp {lamp} {}
187 ModeManagerTy() =
delete;
188 ModeManagerTy(
const ModeManagerTy&) =
delete;
189 ModeManagerTy& operator=(
const ModeManagerTy&) =
delete;
192 auto get_context() {
return ContextTy<SelfTy, SelfTy>(*
this); }
195 template<
typename CallBack>
static void LMBD_INLINE dispatch_group(
auto& ctx, CallBack&& cb)
197 uint8_t groupId = ctx.get_active_group(nbGroupsTotal);
199 details::unroll<nbGroupsTotal>([&](
auto Idx) LMBD_INLINE {
208 template<
bool systemCallbacksOnly,
typename CallBack>
static void LMBD_INLINE foreach_group(
auto& ctx, CallBack&& cb)
210 if constexpr (systemCallbacksOnly)
212 details::unroll<nbGroupsTotal>([&](
auto Idx) LMBD_INLINE {
213 using GroupHere = GroupAt<Idx>;
214 constexpr bool hasCallbacks = GroupHere::hasSystemCallbacks;
216 if constexpr (hasCallbacks)
224 details::unroll<nbGroupsTotal>([&](
auto Idx) LMBD_INLINE {
235 enum class Store : uint16_t
245 static constexpr uint32_t storeId = modes::store::derivateStoreId<modes::store::hash(
"ManagerStoreId"), AllGroupsTy>;
262 std::array<ActiveIndexTy, maxFavoriteCount>
favorites = {};
270 ActiveIndexTy::from({0, 2, 80, 0}),
272 ActiveIndexTy::from({0, 2, 165, 0}),
274 ActiveIndexTy::from({0, 2, 0, 0}),
276 ActiveIndexTy::from({0, 0, 0, 0}),
278 ActiveIndexTy::from({0, 0, 128, 0}),
280 ActiveIndexTy::from({0, 0, 255, 0}),
288 static_assert(
maxFavoriteCount < 16,
"Maximum of 15 favorite as been exceeded");
296 enum class FavoriteAction : uint8_t
303 FavoriteAction
action = FavoriteAction::None;
318 RampHandlerTy<Config>
rampHandler = {Config::defaultCustomRampStepSpeedMs};
352 auto& self = ctx.state;
353 self.rampHandler.reset();
354 self.clearStripOnModeChange = Config::defaultClearStripOnModeChange;
360 auto& self = ctx.state;
361 if (self.clearStripOnModeChange)
366 ctx.lamp.forceDisableLedDitherMask =
false;
369 static void LMBD_INLINE on_favorite_hold_end(
auto& ctx,
bool shouldExecuteAction)
371 auto& p = ctx.state.pendingFavorite;
372 if (shouldExecuteAction)
374 if (p.action == PendingFavorite::FavoriteAction::Add && p.slot <= ctx.state.usedFavoriteCount)
375 ctx.set_favorite_at(p.slot, p.snapshot);
376 else if (p.action == PendingFavorite::FavoriteAction::Delete)
377 ctx.delete_favorite_now();
384 template<
typename Group> StateTyOf<Group>* LMBD_INLINE getStateGroupOf()
386 using StateTy = StateTyOf<Group>;
387 using OptionalTy = std::optional<StateTy>;
389 StateTy* substate =
nullptr;
390 details::unroll<nbGroupsTotal>([&](
auto Idx) LMBD_INLINE {
391 using GroupHere = GroupAt<Idx>;
392 constexpr bool IsHere = std::is_same_v<GroupHere, Group>;
394 if constexpr (IsHere)
396 OptionalTy& opt = std::get<OptionalTy>(
state.groupStates);
397 if (!opt.has_value())
402 StateTy& stateHere = *opt;
403 substate = &stateHere;
406 assert(substate !=
nullptr &&
"this should not have compiled at all!");
408 static_assert(details::ModeBelongsTo<Group, AllGroups>);
413 template<
typename Mode> StateTyOf<Mode>& LMBD_INLINE getStateOf()
415 using TargetStateTy = StateTyOf<Mode>;
418 if constexpr (std::is_same_v<TargetStateTy, NoState>)
424 else if constexpr (std::is_same_v<TargetStateTy, StateTy>)
430 else if constexpr (details::GroupBelongsTo<Mode, AllGroups>)
432 TargetStateTy* substate = getStateGroupOf<Mode>();
434 if (substate ==
nullptr)
436 substate = (TargetStateTy*)&placeholder;
437 assert(
false &&
"this code should be unreachable, but is it?");
444 static_assert(details::ModeExists<Mode, AllGroups>);
447 TargetStateTy* substate =
nullptr;
449 details::unroll<nbGroupsTotal>([&](
auto Idx) LMBD_INLINE {
450 using Group = GroupAt<Idx>;
451 using AllModes =
typename Group::AllModesTy;
452 if constexpr (details::ModeBelongsTo<Mode, AllModes>)
454 substate = Group::template getStateOf<Mode>(*
this);
457 assert(substate !=
nullptr &&
"this should not have compiled at all!");
459 if (substate ==
nullptr)
461 substate = (TargetStateTy*)&placeholder;
462 assert(
false &&
"this code should be unreachable, but is it?");
473 static constexpr bool isGroupManager =
true;
474 static constexpr bool isModeManager =
true;
477 static void next_group(
auto& ctx)
480 uint8_t groupIdBefore = ctx.get_active_group(nbAccessibleGroups);
481 ctx.set_active_group(groupIdBefore + 1, nbAccessibleGroups);
485 static void next_mode(
auto& ctx)
487 dispatch_group(ctx, [](
auto group) {
499 static void jump_to_new_active_index(
auto& ctx,
500 const ActiveIndexTy& newActiveIndex,
501 const bool ignoreRampAndIndex =
false,
502 const bool quitFavoriteGroup =
false)
508 auto manager = ctx.modeManager.get_context();
513 ctx.modeManager.quit_mode(manager);
515 ctx.modeManager.activeIndex.groupIndex = newActiveIndex.groupIndex;
516 ctx.modeManager.activeIndex.modeIndex = newActiveIndex.modeIndex;
518 ctx.modeManager.enter_mode(manager);
521 if (quitFavoriteGroup)
523 ctx.state.isInFavoriteMockGroup =
false;
527 const bool isRampRestored =
528 (not ignoreRampAndIndex) and ctx.modeManager.activeIndex.rawIndex == newActiveIndex.rawIndex;
530 if (not ignoreRampAndIndex)
533 ctx.modeManager.activeIndex.customIndex = newActiveIndex.customIndex;
534 ctx.modeManager.activeIndex.rampIndex = newActiveIndex.rampIndex;
538 if (not isRampRestored)
540 custom_ramp_update(ctx, ctx.get_active_custom_ramp());
551 static bool jump_to_favorite(
auto& ctx, uint8_t which_one,
bool shouldSaveLastActiveIndex)
554 if (ctx.state.usedFavoriteCount <= 0)
558 which_one = (which_one % ctx.state.usedFavoriteCount);
559 ctx.state.lastFavoriteStep = which_one;
563 if (shouldSaveLastActiveIndex and not ctx.state.isInFavoriteMockGroup)
565 ctx.state.beforeFavoriteActiveIndex = ctx.modeManager.activeIndex;
568 if (which_one >= ctx.state.maxFavoriteCount)
571 if (not ctx.state.isInFavoriteMockGroup)
575 auto manager = ctx.modeManager.get_context();
576 ctx.modeManager.quit_mode(manager);
579 ctx.state.isInFavoriteMockGroup =
true;
582 const auto targetFavorite = ctx.state.favorites[which_one];
584 jump_to_new_active_index(ctx, targetFavorite);
588 display_favorite_number_ramp(ctx, which_one, ctx.state.usedFavoriteCount,
true, 1000);
599 static bool exit_favorite_group(
auto& ctx, ActiveIndexTy newActiveIndex)
601 if (not ctx.state.isInFavoriteMockGroup)
604#ifdef LMBD_SIMULATION
605 fprintf(stderr,
"Exit fake favorite group\n");
608 jump_to_new_active_index(ctx, newActiveIndex,
false,
true);
623 static bool set_favorite_at(
auto& ctx, uint8_t which_one, ActiveIndexTy desiredFavorite)
626 if (which_one != ctx.state.maxFavoriteCount && which_one == ctx.state.usedFavoriteCount)
629 ctx.state.usedFavoriteCount = std::min<uint8_t>(ctx.state.usedFavoriteCount + 1, ctx.state.maxFavoriteCount);
632 if (which_one < ctx.state.maxFavoriteCount)
634 ctx.state.favorites[which_one] = desiredFavorite;
647 static bool delete_favorite_now(
auto& ctx)
650 const auto which_one = ctx.state.lastFavoriteStep;
651 if (ctx.state.usedFavoriteCount > 0 and which_one < ctx.state.maxFavoriteCount)
653 ctx.state.usedFavoriteCount -= 1;
655 for (uint8_t i = which_one; i < ctx.state.maxFavoriteCount - 1; ++i)
658 ctx.state.favorites[i] = ctx.state.favorites[i + 1];
661 if (ctx.state.usedFavoriteCount > 0)
663 jump_to_favorite(ctx, which_one,
false);
668 exit_favorite_group(ctx, ctx.state.beforeFavoriteActiveIndex);
680 static void handle_scroll_modes(
auto& ctx, uint32_t holdDuration)
682 auto& scrollHandler = ctx.state.scrollHandler;
683 scrollHandler.isForward =
false;
685 static constexpr uint32_t scrollActivationTiming = 750;
686 if (holdDuration <= scrollActivationTiming)
689 overlay_animate_ramp(
690 ctx, holdDuration, scrollActivationTiming, colors::PaletteGradient<colors::White, colors::Cyan>);
694 scrollHandler.update_ramp(128, holdDuration, [&](uint8_t rampValue) {
695 uint8_t modeIndex = ctx.get_active_mode();
696 uint8_t groupIndex = ctx.get_active_group();
697 uint8_t modeCount = ctx.get_modes_count();
699 ctx.state.isLastScrollAGroupChange =
false;
708 ctx.set_active_mode(modeIndex - 1, modeCount);
714 ctx.state.isLastScrollAGroupChange =
true;
719 ctx.set_active_group(groupIndex - 1, nbAccessibleGroups);
726 ctx.set_active_group(nbAccessibleGroups - 1, nbAccessibleGroups);
730 modeCount = ctx.get_modes_count();
731 ctx.set_active_mode(modeCount - 1, modeCount);
740 if (modeIndex + 1 < modeCount)
748 ctx.state.isLastScrollAGroupChange =
true;
750 if (groupIndex + 1 < nbAccessibleGroups)
758 ctx.set_active_group(0, nbAccessibleGroups);
762 ctx.set_active_mode(0, modeCount);
773 static void enter_hidden_group(
auto& ctx, uint8_t index)
775 assert(index < hiddenGroupsCount);
776 ctx.set_active_group(nbAccessibleGroups + index, nbGroupsTotal);
780 static bool overlay_animate_ramp(
781 auto& ctx,
float holdDuration,
float stepSize,
const colors::PaletteTy& palette,
const uint32_t timeout = 0)
784 const uint32_t stepProgress = floor((holdDuration * 256.0) / stepSize);
785 return overlay_animate_ramp(ctx, stepProgress % 256, palette);
787 static bool overlay_animate_ramp(
auto& ctx,
790 const uint32_t timeout = 0)
812 return (progress >= 250);
816 static void overlay_animate_dot(
auto& ctx,
821 const uint32_t timeout = 0)
840 static uint8_t get_modes_count(
auto& ctx)
843 dispatch_group(ctx, [&](
auto group) {
844 value =
decltype(group)::LocalModeTy::nbModes;
850 static void enter_group(
auto& ctx,
const uint8_t value)
853 assert(value < nbGroupsTotal);
855 auto manager = ctx.modeManager.get_context();
858 ctx.modeManager.quit_mode(manager);
861 ctx.modeManager.activeIndex.groupIndex = value;
863 ctx.modeManager.activeIndex.modeIndex = ctx.state.lastModeMemory[value];
866 ctx.modeManager.enter_mode(manager);
870 static void quit_group(
auto& ctx)
873 if (not ctx.state.isInFavoriteMockGroup)
876 uint8_t modeIdBefore = ctx.get_active_mode();
879 auto keyModeMemory = ctx.template storageFor<Store::modeMemory>(ctx.state.lastModeMemory);
882 uint8_t groupIdBefore = ctx.get_active_group(nbGroupsTotal);
883 ctx.state.lastModeMemory[groupIdBefore] = modeIdBefore;
888 static void enter_mode(
auto& ctx)
890 ctx.state.before_enter_mode(ctx);
893 dispatch_group(ctx, [](
auto group) {
897 ctx.state.after_enter_mode(ctx);
899 const uint8_t groupId = ctx.get_active_group();
900 const uint8_t modeId = ctx.get_active_mode();
901 logic::user_signals::send_mode(groupId, modeId);
903 if (ctx.everyCustomRamp[groupId][modeId])
904 logic::user_signals::send_ramp(ctx.get_active_custom_ramp(),
true);
908 static void quit_mode(
auto& ctx)
910 dispatch_group(ctx, [](
auto group) {
915 auto manager = ctx.modeManager.get_context();
924 static void loop(
auto& ctx)
927 if (ctx.state.isSunsetTimingPending > 0)
929 ctx.state.isSunsetTimingPending -= 1;
930 if (ctx.state.isSunsetTimingPending == 0)
939 if (ctx.state.skipNextFrameEffect > 0)
941 ctx.state.skipNextFrameEffect -= 1;
944 if (ctx.state.skipNextFrameEffect == 0)
946 ctx.lamp.restoreBrightnessNow();
951 ctx.lamp.refresh_tick_value();
954 dispatch_group(ctx, [](
auto group) {
959 ctx.lamp.apply_low_brightness_mask();
962 overlay.display_update(ctx);
965 if (ctx.state.skipNextFrameEffect == 0)
966 ctx.lamp.end_blackout();
974 static void sunset_update(
auto& ctx,
float progress)
976 dispatch_group(ctx, [&](
auto group) {
977 group.sunset_update(progress);
986 static void sunrise_update(
auto& ctx,
float progress)
988 dispatch_group(ctx, [&](
auto group) {
989 group.sunrise_update(progress);
996 static void sunset_timer_animation_internal_loop(
auto& ctx)
1000 ctx.modeManager.sunsetHandler.loop_suntimer_animation(ctx);
1001 ctx.lamp.forceDisableLedDitherMask =
true;
1008 static void sunset_timer_internal_reset(
auto& ctx)
1010 ctx.modeManager.sunsetHandler.reset_suntimer_animation(ctx);
1011 ctx.lamp.forceDisableLedDitherMask =
false;
1017 static void sunrise_timer_animation_internal_loop(
auto& ctx)
1021 ctx.modeManager.sunriseHandler.loop_suntimer_animation(ctx);
1022 ctx.lamp.forceDisableLedDitherMask =
true;
1029 static void sunrise_timer_internal_reset(
auto& ctx)
1031 ctx.modeManager.sunriseHandler.reset_suntimer_animation(ctx);
1032 ctx.lamp.forceDisableLedDitherMask =
false;
1042 dispatch_group(ctx, [&](
auto group) {
1043 group.brightness_update(brightness);
1051 ctx.lamp.refresh_tick_value();
1054 ctx.sunset_timer_internal_reset();
1055 ctx.sunrise_timer_internal_reset();
1057 foreach_group<true>(ctx, [](
auto group) {
1058 group.power_on_sequence();
1062 if (ctx.state.isInFavoriteMockGroup and jump_to_favorite(ctx, ctx.state.lastFavoriteStep,
false))
1069 uint8_t groupIdBefore = ctx.get_active_group(nbGroupsTotal);
1070 ctx.set_active_group(groupIdBefore);
1077 foreach_group<true>(ctx, [](
auto group) {
1078 group.power_off_sequence();
1081 ctx.lamp.forceDisableLedDitherMask =
false;
1091 ctx.template storageSaveOnly<Store::lastActive>(ctx.modeManager.activeIndex);
1094 ctx.template storageSaveOnly<Store::usedFavoriteCount>(ctx.modeManager.state.usedFavoriteCount);
1095 ctx.template storageSaveOnly<Store::favoriteModes>(ctx.modeManager.state.favorites);
1096 ctx.template storageSaveOnly<Store::lastUsedFavorite>(ctx.modeManager.state.lastFavoriteStep);
1097 ctx.template storageSaveOnly<Store::isInFavoriteGroup>(ctx.state.isInFavoriteMockGroup);
1098 ctx.template storageSaveOnly<Store::modeMemory>(ctx.state.lastModeMemory);
1100 foreach_group<not hasCustomRamp>(ctx, [&ctx](
auto group) {
1101 if constexpr (group.hasCustomRamp)
1103 using StoreHere =
typename decltype(group)::StoreEnum;
1104 group.template storageSaveOnly<StoreHere::rampMemory>(group.state.customRampMemory);
1105 group.template storageSaveOnly<StoreHere::indexMemory>(group.state.customIndexMemory);
1108 group.write_parameters();
1119 using LocalStore = details::LocalStoreOf<
decltype(ctx)>;
1120 LocalStore::template migrateStoreIfNeeded<storeId>();
1123 ctx.template storageLoadOnly<Store::lastActive>(ctx.modeManager.activeIndex,
1124 ActiveIndexTy::from(Config::initialActiveIndex));
1129 ctx.template storageLoadOnly<Store::usedFavoriteCount>(ctx.modeManager.state.usedFavoriteCount,
1130 ctx.modeManager.state.defaultFavoriteCount_indexable);
1131 ctx.template storageLoadOnly<Store::favoriteModes>(ctx.state.favorites,
1132 ctx.modeManager.state.defaultFavorites_indexable);
1137 ctx.template storageLoadOnly<Store::usedFavoriteCount>(ctx.modeManager.state.usedFavoriteCount,
1138 ctx.modeManager.state.defaultFavoriteCount_simple);
1139 ctx.template storageLoadOnly<Store::favoriteModes>(ctx.state.favorites,
1140 ctx.modeManager.state.defaultFavorites_simple);
1143 ctx.template storageLoadOnly<Store::lastUsedFavorite>(ctx.modeManager.state.lastFavoriteStep);
1144 ctx.template storageLoadOnly<Store::isInFavoriteGroup>(ctx.state.isInFavoriteMockGroup);
1145 ctx.template storageLoadOnly<Store::modeMemory>(ctx.state.lastModeMemory);
1148 foreach_group<not hasCustomRamp>(ctx, [&ctx](
auto group) {
1149 using LocalStore = details::LocalStoreOf<
decltype(group)>;
1150 LocalStore::template migrateStoreIfNeeded<storeId>();
1152 if constexpr (group.hasCustomRamp)
1154 using StoreHere =
typename LocalStore::EnumTy;
1156 group.template storageLoadOnly<StoreHere::rampMemory>(group.state.customRampMemory,
1157 group.template get_custom_ramp_default_value());
1158 group.template storageLoadOnly<StoreHere::indexMemory>(group.state.customIndexMemory);
1161 group.read_parameters();
1168 dispatch_group(ctx, [](
auto group) {
1169 group.user_thread();
1179 static void custom_ramp_update(
auto& ctx, uint8_t rampValue, uint32_t timeout = 0)
1181 uint8_t groupId = ctx.get_active_group();
1182 uint8_t modeId = ctx.get_active_mode();
1184 if (ctx.everyCustomRamp[groupId][modeId] && ctx.state.rampHandler.animEffect)
1186 switch (ctx.state.rampHandler.animChoice)
1198 dispatch_group(ctx, [&](
auto group) {
1199 group.custom_ramp_update(rampValue);
1202 if (ctx.everyCustomRamp[groupId][modeId])
1203 logic::user_signals::send_ramp(rampValue);
1207 static bool custom_click(
auto& ctx, uint8_t nbClick)
1209 bool retVal =
false;
1210 dispatch_group(ctx, [&](
auto group) {
1211 retVal = group.custom_click(nbClick);
1217 static bool custom_hold(
auto& ctx, uint8_t nbClickAndHold,
bool isEndOfHoldEvent, uint32_t holdDuration)
1219 bool retVal =
false;
1220 dispatch_group(ctx, [&](
auto group) {
1221 retVal = group.custom_hold(nbClickAndHold, isEndOfHoldEvent, holdDuration);
1238 static void display_favorite_number_ramp(
auto& ctx,
1239 const uint8_t favoriteIndex,
1240 const uint8_t maxFavoriteIndex,
1241 const bool display =
false,
1242 const uint32_t timeout = 0)
1244 const uint8_t maxPixelDisplay = std::min<uint8_t>(ctx.state.maxFavoriteCount, maxFavoriteIndex);
1245 for (uint8_t i = 0; i < maxPixelDisplay; ++i)
1247 const bool shouldDisplay = (display and i <= favoriteIndex);
1248 overlay_animate_dot(
1249 ctx, i, i, shouldDisplay ? 255 : 0, colors::PaletteGradient<colors::Black, colors::Cyan>, timeout);
1253 static uint8_t get_number_of_allowed_favorites(
auto& ctx)
1257 return ctx.state.usedFavoriteCount + ((ctx.state.usedFavoriteCount < ctx.state.maxFavoriteCount) ? 1 : 0);
1261 static bool is_in_favorite_group(
auto& ctx) {
return ctx.state.isInFavoriteMockGroup; }
1267 template<
bool displayFavoriteNumber = true>
1268 static void set_current_mode_as_favorite(
auto& ctx, uint8_t favoriteIndex, uint32_t displayTimeout_s = 0)
1270 const uint8_t numberOfFavoriteSet = get_number_of_allowed_favorites(ctx);
1271 if (favoriteIndex >= numberOfFavoriteSet)
1274 "Cannot set a favorite at index %d, max index must be less than %d", favoriteIndex, numberOfFavoriteSet);
1279 if constexpr (displayFavoriteNumber)
1282 display_favorite_number_ramp(
1283 ctx, favoriteIndex, numberOfFavoriteSet, favoriteIndex < numberOfFavoriteSet, displayTimeout_s);
1286 ctx.state.pendingFavorite.action = StateTy::PendingFavorite::FavoriteAction::Add;
1287 ctx.state.pendingFavorite.slot = favoriteIndex;
1288 ctx.state.pendingFavorite.snapshot = ctx.modeManager.activeIndex;
1290 ctx.state.on_favorite_hold_end(ctx,
true);
1300 template<
bool displayFavoriteNumber = true>
1301 static void animate_favorite_pick(
auto& ctx,
float holdDuration,
float stepSize,
bool isEndOfHold)
1303 const uint8_t numberOfFavoriteSet = get_number_of_allowed_favorites(ctx);
1306 uint32_t stepCount = numberOfFavoriteSet + floor(holdDuration / stepSize);
1307 stepCount = stepCount % (numberOfFavoriteSet + 1);
1310 if (stepCount >= numberOfFavoriteSet)
1313 if (holdDuration <= 2 * stepSize)
1315 overlay_animate_ramp(ctx, holdDuration, stepSize, colors::PaletteGradient<colors::White, colors::Cyan>);
1321 overlay_animate_ramp(ctx, holdDuration, stepSize, colors::PaletteGradient<colors::Green, colors::White>);
1323 if constexpr (displayFavoriteNumber)
1325 display_favorite_number_ramp(ctx, stepCount, numberOfFavoriteSet, stepCount < numberOfFavoriteSet);
1333 if (stepCount < numberOfFavoriteSet)
1334 set_current_mode_as_favorite(ctx, stepCount);
1336 ctx.state.on_favorite_hold_end(ctx,
false);
1347 template<
bool displayFavoriteNumber = true>
1348 static void animate_favorite_delete(
auto& ctx,
float holdDuration,
float stepSize,
bool isEndOfHold)
1351 if (ctx.state.usedFavoriteCount <= 0)
1356 ctx.state.on_favorite_hold_end(ctx,
false);
1360 ctx.state.isInDeleteFavorite =
true;
1362 uint32_t stepCount = floor(holdDuration / stepSize);
1363 stepCount = stepCount % 2;
1366 const bool justSwitched = stepCount != ctx.state.pendingFavorite.slot;
1369 overlay_animate_ramp(ctx, holdDuration, stepSize, colors::PaletteGradient<colors::Orange, colors::Red>);
1371 if constexpr (displayFavoriteNumber)
1374 display_favorite_number_ramp(ctx, ctx.state.lastFavoriteStep, ctx.state.usedFavoriteCount,
true);
1383 ctx.state.pendingFavorite.action = StateTy::PendingFavorite::FavoriteAction::Delete;
1384 ctx.state.on_favorite_hold_end(ctx,
true);
1389 ctx.state.pendingFavorite.slot = stepCount;
1397 ActiveIndexTy activeIndex;
1400 hardware::LampTy& lamp;
1403 inline static draw::overlay::Manager<> overlay;
1412 Animation animation;
1422 SunTimerHandling<anims::fadeout::GravityDissolve<>> sunsetHandler;
1423 SunTimerHandling<anims::fadein::GravityFadeIn<>> sunriseHandler;
1431 NoState placeholder;
1441 ModeManagerTy<ManagerConfig, std::tuple<Groups...>, 0>;
1470 ModeManagerTy<ManagerConfig, std::tuple<Groups...>, hiddenGroupCnt>;
Handle the main alerts behavior.
Define assertions helpers.
void add_time_minutes(const uint8_t time_minutes)
Add some time to the current timer, or start it with a target time in minutes. This time cannot be sh...
Definition: sun_timers.cpp:101
void raise(const Type type)
Raise an alert.
Definition: alerts.cpp:1031
ContextTy and associated definitions.
Define some animations to fade in modes.
Define some animations to fade out modes.
Filesystem interaction tools.
Define the main led strip interaction object.
void lampda_print(const char *format,...)
C linkage to print functions.
Definition: text_out.cpp:336
AlertManager_t manager
Instanciation of the AlertManager.
Definition: alerts.cpp:30
bool is_enabled()
Return true if the blinks are enabled.
Definition: battery_blink.cpp:105
SunsetTimer sunset_timer
object to access to control the timers
Definition: sun_timers.cpp:237
static constexpr PaletteTy PaletteBlackBodyColors
Black body radiation, with the high end changed to be nicer.
Definition: palettes.hpp:396
std::array< uint32_t, 16 > PaletteTy
Palette types.
Definition: palettes.hpp:18
static constexpr PaletteTy PaletteRainbowColors
HSV Rainbow.
Definition: palettes.hpp:360
@ DOT
Display a pixel dot.
@ indexable
Equivalent to LMBD_LAMP_TYPE__INDEXABLE.
@ simple
Equivalent to LMBD_LAMP_TYPE__SIMPLE.
static void clear_stored()
Force clear the stored parameters.
Definition: keystore.hpp:75
Contains basic interface types to implement custom user modes.
Definition: control_fixed_modes.hpp:14
ModeManagerTy< ManagerConfig, std::tuple< Groups... >, hiddenGroupCnt > ManagerFoHiddenConfig
Same as modes::ManagerFor but with custom defaults, and additional hidden groups.
Definition: manager_type.hpp:1470
ModeManagerTy< DefaultManagerConfig, std::tuple< Groups... >, hiddenGroupCnt > ManagerForHiddenGroups
Group together several mode groups defined through modes::GroupFor. Will use the last hiddenGroupCnt ...
Definition: manager_type.hpp:1463
ModeManagerTy< ManagerConfig, std::tuple< Groups... >, 0 > ManagerForConfig
Same as modes::ManagerFor but with custom defaults.
Definition: manager_type.hpp:1441
@ rampSaturates
(bool) Mode saturates on custom ramp, or else wrap?
ModeManagerTy< DefaultManagerConfig, std::tuple< Groups... >, 0 > ManagerFor
Group together several mode groups defined through modes::GroupFor.
Definition: manager_type.hpp:1451
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
GlobalSimStateTy state
Store the global simulation state.
Definition: simulator_state.cpp:7
Default manager configuration, enables you to customize defaults.
Definition: default_config.hpp:42
Definition: manager_type.hpp:295
FavoriteAction action
action on this favorite
Definition: manager_type.hpp:303
ActiveIndexTy snapshot
current active index
Definition: manager_type.hpp:305
uint8_t slot
index of the favorite to add/remove
Definition: manager_type.hpp:304
Definition: manager_type.hpp:252
RampHandlerTy< Config > scrollHandler
Ramp handlers: mode scroll ramp.
Definition: manager_type.hpp:320
static constexpr uint8_t defaultFavoriteCount_simple
By default, the system will have X favorites.
Definition: manager_type.hpp:284
static constexpr std::array< ActiveIndexTy, maxFavoriteCount > defaultFavorites_indexable
The default favorites are defined below :
Definition: manager_type.hpp:268
bool clearStripOnModeChange
Should clear the strip before switching mode.
Definition: manager_type.hpp:322
RampHandlerTy< Config > rampHandler
Ramp handlers: custom ramp (or "color ramp")
Definition: manager_type.hpp:318
bool isInFavoriteMockGroup
Indicates that we are in the fake favorite page.
Definition: manager_type.hpp:310
static constexpr uint8_t defaultFavoriteCount_indexable
By default, the system will have X favorites.
Definition: manager_type.hpp:266
uint8_t lastFavoriteStep
last used favorite index
Definition: manager_type.hpp:292
AllStatesTy groupStates
All group states, containing all modes individual states.
Definition: manager_type.hpp:254
ActiveIndexTy beforeFavoriteActiveIndex
store the index we need to go to when quitting the favorite page
Definition: manager_type.hpp:311
static constexpr std::array< ActiveIndexTy, maxFavoriteCount > defaultFavorites_simple
The default favorites are defined below :
Definition: manager_type.hpp:286
uint8_t usedFavoriteCount
number of favorite set by user [0, maxFavoriteCount]
Definition: manager_type.hpp:263
bool isLastScrollAGroupChange
last mode change in scroll changed group
Definition: manager_type.hpp:314
uint32_t lastScrollStopped
keep track of the last scrool release time
Definition: manager_type.hpp:315
static void LMBD_INLINE before_enter_mode(auto &ctx)
configuration-related actions done before entering mode
Definition: manager_type.hpp:350
uint8_t isSunsetTimingPending
Indicates that a sunset timer ramp is active.
Definition: manager_type.hpp:312
void reset()
Reset the stored variable states.
Definition: manager_type.hpp:328
std::array< ActiveIndexTy, maxFavoriteCount > favorites
Store the active index of every favorite.
Definition: manager_type.hpp:262
uint8_t skipNextFrameEffect
should the next .loop() mode be skipped?
Definition: manager_type.hpp:325
std::array< uint8_t, nbGroupsTotal > lastModeMemory
When switching group, remember which mode was on last time we visited it.
Definition: manager_type.hpp:257
static constexpr uint8_t maxFavoriteCount
Maximum allowed favorite count.
Definition: manager_type.hpp:260
static void LMBD_INLINE after_enter_mode(auto &ctx)
configuration-related actions done after mode entering
Definition: manager_type.hpp:358
PendingFavorite pendingFavorite
store the pending favorite to add or delete
Definition: manager_type.hpp:308
bool isInDeleteFavorite
indicates that we are in a favorite deletion process
Definition: manager_type.hpp:291
Definition: manager_type.hpp:1410
void loop_suntimer_animation(auto &ctx)
loop suntimer animations
Definition: manager_type.hpp:1418
void reset_suntimer_animation(auto &ctx)
Reset the sunset animations.
Definition: manager_type.hpp:1416
void update_suntimer_drop_animation(auto &ctx, float progress)
Update the sunset drop animation.
Definition: manager_type.hpp:1414
Handle the sun timers: sunset to auto shutdown the system and sunrise for auto turn on.
Output user action signals on the serial and bluetooth links.