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