Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
sun_timers.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstdint>
9
10namespace lampda {
11namespace logic {
12
15template<uint32_t BrightnessSunTime> class SunTimerBase
16{
17public:
18 static constexpr uint32_t BrightnessSunTime_min = BrightnessSunTime;
19 static constexpr uint32_t BrightnessSunTime_s = BrightnessSunTime_min * 60;
20 static constexpr uint32_t BrightnessSunTime_ms = BrightnessSunTime_s * 1000;
21 static constexpr uint16_t brightnessDecreasePerLoop = 1;
22
24 static constexpr uint16_t brightnessIncreasePerLoop = 1;
26 static constexpr uint16_t minimalSunUpdateCalls = 50;
28 static constexpr uint16_t minimalSunLoopDuration_ms = 5;
29
30 virtual ~SunTimerBase() = default;
31
32 bool is_enabled() const { return endTime_s > 0; }
33
35 uint32_t get_remaining_time_s() const;
36
42 bool cancel();
43
45 void set_deadline(const uint32_t timeshutdown_s);
46
49 void add_time_minutes(const uint8_t time_minutes);
50
52 void lock_brightness_update(bool shouldLock) { isAllowedToControlBrightness = not shouldLock; }
53
56
59 void sun_process_loop();
60
61protected:
63 virtual bool should_accept_start() const = 0;
64
67 virtual void sun_timer_progress_update_callback(const float progress) const = 0;
68
73 bool release();
74
77 virtual void end_of_timer_handle() = 0;
78
80 virtual void timer_brightness_progress_update(const float progress) = 0;
81
82protected:
84 uint32_t get_loop_timing_ms() const;
85
87 float get_percent_of_advance() const;
88
91 float signal_sun_update() const;
92
95 volatile uint32_t endTime_s = 0;
98
99private:
101 uint32_t trueBrightnessSunTime_ms = BrightnessSunTime_ms;
102};
103
105class SunsetTimer : public SunTimerBase<3>
106{
107public:
112 void bump_timer();
113
114protected:
115 bool should_accept_start() const override;
116
117 void sun_timer_progress_update_callback(const float progress) const override;
118
119 void end_of_timer_handle() override;
120
121 void timer_brightness_progress_update(const float progress) override;
122};
123
125class SunriseTimer : public SunTimerBase<3>
126{
127public:
130
131protected:
132 bool should_accept_start() const override;
133
134 void sun_timer_progress_update_callback(const float progress) const override;
135
136 void end_of_timer_handle() override;
137
138 void timer_brightness_progress_update(const float progress) override;
139};
140
143extern SunriseTimer sunrise_timer;
144
147
148inline void sun_timers_cancel_all()
149{
151 sunrise_timer.cancel();
152}
153
154inline void sun_timers_brightness_lock_all(const bool shouldLock)
155{
157 sunrise_timer.lock_brightness_update(shouldLock);
158}
159
160} // namespace logic
161} // namespace lampda
Sun timer operations handler All sun operations are exclusive, thay cannot run at the same time.
Definition: sun_timers.h:16
bool cancel()
Cancel the current active timer.
Definition: sun_timers.cpp:20
virtual bool should_accept_start() const =0
Overloadable: A timer can refuse to start under some conditions.
uint32_t get_remaining_time_s() const
Return the time left before the end of the timer, in seconds. 0 if the timer is not enabled.
Definition: sun_timers.cpp:135
virtual void sun_timer_progress_update_callback(const float progress) const =0
Wrapper to the custom user code to call for a sun timer update.
uint32_t get_loop_timing_ms() const
Compute the timing delay of the loop to stay at the target brightness control frequency.
Definition: sun_timers.cpp:123
float signal_sun_update() const
Signal a sun timer update to consummers.
Definition: sun_timers.cpp:158
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
volatile uint32_t endTime_s
End of the timer operation, in the lamp time counter. 0 when the timer is not running....
Definition: sun_timers.h:95
void sun_process_loop()
sun process task INTERNAL USE ONLY
Definition: sun_timers.cpp:185
static constexpr uint16_t minimalSunUpdateCalls
minimum calls to the update sunset function that will be made
Definition: sun_timers.h:26
static constexpr uint16_t minimalSunLoopDuration_ms
Minimum time between sunset loop calls.
Definition: sun_timers.h:28
virtual void end_of_timer_handle()=0
Called when the timer reaches its target. The brightness scale is left as is by the release: the hand...
bool isAllowedToControlBrightness
Is the handler allowed to control lamp brightness.
Definition: sun_timers.h:97
void shortcut_to_brightness_phase()
Shortcut the whole timer to the brightness control phase.
Definition: sun_timers.cpp:171
void lock_brightness_update(bool shouldLock)
Lock the hability of the sunset timer to control the brightness.
Definition: sun_timers.h:52
void add_time_minutes(const uint8_t time_minutes)
Add some time to the current timer, or start it with a target time in minutes. This time cannot be sh...
Definition: sun_timers.cpp:101
float get_percent_of_advance() const
Return the sun timer progress between 0 to 1.
Definition: sun_timers.cpp:143
static constexpr uint16_t brightnessIncreasePerLoop
Brightness increase per loop call during the brightness phase.
Definition: sun_timers.h:24
bool release()
Release the timer, without restoring the brightness scale.
Definition: sun_timers.cpp:29
virtual void timer_brightness_progress_update(const float progress)=0
Called when the timer progresses through the brightness phase.
Timer to start the system and ramp up brightness until we reach back the latest user set brightness.
Definition: sun_timers.h:126
void end_of_timer_handle() override
Called when the timer reaches its target. The brightness scale is left as is by the release: the hand...
Definition: sun_timers.cpp:334
void cancel_at_current_brightness()
Cancel the timer, and keep the brightness reached by the sunrise as the new saved brightness.
Definition: sun_timers.cpp:316
void timer_brightness_progress_update(const float progress) override
Called when the timer progresses through the brightness phase.
Definition: sun_timers.cpp:344
bool should_accept_start() const override
Definition: sun_timers.cpp:308
void sun_timer_progress_update_callback(const float progress) const override
Wrapper to the custom user code to call for a sun timer update.
Definition: sun_timers.cpp:329
Timer to slowly ramp down the brightness until zero then shutdown.
Definition: sun_timers.h:106
void timer_brightness_progress_update(const float progress) override
Called when the timer progresses through the brightness phase.
Definition: sun_timers.cpp:291
bool should_accept_start() const override
Definition: sun_timers.cpp:246
void end_of_timer_handle() override
Called when the timer reaches its target. The brightness scale is left as is by the release: the hand...
Definition: sun_timers.cpp:282
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 sun_timer_progress_update_callback(const float progress) const override
Wrapper to the custom user code to call for a sun timer update.
Definition: sun_timers.cpp:277
void sun_timers_init_all()
init all timers: call once on program start
Definition: sun_timers.cpp:391
SunsetTimer sunset_timer
object to access to control the timers
Definition: sun_timers.cpp:237
Program scope.
Definition: control_fixed_modes.hpp:14