Zephyr C++ Toolkit
Loading...
Searching...
No Matches
zct::Timer Class Reference

A timer that can be used to execute callbacks at regular intervals in an event driven application. More...

#include <Timer.hpp>

Public Member Functions

 Timer (const char *name, std::function< void()> expiryCallback)
 
void start (int64_t period_ms)
 
void start (int64_t startDuration_ms, int64_t period_ms)
 
void stop ()
 
bool isRunning () const
 
void updateAfterExpiry ()
 
int64_t getNextExpiryTimeTicks () const
 
void setIsRegistered (bool isRegistered)
 
bool getIsRegistered () const
 
void setExpiryCallback (std::function< void()> callback)
 
const std::function< void()> & getExpiryCallback () const
 

Protected Attributes

int64_t period_ticks = 0
 
int64_t startTime_ticks = 0
 
int64_t nextExpiryTime_ticks = 0
 
bool m_isRunning = false
 
bool m_isRegistered = false
 
const char * m_name
 
std::function< void()> m_expiryCallback
 

Detailed Description

A timer that can be used to execute callbacks at regular intervals in an event driven application.

This class is designed to be used with the zct::TimerManager class. The timer manager monitors a list of timers and executes callbacks when the timers expire.

This class is also designed to be used with the zct::EventThread class. The event thread can block until either a timer expires or an external event is received.

Constructor & Destructor Documentation

◆ Timer()

zct::Timer::Timer ( const char *  name,
std::function< void()>  expiryCallback 
)
inline

Create a new timer with a callback function.

The timer will not be running after creation.

Parameters
nameThe name of the timer, used for logging purposes.
expiryCallbackCallback function to call when the timer expires. This will be called by the EventThread when the timer expires.

Member Function Documentation

◆ getExpiryCallback()

const std::function< void()> & zct::Timer::getExpiryCallback ( ) const
inline

Get the expiry callback function.

Returns
The expiry callback function, or nullptr if no callback is set.

◆ getIsRegistered()

bool zct::Timer::getIsRegistered ( ) const
inline

Get the isRegistered flag.

Returns
The value of the isRegistered flag.

◆ getNextExpiryTimeTicks()

int64_t zct::Timer::getNextExpiryTimeTicks ( ) const
inline

Get the next expiry time of the timer.

Returns
The next expiry time of the timer in ticks.

◆ isRunning()

bool zct::Timer::isRunning ( ) const
inline

Check if the timer is running.

Returns
true if the timer is running, false otherwise.

◆ setExpiryCallback()

void zct::Timer::setExpiryCallback ( std::function< void()>  callback)
inline

Set the expiry callback function.

Parameters
callbackThe callback function to call when the timer expires. Set to nullptr to disable callback.

◆ setIsRegistered()

void zct::Timer::setIsRegistered ( bool  isRegistered)
inline

Set the isRegistered flag. Used to log warnings if a timer is started when it is not registered with any timer managers.

Parameters
isRegisteredThe value to set the isRegistered flag to.

◆ start() [1/2]

void zct::Timer::start ( int64_t  period_ms)
inline

Start the timer in reoccurring mode. The timer will expire for the first time after period_ms from when this is called, and then period_ms after that.

Parameters
period_msThe period of the timer. Should be a positive integer.

◆ start() [2/2]

void zct::Timer::start ( int64_t  startDuration_ms,
int64_t  period_ms 
)
inline

Start the timer in either one-shot or reoccurring mode.

Parameters
startDuration_msThe time to wait before the first expiry. Must either be 0 (no-wait) or positive.
period_msThe period of the timer. Set to -1 for a one-shot timer, or 0/positive for a recurring timer.

◆ stop()

void zct::Timer::stop ( )
inline

Stop the timer. This will prevent the timer from expiring until start() is called again.

This does not deregister the timer from the timer manager (if registered), nor does this clear the event that is saved in the timer for when it expires.

◆ updateAfterExpiry()

void zct::Timer::updateAfterExpiry ( )
inline

Designed to be called by the state machine when the timer expires.

This will either:

  1. Stop the timer if it is a one-shot timer.
  2. Update the next expiry time if it is a recurring timer.

Member Data Documentation

◆ m_expiryCallback

std::function<void()> zct::Timer::m_expiryCallback
protected

◆ m_isRegistered

bool zct::Timer::m_isRegistered = false
protected

◆ m_isRunning

bool zct::Timer::m_isRunning = false
protected

◆ m_name

const char* zct::Timer::m_name
protected

◆ nextExpiryTime_ticks

int64_t zct::Timer::nextExpiryTime_ticks = 0
protected

◆ period_ticks

int64_t zct::Timer::period_ticks = 0
protected

◆ startTime_ticks

int64_t zct::Timer::startTime_ticks = 0
protected

The documentation for this class was generated from the following file: