29#ifndef ETL_MESSAGE_TIMER_INCLUDED
30#define ETL_MESSAGE_TIMER_INCLUDED
34#include "message_types.h"
36#include "message_router.h"
37#include "message_bus.h"
38#include "static_assert.h"
45#if defined(ETL_IN_UNIT_TEST) && ETL_NOT_USING_STL
46 #define ETL_DISABLE_TIMER_UPDATES
47 #define ETL_ENABLE_TIMER_UPDATES
48 #define ETL_TIMER_UPDATES_ENABLED true
50 #undef ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK
51 #undef ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK
53 #if !defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK) && !defined(ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK)
54 #error ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK or ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK not defined
57 #if defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK) && defined(ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK)
58 #error Only define one of ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK or ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK
61 #if defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK)
62 #define ETL_DISABLE_TIMER_UPDATES (++process_semaphore)
63 #define ETL_ENABLE_TIMER_UPDATES (--process_semaphore)
64 #define ETL_TIMER_UPDATES_ENABLED (process_semaphore.load() == 0)
67 #if defined(ETL_MESSAGE_TIMER_USE_INTERRUPT_LOCK)
68 #if !defined(ETL_MESSAGE_TIMER_DISABLE_INTERRUPTS) || !defined(ETL_MESSAGE_TIMER_ENABLE_INTERRUPTS)
69 #error ETL_MESSAGE_TIMER_DISABLE_INTERRUPTS and/or ETL_MESSAGE_TIMER_ENABLE_INTERRUPTS not defined
72 #define ETL_DISABLE_TIMER_UPDATES ETL_MESSAGE_TIMER_DISABLE_INTERRUPTS
73 #define ETL_ENABLE_TIMER_UPDATES ETL_MESSAGE_TIMER_ENABLE_INTERRUPTS
74 #define ETL_TIMER_UPDATES_ENABLED true
86 : p_message(ETL_NULLPTR),
87 p_router(ETL_NULLPTR),
89 delta(etl::timer::state::Inactive),
90 destination_router_id(etl::imessage_bus::ALL_MESSAGE_ROUTERS),
91 id(etl::timer::id::NO_TIMER),
92 previous(etl::timer::id::NO_TIMER),
93 next(etl::timer::id::NO_TIMER),
108 delta(etl::timer::state::Inactive),
111 previous(etl::timer::id::NO_TIMER),
112 next(etl::timer::id::NO_TIMER),
122 return delta != etl::timer::state::Inactive;
130 delta = etl::timer::state::Inactive;
137 etl::message_router_id_t destination_router_id;
150 namespace private_message_timer
161 : head(
etl::timer::id::NO_TIMER),
162 tail(
etl::timer::id::NO_TIMER),
163 current(
etl::timer::id::NO_TIMER),
171 return head == etl::timer::id::NO_TIMER;
181 if (head == etl::timer::id::NO_TIMER)
186 timer.previous = etl::timer::id::NO_TIMER;
187 timer.next = etl::timer::id::NO_TIMER;
194 while (test_id != etl::timer::id::NO_TIMER)
199 if (timer.delta <= test.delta)
207 timer.previous = test.previous;
208 test.previous = timer.id;
209 timer.next = test.id;
212 test.delta -= timer.delta;
214 if (timer.previous != etl::timer::id::NO_TIMER)
216 ptimers[timer.previous].next = timer.id;
222 timer.delta -= test.delta;
225 test_id = next(test_id);
229 if (test_id == etl::timer::id::NO_TIMER)
232 ptimers[tail].next = timer.id;
233 timer.previous = tail;
234 timer.next = etl::timer::id::NO_TIMER;
251 ptimers[timer.previous].next = timer.next;
256 tail = timer.previous;
260 ptimers[timer.next].previous = timer.previous;
266 if (timer.next != etl::timer::id::NO_TIMER)
268 ptimers[timer.next].delta += timer.delta;
272 timer.previous = etl::timer::id::NO_TIMER;
273 timer.next = etl::timer::id::NO_TIMER;
274 timer.delta = etl::timer::state::Inactive;
280 return ptimers[head];
286 return ptimers[head];
299 current = ptimers[last].previous;
306 current = ptimers[last].next;
315 while (
id != etl::timer::id::NO_TIMER)
319 timer.next = etl::timer::id::NO_TIMER;
322 head = etl::timer::id::NO_TIMER;
323 tail = etl::timer::id::NO_TIMER;
324 current = etl::timer::id::NO_TIMER;
355 bool is_space = (registered_timers < MAX_TIMERS);
367 if (
timer.
id == etl::timer::id::NO_TIMER)
389 if (
id_ != etl::timer::id::NO_TIMER)
393 if (
timer.
id != etl::timer::id::NO_TIMER)
395 if (
timer.is_active())
398 active_list.remove(
timer.
id,
true);
438 for (
int i = 0;
i < MAX_TIMERS; ++
i)
443 registered_timers = 0;
463 while (
has_active && (count >= active_list.front().delta))
467 count -=
timer.delta;
469 active_list.remove(
timer.
id,
true);
477 if (timer.p_router != ETL_NULLPTR)
479 timer.p_router->receive(timer.destination_router_id, *(timer.p_message));
482 has_active = !active_list.empty();
488 active_list.front().delta -= count;
507 if (
id_ != etl::timer::id::NO_TIMER)
512 if (
timer.
id != etl::timer::id::NO_TIMER)
515 if (
timer.period != etl::timer::state::Inactive)
518 if (
timer.is_active())
520 active_list.remove(
timer.
id,
false);
543 if (
id_ != etl::timer::id::NO_TIMER)
548 if (
timer.
id != etl::timer::id::NO_TIMER)
550 if (
timer.is_active())
553 active_list.remove(
timer.
id,
false);
598 bool result = !active_list.empty();
610 uint32_t delta =
static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
613 if (!active_list.empty())
615 delta = active_list.front().delta;
632 process_semaphore(0),
634 registered_timers(0),
652 private_message_timer::list active_list;
656#if defined(ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK)
658#if defined(ETL_TIMER_SEMAPHORE_TYPE)
664 #error No atomic type available
680 template <u
int_least8_t MAX_TIMERS_>
685 ETL_STATIC_ASSERT(
MAX_TIMERS_ <= 254,
"No more than 254 timers are allowed");
701#undef ETL_DISABLE_TIMER_UPDATES
702#undef ETL_ENABLE_TIMER_UPDATES
703#undef ETL_TIMER_UPDATES_ENABLED
This is the base of all message routers.
Definition message_router_generator.h:121
Interface for message timer.
Definition message_timer.h:341
uint32_t time_to_next() const
Definition message_timer.h:608
bool has_active_timer() const
Check if there is an active timer.
Definition message_timer.h:595
void enable(bool state_)
Enable/disable the timer.
Definition message_timer.h:416
bool start(etl::timer::id::type id_, bool immediate_=false)
Starts a timer.
Definition message_timer.h:502
bool unregister_timer(etl::timer::id::type id_)
Unregister a timer.
Definition message_timer.h:385
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.h:347
bool is_running() const
Get the enable/disable state.
Definition message_timer.h:424
bool set_mode(etl::timer::id::type id_, bool repeating_)
Sets a timer's mode.
Definition message_timer.h:581
imessage_timer(message_timer_data *const timer_array_, const uint_least8_t MAX_TIMERS_)
Constructor.
Definition message_timer.h:627
void clear()
Clears the timer of data.
Definition message_timer.h:432
bool stop(etl::timer::id::type id_)
Stops a timer.
Definition message_timer.h:538
~imessage_timer()
Destructor.
Definition message_timer.h:642
bool set_period(etl::timer::id::type id_, uint32_t period_)
Sets a timer's period.
Definition message_timer.h:567
The message timer.
Definition message_timer.h:682
message_timer()
Constructor.
Definition message_timer.h:690
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.h:83
bool is_active() const
Returns true if the timer is active.
Definition message_timer.h:120
void set_inactive()
Sets the timer to the inactive state.
Definition message_timer.h:128
pair holds two objects of arbitrary type
Definition utility.h:164
Common definitions for the timer framework.
Definition timer.h:55