Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
default_behavior.hpp
Go to the documentation of this file.
1
5#ifndef DEFAULT_BEHAVIOR_MANAGER_HPP
6#define DEFAULT_BEHAVIOR_MANAGER_HPP
7
8//
9// note: this code is included as-is by:
10// - src/modes/user/indexable_behavior.hpp
11// - src/modes/user/simple_behavior.hpp
12//
13
14#include <cstdint>
15namespace lampda::user {
16
17//
18// These are defined in src/modes/user/{flavor}_behavior.hpp
19//
20
21void button_clicked_default(const uint8_t);
22
23void button_hold_default(const uint8_t, const bool, const uint32_t);
24
25//
26// These callbacks are the same for all lamp flavors*
27//
28
29// *meaning: LampTy is the one doing the "polyfill" between flavors
30
32{
33 auto manager = get_context();
34
35 // initialize the lamp object
36 manager.lamp.startup();
37
38 // callbacks
39 manager.power_on_sequence();
40}
41
43{
44 // callbacks
45 auto manager = get_context();
46 manager.power_off_sequence();
47
48 // clear lamp on power-off
49 manager.lamp.clear();
50 manager.lamp.show_now();
51
52 // (no-op) internal symbol used during build
53 ensure_build_canary();
54}
55
56void brightness_update(const brightness_t brightness)
57{
58 auto manager = get_context();
59
60 // dont handle invalid commands
61 if (brightness <= ::lampda::brightness::absoluteMaximumBrightness)
62 {
63 // force update of the internal references
64 manager.lamp.align_internal_to_system_brightness();
65
66 // set brightness for underlying object (w/o re-entry in update_brightness)
67 manager.lamp.setBrightness(brightness, true, true);
68
69 // callbacks
70 manager.brightness_update(brightness);
71 }
72 else
73 {
74 // this call could be just a max brigthness update
75 manager.lamp.enforce_internal_brightness_limits();
76 }
77}
78
79void sunset_timer_update(const float progress)
80{
81 auto manager = get_context();
82
83 // callbacks
84 manager.sunset_update(progress);
85}
86
88{
89 auto manager = get_context();
90 manager.write_parameters();
91}
92
94{
95 auto manager = get_context();
96 manager.read_parameters();
97}
98
99bool button_start_click_default(const uint8_t clicks) { return false; }
100
101bool button_start_hold_default(const uint8_t clicks, const bool isEndOfHoldEvent, const uint32_t holdDuration)
102{
103 switch (clicks)
104 {
105 case 5:
106 {
107 if (not isEndOfHoldEvent and holdDuration > 0)
108 {
109 // sunset timer !
110 auto manager = get_context();
111 if (manager.overlay_animate_ramp(
112 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::White, modes::colors::Red>))
113 {
114 // update the sunset timing
115 manager.state.isSunsetTimingPending = 2;
116 }
117
118 return true;
119 }
120 break;
121 }
122 }
123
124 return false;
125}
126
127bool button_clicked_usermode(const uint8_t clicks)
128{
129 auto manager = get_context();
130 return manager.custom_click(clicks);
131}
132
133bool button_hold_usermode(const uint8_t clicks, const bool isEndOfHoldEvent, const uint32_t holdDuration)
134{
135 auto manager = get_context();
136 return manager.custom_hold(clicks, isEndOfHoldEvent, holdDuration);
137}
138
139void loop()
140{
141 auto manager = get_context();
142 manager.loop();
143
144 // signal display update every loop
145 manager.lamp.signal_display();
146}
147
149{
150#ifdef LMBD_LAMP_TYPE__INDEXABLE
152 return true;
153#else
154 auto manager = get_context();
155 return manager.should_spawn_thread();
156#endif
157}
158
160{
161 auto manager = get_context();
162 manager.lamp.show();
163
164 if (manager.should_spawn_thread())
165 manager.user_thread();
166}
167
169namespace default_behaviors {
170
172bool button_clicked(const uint8_t clicks)
173{
174 auto manager = get_context();
175
176 switch (clicks)
177 {
178 case 6: // 6 clicks: jump to first mode of first category
179 {
180 if (manager.state.isInFavoriteMockGroup)
181 { // reset favorite indicator
182 manager.state.isInFavoriteMockGroup = false;
183 }
184 // return to first state
185 manager.set_active_group(0);
186 manager.set_active_mode(0);
187 manager.blip(250);
188 return true;
189 }
190 }
191
192 // nothing
193 return false;
194}
195
197bool button_hold(const uint8_t clicks, const bool isEndOfHoldEvent, const uint32_t holdDuration)
198{
199 auto manager = get_context();
200 auto& rampHandler = manager.state.rampHandler;
201
202 switch (clicks)
203 {
204 case 3: // 3 click+hold: configure custom ramp
205 // no ramps in favorite group
206 if (not manager.state.isInFavoriteMockGroup)
207 {
208 rampHandler.update_ramp(manager.get_active_custom_ramp(), holdDuration, [&](uint8_t rampValue) {
209 manager.custom_ramp_update(rampValue);
210 manager.set_active_custom_ramp(rampValue);
211 });
212 return true;
213 }
214 break;
215
216 // 5 click+hold: Add 5 minutes to sunset timer
217 case 5:
218 {
219 if (not isEndOfHoldEvent and holdDuration > 0 and logic::sunset::is_enabled())
220 {
221 // sunset timer !
222 auto manager = get_context();
223 if (manager.overlay_animate_ramp(
224 holdDuration, 1000, modes::colors::PaletteGradient<modes::colors::White, modes::colors::Red>))
225 {
226 // update the sunset timing
227 manager.state.isSunsetTimingPending = 2;
228 }
229 }
230 break;
231 }
232
233 case 13: // 13 clicks + hold: reset the whole system and stored parameters
234 {
235 if (not isEndOfHoldEvent and holdDuration > 0)
236 {
237 auto manager = get_context();
238 if (manager.overlay_animate_ramp(
239 holdDuration, 5000, modes::colors::PaletteGradient<modes::colors::Red, modes::colors::Red>))
240 {
241 // reset the file system and memory
242 platform::lampda_print("clearing the whole file format");
244
245 // shutdown the lamp
246 const bool shouldSaveUserParameters = false;
247 logic::behavior::internal::handle_shutdown_state(shouldSaveUserParameters);
248 }
249 }
250 break;
251 }
252
253 case 20: // 20 clicks + hold: reset the whole system and stored parameters
254 {
255 if (not isEndOfHoldEvent and holdDuration > 0)
256 {
257 auto manager = get_context();
258 if (manager.overlay_animate_ramp(
259 holdDuration, 5000, modes::colors::PaletteGradient<modes::colors::Red, modes::colors::Red>))
260 {
261 // reset the file system and memory
262 platform::lampda_print("clearing the whole file format");
264
265 // shutdown the lamp
266 const bool shouldSaveUserParameters = false;
267 const bool shouldSaveSystemParameters = false;
268 logic::behavior::internal::handle_shutdown_state(shouldSaveUserParameters, shouldSaveSystemParameters);
269 }
270 }
271 break;
272 }
273 }
274
275 return false;
276}
277
278namespace __private_elk {
279
281void handle_brigthness_control(const uint8_t requiredBrigthness)
282{
283 // ignore if not on
285 return;
286
287 // update brightness
288 static constexpr float brightnessMultiplier = ::lampda::brightness::absoluteMaximumBrightness / 100.0f;
289 const brightness_t desiredBrightness =
290 min<brightness_t>(::lampda::brightness::absoluteMaximumBrightness,
291 static_cast<brightness_t>(requiredBrigthness * brightnessMultiplier));
292 // Update the system brightness for real, not the temporary. We want the changes to be saved
293 logic::brightness::update_brightness(desiredBrightness);
294 // update saved brightness
296 // and change the sunset timer if needed
298}
299
301void handle_on_off_command(const bool shouldBeOn)
302{
303 // turn on
304 if (shouldBeOn)
305 {
308 }
309 // turn off
310 else
311 {
314 }
315}
316
321void handle_speed_command(const uint8_t speed)
322{
323 // ignore if not on
325 return;
326
327 auto manager = get_context();
328
329 // update ramp value in modes
330 manager.custom_ramp_update(speed, 1000);
331 // override user choice
332 manager.set_active_custom_ramp(speed);
333}
334
335} // namespace __private_elk
336
337bool handle_elk_command(const utils::ELK::Package& elkControlCommand)
338{
339 switch (elkControlCommand.type)
340 {
342 {
343 __private_elk::handle_brigthness_control(elkControlCommand.data[0]);
344 return true;
345 }
347 {
348 const bool shouldTurnOn = elkControlCommand.data[0] > 0;
349 __private_elk::handle_on_off_command(shouldTurnOn);
350 return true;
351 }
353 {
354 const uint8_t speed = (elkControlCommand.data[0] / 100.0) * UINT8_MAX;
355 __private_elk::handle_speed_command(speed);
356 return true;
357 }
358 default:
359 return false;
360 }
361}
362
363} // namespace default_behaviors
364
365} // namespace lampda::user
366
367#endif
void clear(const Type type)
Clear an alert.
Definition: alerts.cpp:882
void handle_on_off_command(const bool shouldBeOn)
handle the On or Off command
Definition: default_behavior.hpp:301
void handle_brigthness_control(const uint8_t requiredBrigthness)
handle the brightness command
Definition: default_behavior.hpp:281
void handle_speed_command(const uint8_t speed)
Handle the speed command.
Definition: default_behavior.hpp:321
void set_power_on()
set system state to "output on"
Definition: behavior.cpp:123
void set_power_off()
set system state to "output off". Can be ignored
Definition: behavior.cpp:124
bool is_in_output_state()
return true if the system is in output mode
Definition: behavior.cpp:137
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 bump_timer()
signal to the timer that some time must be added. Limited to 10 minutes
Definition: sunset_timer.cpp:154
bool is_enabled()
True if timer is running.
Definition: sunset_timer.cpp:189
void clear_internal_fs()
hard clean of the whole filesystem, you will loose all stored data.
Definition: fileSystem.cpp:89
void lampda_print(const char *format,...)
Definition: print.cpp:43
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:197
bool button_clicked(const uint8_t clicks)
must be called by the lampda::user::button_clicked_default
Definition: default_behavior.hpp:172
Contains code handling custom user mode functions for indexable strips.
Definition: default_behavior.hpp:15
void brightness_update(const brightness_t brightness)
Called when the system changes the LED strip brightness.
Definition: default_behavior.hpp:56
void user_thread()
Called at each tick of the secondary thread.
Definition: default_behavior.hpp:159
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:133
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:101
void read_parameters()
Called when system wants to read parameters from filesystem.
Definition: default_behavior.hpp:93
void power_on_sequence()
Called when the system powers on (must be non blocking function!)
Definition: default_behavior.hpp:31
void sunset_timer_update(const float progress)
Called when the sunset timer progresses [0; 1].
Definition: default_behavior.hpp:79
bool button_start_click_default(const uint8_t clicks)
Called to handle button click on lamp start.
Definition: default_behavior.hpp:99
bool should_spawn_thread()
Determine if a second user_thread() loop thread should be spawned.
Definition: default_behavior.hpp:148
void write_parameters()
Called when system wants to write parameters to filesystem.
Definition: default_behavior.hpp:87
void 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:118
void power_off_sequence()
Called when the system powers off (must be non blocking function!)
Definition: default_behavior.hpp:42
void loop()
Called at each tick of the main loop.
Definition: default_behavior.hpp:139
void button_clicked_default(const uint8_t)
Called to handle button click events for default user mode behaviors.
Definition: indexable_behavior.hpp:18
bool button_clicked_usermode(const uint8_t clicks)
Called to handle button click events if "usermode UI" is active.
Definition: default_behavior.hpp:127
@ PATTERN_SPEED
set the current pattern speed
@ BRIGHTNESS
set a brightness
@ ONOFF
turn output onand off
uint16_t brightness_t
Define the type of the brightness parameters.
Definition: constants.h:147
Definition: elk_decoder.h:33