5#ifndef INDEXABLE_BEHAVIOR_MANAGER_H
6#define INDEXABLE_BEHAVIOR_MANAGER_H
29 auto manager = get_context();
35 if (manager.state.isInFavoriteMockGroup)
38 manager.state.lastFavoriteStep += 1;
40 if (not manager.jump_to_favorite(manager.state.lastFavoriteStep,
false))
42 manager.exit_favorite_group(manager.state.beforeFavoriteActiveIndex);
47 if (manager.state.isLastScrollAGroupChange and manager.lamp.now - manager.state.lastScrollStopped < 2000)
54 manager.state.isLastScrollAGroupChange =
false;
64 int _groupId = manager.get_active_group();
65 int _modeId = manager.get_active_mode();
66 fprintf(stderr,
"group %d *mode %d\n", _groupId, _modeId);
67 if (manager.everySunsetCallback[_groupId][_modeId])
68 fprintf(stderr,
" - hasSunsetAnimation\n");
69 if (manager.everySunriseCallback[_groupId][_modeId])
70 fprintf(stderr,
" - hasSunriseAnimation\n");
71 if (manager.everyBrightCallback[_groupId][_modeId])
72 fprintf(stderr,
" - hasBrightCallback\n");
73 if (manager.everyCustomRamp[_groupId][_modeId])
74 fprintf(stderr,
" - hasCustomRamp\n");
75 if (manager.state.rampHandler.animEffect)
76 fprintf(stderr,
" - has anim effect\n");
77 if (manager.everyRequireUserThread[_groupId][_modeId])
78 fprintf(stderr,
" - requireUserThread\n");
79 if (manager.everySystemCallbacks[_groupId][_modeId])
80 fprintf(stderr,
" - hasSystemCallbacks\n");
81 if (manager.everyButtonCustomUI[_groupId][_modeId])
82 fprintf(stderr,
" - hasButtonCustomUI\n");
91 if (manager.state.isInFavoriteMockGroup)
94 manager.exit_favorite_group(manager.state.beforeFavoriteActiveIndex);
102#ifdef LMBD_SIMULATION
104 int _groupId = manager.get_active_group();
105 int _modeId = manager.get_active_mode();
106 fprintf(stderr,
"group %d *mode %d\n", _groupId, _modeId);
107 if (manager.everySunsetCallback[_groupId][_modeId])
108 fprintf(stderr,
" - hasSunsetAnimation\n");
109 if (manager.everySunriseCallback[_groupId][_modeId])
110 fprintf(stderr,
" - hasSunriseAnimation\n");
111 if (manager.everyBrightCallback[_groupId][_modeId])
112 fprintf(stderr,
" - hasBrightCallback\n");
113 if (manager.everyCustomRamp[_groupId][_modeId])
114 fprintf(stderr,
" - hasCustomRamp\n");
115 if (manager.state.rampHandler.animEffect)
116 fprintf(stderr,
" - has anim effect\n");
117 if (manager.everyRequireUserThread[_groupId][_modeId])
118 fprintf(stderr,
" - requireUserThread\n");
119 if (manager.everySystemCallbacks[_groupId][_modeId])
120 fprintf(stderr,
" - hasSystemCallbacks\n");
121 if (manager.everyButtonCustomUI[_groupId][_modeId])
122 fprintf(stderr,
" - hasButtonCustomUI\n");
132 if (not manager.state.isInFavoriteMockGroup)
135 manager.jump_to_favorite(manager.state.lastFavoriteStep,
true);
137#ifdef LMBD_SIMULATION
139 int _groupId = manager.get_active_group();
140 int _modeId = manager.get_active_mode();
141 fprintf(stderr,
"group %d *mode %d\n", _groupId, _modeId);
142 if (manager.everySunsetCallback[_groupId][_modeId])
143 fprintf(stderr,
" - hasSunsetAnimation\n");
144 if (manager.everySunriseCallback[_groupId][_modeId])
145 fprintf(stderr,
" - hasSunriseAnimation\n");
146 if (manager.everyBrightCallback[_groupId][_modeId])
147 fprintf(stderr,
" - hasBrightCallback\n");
148 if (manager.everyCustomRamp[_groupId][_modeId])
149 fprintf(stderr,
" - hasCustomRamp\n");
150 if (manager.state.rampHandler.animEffect)
151 fprintf(stderr,
" - has anim effect\n");
152 if (manager.everyRequireUserThread[_groupId][_modeId])
153 fprintf(stderr,
" - requireUserThread\n");
154 if (manager.everySystemCallbacks[_groupId][_modeId])
155 fprintf(stderr,
" - hasSystemCallbacks\n");
156 if (manager.everyButtonCustomUI[_groupId][_modeId])
157 fprintf(stderr,
" - hasButtonCustomUI\n");
175 auto manager = get_context();
176 auto& rampHandler = manager.state.rampHandler;
179 static bool isBrightnessLocked =
190 static uint32_t lockFirstCall = 0;
192 const bool isGoingDown = newBrightness <= latestBrightness;
194 bool shouldPreventBrightnessUpdate =
195 isGoingDown and isBrightnessLocked and newBrightness <= manager.lamp.ledMasking_brightnessThreshold;
197 if (not shouldPreventBrightnessUpdate)
203 if (lockFirstCall == 0)
207 if (lockFirstCall > 0 and
hal::time_ms() - lockFirstCall >= 2000)
208 shouldPreventBrightnessUpdate =
false;
212 if (isEndOfHoldEvent)
214 isBrightnessLocked =
false;
216 latestBrightness = newBrightness;
218 shouldPreventBrightnessUpdate =
false;
223 isBrightnessLocked = newBrightness > manager.lamp.ledMasking_brightnessThreshold;
225 return shouldPreventBrightnessUpdate;
238 if (not manager.state.isInFavoriteMockGroup && not manager.state.isInDeleteFavorite)
241 manager.animate_favorite_pick(holdDuration, 1500, isEndOfHoldEvent);
246 manager.animate_favorite_delete(holdDuration, 2000, isEndOfHoldEvent);
249 if (isEndOfHoldEvent)
251 manager.state.isInDeleteFavorite =
false;
259 if (not manager.state.isInFavoriteMockGroup)
262 if (isEndOfHoldEvent)
265 manager.state.lastScrollStopped = manager.lamp.now;
269 manager.handle_scroll_modes(holdDuration);
295 auto manager = get_context();
296 if (manager.get_hidden_groups_count() <= 0)
298 bsp::lampda_print(
"Cannot use bluetooth control without an hidden bluetooth group");
302 const auto bluetoothGroup = manager.template get_group_id_of_mode<modes::bluetooth::ColorControlMode>();
303 if (bluetoothGroup < 0)
309 const auto availableModes = manager.get_modes_count();
310 if (availableModes <= patternIndex)
316 manager.exit_favorite_group(manager.state.beforeFavoriteActiveIndex);
319 const bool alreadyInCorrectMode = patternIndex == 0 and manager.get_active_group() == bluetoothGroup and
320 manager.get_active_mode() == patternIndex;
322 if (not alreadyInCorrectMode)
324 manager.set_active_group(bluetoothGroup);
325 manager.set_active_mode(patternIndex);
329 const int trueIndex = manager.template get_mode_id<modes::bluetooth::ColorControlMode>();
330 assert(trueIndex == 0 &&
"Bluetooth group should have the programmable color output as the first group index");
333 if (patternIndex == 0)
335 auto& modeState = manager.template get_state_of_mode<modes::bluetooth::ColorControlMode>();
338 const uint16_t compressedColor = modes::colors::get_compressed_16b_color(requestedColor);
339 const uint8_t ramp = (compressedColor >> 8) & 0xFF;
340 const uint8_t index = compressedColor & 0xFF;
341 manager.set_active_custom_ramp(ramp);
342 manager.set_active_custom_index(index);
356 auto manager = get_context();
357 if (favoriteIndex >= manager.state.usedFavoriteCount)
359 bsp::lampda_print(
"Cannot jump to favorite %d, max index should be less than %d",
361 manager.state.usedFavoriteCount);
365 manager.jump_to_favorite(favoriteIndex,
true);
378 auto manager = get_context();
381 if (manager.state.isInFavoriteMockGroup)
384 const uint8_t allowedFavorites = manager.get_number_of_allowed_favorites();
387 if (favoriteIndex >= allowedFavorites)
389 bsp::lampda_print(
"Max favorites is [0; %d[, cannot add index %d", allowedFavorites, favoriteIndex);
394 manager.set_current_mode_as_favorite(favoriteIndex, 1000);
402 if (default_behaviors::handle_user_command(command))
408 switch (command.get_type())
414 __private::handle_pattern_select_command(0, color);
423 __private::handle_pattern_select_command(index);
432 __private::handle_go_to_favorite_command(index);
441 __private::handle_set_favorite_command(index);
Define assertions helpers.
Define a command destined to the user layer. Command can only be created by the static make_* methods...
Definition: user_commands.h:21
bool parse_set_favorite_command(uint8_t &index) const
Parse the current command for a set favorite.
Definition: user_commands.cpp:341
bool parse_set_ble_mode_command(uint8_t &index) const
Parse the current command for a set current mode to BLE mode.
Definition: user_commands.cpp:325
bool parse_go_to_favorite_command(uint8_t &index) const
Parse the current command for a go to favorite.
Definition: user_commands.cpp:333
@ SetBleMode
switch to a target BLE mode
@ SetBleCustomColorMode
switch to BLE custom color mode
@ SetFavoriteIndex
Set the current mode to the target favorite index.
@ GoToFavoriteIndex
Go to the target favorite index.
bool parse_set_ble_custom_color_mode_command(uint32_t &color) const
Parse the current command for a set current mode to BLE custom color.
Definition: user_commands.cpp:316
Manipulate color representations.
void handle_set_favorite_command(const uint8_t favoriteIndex)
Set the current mode to favorite.
Definition: indexable_behavior.hpp:372
void handle_go_to_favorite_command(const uint8_t favoriteIndex)
Go to the given favorite index if possible.
Definition: indexable_behavior.hpp:350
void handle_pattern_select_command(const uint8_t patternIndex, const uint32_t requestedColor=0)
Handle a pattern change command.
Definition: indexable_behavior.hpp:288
void lampda_print(const char *format,...)
C linkage to print functions.
Definition: text_out.cpp:336
uint32_t time_ms(void)
Returns the number of milliseconds since the Arduino board began running the current program....
bool is_in_output_state()
return true if the system is in output mode
Definition: behavior.cpp:156
brightness_t get_brightness()
Return the current brightness value.
Definition: brightness_handle.cpp:42
bool button_hold(const uint8_t clicks, const bool isEndOfHoldEvent, const uint32_t holdDuration)
must be called by the lampda::user::button_hold_default
Definition: default_behavior.hpp:238
bool button_clicked(const uint8_t clicks)
must be called by the lampda::user::button_clicked_default
Definition: default_behavior.hpp:212
Contains code handling custom user mode functions for indexable strips.
Definition: default_behavior.hpp:23
bool button_clicked_default(const uint8_t)
Called to handle button click events for default user mode behaviors.
Definition: indexable_behavior.hpp:20
bool button_hold_default(const uint8_t, const bool, const uint32_t)
Called to handle button click+hold events for user mode behaviors.
Definition: indexable_behavior.hpp:166
void handle_user_command(const common::UserCommand &command)
Handle user command.
Definition: indexable_behavior.hpp:399
uint16_t brightness_t
Define the type of the brightness parameters.
Definition: constants.h:151