Embedded Template Library 1.0
Loading...
Searching...
No Matches
callback_timer_deferred_locked.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2025 John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef ETL_CALLBACK_TIMER_DEFERRED_LOCKED_INCLUDED
30#define ETL_CALLBACK_TIMER_DEFERRED_LOCKED_INCLUDED
31
32#include "callback_timer_locked.h"
33#include "etl/nullptr.h"
34#include "etl/optional.h"
35#include "priority_queue.h"
36#include "delegate.h"
37
38namespace etl
39{
40 //***************************************************************************
42 //***************************************************************************
43 template <uint_least8_t MAX_TIMERS_, uint32_t MAX_HANDLERS_>
45 {
46 public:
47
48 ETL_STATIC_ASSERT(MAX_TIMERS_ <= 254U, "No more than 254 timers are allowed");
49
54
55 private:
56
57 class CallbackNode
58 {
59 public:
60
62 {
63 }
64
65 bool operator < (const CallbackNode& p) const
66 {
67 return this->priority > p.priority; // comparison was inverted here to easy the code design
68 }
69
71 uint_least8_t priority;
72 };
73
74 public:
75 //*******************************************
77 //*******************************************
82
83 //*******************************************
85 //*******************************************
91
92 //*******************************************
94 //*******************************************
95 bool tick(uint32_t count) final
96 {
97 if (enabled)
98 {
99 if (try_lock())
100 {
101 // We have something to do?
102 bool has_active = !active_list.empty();
103
104 if (has_active)
105 {
106 while (has_active && (count >= active_list.front().delta))
107 {
108 timer_data& timer = active_list.front();
109
110 count -= timer.delta;
111
112 active_list.remove(timer.id, true);
113
114 if (timer.callback.is_valid())
115 {
116 if (!handler_queue.full())
117 {
118 handler_queue.push(CallbackNode(timer.callback, timer_priorities[timer.id]));
119 }
120 }
121
122 if (timer.repeating)
123 {
124 // Reinsert the timer.
125 timer.delta = timer.period;
126 active_list.insert(timer.id);
127 }
128
129 has_active = !active_list.empty();
130 }
131
132 if (has_active)
133 {
134 // Subtract any remainder from the next due timeout.
135 active_list.front().delta -= count;
136 }
137 }
138
139 unlock();
140
141 return true;
142 }
143 }
144
145 return false;
146 }
147
148 //*******************************************
152 //*******************************************
154 {
156
157 do
158 {
159 lock();
160
161 if (handler_queue.empty())
162 {
163 work_todo_callback.clear();
164 }
165 else
166 {
167 CallbackNode &work_todo_callback_node = handler_queue.top();
169 handler_queue.pop();
170 }
171
172 unlock();
173
174 work_todo_callback.call_if();
175 } while (work_todo_callback.is_valid());
176 }
177
178 // Overloads
179
180 //*******************************************
182 //*******************************************
183 etl::timer::id::type register_timer(const callback_type& callback_,
185 bool repeating_)
186 {
188 }
189
190 //*******************************************
195 //*******************************************
196 etl::timer::id::type register_timer(const callback_type& callback_,
198 bool repeating_,
200 {
202
203 if (id != etl::timer::id::NO_TIMER)
204 {
205 timer_priorities[id] = priority_;
206 }
207
208 return id;
209 }
210
211 private:
212
214 uint_least8_t timer_priorities[MAX_TIMERS_];
215 timer_data timer_array[MAX_TIMERS_];
216 };
217}
218
219#endif
The deferred callback timer.
Definition callback_timer_deferred_locked.h:45
etl::timer::id::type register_timer(const callback_type &callback_, uint32_t period_, bool repeating_, uint_least8_t priority_)
Definition callback_timer_deferred_locked.h:196
callback_timer_deferred_locked(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
Constructor.
Definition callback_timer_deferred_locked.h:86
void handle_deferred(void)
Definition callback_timer_deferred_locked.h:153
callback_timer_deferred_locked()
Constructor.
Definition callback_timer_deferred_locked.h:78
bool tick(uint32_t count) final
Handle the tick call.
Definition callback_timer_deferred_locked.h:95
etl::timer::id::type register_timer(const callback_type &callback_, uint32_t period_, bool repeating_)
Register a timer.
Definition callback_timer_deferred_locked.h:183
Definition callback.h:45
Interface for callback timer.
Definition callback_timer_locked.h:49
void set_locks(try_lock_type try_lock_, lock_type lock_, lock_type unlock_)
Sets the lock and unlock delegates.
Definition callback_timer_locked.h:257
etl::timer::id::type register_timer(const callback_type &callback_, uint32_t period_, bool repeating_)
Register a timer.
Definition callback_timer_locked.h:60
Definition priority_queue.h:461
bool full() const
Definition priority_queue.h:359
bool empty() const
Definition priority_queue.h:350
void push(const_reference value)
Definition priority_queue.h:154
reference top()
Definition priority_queue.h:134
void pop()
Definition priority_queue.h:312
bitset_ext
Definition absolute.h:38
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:314
The configuration of a timer.
Definition callback_timer_locked.h:327
Definition timer.h:88
Common definitions for the timer framework.
Definition timer.h:55