Embedded Template Library 1.0
Loading...
Searching...
No Matches
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 //*******************************************
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.
88 ++number_of_registered_timers;
89 id = i;
90 break;
91 }
92 }
93 }
94 }
95
96 return id;
97 }
98
99 //*******************************************
101 //*******************************************
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 //*******************************************
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 //*******************************************
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 //*******************************************
287 {
288 if (stop(id_))
289 {
290 timer_array[id_].period = period_;
291 return true;
292 }
293
294 return false;
295 }
296
297 //*******************************************
299 //*******************************************
301 {
302 if (stop(id_))
303 {
304 timer_array[id_].repeating = repeating_;
305 return true;
306 }
307
308 return false;
309 }
310
311 //*******************************************
313 //*******************************************
315 {
316 try_lock = try_lock_;
317 lock = lock_;
318 unlock = unlock_;
319 }
320
321 //*******************************************
323 //*******************************************
324 bool has_active_timer() const
325 {
326 lock();
327 bool result = !active_list.empty();
328 unlock();
329
330 return result;
331 }
332
333 //*******************************************
336 //*******************************************
338 {
339 uint32_t delta = static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
340
341 lock();
342 if (!active_list.empty())
343 {
344 delta = active_list.front().delta;
345 }
346 unlock();
347
348 return delta;
349 }
350
351 protected:
352
353 //*************************************************************************
356 {
357 //*******************************************
358 timer_data()
359 : p_message(ETL_NULLPTR)
360 , p_router(ETL_NULLPTR)
361 , period(0)
362 , delta(etl::timer::state::Inactive)
363 , destination_router_id(etl::imessage_bus::ALL_MESSAGE_ROUTERS)
364 , id(etl::timer::id::NO_TIMER)
365 , previous(etl::timer::id::NO_TIMER)
366 , next(etl::timer::id::NO_TIMER)
367 , repeating(true)
368 {
369 }
370
371 //*******************************************
373 const etl::imessage& message_,
376 bool repeating_,
377 etl::message_router_id_t destination_router_id_ = etl::imessage_bus::ALL_MESSAGE_ROUTERS)
378 : p_message(&message_)
379 , p_router(&irouter_)
380 , period(period_)
381 , delta(etl::timer::state::Inactive)
382 , destination_router_id(destination_router_id_)
383 , id(id_)
384 , previous(etl::timer::id::NO_TIMER)
385 , next(etl::timer::id::NO_TIMER)
386 , repeating(repeating_)
387 {
388 }
389
390 //*******************************************
392 //*******************************************
393 bool is_active() const
394 {
395 return delta != etl::timer::state::Inactive;
396 }
397
398 //*******************************************
400 //*******************************************
402 {
403 delta = etl::timer::state::Inactive;
404 }
405
406 const etl::imessage* p_message;
407 etl::imessage_router* p_router;
408 uint32_t period;
409 uint32_t delta;
410 etl::message_router_id_t destination_router_id;
412 uint_least8_t previous;
413 uint_least8_t next;
414 bool repeating;
415
416 private:
417
418 // Disabled.
421 };
422
423 //*******************************************
425 //*******************************************
427 : timer_array(timer_array_)
428 , active_list(timer_array_)
429 , enabled(false)
430 , number_of_registered_timers(0U)
431 , MAX_TIMERS(MAX_TIMERS_)
432 {
433 }
434
435 //*******************************************
437 //*******************************************
441
442 private:
443
444 //*************************************************************************
446 //*************************************************************************
447 class timer_list
448 {
449 public:
450
451 //*******************************
452 timer_list(timer_data* ptimers_)
453 : head(etl::timer::id::NO_TIMER)
454 , tail(etl::timer::id::NO_TIMER)
455 , current(etl::timer::id::NO_TIMER)
456 , ptimers(ptimers_)
457 {
458 }
459
460 //*******************************
461 bool empty() const
462 {
463 return head == etl::timer::id::NO_TIMER;
464 }
465
466 //*******************************
467 // Inserts the timer at the correct delta position
468 //*******************************
469 void insert(etl::timer::id::type id_)
470 {
471 timer_data& timer = ptimers[id_];
472
473 if (head == etl::timer::id::NO_TIMER)
474 {
475 // No entries yet.
476 head = id_;
477 tail = id_;
478 timer.previous = etl::timer::id::NO_TIMER;
479 timer.next = etl::timer::id::NO_TIMER;
480 }
481 else
482 {
483 // We already have entries.
484 etl::timer::id::type test_id = begin();
485
486 while (test_id != etl::timer::id::NO_TIMER)
487 {
488 timer_data& test = ptimers[test_id];
489
490 // Find the correct place to insert.
491 if (timer.delta <= test.delta)
492 {
493 if (test.id == head)
494 {
495 head = timer.id;
496 }
497
498 // Insert before test.
499 timer.previous = test.previous;
500 test.previous = timer.id;
501 timer.next = test.id;
502
503 // Adjust the next delta to compensate.
504 test.delta -= timer.delta;
505
506 if (timer.previous != etl::timer::id::NO_TIMER)
507 {
508 ptimers[timer.previous].next = timer.id;
509 }
510 break;
511 }
512 else
513 {
514 timer.delta -= test.delta;
515 }
516
517 test_id = next(test_id);
518 }
519
520 // Reached the end?
521 if (test_id == etl::timer::id::NO_TIMER)
522 {
523 // Tag on to the tail.
524 ptimers[tail].next = timer.id;
525 timer.previous = tail;
526 timer.next = etl::timer::id::NO_TIMER;
527 tail = timer.id;
528 }
529 }
530 }
531
532 //*******************************
533 void remove(etl::timer::id::type id_, bool has_expired)
534 {
535 timer_data& timer = ptimers[id_];
536
537 if (head == id_)
538 {
539 head = timer.next;
540 }
541 else
542 {
543 ptimers[timer.previous].next = timer.next;
544 }
545
546 if (tail == id_)
547 {
548 tail = timer.previous;
549 }
550 else
551 {
552 ptimers[timer.next].previous = timer.previous;
553 }
554
555 if (!has_expired)
556 {
557 // Adjust the next delta.
558 if (timer.next != etl::timer::id::NO_TIMER)
559 {
560 ptimers[timer.next].delta += timer.delta;
561 }
562 }
563
564 timer.previous = etl::timer::id::NO_TIMER;
565 timer.next = etl::timer::id::NO_TIMER;
566 timer.delta = etl::timer::state::Inactive;
567 }
568
569 //*******************************
570 timer_data& front()
571 {
572 return ptimers[head];
573 }
574
575 //*******************************
576 const timer_data& front() const
577 {
578 return ptimers[head];
579 }
580
581 //*******************************
583 {
584 current = head;
585 return current;
586 }
587
588 //*******************************
590 {
591 current = ptimers[last].previous;
592 return current;
593 }
594
595 //*******************************
597 {
598 current = ptimers[last].next;
599 return current;
600 }
601
602 //*******************************
603 void clear()
604 {
606
607 while (id != etl::timer::id::NO_TIMER)
608 {
609 timer_data& timer = ptimers[id];
610 id = next(id);
611 timer.next = etl::timer::id::NO_TIMER;
612 }
613
614 head = etl::timer::id::NO_TIMER;
615 tail = etl::timer::id::NO_TIMER;
616 current = etl::timer::id::NO_TIMER;
617 }
618
619 private:
620
623 etl::timer::id::type current;
624
625 timer_data* const ptimers;
626 };
627
628 // The array of timer data structures.
629 timer_data* const timer_array;
630
631 // The list of active timers.
632 timer_list active_list;
633
634 bool enabled;
635 uint_least8_t number_of_registered_timers;
636
637 try_lock_type try_lock;
638 lock_type lock;
639 unlock_type unlock;
640
641 public:
642
643 const uint_least8_t MAX_TIMERS;
644 };
645
646 //***************************************************************************
648 //***************************************************************************
649 template <uint_least8_t MAX_TIMERS_>
651 {
652 public:
653
654 ETL_STATIC_ASSERT(MAX_TIMERS_ <= 254, "No more than 254 timers are allowed");
655
660
661 //*******************************************
663 //*******************************************
665 : imessage_timer_locked(timer_array, MAX_TIMERS_)
666 {
667 }
668
669 //*******************************************
671 //*******************************************
677
678 private:
679
680 timer_data timer_array[MAX_TIMERS_];
681 };
682}
683
684#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_locked.h:51
bool has_active_timer() const
Check if there is an active timer.
Definition message_timer_locked.h:324
bool is_running() const
Get the enable/disable state.
Definition message_timer_locked.h:141
uint32_t time_to_next() const
Definition message_timer_locked.h:337
~imessage_timer_locked()
Destructor.
Definition message_timer_locked.h:438
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:426
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:73
The message timer.
Definition message_timer_locked.h:651
message_timer_locked()
Constructor.
Definition message_timer_locked.h:664
message_timer_locked(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
Constructor.
Definition message_timer_locked.h:672
ETL_CONSTEXPR14 TIterator remove(TIterator first, TIterator last, const T &value)
Definition algorithm.h:2192
bitset_ext
Definition absolute.h:38
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition iterator.h:962
The configuration of a timer.
Definition message_timer_locked.h:356
bool is_active() const
Returns true if the timer is active.
Definition message_timer_locked.h:393
void set_inactive()
Sets the timer to the inactive state.
Definition message_timer_locked.h:401
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