29#ifndef ETL_MESSAGE_TIMER_LOCKED_INCLUDED
30#define ETL_MESSAGE_TIMER_LOCKED_INCLUDED
36#include "message_bus.h"
37#include "message_router.h"
38#include "message_types.h"
40#include "static_assert.h"
59 typedef etl::delegate<void(etl::timer::id::type)> event_callback_type;
67 etl::message_router_id_t destination_router_id_ = etl::imessage_router::ALL_MESSAGE_ROUTERS)
69 etl::timer::id::type
id = etl::timer::id::NO_TIMER;
71 bool is_space = (number_of_registered_timers < Max_Timers);
76 if (!router_.is_null_router())
79 for (uint_least8_t i = 0U; i < Max_Timers; ++i)
83 if (
timer.
id == etl::timer::id::NO_TIMER)
86 new (&
timer)
timer_data(i, message_, router_, period_, repeating_, destination_router_id_);
87 ++number_of_registered_timers;
105 if (id_ != etl::timer::id::NO_TIMER)
109 if (
timer.
id != etl::timer::id::NO_TIMER)
111 if (
timer.is_active())
114 active_list.remove(
timer.
id,
false);
115 remove_callback.call_if(
timer.
id);
121 --number_of_registered_timers;
155 for (
int i = 0; i < Max_Timers; ++i)
160 number_of_registered_timers = 0U;
169 bool tick(uint32_t count)
176 bool has_active = !active_list.empty();
180 while (has_active && (count >= active_list.front().delta))
182 timer_data&
timer = active_list.front();
184 count -=
timer.delta;
186 active_list.remove(
timer.
id,
true);
187 remove_callback.call_if(
timer.
id);
189 if (
timer.p_router != ETL_NULLPTR)
191 timer.p_router->receive(
timer.destination_router_id, *(
timer.p_message));
196 timer.delta = timer.period;
197 active_list.insert(timer.id);
198 insert_callback.call_if(timer.id);
201 has_active = !active_list.empty();
207 active_list.front().delta -= count;
223 bool start(etl::timer::id::type id_,
bool immediate_ =
false)
228 if (id_ != etl::timer::id::NO_TIMER)
233 if (
timer.
id != etl::timer::id::NO_TIMER)
236 if (
timer.period != etl::timer::state::Inactive)
239 if (
timer.is_active())
241 active_list.remove(
timer.
id,
false);
242 remove_callback.call_if(
timer.
id);
247 insert_callback.call_if(
timer.
id);
261 bool stop(etl::timer::id::type id_)
266 if (id_ != etl::timer::id::NO_TIMER)
271 if (
timer.
id != etl::timer::id::NO_TIMER)
273 if (
timer.is_active())
276 active_list.remove(
timer.
id,
false);
277 remove_callback.call_if(
timer.
id);
295 timer_array[id_].period = period_;
305 bool set_mode(etl::timer::id::type id_,
bool repeating_)
309 timer_array[id_].repeating = repeating_;
319 void set_locks(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
321 try_lock = try_lock_;
332 bool result = !active_list.empty();
345 uint32_t delta =
static_cast<uint32_t
>(etl::timer::interval::No_Active_Interval);
348 if (!active_list.empty())
350 delta = active_list.front().delta;
362 insert_callback = insert_;
370 remove_callback = remove_;
374 void clear_insert_callback()
376 insert_callback.clear();
380 void clear_remove_callback()
382 remove_callback.clear();
393 : p_message(ETL_NULLPTR)
394 , p_router(ETL_NULLPTR)
396 , delta(etl::timer::state::Inactive)
397 , destination_router_id(etl::imessage_bus::ALL_MESSAGE_ROUTERS)
398 , id(etl::timer::id::NO_TIMER)
399 , previous(etl::timer::id::NO_TIMER)
400 , next(etl::timer::id::NO_TIMER)
407 etl::message_router_id_t destination_router_id_ = etl::imessage_bus::ALL_MESSAGE_ROUTERS)
408 : p_message(&message_)
409 , p_router(&irouter_)
411 , delta(etl::timer::state::Inactive)
412 , destination_router_id(destination_router_id_)
414 , previous(etl::timer::id::NO_TIMER)
415 , next(etl::timer::id::NO_TIMER)
416 , repeating(repeating_)
425 return delta != etl::timer::state::Inactive;
433 delta = etl::timer::state::Inactive;
440 etl::message_router_id_t destination_router_id;
441 etl::timer::id::type id;
442 uint_least8_t previous;
457 : timer_array(timer_array_)
458 , active_list(timer_array_)
460 , number_of_registered_timers(0U)
461 , Max_Timers(Max_Timers_)
480 timer_list(timer_data* ptimers_)
490 return head == etl::timer::id::NO_TIMER;
496 void insert(etl::timer::id::type id_)
498 timer_data& timer = ptimers[id_];
500 if (head == etl::timer::id::NO_TIMER)
505 timer.previous = etl::timer::id::NO_TIMER;
506 timer.next = etl::timer::id::NO_TIMER;
511 etl::timer::id::type test_id = begin();
513 while (test_id != etl::timer::id::NO_TIMER)
515 timer_data& test = ptimers[test_id];
518 if (timer.delta <= test.delta)
526 timer.previous = test.previous;
527 test.previous = timer.id;
528 timer.next = test.id;
531 test.delta -= timer.delta;
533 if (timer.previous != etl::timer::id::NO_TIMER)
535 ptimers[timer.previous].next = timer.id;
541 timer.delta -= test.delta;
544 test_id = next(test_id);
548 if (test_id == etl::timer::id::NO_TIMER)
551 ptimers[tail].next = timer.id;
552 timer.previous = tail;
553 timer.next = etl::timer::id::NO_TIMER;
560 void remove(etl::timer::id::type id_,
bool has_expired)
562 timer_data& timer = ptimers[id_];
570 ptimers[timer.previous].next = timer.next;
575 tail = timer.previous;
579 ptimers[timer.next].previous = timer.previous;
585 if (timer.next != etl::timer::id::NO_TIMER)
587 ptimers[timer.next].delta += timer.delta;
591 timer.previous = etl::timer::id::NO_TIMER;
592 timer.next = etl::timer::id::NO_TIMER;
593 timer.delta = etl::timer::state::Inactive;
599 return ptimers[head];
603 const timer_data& front()
const
605 return ptimers[head];
609 etl::timer::id::type begin()
615 etl::timer::id::type next(etl::timer::id::type last)
617 return ptimers[last].next;
623 etl::timer::id::type
id = begin();
625 while (
id != etl::timer::id::NO_TIMER)
627 timer_data& timer = ptimers[id];
629 timer.next = etl::timer::id::NO_TIMER;
632 head = etl::timer::id::NO_TIMER;
633 tail = etl::timer::id::NO_TIMER;
638 etl::timer::id::type head;
639 etl::timer::id::type tail;
641 timer_data*
const ptimers;
648 timer_list active_list;
651 uint_least8_t number_of_registered_timers;
653 try_lock_type try_lock;
657 event_callback_type insert_callback;
658 event_callback_type remove_callback;
662 const uint_least8_t Max_Timers;
668 template <u
int_least8_t Max_Timers_>
673 ETL_STATIC_ASSERT(Max_Timers_ <= 254,
"No more than 254 timers are allowed");
675 typedef imessage_timer_locked::callback_type callback_type;
676 typedef imessage_timer_locked::try_lock_type try_lock_type;
677 typedef imessage_timer_locked::lock_type lock_type;
678 typedef imessage_timer_locked::unlock_type unlock_type;
694 this->
set_locks(try_lock_, lock_, unlock_);
699 timer_data timer_array[Max_Timers_];
Declaration.
Definition delegate_cpp03.h:191
This is the base of all message routers.
Definition message_router.h:141
Interface for message timer.
Definition message_timer_locked.h:51
void set_insert_callback(event_callback_type insert_)
Set a callback when a timer is inserted on list.
Definition message_timer_locked.h:360
bool has_active_timer() const
Check if there is an active timer.
Definition message_timer_locked.h:329
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:343
~imessage_timer_locked()
Destructor.
Definition message_timer_locked.h:468
bool set_period(etl::timer::id::type id_, uint32_t period_)
Sets a timer's period.
Definition message_timer_locked.h:291
void enable(bool state_)
Enable/disable the timer.
Definition message_timer_locked.h:133
imessage_timer_locked(timer_data *const timer_array_, const uint_least8_t Max_Timers_)
Constructor.
Definition message_timer_locked.h:456
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:223
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:319
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:66
bool stop(etl::timer::id::type id_)
Stops a timer.
Definition message_timer_locked.h:261
bool unregister_timer(etl::timer::id::type id_)
Unregister a timer.
Definition message_timer_locked.h:101
void set_remove_callback(event_callback_type remove_)
Set a callback when a timer is removed from list.
Definition message_timer_locked.h:368
bool set_mode(etl::timer::id::type id_, bool repeating_)
Sets a timer's mode.
Definition message_timer_locked.h:305
message_timer_locked(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
Constructor.
Definition message_timer_locked.h:691
message_timer_locked()
Constructor.
Definition message_timer_locked.h:683
The configuration of a timer.
Definition message_timer_locked.h:390
bool is_active() const
Returns true if the timer is active.
Definition message_timer_locked.h:423
void set_inactive()
Sets the timer to the inactive state.
Definition message_timer_locked.h:431
Common definitions for the timer framework.
Definition timer.h:55