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