Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
indexable_behavior.hpp
Go to the documentation of this file.
1
5#ifndef INDEXABLE_BEHAVIOR_MANAGER_H
6#define INDEXABLE_BEHAVIOR_MANAGER_H
7
9
11
12//
13// note: this code is included as-is by:
14// - user/indexable_functions.h
15//
16
17#include <cstdint>
18namespace lampda::user {
19
20bool button_clicked_default(const uint8_t clicks)
21{
22 // Handle default common behavior
24 {
25 // some event is already handled
26 return true;
27 }
28
29 auto manager = get_context();
30
31 switch (clicks)
32 {
33 case 2: // 2 clicks: next mode
34 {
35 if (manager.state.isInFavoriteMockGroup)
36 {
37 // if in favorite, next favorite
38 manager.state.lastFavoriteStep += 1;
39 // sanity check, if it fails, quit favorites
40 if (not manager.jump_to_favorite(manager.state.lastFavoriteStep, false))
41 {
42 manager.exit_favorite_group(manager.state.beforeFavoriteActiveIndex);
43 }
44 }
45 else
46 {
47 if (manager.state.isLastScrollAGroupChange and manager.lamp.now - manager.state.lastScrollStopped < 2000)
48 {
49 // last action was a group change & timer did not run out yet:
50 // change group
51 manager.next_group();
52
53 // prevent further jumps
54 manager.state.isLastScrollAGroupChange = false;
55 }
56 else
57 {
58 // else: just change mode
59 manager.next_mode();
60 }
61 }
62#ifdef LMBD_SIMULATION
63 {
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");
83 }
84#endif
85
86 return true;
87 }
88
89 case 3: // 3 clicks: next group or quit favorite group
90 {
91 if (manager.state.isInFavoriteMockGroup)
92 {
93 // return to previous state
94 manager.exit_favorite_group(manager.state.beforeFavoriteActiveIndex);
95 }
96 else
97 {
98 // true next group
99 manager.next_group();
100 }
101
102#ifdef LMBD_SIMULATION
103 {
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");
123 }
124#endif
125 return true;
126 }
127 }
128
129 // jump to favorite, on its configured clicks
131 {
132 if (not manager.state.isInFavoriteMockGroup)
133 {
134 // jump and save last used mode
135 manager.jump_to_favorite(manager.state.lastFavoriteStep, true);
136 }
137#ifdef LMBD_SIMULATION
138 {
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");
158 }
159#endif
160 return true;
161 }
162
163 return false;
164}
165
166bool button_hold_default(const uint8_t clicks, const bool isEndOfHoldEvent, const uint32_t holdDuration)
167{
168 // Handle default common behavior
169 if (default_behaviors::button_hold(clicks, isEndOfHoldEvent, holdDuration))
170 {
171 // some event is already handled
172 return true;
173 }
174
175 auto manager = get_context();
176 auto& rampHandler = manager.state.rampHandler;
177
179 static bool isBrightnessLocked =
180 logic::brightness::get_brightness() > ::lampda::brightness::absoluteMaximumBrightness * 0.25f;
181
182 switch (clicks)
183 {
184 // raise/lower brightness handle
185 case 1:
186 case 2:
187 {
188 const brightness_t newBrightness = logic::brightness::get_brightness();
189 static brightness_t latestBrightness = newBrightness;
190 static uint32_t lockFirstCall = 0;
191
192 const bool isGoingDown = newBrightness <= latestBrightness;
193 // prevent brigthness to drop below the dithering threshold
194 bool shouldPreventBrightnessUpdate =
195 isGoingDown and isBrightnessLocked and newBrightness <= manager.lamp.ledMasking_brightnessThreshold;
196
197 if (not shouldPreventBrightnessUpdate)
198 {
199 lockFirstCall = 0;
200 }
201 else
202 {
203 if (lockFirstCall == 0)
204 lockFirstCall = hal::time_ms();
205
206 // allow to go down after a set time
207 if (lockFirstCall > 0 and hal::time_ms() - lockFirstCall >= 2000)
208 shouldPreventBrightnessUpdate = false;
209 }
210
211 // end of hold: allow brigthness down
212 if (isEndOfHoldEvent)
213 {
214 isBrightnessLocked = false;
215 lockFirstCall = 0;
216 latestBrightness = newBrightness;
217 // release event shall always allow control
218 shouldPreventBrightnessUpdate = false;
219 }
220 // rising up: block next downward
221 if (not isGoingDown)
222 {
223 isBrightnessLocked = newBrightness > manager.lamp.ledMasking_brightnessThreshold;
224 }
225 return shouldPreventBrightnessUpdate;
226 }
227
228 default:
229 break;
230 }
231
233 {
234 case logic::button_bindings::Behavior::Favorite: // click+hold: configure favorite
235 {
236 // lock to prevent addition of favorite from favorite & if we are deleting favorites
237 // the second case can happen when the user delete all favorites
238 if (not manager.state.isInFavoriteMockGroup && not manager.state.isInDeleteFavorite)
239 {
240 // no new favorite in favorite
241 manager.animate_favorite_pick(holdDuration, 1500, isEndOfHoldEvent);
242 }
243 else
244 {
245 // remove current favorite
246 manager.animate_favorite_delete(holdDuration, 2000, isEndOfHoldEvent);
247 }
248
249 if (isEndOfHoldEvent)
250 {
251 manager.state.isInDeleteFavorite = false;
252 }
253 return true;
254 }
255
256 case logic::button_bindings::Behavior::Scroll: // click+hold: scroll across modes and group
257 {
258 // no scroll in favorite
259 if (not manager.state.isInFavoriteMockGroup)
260 {
261 // register end of scroll
262 if (isEndOfHoldEvent)
263 {
264 // update release time
265 manager.state.lastScrollStopped = manager.lamp.now;
266 }
267 else
268 {
269 manager.handle_scroll_modes(holdDuration);
270 }
271 }
272 return true;
273 }
274
275 default:
276 break;
277 }
278 return false;
279}
280
281namespace __private {
282
288void handle_pattern_select_command(const uint8_t patternIndex, const uint32_t requestedColor = 0)
289{
290 // ignore if not on
292 return;
293
294 // get manager and execute a mode or group change
295 auto manager = get_context();
296 if (manager.get_hidden_groups_count() <= 0)
297 {
298 bsp::lampda_print("Cannot use bluetooth control without an hidden bluetooth group");
299 return;
300 }
301
302 const auto bluetoothGroup = manager.template get_group_id_of_mode<modes::bluetooth::ColorControlMode>();
303 if (bluetoothGroup < 0)
304 {
305 bsp::lampda_print("Cannot find the bluetooth group and modes");
306 return;
307 }
308
309 const auto availableModes = manager.get_modes_count();
310 if (availableModes <= patternIndex)
311 {
312 bsp::lampda_print("Unimplemented pattern id %d", patternIndex);
313 return;
314 }
315
316 manager.exit_favorite_group(manager.state.beforeFavoriteActiveIndex);
317
318 // remove the mode change on the special case of mode 0 : custom color can be changed without changing the mode
319 const bool alreadyInCorrectMode = patternIndex == 0 and manager.get_active_group() == bluetoothGroup and
320 manager.get_active_mode() == patternIndex;
321
322 if (not alreadyInCorrectMode)
323 {
324 manager.set_active_group(bluetoothGroup);
325 manager.set_active_mode(patternIndex);
326 }
327
328 // sanity check
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");
331
332 // handle special programmable group mode
333 if (patternIndex == 0)
334 {
335 auto& modeState = manager.template get_state_of_mode<modes::bluetooth::ColorControlMode>();
336
337 // Convert color to compressed color
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);
343 }
344}
345
350void handle_go_to_favorite_command(const uint8_t favoriteIndex)
351{
352 // ignore if not on
354 return;
355
356 auto manager = get_context();
357 if (favoriteIndex >= manager.state.usedFavoriteCount)
358 {
359 bsp::lampda_print("Cannot jump to favorite %d, max index should be less than %d",
360 favoriteIndex,
361 manager.state.usedFavoriteCount);
362 return;
363 }
364 // jump and save last used mode
365 manager.jump_to_favorite(favoriteIndex, true);
366}
367
372void handle_set_favorite_command(const uint8_t favoriteIndex)
373{
374 // ignore if not on
376 return;
377
378 auto manager = get_context();
379
380 // Cannot add a favorite mode to te favorite group: we are already here
381 if (manager.state.isInFavoriteMockGroup)
382 return;
383
384 const uint8_t allowedFavorites = manager.get_number_of_allowed_favorites();
385
386 // not enought favorite set
387 if (favoriteIndex >= allowedFavorites)
388 {
389 bsp::lampda_print("Max favorites is [0; %d[, cannot add index %d", allowedFavorites, favoriteIndex);
390 return;
391 }
392
393 // Set the favorite !
394 manager.set_current_mode_as_favorite(favoriteIndex, 1000);
395}
396
397} // namespace __private
398
400{
401 // Handle default common behavior
402 if (default_behaviors::handle_user_command(command))
403 {
404 // some event is already handled
405 return;
406 }
407
408 switch (command.get_type())
409 {
411 {
412 uint32_t color;
414 __private::handle_pattern_select_command(0, color);
415 else
416 bsp::lampda_print("Failed to parse set_ble_custom_color command");
417 return;
418 }
420 {
421 uint8_t index;
422 if (command.parse_set_ble_mode_command(index))
423 __private::handle_pattern_select_command(index);
424 else
425 bsp::lampda_print("Failed to parse set_ble_mode command");
426 return;
427 }
429 {
430 uint8_t index;
431 if (command.parse_go_to_favorite_command(index))
432 __private::handle_go_to_favorite_command(index);
433 else
434 bsp::lampda_print("Failed to parse go_to_favorite command");
435 return;
436 }
438 {
439 uint8_t index;
440 if (command.parse_set_favorite_command(index))
441 __private::handle_set_favorite_command(index);
442 else
443 bsp::lampda_print("Failed to parse set_favorite command");
444 return;
445 }
446 default:
447 break;
448 }
449
450 bsp::lampda_print("Unsupported user command message type");
451}
452
453} // namespace lampda::user
454
455#endif
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
Behavior get_click_behavior(const uint8_t clicks)
Return the behavior whose click action is linked to this click count, or Behavior::None.
Definition: button_bindings.cpp:103
@ Favorite
click: jump to the favorites. hold: add or remove a favorite
@ Scroll
hold: scroll across modes and groups
Behavior get_hold_behavior(const uint8_t clicks)
Return the behavior whose hold action is linked to this click count, or Behavior::None.
Definition: button_bindings.cpp:114
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