Embedded Template Library 1.0
Loading...
Searching...
No Matches
message_timer.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2017 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_INCLUDED
30#define ETL_MESSAGE_TIMER_INCLUDED
31
32#include "platform.h"
33#include "algorithm.h"
34#include "atomic.h"
35#include "delegate.h"
36#include "message.h"
37#include "message_bus.h"
38#include "message_router.h"
39#include "message_types.h"
40#include "nullptr.h"
41#include "static_assert.h"
42#include "timer.h"
43
44#include <stdint.h>
45
46#if defined(ETL_IN_UNIT_TEST) && ETL_NOT_USING_STL
47 #define ETL_DISABLE_TIMER_UPDATES
48 #define ETL_ENABLE_TIMER_UPDATES
49 #define ETL_TIMER_UPDATES_ENABLED true
50
51 #undef ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK
52 #undef ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK
53#else
54 #if !defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK) && !defined(ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK)
55 #error ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK or ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK not defined
56 #endif
57
58 #if defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK) && defined(ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK)
59 #error Only define one of ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK or ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK
60 #endif
61
62 #if defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK)
63 #define ETL_DISABLE_TIMER_UPDATES (++process_semaphore)
64 #define ETL_ENABLE_TIMER_UPDATES (--process_semaphore)
65 #define ETL_TIMER_UPDATES_ENABLED (process_semaphore.load() == 0)
66 #endif
67
68 #if defined(ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK)
69 #if !defined(ETL_MESSAGE_TIMER_DISABLE_INTERRUPTS) || !defined(ETL_MESSAGE_TIMER_ENABLE_INTERRUPTS)
70 #error ETL_MESSAGE_TIMER_DISABLE_INTERRUPTS and/or ETL_MESSAGE_TIMER_ENABLE_INTERRUPTS not defined
71 #endif
72
73 #define ETL_DISABLE_TIMER_UPDATES ETL_MESSAGE_TIMER_DISABLE_INTERRUPTS
74 #define ETL_ENABLE_TIMER_UPDATES ETL_MESSAGE_TIMER_ENABLE_INTERRUPTS
75 #define ETL_TIMER_UPDATES_ENABLED true
76 #endif
77#endif
78
79namespace etl
80{
81 //*************************************************************************
83 struct message_timer_data
84 {
85 //*******************************************
86 message_timer_data()
87 : p_message(ETL_NULLPTR)
88 , p_router(ETL_NULLPTR)
89 , period(0)
90 , delta(etl::timer::state::Inactive)
91 , destination_router_id(etl::imessage_bus::ALL_MESSAGE_ROUTERS)
92 , id(etl::timer::id::NO_TIMER)
93 , previous(etl::timer::id::NO_TIMER)
94 , next(etl::timer::id::NO_TIMER)
95 , repeating(true)
96 {
97 }
98
99 //*******************************************
100 message_timer_data(etl::timer::id::type id_, const etl::imessage& message_, etl::imessage_router& irouter_, uint32_t period_, bool repeating_,
101 etl::message_router_id_t destination_router_id_ = etl::imessage_bus::ALL_MESSAGE_ROUTERS)
102 : p_message(&message_)
103 , p_router(&irouter_)
104 , period(period_)
105 , delta(etl::timer::state::Inactive)
106 , destination_router_id(destination_router_id_)
107 , id(id_)
108 , previous(etl::timer::id::NO_TIMER)
109 , next(etl::timer::id::NO_TIMER)
110 , repeating(repeating_)
111 {
112 }
113
114 //*******************************************
116 //*******************************************
117 bool is_active() const
118 {
119 return delta != etl::timer::state::Inactive;
120 }
121
122 //*******************************************
124 //*******************************************
126 {
127 delta = etl::timer::state::Inactive;
128 }
129
130 const etl::imessage* p_message;
131 etl::imessage_router* p_router;
132 uint32_t period;
133 uint32_t delta;
134 etl::message_router_id_t destination_router_id;
135 etl::timer::id::type id;
136 uint_least8_t previous;
137 uint_least8_t next;
138 bool repeating;
139
140 private:
141
142 // Disabled.
144 message_timer_data& operator=(const message_timer_data& other);
145 };
146
147 namespace private_message_timer
148 {
149 //*************************************************************************
151 //*************************************************************************
152 class list
153 {
154 public:
155
156 //*******************************
157 list(etl::message_timer_data* ptimers_)
158 : head(etl::timer::id::NO_TIMER)
159 , tail(etl::timer::id::NO_TIMER)
160 , current(etl::timer::id::NO_TIMER)
161 , ptimers(ptimers_)
162 {
163 }
164
165 //*******************************
166 bool empty() const
167 {
168 return head == etl::timer::id::NO_TIMER;
169 }
170
171 //*******************************
172 // Inserts the timer at the correct delta position
173 //*******************************
174 void insert(etl::timer::id::type id_)
175 {
176 etl::message_timer_data& timer = ptimers[id_];
177
178 if (head == etl::timer::id::NO_TIMER)
179 {
180 // No entries yet.
181 head = id_;
182 tail = id_;
183 timer.previous = etl::timer::id::NO_TIMER;
184 timer.next = etl::timer::id::NO_TIMER;
185 }
186 else
187 {
188 // We already have entries.
189 etl::timer::id::type test_id = begin();
190
191 while (test_id != etl::timer::id::NO_TIMER)
192 {
193 etl::message_timer_data& test = ptimers[test_id];
194
195 // Find the correct place to insert.
196 if (timer.delta <= test.delta)
197 {
198 if (test.id == head)
199 {
200 head = timer.id;
201 }
202
203 // Insert before test.
204 timer.previous = test.previous;
205 test.previous = timer.id;
206 timer.next = test.id;
207
208 // Adjust the next delta to compensate.
209 test.delta -= timer.delta;
210
211 if (timer.previous != etl::timer::id::NO_TIMER)
212 {
213 ptimers[timer.previous].next = timer.id;
214 }
215 break;
216 }
217 else
218 {
219 timer.delta -= test.delta;
220 }
221
222 test_id = next(test_id);
223 }
224
225 // Reached the end?
226 if (test_id == etl::timer::id::NO_TIMER)
227 {
228 // Tag on to the tail.
229 ptimers[tail].next = timer.id;
230 timer.previous = tail;
231 timer.next = etl::timer::id::NO_TIMER;
232 tail = timer.id;
233 }
234 }
235 }
236
237 //*******************************
238 void remove(etl::timer::id::type id_, bool has_expired)
239 {
240 etl::message_timer_data& timer = ptimers[id_];
241
242 if (head == id_)
243 {
244 head = timer.next;
245 }
246 else
247 {
248 ptimers[timer.previous].next = timer.next;
249 }
250
251 if (tail == id_)
252 {
253 tail = timer.previous;
254 }
255 else
256 {
257 ptimers[timer.next].previous = timer.previous;
258 }
259
260 if (!has_expired)
261 {
262 // Adjust the next delta.
263 if (timer.next != etl::timer::id::NO_TIMER)
264 {
265 ptimers[timer.next].delta += timer.delta;
266 }
267 }
268
269 timer.previous = etl::timer::id::NO_TIMER;
270 timer.next = etl::timer::id::NO_TIMER;
271 timer.delta = etl::timer::state::Inactive;
272 }
273
274 //*******************************
276 {
277 return ptimers[head];
278 }
279
280 //*******************************
281 const etl::message_timer_data& front() const
282 {
283 return ptimers[head];
284 }
285
286 //*******************************
287 etl::timer::id::type begin()
288 {
289 current = head;
290 return current;
291 }
292
293 //*******************************
294 etl::timer::id::type previous(etl::timer::id::type last)
295 {
296 current = ptimers[last].previous;
297 return current;
298 }
299
300 //*******************************
301 etl::timer::id::type next(etl::timer::id::type last)
302 {
303 current = ptimers[last].next;
304 return current;
305 }
306
307 //*******************************
308 void clear()
309 {
310 etl::timer::id::type id = begin();
311
312 while (id != etl::timer::id::NO_TIMER)
313 {
314 etl::message_timer_data& timer = ptimers[id];
315 id = next(id);
316 timer.next = etl::timer::id::NO_TIMER;
317 }
318
319 head = etl::timer::id::NO_TIMER;
320 tail = etl::timer::id::NO_TIMER;
321 current = etl::timer::id::NO_TIMER;
322 }
323
324 private:
325
326 etl::timer::id::type head;
327 etl::timer::id::type tail;
328 etl::timer::id::type current;
329
330 etl::message_timer_data* const ptimers;
331 };
332 } // namespace private_message_timer
333
334 //***************************************************************************
336 //***************************************************************************
338 {
339 public:
340
341 typedef etl::delegate<void(etl::timer::id::type)> event_callback_type;
342
343 //*******************************************
345 //*******************************************
346 etl::timer::id::type register_timer(const etl::imessage& message_, etl::imessage_router& router_, uint32_t period_, bool repeating_,
347 etl::message_router_id_t destination_router_id_ = etl::imessage_router::ALL_MESSAGE_ROUTERS)
348 {
349 etl::timer::id::type id = etl::timer::id::NO_TIMER;
350
351 bool is_space = (registered_timers < Max_Timers);
352
353 if (is_space)
354 {
355 // There's no point adding null message routers.
356 if (!router_.is_null_router())
357 {
358 // Search for the free space.
359 for (uint_least8_t i = 0U; i < Max_Timers; ++i)
360 {
361 etl::message_timer_data& timer = timer_array[i];
362
363 if (timer.id == etl::timer::id::NO_TIMER)
364 {
365 // Create in-place.
366 new (&timer) message_timer_data(i, message_, router_, period_, repeating_, destination_router_id_);
367 ++registered_timers;
368 id = i;
369 break;
370 }
371 }
372 }
373 }
374
375 return id;
376 }
377
378 //*******************************************
380 //*******************************************
381 bool unregister_timer(etl::timer::id::type id_)
382 {
383 bool result = false;
384
385 if (id_ != etl::timer::id::NO_TIMER)
386 {
387 etl::message_timer_data& timer = timer_array[id_];
388
389 if (timer.id != etl::timer::id::NO_TIMER)
390 {
391 if (timer.is_active())
392 {
393 ETL_DISABLE_TIMER_UPDATES;
394 active_list.remove(timer.id, true);
395 remove_callback.call_if(timer.id);
396 ETL_ENABLE_TIMER_UPDATES;
397 }
398
399 // Reset in-place.
400 new (&timer) message_timer_data();
401 --registered_timers;
402
403 result = true;
404 }
405 }
406
407 return result;
408 }
409
410 //*******************************************
412 //*******************************************
413 void enable(bool state_)
414 {
415 enabled = state_;
416 }
417
418 //*******************************************
420 //*******************************************
421 bool is_running() const
422 {
423 return enabled;
424 }
425
426 //*******************************************
428 //*******************************************
429 void clear()
430 {
431 ETL_DISABLE_TIMER_UPDATES;
432 active_list.clear();
433 ETL_ENABLE_TIMER_UPDATES;
434
435 for (int i = 0; i < Max_Timers; ++i)
436 {
437 new (&timer_array[i]) message_timer_data();
438 }
439
440 registered_timers = 0;
441 }
442
443 //*******************************************
444 // Called by the timer service to indicate the
445 // amount of time that has elapsed since the last successful call to 'tick'.
446 // Returns true if the tick was processed,
447 // false if not.
448 //*******************************************
449 bool tick(uint32_t count)
450 {
451 if (enabled)
452 {
453 if (ETL_TIMER_UPDATES_ENABLED)
454 {
455 // We have something to do?
456 bool has_active = !active_list.empty();
457
458 if (has_active)
459 {
460 while (has_active && (count >= active_list.front().delta))
461 {
462 etl::message_timer_data& timer = active_list.front();
463
464 count -= timer.delta;
465
466 active_list.remove(timer.id, true);
467 remove_callback.call_if(timer.id);
468
469 if (timer.repeating)
470 {
471 timer.delta = timer.period;
472 active_list.insert(timer.id);
473 insert_callback.call_if(timer.id);
474 }
475
476 if (timer.p_router != ETL_NULLPTR)
477 {
478 timer.p_router->receive(timer.destination_router_id, *(timer.p_message));
479 }
480
481 has_active = !active_list.empty();
482 }
483
484 if (has_active)
485 {
486 // Subtract any remainder from the next due timeout.
487 active_list.front().delta -= count;
488 }
489 }
490
491 return true;
492 }
493 }
494
495 return false;
496 }
497
498 //*******************************************
500 //*******************************************
501 bool start(etl::timer::id::type id_, bool immediate_ = false)
502 {
503 bool result = false;
504
505 // Valid timer id?
506 if (id_ != etl::timer::id::NO_TIMER)
507 {
508 etl::message_timer_data& timer = timer_array[id_];
509
510 // Registered timer?
511 if (timer.id != etl::timer::id::NO_TIMER)
512 {
513 // Has a valid period.
514 if (timer.period != etl::timer::state::Inactive)
515 {
516 ETL_DISABLE_TIMER_UPDATES;
517 if (timer.is_active())
518 {
519 active_list.remove(timer.id, false);
520 remove_callback.call_if(timer.id);
521 }
522
523 timer.delta = immediate_ ? 0 : timer.period;
524 active_list.insert(timer.id);
525 insert_callback.call_if(timer.id);
526 ETL_ENABLE_TIMER_UPDATES;
527
528 result = true;
529 }
530 }
531 }
532
533 return result;
534 }
535
536 //*******************************************
538 //*******************************************
539 bool stop(etl::timer::id::type id_)
540 {
541 bool result = false;
542
543 // Valid timer id?
544 if (id_ != etl::timer::id::NO_TIMER)
545 {
546 etl::message_timer_data& timer = timer_array[id_];
547
548 // Registered timer?
549 if (timer.id != etl::timer::id::NO_TIMER)
550 {
551 if (timer.is_active())
552 {
553 ETL_DISABLE_TIMER_UPDATES;
554 active_list.remove(timer.id, false);
555 remove_callback.call_if(timer.id);
556 ETL_ENABLE_TIMER_UPDATES;
557 }
558
559 result = true;
560 }
561 }
562
563 return result;
564 }
565
566 //*******************************************
568 //*******************************************
569 bool set_period(etl::timer::id::type id_, uint32_t period_)
570 {
571 if (stop(id_))
572 {
573 timer_array[id_].period = period_;
574 return true;
575 }
576
577 return false;
578 }
579
580 //*******************************************
582 //*******************************************
583 bool set_mode(etl::timer::id::type id_, bool repeating_)
584 {
585 if (stop(id_))
586 {
587 timer_array[id_].repeating = repeating_;
588 return true;
589 }
590
591 return false;
592 }
593
594 //*******************************************
596 //*******************************************
597 bool has_active_timer() const
598 {
599 ETL_DISABLE_TIMER_UPDATES;
600 bool result = !active_list.empty();
601 ETL_ENABLE_TIMER_UPDATES;
602
603 return result;
604 }
605
606 //*******************************************
610 //*******************************************
611 uint32_t time_to_next() const
612 {
613 uint32_t delta = static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
614
615 ETL_DISABLE_TIMER_UPDATES;
616 if (!active_list.empty())
617 {
618 delta = active_list.front().delta;
619 }
620 ETL_ENABLE_TIMER_UPDATES;
621
622 return delta;
623 }
624
625 //*******************************************
627 //*******************************************
628 void set_insert_callback(event_callback_type insert_)
629 {
630 insert_callback = insert_;
631 }
632
633 //*******************************************
635 //*******************************************
636 void set_remove_callback(event_callback_type remove_)
637 {
638 remove_callback = remove_;
639 }
640
641 //*******************************************
642 void clear_insert_callback()
643 {
644 insert_callback.clear();
645 }
646
647 //*******************************************
648 void clear_remove_callback()
649 {
650 remove_callback.clear();
651 }
652
653 protected:
654
655 //*******************************************
657 //*******************************************
658 imessage_timer(message_timer_data* const timer_array_, const uint_least8_t Max_Timers_)
659 : timer_array(timer_array_)
660 , active_list(timer_array_)
661 , enabled(false)
662 ,
663#if defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK)
664 process_semaphore(0)
665 ,
666#endif
667 registered_timers(0)
668 , Max_Timers(Max_Timers_)
669 {
670 }
671
672 //*******************************************
674 //*******************************************
676
677 private:
678
679 // The array of timer data structures.
680 message_timer_data* const timer_array;
681
682 // The list of active timers.
683 private_message_timer::list active_list;
684
685 bool enabled;
686
687#if defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK)
688
689 #if defined(ETL_TIMER_SEMAPHORE_TYPE)
690 typedef ETL_TIMER_SEMAPHORE_TYPE timer_semaphore_t;
691 #else
692 #if ETL_HAS_ATOMIC
693 typedef etl::atomic_uint16_t timer_semaphore_t;
694 #else
695 #error No atomic type available
696 #endif
697 #endif
698
699 mutable etl::timer_semaphore_t process_semaphore;
700#endif
701 uint_least8_t registered_timers;
702
703 event_callback_type insert_callback;
704 event_callback_type remove_callback;
705
706 public:
707
708 const uint_least8_t Max_Timers;
709 };
710
711 //***************************************************************************
713 //***************************************************************************
714 template <uint_least8_t Max_Timers_>
716 {
717 public:
718
719 ETL_STATIC_ASSERT(Max_Timers_ <= 254, "No more than 254 timers are allowed");
720
721 //*******************************************
723 //*******************************************
725 : imessage_timer(timer_array, Max_Timers_)
726 {
727 }
728
729 private:
730
731 message_timer_data timer_array[Max_Timers_];
732 };
733} // namespace etl
734
735#undef ETL_DISABLE_TIMER_UPDATES
736#undef ETL_ENABLE_TIMER_UPDATES
737#undef ETL_TIMER_UPDATES_ENABLED
738
739#endif
Declaration.
Definition delegate_cpp03.h:191
This is the base of all message routers.
Definition message_router.h:138
Interface for message timer.
Definition message_timer.h:338
uint32_t time_to_next() const
Definition message_timer.h:611
bool has_active_timer() const
Check if there is an active timer.
Definition message_timer.h:597
void set_remove_callback(event_callback_type remove_)
Set a callback when a timer is removed from list.
Definition message_timer.h:636
void enable(bool state_)
Enable/disable the timer.
Definition message_timer.h:413
bool start(etl::timer::id::type id_, bool immediate_=false)
Starts a timer.
Definition message_timer.h:501
bool unregister_timer(etl::timer::id::type id_)
Unregister a timer.
Definition message_timer.h:381
imessage_timer(message_timer_data *const timer_array_, const uint_least8_t Max_Timers_)
Constructor.
Definition message_timer.h:658
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.h:346
bool is_running() const
Get the enable/disable state.
Definition message_timer.h:421
bool set_mode(etl::timer::id::type id_, bool repeating_)
Sets a timer's mode.
Definition message_timer.h:583
void set_insert_callback(event_callback_type insert_)
Set a callback when a timer is inserted on list.
Definition message_timer.h:628
void clear()
Clears the timer of data.
Definition message_timer.h:429
bool stop(etl::timer::id::type id_)
Stops a timer.
Definition message_timer.h:539
~imessage_timer()
Destructor.
Definition message_timer.h:675
bool set_period(etl::timer::id::type id_, uint32_t period_)
Sets a timer's period.
Definition message_timer.h:569
Definition message.h:75
message_timer()
Constructor.
Definition message_timer.h:724
A specialised intrusive linked list for timer data.
Definition message_timer.h:153
bitset_ext
Definition absolute.h:40
The configuration of a timer.
Definition message_timer.h:84
bool is_active() const
Returns true if the timer is active.
Definition message_timer.h:117
void set_inactive()
Sets the timer to the inactive state.
Definition message_timer.h:125
Definition timer.h:88
Common definitions for the timer framework.
Definition timer.h:55