5#ifndef DEFAULT_BEHAVIOR_MANAGER_HPP
6#define DEFAULT_BEHAVIOR_MANAGER_HPP
14#include "src/system/component/time_handling.h"
41 auto manager = get_context();
44 manager.lamp.startup();
47 manager.power_on_sequence();
53 auto manager = get_context();
54 manager.power_off_sequence();
58 manager.lamp.show_now();
61 ensure_build_canary();
66 auto manager = get_context();
69 if (brightness <= ::lampda::brightness::absoluteMaximumBrightness)
73 manager.lamp.align_internal_to_system_brightness(brightness);
76 manager.lamp.setBrightness(brightness,
true,
true);
79 manager.brightness_update(brightness);
84 manager.lamp.enforce_internal_brightness_limits();
90 auto manager = get_context();
91 manager.write_parameters();
96 auto manager = get_context();
97 manager.read_parameters();
110 auto manager = get_context();
117 const bool hasPairedDeviceAndNoPairing =
119 if (hasPairedDeviceAndNoPairing)
121 if (manager.overlay_animate_ramp(
122 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::Blue, modes::colors::Blue>))
135 if (isNotCurrentlyPairing and
136 manager.overlay_animate_ramp(
137 holdDuration, 3000, modes::colors::PaletteGradient<modes::colors::Blue, modes::colors::Blue>))
151 not isEndOfHoldEvent and holdDuration > 0)
153 auto manager = get_context();
154 if (manager.overlay_animate_ramp(
155 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::White, modes::colors::Red>))
158 manager.state.isSunsetTimingPending = 2;
169 auto manager = get_context();
170 return manager.custom_click(clicks);
175 auto manager = get_context();
176 return manager.custom_hold(clicks, isEndOfHoldEvent, holdDuration);
181 auto manager = get_context();
185 manager.lamp.signal_display();
190#ifdef LMBD_LAMP_TYPE__INDEXABLE
194 auto manager = get_context();
195 return manager.should_spawn_thread();
201 auto manager = get_context();
204 if (manager.should_spawn_thread())
205 manager.user_thread();
209namespace default_behaviors {
214 auto manager = get_context();
220 manager.exit_favorite_group(manager.state.beforeFavoriteActiveIndex);
223 manager.set_active_group(0);
224 manager.set_active_mode(0);
238bool button_hold(
const uint8_t clicks,
const bool isEndOfHoldEvent,
const uint32_t holdDuration)
240 auto manager = get_context();
241 auto& rampHandler = manager.state.rampHandler;
247 if (manager.overlay_animate_ramp(
248 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::White, modes::colors::Red>))
251 manager.state.isSunsetTimingPending = 2;
257 not manager.state.isInFavoriteMockGroup)
259 rampHandler.update_ramp(manager.get_active_custom_ramp(), holdDuration, [&](uint8_t rampValue) {
260 manager.custom_ramp_update(rampValue);
261 manager.set_active_custom_ramp(rampValue);
270 if (not isEndOfHoldEvent and holdDuration > 0)
272 auto manager = get_context();
273 if (manager.overlay_animate_ramp(
274 holdDuration, 5000, modes::colors::PaletteGradient<modes::colors::Red, modes::colors::Red>))
287 if (not isEndOfHoldEvent and holdDuration > 0)
289 auto manager = get_context();
290 if (manager.overlay_animate_ramp(
291 holdDuration, 5000, modes::colors::PaletteGradient<modes::colors::Red, modes::colors::Red>))
298 logic::behavior::internal::handle_shutdown_state();
317 logic::sun_timers_brightness_lock_all(
true);
320 min<brightness_t>(::lampda::brightness::absoluteMaximumBrightness, requiredbrightness);
325 logic::sun_timers_brightness_lock_all(
false);
358 auto manager = get_context();
361 manager.custom_ramp_update(ramp, 1000);
363 manager.set_active_custom_ramp(ramp);
371 if (not time.is_valid())
390 if (not time.is_valid())
400 if (not realTime.is_valid())
402 bsp::lampda_print(
"Refusing sunset command %d %dh %dm %ds : Time is not synchronized yet.",
411 if (internalLampActionTime <= 0)
413 bsp::lampda_print(
"Refusing sunset command %d %dh %dm %ds: Target time is incoherent",
433 if (not time.is_valid())
443 if (not realTime.is_valid())
445 bsp::lampda_print(
"Refusing sunrise command %d %dh %dm %ds : Time is not synchronized yet.",
454 if (internalLampActionTime <= 0)
456 bsp::lampda_print(
"Refusing sunrise command %d %dh %dm %ds: Target time is incoherent",
464 logic::sunrise_timer.set_deadline(internalLampActionTime);
467void handle_mode_control(
const uint8_t groupIndex,
const uint8_t modeIndex)
473 auto manager = get_context();
475 if (groupIndex >= manager.get_groups_count())
477 bsp::lampda_print(
"Group id %d is greater than max group id %d", groupIndex, manager.get_groups_count());
481 modes::ActiveIndexTy index {groupIndex, modeIndex, 0, 0};
482 manager.jump_to_new_active_index(index,
true,
true);
484 if (modeIndex >= manager.get_modes_count())
486 bsp::lampda_print(
"Mode id %d is greater than max mode id %d", modeIndex, manager.get_modes_count());
487 manager.set_active_mode(manager.get_modes_count() - 1);
496bool handle_user_command(
const common::UserCommand& command)
498 switch (command.get_type())
503 if (command.parse_ramp(ramp))
504 __private::handle_ramp_command(ramp);
512 if (command.parse_brightness(brgt))
513 __private::handle_brightness_control(brgt);
522 if (command.parse_set_mode(groupId, modeId))
523 __private::handle_mode_control(groupId, modeId);
531 if (command.parse_turn_onoff(shouldTurnOn))
532 __private::handle_on_off_command(shouldTurnOn);
539 component::time::RealTime time;
540 if (command.parse_set_real_time_command(time))
541 __private::handle_set_time_command(time);
548 component::time::RealTime time;
549 if (command.parse_set_sunset_to_time_command(time))
550 __private::handle_sunset_to_time_command(time);
557 component::time::RealTime time;
558 if (command.parse_set_sunrise_to_time_command(time))
559 __private::handle_sunrise_to_time_command(time);
568 if (command.parse_signal_sunset_progress_command(progress))
569 get_context().sunset_update(progress);
577 if (command.parse_signal_sunrise_progress_command(progress))
578 get_context().sunrise_update(progress);
585 if (command.parse_signal_suntimer_cancel_command())
588 manager.sunset_timer_internal_reset();
589 manager.sunrise_timer_internal_reset();
598 if (command.parse_disable_sun_timer_command(isSunrise))
603 logic::sunrise_timer.cancel_at_current_brightness();
619 if (command.parse_set_mic_sensitivity_command(sensitivity))
627 uint32_t duration_ms;
628 if (command.parse_blip_output_command(duration_ms))
632 if (
manager.state.skipNextFrameEffect == 0)
641 if (command.parse_print_status_command())
654 manager.get_active_custom_ramp(),
655 manager.state.isInFavoriteMockGroup ?
'F' :
'X',
665 if (command.parse_print_favorites_command())
669 manager.state.usedFavoriteCount,
670 manager.state.maxFavoriteCount,
671 manager.state.isInFavoriteMockGroup ?
"true" :
"false");
Handle the output LED strip brightness.
Usage interface for the bluetooth system.
@ SetSunsetToTime
set the sunset to a target real time
@ SignalSunriseProgress
Signal a sunrise update event.
@ PrintStatus
Print the current mode, group, ramp, brightness and favorite state on one line.
@ SetRealTime
set the system real time
@ PrintFavorites
Print the favorite mode count.
@ DisableSunTimer
Disable the sunset or the sunrise timer.
@ Brightness
set brightness
@ BlipOutput
Turn off the output for a short time.
@ SetMicSensitivity
Set the microphone sensitivity.
@ SignalSunsetProgress
Signal a sunset update event.
@ SignalSuntimerCancel
Signal that a suntimer was canceled.
@ SetUserRamp
set the user ramp value
@ SetSunriseToTime
set the sunrise to a target real time
bool cancel()
Cancel the current active timer.
Definition: sun_timers.cpp:20
void set_deadline(const uint32_t timeshutdown_s)
Start the sun timer with a deadline, or prolonge the current sun timer.
Definition: sun_timers.cpp:51
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: sun_timers.cpp:254
void clear(const Type type)
Clear an alert.
Definition: alerts.cpp:1063
void handle_sunset_to_time_command(const component::time::RealTime &time)
Handle the timing command.
Definition: default_behavior.hpp:388
void handle_on_off_command(const bool shouldBeOn)
handle the On or Off command
Definition: default_behavior.hpp:332
void handle_brightness_control(const brightness_t requiredbrightness)
handle the brightness command
Definition: default_behavior.hpp:311
void handle_set_time_command(const component::time::RealTime &time)
Handle the set time command.
Definition: default_behavior.hpp:369
void handle_sunrise_to_time_command(const component::time::RealTime &time)
Handle the timing command.
Definition: default_behavior.hpp:431
void handle_ramp_command(const uint8_t ramp)
Handle the ramp command.
Definition: default_behavior.hpp:352
bool is_connected()
Return true if a bluetooth user is connected.
Definition: ble.cpp:427
void force_pairing_mode()
Start the pairing mode (open to all devices), even if a device is connected. The connected device is ...
Definition: ble.cpp:538
bool is_advertising()
Return true is the bluetooth is visible by other devices.
Definition: ble.cpp:423
bool is_open_to_all()
Return true if the device is advertising, and all can connect.
Definition: ble.cpp:425
bool is_bounded()
Return true if the bluetooth connection is bounded to a pair.
Definition: ble.cpp:429
void start_advertising(bool allowUnknownConnections)
start the advertising sequence (with a timeout)
Definition: ble.cpp:492
bool format()
Delete the user file.
Definition: filesystem.cpp:372
void clear_internal_fs()
hard clean of the whole filesystem, you will loose all stored data.
Definition: filesystem.cpp:65
void lampda_print(const char *format,...)
C linkage to print functions.
Definition: text_out.cpp:336
void lampda_print_raw(const char *format,...)
C linkage to print functions.
Definition: text_out.cpp:320
void set_sensitivity(const uint8_t sensitivity)
Set the microphone sensitivity, applied as a gain on the raw samples (FFT, sound level and auto gain ...
Definition: sound.cpp:47
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:98
RealTime get_real_time()
Return a real time value, only valid if it was set first.
Definition: time_handling.cpp:96
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
AlertManager_t manager
Instanciation of the AlertManager.
Definition: alerts.cpp:30
void set_power_on()
set system state to "output on"
Definition: behavior.cpp:142
void set_power_off()
set system state to "output off". Can be ignored
Definition: behavior.cpp:143
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
void update_brightness(const brightness_t newBrightness, const bool shouldSkipUserCallbacks)
update the internal brightness values
Definition: brightness_handle.cpp:73
void update_saved_brightness()
Update the saved brightness value with the current brightness.
Definition: brightness_handle.cpp:61
SunsetTimer sunset_timer
object to access to control the timers
Definition: sun_timers.cpp:237
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 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
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:173
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:102
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
bool button_start_click_default(const uint8_t clicks)
Called to handle button click on lamp start.
Definition: default_behavior.hpp:100
bool should_spawn_thread()
Determine if a second user_thread() loop thread should be spawned.
Definition: default_behavior.hpp:188
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
bool button_clicked_usermode(const uint8_t clicks)
Called to handle button click events if "usermode UI" is active.
Definition: default_behavior.hpp:167
uint16_t brightness_t
Define the type of the brightness parameters.
Definition: constants.h:151
Interface for the physical components of the microphone.
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