5#ifndef DEFAULT_BEHAVIOR_MANAGER_HPP
6#define DEFAULT_BEHAVIOR_MANAGER_HPP
14#include "src/system/component/time_handling.h"
35 auto manager = get_context();
38 manager.lamp.startup();
41 manager.power_on_sequence();
47 auto manager = get_context();
48 manager.power_off_sequence();
52 manager.lamp.show_now();
55 ensure_build_canary();
60 auto manager = get_context();
63 if (brightness <= ::lampda::brightness::absoluteMaximumBrightness)
66 manager.lamp.align_internal_to_system_brightness();
69 manager.lamp.setBrightness(brightness,
true,
true);
72 manager.brightness_update(brightness);
77 manager.lamp.enforce_internal_brightness_limits();
83 auto manager = get_context();
86 manager.sunset_update(progress);
91 auto manager = get_context();
92 manager.write_parameters();
97 auto manager = get_context();
98 manager.read_parameters();
109 if (not isEndOfHoldEvent and holdDuration > 0)
112 auto manager = get_context();
113 if (manager.overlay_animate_ramp(
114 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::White, modes::colors::Red>))
117 manager.state.isSunsetTimingPending = 2;
131 auto manager = get_context();
132 return manager.custom_click(clicks);
137 auto manager = get_context();
138 return manager.custom_hold(clicks, isEndOfHoldEvent, holdDuration);
143 auto manager = get_context();
147 manager.lamp.signal_display();
152#ifdef LMBD_LAMP_TYPE__INDEXABLE
156 auto manager = get_context();
157 return manager.should_spawn_thread();
163 auto manager = get_context();
166 if (manager.should_spawn_thread())
167 manager.user_thread();
171namespace default_behaviors {
176 auto manager = get_context();
182 if (manager.state.isInFavoriteMockGroup)
184 manager.state.isInFavoriteMockGroup =
false;
187 manager.set_active_group(0);
188 manager.set_active_mode(0);
199bool button_hold(
const uint8_t clicks,
const bool isEndOfHoldEvent,
const uint32_t holdDuration)
201 auto manager = get_context();
202 auto& rampHandler = manager.state.rampHandler;
208 if (not manager.state.isInFavoriteMockGroup)
210 rampHandler.update_ramp(manager.get_active_custom_ramp(), holdDuration, [&](uint8_t rampValue) {
211 manager.custom_ramp_update(rampValue);
212 manager.set_active_custom_ramp(rampValue);
224 auto manager = get_context();
225 if (manager.overlay_animate_ramp(
226 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::White, modes::colors::Red>))
229 manager.state.isSunsetTimingPending = 2;
237 if (not isEndOfHoldEvent and holdDuration > 0)
239 auto manager = get_context();
240 if (manager.overlay_animate_ramp(
241 holdDuration, 5000, modes::colors::PaletteGradient<modes::colors::Red, modes::colors::Red>))
248 const bool shouldSaveUserParameters =
false;
249 logic::behavior::internal::handle_shutdown_state(shouldSaveUserParameters);
257 if (not isEndOfHoldEvent and holdDuration > 0)
259 auto manager = get_context();
260 if (manager.overlay_animate_ramp(
261 holdDuration, 5000, modes::colors::PaletteGradient<modes::colors::Red, modes::colors::Red>))
268 const bool shouldSaveUserParameters =
false;
269 const bool shouldSaveSystemParameters =
false;
270 logic::behavior::internal::handle_shutdown_state(shouldSaveUserParameters, shouldSaveSystemParameters);
292 min<brightness_t>(::lampda::brightness::absoluteMaximumBrightness, requiredbrightness);
330 auto manager = get_context();
333 manager.custom_ramp_update(ramp, 1000);
335 manager.set_active_custom_ramp(ramp);
343 if (not time.is_valid())
362 if (not time.is_valid())
372 if (not realTime.is_valid())
374 bsp::lampda_print(
"Refusing sunset command %d %dh %dm %ds : Time is not synchronized yet.",
383 if (internalLampActionTime <= 0)
385 bsp::lampda_print(
"Refusing sunset command %d %dh %dm %ds: Target time is incoherent",
400void handle_mode_control(
const uint8_t groupIndex,
const uint8_t modeIndex)
406 auto manager = get_context();
408 if (groupIndex >= manager.get_groups_count())
410 bsp::lampda_print(
"Group id %d is greater than max group id %d", groupIndex, manager.get_groups_count());
413 manager.set_active_group(groupIndex);
415 if (modeIndex >= manager.get_modes_count())
417 bsp::lampda_print(
"Mode id %d is greater than max mode id %d", modeIndex, manager.get_modes_count());
420 manager.set_active_mode(modeIndex);
426bool handle_user_command(
const common::UserCommand& command)
428 switch (command.get_type())
433 if (command.parse_ramp(ramp))
434 __private::handle_ramp_command(ramp);
442 if (command.parse_brightness(brgt))
443 __private::handle_brightness_control(brgt);
452 if (command.parse_set_mode(groupId, modeId))
453 __private::handle_mode_control(groupId, modeId);
461 if (command.parse_turn_onoff(shouldTurnOn))
462 __private::handle_on_off_command(shouldTurnOn);
469 component::time::RealTime time;
470 if (command.parse_set_real_time_command(time))
471 __private::handle_set_time_command(time);
478 component::time::RealTime time;
479 if (command.parse_set_sunset_to_time_command(time))
480 __private::handle_sunset_to_time_command(time);
@ SetSunsetToTime
set the sunset to a target real time
@ SetRealTime
set the system real time
@ Brightness
set brightness
@ SetUserRamp
set the user ramp value
void clear(const Type type)
Clear an alert.
Definition: alerts.cpp:902
void handle_sunset_to_time_command(const component::time::RealTime &time)
Handle the timing command.
Definition: default_behavior.hpp:360
void handle_on_off_command(const bool shouldBeOn)
handle the On or Off command
Definition: default_behavior.hpp:304
void handle_brightness_control(const brightness_t requiredbrightness)
handle the brightness command
Definition: default_behavior.hpp:283
void handle_set_time_command(const component::time::RealTime &time)
Handle the set time command.
Definition: default_behavior.hpp:341
void handle_ramp_command(const uint8_t ramp)
Handle the ramp command.
Definition: default_behavior.hpp:324
void lampda_print(const char *format,...)
C linkage to print functions.
Definition: text_out.cpp:206
void clear_internal_fs()
hard clean of the whole filesystem, you will loose all stored data.
Definition: fileSystem.cpp:90
uint32_t get_platform_time_from_target_time(const RealTime &time)
Given a real time, return the expected platform time in seconds at wich it will be reached.
Definition: time_handling.cpp:92
RealTime get_real_time()
Return a real time value, only valid if it was set first.
Definition: time_handling.cpp:90
bool set_real_time(const RealTime &realTime)
Set the real time in the lamp, only valid until the next shutdown.
Definition: time_handling.cpp:75
void set_power_on()
set system state to "output on"
Definition: behavior.cpp:125
void set_power_off()
set system state to "output off". Can be ignored
Definition: behavior.cpp:126
bool is_in_output_state()
return true if the system is in output mode
Definition: behavior.cpp:139
void update_brightness(const brightness_t newBrightness, const bool shouldCallUserBrightnessCallback)
update the internal brightness values
Definition: brightness_handle.cpp:66
void update_saved_brightness()
Update the saved brightness value with the current brightness.
Definition: brightness_handle.cpp:58
void lock_brightness_update(bool shouldLock)
Lock the hability of the sunset timer to control the brightness.
Definition: sunset_timer.cpp:245
void set_deadline(const uint32_t timeshutdown_s)
Set the sunset timer to a known clock deadline, replacing existing timers. Start the timer,...
Definition: sunset_timer.cpp:128
void bump_timer()
signal to the timer that some time may be added. Only add time if the timer is in the fadeout phase
Definition: sunset_timer.cpp:204
bool is_enabled()
Indicate if the sunset is set.
Definition: sunset_timer.cpp:24
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:199
bool button_clicked(const uint8_t clicks)
must be called by the lampda::user::button_clicked_default
Definition: default_behavior.hpp:174
Contains code handling custom user mode functions for indexable strips.
Definition: default_behavior.hpp:17
bool button_clicked_default(const uint8_t)
Called to handle button click events for default user mode behaviors.
Definition: indexable_behavior.hpp:18
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:168
void brightness_update(const brightness_t brightness)
Called when the system changes the LED strip brightness.
Definition: default_behavior.hpp:58
void user_thread()
Called at each tick of the secondary thread.
Definition: default_behavior.hpp:161
bool button_hold_usermode(const uint8_t clicks, const bool isEndOfHoldEvent, const uint32_t holdDuration)
Called to handle button click+hold events if "usermode UI" is on.
Definition: default_behavior.hpp:135
bool button_start_hold_default(const uint8_t clicks, const bool isEndOfHoldEvent, const uint32_t holdDuration)
Called to handle button hold on lamp start.
Definition: default_behavior.hpp:103
void read_parameters()
Called when system wants to read parameters from filesystem.
Definition: default_behavior.hpp:95
void power_on_sequence()
Called when the system powers on (must be non blocking function!)
Definition: default_behavior.hpp:33
void sunset_timer_update(const float progress)
Called when the sunset timer progresses [0; 1].
Definition: default_behavior.hpp:81
bool button_start_click_default(const uint8_t clicks)
Called to handle button click on lamp start.
Definition: default_behavior.hpp:101
bool should_spawn_thread()
Determine if a second user_thread() loop thread should be spawned.
Definition: default_behavior.hpp:150
void write_parameters()
Called when system wants to write parameters to filesystem.
Definition: default_behavior.hpp:89
void power_off_sequence()
Called when the system powers off (must be non blocking function!)
Definition: default_behavior.hpp:44
void loop()
Called at each tick of the main loop.
Definition: default_behavior.hpp:141
bool button_clicked_usermode(const uint8_t clicks)
Called to handle button click events if "usermode UI" is active.
Definition: default_behavior.hpp:129
uint16_t brightness_t
Define the type of the brightness parameters.
Definition: constants.h:147
Store a real time reference.
Definition: time_handling.h:18
uint8_t minutes
minutes, in the [0; 59]
Definition: time_handling.h:30
uint8_t dayOfTheWeek
[0; 6] the index of the day of the week. Monday is 0, sunday is 6
Definition: time_handling.h:28
uint8_t hour
hour of the day, in the [0; 23]
Definition: time_handling.h:29
uint8_t seconds
seconds, in the [0; 59]
Definition: time_handling.h:31