Skip to content

Commit

Permalink
Refactored callback_timer_locked to allow base class extention of tic…
Browse files Browse the repository at this point in the history
…k method
  • Loading branch information
Mario Luzeiro committed Sep 7, 2024
1 parent 081e920 commit 1bb9801
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions include/etl/callback_timer_locked.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,55 +156,7 @@ namespace etl
// Returns true if the tick was processed,
// false if not.
//*******************************************
bool tick(uint32_t count)
{
if (enabled)
{
if (try_lock())
{
// We have something to do?
bool has_active = !active_list.empty();

if (has_active)
{
while (has_active && (count >= active_list.front().delta))
{
timer_data& timer = active_list.front();

count -= timer.delta;

active_list.remove(timer.id, true);

if (timer.callback.is_valid())
{
timer.callback();
}

if (timer.repeating)
{
// Reinsert the timer.
timer.delta = timer.period;
active_list.insert(timer.id);
}

has_active = !active_list.empty();
}

if (has_active)
{
// Subtract any remainder from the next due timeout.
active_list.front().delta -= count;
}
}

unlock();

return true;
}
}

return false;
}
virtual bool tick(uint32_t count) = 0;

//*******************************************
/// Starts a timer.
Expand Down Expand Up @@ -653,6 +605,8 @@ namespace etl
unlock_type unlock; ///< The callback that unlocks.

public:
template <uint_least8_t>
friend class callback_timer_locked;

const uint_least8_t MAX_TIMERS;
};
Expand Down Expand Up @@ -689,6 +643,58 @@ namespace etl
this->set_locks(try_lock_, lock_, unlock_);
}

// Implement virtual functions

bool tick(uint32_t count) final
{
if (enabled)
{
if (try_lock())
{
// We have something to do?
bool has_active = !active_list.empty();

if (has_active)
{
while (has_active && (count >= active_list.front().delta))
{
timer_data& timer = active_list.front();

count -= timer.delta;

active_list.remove(timer.id, true);

if (timer.callback.is_valid())
{
timer.callback();
}

if (timer.repeating)
{
// Reinsert the timer.
timer.delta = timer.period;
active_list.insert(timer.id);
}

has_active = !active_list.empty();
}

if (has_active)
{
// Subtract any remainder from the next due timeout.
active_list.front().delta -= count;
}
}

unlock();

return true;
}
}

return false;
}

private:

timer_data timer_array[MAX_TIMERS_];
Expand Down

0 comments on commit 1bb9801

Please sign in to comment.