A timer that can be used to execute callbacks at regular intervals in an event driven application.
More...
#include <Timer.hpp>
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.
◆ 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
-
name | The name of the timer, used for logging purposes. |
expiryCallback | Callback function to call when the timer expires. This will be called by the EventThread when the timer expires. |
◆ 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
-
callback | The 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
-
isRegistered | The 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_ms | The 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_ms | The time to wait before the first expiry. Must either be 0 (no-wait) or positive. |
period_ms | The 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:
- Stop the timer if it is a one-shot timer.
- Update the next expiry time if it is a recurring timer.
◆ 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:
- /home/runner/work/ZephyrCppToolkit/ZephyrCppToolkit/include/ZephyrCppToolkit/Events/Timer.hpp