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