5#ifndef DEFAULT_BEHAVIOR_MANAGER_HPP
6#define DEFAULT_BEHAVIOR_MANAGER_HPP
14#include "src/system/component/time_handling.h"
36 auto manager = get_context();
39 manager.lamp.startup();
42 manager.power_on_sequence();
48 auto manager = get_context();
49 manager.power_off_sequence();
53 manager.lamp.show_now();
56 ensure_build_canary();
61 auto manager = get_context();
64 if (brightness <= ::lampda::brightness::absoluteMaximumBrightness)
67 manager.lamp.align_internal_to_system_brightness();
70 manager.lamp.setBrightness(brightness,
true,
true);
73 manager.brightness_update(brightness);
78 manager.lamp.enforce_internal_brightness_limits();
84 auto manager = get_context();
87 manager.sunset_update(progress);
92 auto manager = get_context();
93 manager.write_parameters();
98 auto manager = get_context();
99 manager.read_parameters();
112 auto manager = get_context();
122 if (hasPairedDeviceAndNoPairing)
124 if (manager.overlay_animate_ramp(
125 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::Blue, modes::colors::Blue>))
137 const bool isNotCurrentlyPairing =
140 if (isNotCurrentlyPairing and
141 manager.overlay_animate_ramp(
142 holdDuration, 3000, modes::colors::PaletteGradient<modes::colors::Blue, modes::colors::Blue>))
154 if (not isEndOfHoldEvent and holdDuration > 0)
157 auto manager = get_context();
158 if (manager.overlay_animate_ramp(
159 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::White, modes::colors::Red>))
162 manager.state.isSunsetTimingPending = 2;
176 auto manager = get_context();
177 return manager.custom_click(clicks);
182 auto manager = get_context();
183 return manager.custom_hold(clicks, isEndOfHoldEvent, holdDuration);
188 auto manager = get_context();
192 manager.lamp.signal_display();
197#ifdef LMBD_LAMP_TYPE__INDEXABLE
201 auto manager = get_context();
202 return manager.should_spawn_thread();
208 auto manager = get_context();
211 if (manager.should_spawn_thread())
212 manager.user_thread();
216namespace default_behaviors {
221 auto manager = get_context();
228 manager.exit_favorite_group(manager.state.beforeFavoriteActiveIndex);
231 manager.set_active_group(0);
232 manager.set_active_mode(0);
247bool button_hold(
const uint8_t clicks,
const bool isEndOfHoldEvent,
const uint32_t holdDuration)
249 auto manager = get_context();
250 auto& rampHandler = manager.state.rampHandler;
256 if (not manager.state.isInFavoriteMockGroup)
258 rampHandler.update_ramp(manager.get_active_custom_ramp(), holdDuration, [&](uint8_t rampValue) {
259 manager.custom_ramp_update(rampValue);
260 manager.set_active_custom_ramp(rampValue);
272 auto manager = get_context();
273 if (manager.overlay_animate_ramp(
274 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::White, modes::colors::Red>))
277 manager.state.isSunsetTimingPending = 2;
285 if (not isEndOfHoldEvent and holdDuration > 0)
287 auto manager = get_context();
288 if (manager.overlay_animate_ramp(
289 holdDuration, 5000, modes::colors::PaletteGradient<modes::colors::Red, modes::colors::Red>))
302 if (not isEndOfHoldEvent and holdDuration > 0)
304 auto manager = get_context();
305 if (manager.overlay_animate_ramp(
306 holdDuration, 5000, modes::colors::PaletteGradient<modes::colors::Red, modes::colors::Red>))
313 logic::behavior::internal::handle_shutdown_state();
335 min<brightness_t>(::lampda::brightness::absoluteMaximumBrightness, requiredbrightness);
373 auto manager = get_context();
376 manager.custom_ramp_update(ramp, 1000);
378 manager.set_active_custom_ramp(ramp);
386 if (not time.is_valid())
405 if (not time.is_valid())
415 if (not realTime.is_valid())
417 bsp::lampda_print(
"Refusing sunset command %d %dh %dm %ds : Time is not synchronized yet.",
426 if (internalLampActionTime <= 0)
428 bsp::lampda_print(
"Refusing sunset command %d %dh %dm %ds: Target time is incoherent",
443void handle_mode_control(
const uint8_t groupIndex,
const uint8_t modeIndex)
449 auto manager = get_context();
451 if (groupIndex >= manager.get_groups_count())
453 bsp::lampda_print(
"Group id %d is greater than max group id %d", groupIndex, manager.get_groups_count());
456 manager.set_active_group(groupIndex);
458 if (modeIndex >= manager.get_modes_count())
460 bsp::lampda_print(
"Mode id %d is greater than max mode id %d", modeIndex, manager.get_modes_count());
463 manager.set_active_mode(modeIndex);
469bool handle_user_command(
const common::UserCommand& command)
471 switch (command.get_type())
476 if (command.parse_ramp(ramp))
477 __private::handle_ramp_command(ramp);
485 if (command.parse_brightness(brgt))
486 __private::handle_brightness_control(brgt);
495 if (command.parse_set_mode(groupId, modeId))
496 __private::handle_mode_control(groupId, modeId);
504 if (command.parse_turn_onoff(shouldTurnOn))
505 __private::handle_on_off_command(shouldTurnOn);
512 component::time::RealTime time;
513 if (command.parse_set_real_time_command(time))
514 __private::handle_set_time_command(time);
521 component::time::RealTime time;
522 if (command.parse_set_sunset_to_time_command(time))
523 __private::handle_sunset_to_time_command(time);
Interface for the platform specific bluetooth.
@ 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:403
void handle_on_off_command(const bool shouldBeOn)
handle the On or Off command
Definition: default_behavior.hpp:347
void handle_brightness_control(const brightness_t requiredbrightness)
handle the brightness command
Definition: default_behavior.hpp:326
void handle_set_time_command(const component::time::RealTime &time)
Handle the set time command.
Definition: default_behavior.hpp:384
void handle_ramp_command(const uint8_t ramp)
Handle the ramp command.
Definition: default_behavior.hpp:367
bool format()
Delete the user file.
Definition: filesystem.cpp:338
void clear_internal_fs()
hard clean of the whole filesystem, you will loose all stored data.
Definition: filesystem.cpp:48
void lampda_print(const char *format,...)
C linkage to print functions.
Definition: text_out.cpp:227
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
bool is_open_to_all()
Return true if the device is advertising, and all can connect.
bool is_advertising()
Return true is the bluetooth is visible by other devices.
void start_advertising(bool allowUnknownConnections)
start the advertising sequence (with a timeout)
bool is_connected()
Return true if a bluetooth user is connected.
bool is_bounded()
Return true if the bluetooth connection is bounded to a pair.
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:247
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:130
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:206
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:247
bool button_clicked(const uint8_t clicks)
must be called by the lampda::user::button_clicked_default
Definition: default_behavior.hpp:219
Contains code handling custom user mode functions for indexable strips.
Definition: default_behavior.hpp:18
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:157
void brightness_update(const brightness_t brightness)
Called when the system changes the LED strip brightness.
Definition: default_behavior.hpp:59
void user_thread()
Called at each tick of the secondary thread.
Definition: default_behavior.hpp:206
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:180
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:104
void read_parameters()
Called when system wants to read parameters from filesystem.
Definition: default_behavior.hpp:96
void power_on_sequence()
Called when the system powers on (must be non blocking function!)
Definition: default_behavior.hpp:34
void sunset_timer_update(const float progress)
Called when the sunset timer progresses [0; 1].
Definition: default_behavior.hpp:82
bool button_start_click_default(const uint8_t clicks)
Called to handle button click on lamp start.
Definition: default_behavior.hpp:102
bool should_spawn_thread()
Determine if a second user_thread() loop thread should be spawned.
Definition: default_behavior.hpp:195
void write_parameters()
Called when system wants to write parameters to filesystem.
Definition: default_behavior.hpp:90
void power_off_sequence()
Called when the system powers off (must be non blocking function!)
Definition: default_behavior.hpp:45
void loop()
Called at each tick of the main loop.
Definition: default_behavior.hpp:186
bool button_clicked_usermode(const uint8_t clicks)
Called to handle button click events if "usermode UI" is active.
Definition: default_behavior.hpp:174
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