Embedded Template Library 1.0
message_timer_locked.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2021 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_MESSAGE_TIMER_LOCKED_INCLUDED
30#define ETL_MESSAGE_TIMER_LOCKED_INCLUDED
31
32#include "platform.h"
33#include "nullptr.h"
34#include "message_types.h"
35#include "message.h"
36#include "message_router.h"
37#include "message_bus.h"
38#include "static_assert.h"
39#include "timer.h"
40#include "delegate.h"
41#include "algorithm.h"
42
43#include <stdint.h>
44
45namespace etl
46{
47 //***************************************************************************
49 //***************************************************************************
51 {
52 public:
53
54 typedef etl::delegate<void(void)> callback_type;
55 typedef etl::delegate<bool(void)> try_lock_type;
56 typedef etl::delegate<void(void)> lock_type;
57 typedef etl::delegate<void(void)> unlock_type;
58
59 public:
60
61 //*******************************************
63 //*******************************************
64 etl::timer::id::type register_timer(const etl::imessage& message_,
65 etl::imessage_router& router_,
66 uint32_t period_,
67 bool repeating_,
68 etl::message_router_id_t destination_router_id_ = etl::imessage_router::ALL_MESSAGE_ROUTERS)
69 {
70 etl::timer::id::type id = etl::timer::id::NO_TIMER;
71
72 bool is_space = (number_of_registered_timers < MAX_TIMERS);
73
74 if (is_space)
75 {
76 // There's no point adding null message routers.
77 if (!router_.is_null_router())
78 {
79 // Search for the free space.
80 for (uint_least8_t i = 0U; i < MAX_TIMERS; ++i)
81 {
82 timer_data& timer = timer_array[i];
83
84 if (timer.id == etl::timer::id::NO_TIMER)
85 {
86 // Create in-place.
87 new (&timer) timer_data(i, message_, router_, period_, repeating_, destination_router_id_);
88 ++number_of_registered_timers;
89 id = i;
90 break;
91 }
92 }
93 }
94 }
95
96 return id;
97 }
98
99 //*******************************************
101 //*******************************************
102 bool unregister_timer(etl::timer::id::type id_)
103 {
104 bool result = false;
105
106 if (id_ != etl::timer::id::NO_TIMER)
107 {
108 timer_data& timer = timer_array[id_];
109
110 if (timer.id != etl::timer::id::NO_TIMER)
111 {
112 if (timer.is_active())
113 {
114 lock();
115 active_list.remove(timer.id, true);
116 unlock();
117 }
118
119 // Reset in-place.
120 new (&timer) timer_data();
121 --number_of_registered_timers;
122
123 result = true;
124 }
125 }
126
127 return result;
128 }
129
130 //*******************************************
132 //*******************************************
133 void enable(bool state_)
134 {
135 enabled = state_;
136 }
137
138 //*******************************************
140 //*******************************************
141 bool is_running() const
142 {
143 return enabled;
144 }
145
146 //*******************************************
148 //*******************************************
149 void clear()
150 {
151 lock();
152 active_list.clear();
153 unlock();
154
155 for (int i = 0; i < MAX_TIMERS; ++i)
156 {
157 new (&timer_array[i]) timer_data();
158 }
159
160 number_of_registered_timers = 0U;
161 }
162
163 //*******************************************
164 // Called by the timer service to indicate the
165 // amount of time that has elapsed since the last successful call to 'tick'.
166 // Returns true if the tick was processed,
167 // false if not.
168 //*******************************************
169 bool tick(uint32_t count)
170 {
171 if (enabled)
172 {
173 if (try_lock())
174 {
175 // We have something to do?
176 bool has_active = !active_list.empty();
177
178 if (has_active)
179 {
180 while (has_active && (count >= active_list.front().delta))
181 {
182 timer_data& timer = active_list.front();
183
184 count -= timer.delta;
185
186 active_list.remove(timer.id, true);
187
188 if (timer.p_router != ETL_NULLPTR)
189 {
190 timer.p_router->receive(timer.destination_router_id, *(timer.p_message));
191 }
192
193 if (timer.repeating)
194 {
195 timer.delta = timer.period;
196 active_list.insert(timer.id);
197 }
198
199 has_active = !active_list.empty();
200 }
201
202 if (has_active)
203 {
204 // Subtract any remainder from the next due timeout.
205 active_list.front().delta -= count;
206 }
207 }
208
209 unlock();
210
211 return true;
212 }
213 }
214
215 return false;
216 }
217
218 //*******************************************
220 //*******************************************
221 bool start(etl::timer::id::type id_, bool immediate_ = false)
222 {
223 bool result = false;
224
225 // Valid timer id?
226 if (id_ != etl::timer::id::NO_TIMER)
227 {
228 timer_data& timer = timer_array[id_];
229
230 // Registered timer?
231 if (timer.id != etl::timer::id::NO_TIMER)
232 {
233 // Has a valid period.
234 if (timer.period != etl::timer::state::INACTIVE)
235 {
236 lock();
237 if (timer.is_active())
238 {
239 active_list.remove(timer.id, false);
240 }
241
242 timer.delta = immediate_ ? 0 : timer.period;
243 active_list.insert(timer.id);
244 unlock();
245
246 result = true;
247 }
248 }
249 }
250
251 return result;
252 }
253
254 //*******************************************
256 //*******************************************
257 bool stop(etl::timer::id::type id_)
258 {
259 bool result = false;
260
261 // Valid timer id?
262 if (id_ != etl::timer::id::NO_TIMER)
263 {
264 timer_data& timer = timer_array[id_];
265
266 // Registered timer?
267 if (timer.id != etl::timer::id::NO_TIMER)
268 {
269 if (timer.is_active())
270 {
271 lock();
272 active_list.remove(timer.id, false);
273 unlock();
274 }
275
276 result = true;
277 }
278 }
279
280 return result;
281 }
282
283 //*******************************************
285 //*******************************************
286 bool set_period(etl::timer::id::type id_, uint32_t period_)
287 {
288 if (stop(id_))
289 {
290 timer_array[id_].period = period_;
291 return true;
292 }
293
294 return false;
295 }
296
297 //*******************************************
299 //*******************************************
300 bool set_mode(etl::timer::id::type id_, bool repeating_)
301 {
302 if (stop(id_))
303 {
304 timer_array[id_].repeating = repeating_;
305 return true;
306 }
307
308 return false;
309 }
310
311 //*******************************************
313 //*******************************************
314 void set_locks(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
315 {
316 try_lock = try_lock_;
317 lock = lock_;
318 unlock = unlock_;
319 }
320
321 protected:
322
323 //*************************************************************************
326 {
327 //*******************************************
328 timer_data()
329 : p_message(ETL_NULLPTR)
330 , p_router(ETL_NULLPTR)
331 , period(0)
332 , delta(etl::timer::state::INACTIVE)
333 , destination_router_id(etl::imessage_bus::ALL_MESSAGE_ROUTERS)
334 , id(etl::timer::id::NO_TIMER)
335 , previous(etl::timer::id::NO_TIMER)
336 , next(etl::timer::id::NO_TIMER)
337 , repeating(true)
338 {
339 }
340
341 //*******************************************
342 timer_data(etl::timer::id::type id_,
343 const etl::imessage& message_,
344 etl::imessage_router& irouter_,
345 uint32_t period_,
346 bool repeating_,
347 etl::message_router_id_t destination_router_id_ = etl::imessage_bus::ALL_MESSAGE_ROUTERS)
348 : p_message(&message_)
349 , p_router(&irouter_)
350 , period(period_)
351 , delta(etl::timer::state::INACTIVE)
352 , destination_router_id(destination_router_id_)
353 , id(id_)
354 , previous(etl::timer::id::NO_TIMER)
355 , next(etl::timer::id::NO_TIMER)
356 , repeating(repeating_)
357 {
358 }
359
360 //*******************************************
362 //*******************************************
363 bool is_active() const
364 {
365 return delta != etl::timer::state::INACTIVE;
366 }
367
368 //*******************************************
370 //*******************************************
372 {
373 delta = etl::timer::state::INACTIVE;
374 }
375
376 const etl::imessage* p_message;
377 etl::imessage_router* p_router;
378 uint32_t period;
379 uint32_t delta;
380 etl::message_router_id_t destination_router_id;
381 etl::timer::id::type id;
382 uint_least8_t previous;
383 uint_least8_t next;
384 bool repeating;
385
386 private:
387
388 // Disabled.
389 timer_data(const timer_data& other);
390 timer_data& operator =(const timer_data& other);
391 };
392
393 //*******************************************
395 //*******************************************
396 imessage_timer_locked(timer_data* const timer_array_, const uint_least8_t MAX_TIMERS_)
397 : timer_array(timer_array_)
398 , active_list(timer_array_)
399 , enabled(false)
400 , number_of_registered_timers(0U)
401 , MAX_TIMERS(MAX_TIMERS_)
402 {
403 }
404
405 //*******************************************
407 //*******************************************
409 {
410 }
411
412 private:
413
414 //*************************************************************************
416 //*************************************************************************
417 class timer_list
418 {
419 public:
420
421 //*******************************
422 timer_list(timer_data* ptimers_)
423 : head(etl::timer::id::NO_TIMER)
424 , tail(etl::timer::id::NO_TIMER)
425 , current(etl::timer::id::NO_TIMER)
426 , ptimers(ptimers_)
427 {
428 }
429
430 //*******************************
431 bool empty() const
432 {
433 return head == etl::timer::id::NO_TIMER;
434 }
435
436 //*******************************
437 // Inserts the timer at the correct delta position
438 //*******************************
439 void insert(etl::timer::id::type id_)
440 {
441 timer_data& timer = ptimers[id_];
442
443 if (head == etl::timer::id::NO_TIMER)
444 {
445 // No entries yet.
446 head = id_;
447 tail = id_;
448 timer.previous = etl::timer::id::NO_TIMER;
449 timer.next = etl::timer::id::NO_TIMER;
450 }
451 else
452 {
453 // We already have entries.
454 etl::timer::id::type test_id = begin();
455
456 while (test_id != etl::timer::id::NO_TIMER)
457 {
458 timer_data& test = ptimers[test_id];
459
460 // Find the correct place to insert.
461 if (timer.delta <= test.delta)
462 {
463 if (test.id == head)
464 {
465 head = timer.id;
466 }
467
468 // Insert before test.
469 timer.previous = test.previous;
470 test.previous = timer.id;
471 timer.next = test.id;
472
473 // Adjust the next delta to compensate.
474 test.delta -= timer.delta;
475
476 if (timer.previous != etl::timer::id::NO_TIMER)
477 {
478 ptimers[timer.previous].next = timer.id;
479 }
480 break;
481 }
482 else
483 {
484 timer.delta -= test.delta;
485 }
486
487 test_id = next(test_id);
488 }
489
490 // Reached the end?
491 if (test_id == etl::timer::id::NO_TIMER)
492 {
493 // Tag on to the tail.
494 ptimers[tail].next = timer.id;
495 timer.previous = tail;
496 timer.next = etl::timer::id::NO_TIMER;
497 tail = timer.id;
498 }
499 }
500 }
501
502 //*******************************
503 void remove(etl::timer::id::type id_, bool has_expired)
504 {
505 timer_data& timer = ptimers[id_];
506
507 if (head == id_)
508 {
509 head = timer.next;
510 }
511 else
512 {
513 ptimers[timer.previous].next = timer.next;
514 }
515
516 if (tail == id_)
517 {
518 tail = timer.previous;
519 }
520 else
521 {
522 ptimers[timer.next].previous = timer.previous;
523 }
524
525 if (!has_expired)
526 {
527 // Adjust the next delta.
528 if (timer.next != etl::timer::id::NO_TIMER)
529 {
530 ptimers[timer.next].delta += timer.delta;
531 }
532 }
533
534 timer.previous = etl::timer::id::NO_TIMER;
535 timer.next = etl::timer::id::NO_TIMER;
536 timer.delta = etl::timer::state::INACTIVE;
537 }
538
539 //*******************************
540 timer_data& front()
541 {
542 return ptimers[head];
543 }
544
545 //*******************************
546 etl::timer::id::type begin()
547 {
548 current = head;
549 return current;
550 }
551
552 //*******************************
553 etl::timer::id::type previous(etl::timer::id::type last)
554 {
555 current = ptimers[last].previous;
556 return current;
557 }
558
559 //*******************************
560 etl::timer::id::type next(etl::timer::id::type last)
561 {
562 current = ptimers[last].next;
563 return current;
564 }
565
566 //*******************************
567 void clear()
568 {
569 etl::timer::id::type id = begin();
570
571 while (id != etl::timer::id::NO_TIMER)
572 {
573 timer_data& timer = ptimers[id];
574 id = next(id);
575 timer.next = etl::timer::id::NO_TIMER;
576 }
577
578 head = etl::timer::id::NO_TIMER;
579 tail = etl::timer::id::NO_TIMER;
580 current = etl::timer::id::NO_TIMER;
581 }
582
583 private:
584
585 etl::timer::id::type head;
586 etl::timer::id::type tail;
587 etl::timer::id::type current;
588
589 timer_data* const ptimers;
590 };
591
592 // The array of timer data structures.
593 timer_data* const timer_array;
594
595 // The list of active timers.
596 timer_list active_list;
597
598 bool enabled;
599 uint_least8_t number_of_registered_timers;
600
601 try_lock_type try_lock;
602 lock_type lock;
603 unlock_type unlock;
604
605 public:
606
607 const uint_least8_t MAX_TIMERS;
608 };
609
610 //***************************************************************************
612 //***************************************************************************
613 template <uint_least8_t MAX_TIMERS_>
615 {
616 public:
617
618 ETL_STATIC_ASSERT(MAX_TIMERS_ <= 254, "No more than 254 timers are allowed");
619
624
625 //*******************************************
627 //*******************************************
629 : imessage_timer_locked(timer_array, MAX_TIMERS_)
630 {
631 }
632
633 //*******************************************
635 //*******************************************
637 : imessage_timer_locked(timer_array, MAX_TIMERS_)
638 {
639 this->set_locks(try_lock_, lock_, unlock_);
640 }
641
642 private:
643
644 timer_data timer_array[MAX_TIMERS_];
645 };
646}
647
648#endif
Declaration.
Definition: delegate_cpp03.h:175
This is the base of all message routers.
Definition: message_router_generator.h:121
Interface for message timer.
Definition: message_timer_locked.h:51
bool is_running() const
Get the enable/disable state.
Definition: message_timer_locked.h:141
~imessage_timer_locked()
Destructor.
Definition: message_timer_locked.h:408
bool set_period(etl::timer::id::type id_, uint32_t period_)
Sets a timer's period.
Definition: message_timer_locked.h:286
void enable(bool state_)
Enable/disable the timer.
Definition: message_timer_locked.h:133
void clear()
Clears the timer of data.
Definition: message_timer_locked.h:149
bool start(etl::timer::id::type id_, bool immediate_=false)
Starts a timer.
Definition: message_timer_locked.h:221
imessage_timer_locked(timer_data *const timer_array_, const uint_least8_t MAX_TIMERS_)
Constructor.
Definition: message_timer_locked.h:396
void set_locks(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
Sets the lock and unlock delegates.
Definition: message_timer_locked.h:314
etl::timer::id::type register_timer(const etl::imessage &message_, etl::imessage_router &router_, uint32_t period_, bool repeating_, etl::message_router_id_t destination_router_id_=etl::imessage_router::ALL_MESSAGE_ROUTERS)
Register a timer.
Definition: message_timer_locked.h:64
bool stop(etl::timer::id::type id_)
Stops a timer.
Definition: message_timer_locked.h:257
bool unregister_timer(etl::timer::id::type id_)
Unregister a timer.
Definition: message_timer_locked.h:102
bool set_mode(etl::timer::id::type id_, bool repeating_)
Sets a timer's mode.
Definition: message_timer_locked.h:300
Definition: message.h:69
The message timer.
Definition: message_timer_locked.h:615
message_timer_locked()
Constructor.
Definition: message_timer_locked.h:628
message_timer_locked(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
Constructor.
Definition: message_timer_locked.h:636
ETL_CONSTEXPR14 TIterator remove(TIterator first, TIterator last, const T &value)
Definition: algorithm.h:1934
bitset_ext
Definition: absolute.h:38
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition: iterator.h:931
The configuration of a timer.
Definition: message_timer_locked.h:326
bool is_active() const
Returns true if the timer is active.
Definition: message_timer_locked.h:363
void set_inactive()
Sets the timer to the inactive state.
Definition: message_timer_locked.h:371
Definition: timer.h:82
Common definitions for the timer framework.
Definition: timer.h:55