Timer¶
The Timer
class is the most flexible way to use software timers, supporting extended time intervals
and delegate callback functions so you can use it with class methods, capturing lambdas, etc.
-
group
timer
Extended timer queue class.
Typedefs
-
typedef std::function<void()>
TimerDelegateStdFunction
¶
-
template <class TimerClass>
classOsTimer64Api
: public CallbackTimerApi<OsTimer64Api<TimerClass>>¶ - #include <Timer.h>
Class template implementing an extended OS Timer with 64-bit microsecond times and delegate callback support.
Public Types
-
template<>
usingClock
= OsTimerApi::Clock¶
-
template<>
usingTickType
= uint64_t¶
-
template<>
usingTimeType
= uint64_t¶
Public Functions
-
OsTimer64Api
()¶
-
~OsTimer64Api
()¶
-
bool
isArmed
() const
-
TickType
ticks
() const
-
void
setCallback
(TimerCallback callback, void *arg)¶
-
void
setCallback
(TimerDelegate delegateFunction)¶
-
void
setInterval
(TickType interval)
-
TickType
getInterval
() const¶
-
void
arm
(bool repeating)
-
void
disarm
()
-
String
name
() const
-
String
toString
() const
-
operator String
() const
Public Members
-
TimerCallback
func
= nullptr
-
void *
arg
= nullptr
Public Static Functions
-
static constexpr const char *
typeName
()
-
static constexpr TickType
minTicks
()
-
static constexpr TickType
maxTicks
()
-
template<>
-
template <typename TimerApi>
classDelegateCallbackTimer
: public CallbackTimer<TimerApi>¶ - #include <Timer.h>
Class template adding delegate callback method support to the basic CallbackTimer template.
Public Types
-
template<>
usingMillis
= NanoTime::TimeSource<Clock, NanoTime::Milliseconds, uint32_t>
-
template<>
usingMicros
= NanoTime::TimeSource<Clock, NanoTime::Microseconds, TimeType>
Public Functions
-
template <TimeType microseconds>
DelegateCallbackTimer &initializeUs
(TimerDelegate delegateFunction) Initialise timer in microseconds, with static check.
- Template Parameters
microseconds
: Timer interval in microseconds
- Parameters
delegateFunction
: Function to call when timer triggers
- Return Value
ExtendedCallbackTimer&
: Reference to timer
-
template <uint32_t milliseconds>
DelegateCallbackTimer &initializeMs
(TimerDelegate delegateFunction) Initialise hardware timer in milliseconds, with static check.
- Template Parameters
milliseconds
: Timer interval in milliseconds
- Parameters
delegateFunction
: Function to call when timer triggers
- Return Value
ExtendedCallbackTimer&
: Reference to timer
-
DelegateCallbackTimer &
initializeMs
(uint32_t milliseconds, TimerDelegate delegateFunction) Initialise millisecond timer.
- Note
- Delegate callback method
- Parameters
milliseconds
: Duration of timer in millisecondsdelegateFunction
: Function to call when timer triggers
-
DelegateCallbackTimer &
initializeUs
(uint32_t microseconds, TimerDelegate delegateFunction) Initialise microsecond timer.
- Note
- Delegate callback method
- Parameters
microseconds
: Duration of timer in millisecondsdelegateFunction
: Function to call when timer triggers
-
void
setCallback
(TimerDelegate delegateFunction) Set timer trigger function using Delegate callback method.
- Note
- Don’t use this for interrupt timers
- Parameters
delegateFunction
: Function to be called on timer trigger
-
template <NanoTime::Unit unit, TimeType time>
CallbackTimer &initialize
(TimerCallback callback, void *arg = nullptr) Initialise timer with an interval (static check) and callback.
- Note
- If interval out of range compilation will fail with error
- Template Parameters
unit
: Time unit for intervaltime
: Timer interval
- Parameters
callback
: Callback function to call when timer triggersarg
: Optional argument passed to callback
- Return Value
CallbackTimer&
: Reference to timer
-
template <NanoTime::Unit unit>
CallbackTimer &initialize
(TimeType time, TimerCallback callback, void *arg = nullptr) Initialise timer with an interval and callback.
- Template Parameters
unit
: Time unit for interval
- Parameters
time
: Timer intervalcallback
: Callback function to call when timer triggersarg
: Optional argument passed to callback
- Return Value
CallbackTimer&
: Reference to timer
-
template <TimeType microseconds>
CallbackTimer &initializeUs
(TimerCallback callback, void *arg = nullptr) Initialise timer in microseconds (static check) with Timer Callback and optional argument.
-
template <TimeType microseconds>
CallbackTimer &initializeUs
(InterruptCallback callback = nullptr) Initialise timer in microseconds (static check) with optional Interrupt Callback (no argument)
-
CallbackTimer &
initializeUs
(TimeType microseconds, TimerCallback callback, void *arg = nullptr) Initialise timer in microseconds with Timer Callback and optional argument.
-
CallbackTimer &
initializeUs
(TimeType microseconds, InterruptCallback callback = nullptr) Initialise timer in microseconds with optional Interrupt Callback (no arg)
-
template <uint32_t milliseconds>
CallbackTimer &initializeMs
(TimerCallback callback, void *arg = nullptr) Initialise hardware timer in milliseconds (static check) with Timer Callback and optional argument.
-
template <uint32_t milliseconds>
CallbackTimer &initializeMs
(InterruptCallback callback = nullptr) Initialise hardware timer in milliseconds (static check) and optional Interrupt Callback (no arg)
-
CallbackTimer &
initializeMs
(uint32_t milliseconds, TimerCallback callback, void *arg = nullptr) Initialise hardware timer in milliseconds with Timer Callback and optional argument.
-
CallbackTimer &
initializeMs
(uint32_t milliseconds, InterruptCallback callback = nullptr) Initialise hardware timer in milliseconds with optional Interrupt Callback (no arg)
-
bool
start
(bool repeating = true) Start timer running.
- Parameters
repeating
: True to restart timer when it triggers, false for one-shot (Default: true)
- Return Value
bool
: True if timer started
-
bool
startOnce
() Start one-shot timer.
- Note
- Timer starts and will run for configured period then stop
- Return Value
bool
: True if timer started
-
void
stop
() Stops timer.
-
bool
restart
() Restart timer.
- Note
- Timer is stopped then started with current configuration
- Return Value
bool
: True if timer started
-
bool
isStarted
() const Check if timer is started.
- Return Value
bool
: True if started
-
TickType
getInterval
() const Get timer interval in clock ticks.
-
bool
checkInterval
(TickType ticks) const Check timer interval is valid.
- Parameters
ticks
: Interval to check
- Return Value
bool
: true if interval is within acceptable range for this timer
-
bool
setInterval
(TickType ticks) Set timer interval in timer ticks.
- Parameters
ticks
: Interval in timer ticks
-
template <TimeType ticks>
voidsetInterval
() Set timer interval in timer ticks (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
ticks
: Interval in ticks
-
template <NanoTime::Unit unit, TimeType time>
voidsetInterval
() Set timer interval in specific time unit (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
unit
:time
: Interval to set
-
template <NanoTime::Unit unit>
boolsetInterval
(TimeType time) Set timer interval in timer ticks.
- Template Parameters
unit
:
- Parameters
time
: Interval in given units
-
bool
setIntervalUs
(TimeType microseconds) Set timer interval in microseconds.
-
template <TimeType microseconds>
voidsetIntervalUs
() Set timer interval in microseconds (static check)
-
bool
setIntervalMs
(uint32_t milliseconds) Set timer interval in milliseconds.
-
template <uint32_t milliseconds>
voidsetIntervalMs
() Set timer interval in milliseconds (static check)
-
void
setCallback
(TimerCallback callback, void *arg = nullptr) Set timer trigger callback.
- Parameters
callback
: Function to call when timer triggersarg
: Optional argument passed to callback
-
void
setCallback
(InterruptCallback callback) Set timer trigger callback.
- Note
- Provided for convenience where callback argument not required
- Parameters
callback
: Function to call when timer triggers
Public Static Functions
-
static constexpr Millis
millis
() Get a millisecond time source.
-
static constexpr Micros
micros
() Get a microsecond time source.
-
template <uint64_t us>
static constexpr uint64_tusToTicks
() Convert microsecond count into timer ticks.
-
static TickType
usToTicks
(TimeType time) Convert microsecond count into timer ticks.
-
template <uint64_t ticks>
static constexpr uint64_tticksToUs
() Convert timer ticks into microseconds.
-
static TimeType
ticksToUs
(TickType ticks) Convert timer ticks into microseconds.
-
template <uint64_t ticks>
static constexpr voidcheckInterval
() Check timer interval in ticks is valid (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
ticks
: Timer interval to check
-
template <NanoTime::Unit unit, uint64_t time>
static constexpr voidcheckInterval
() Check timer interval in specific time unit is valid (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
unit
: Time unit for intervaltime
: Interval to check
-
template <uint64_t milliseconds>
static constexpr voidcheckIntervalMs
() Check timer interval in milliseconds is valid (static check)
-
template <uint64_t microseconds>
static constexpr voidcheckIntervalUs
() Check timer interval in microseconds is valid (static check)
-
template<>
-
class
Timer
: public DelegateCallbackTimer<OsTimer64Api<Timer>>¶ - #include <Timer.h>
Callback timer class.
Subclassed by Profiling::MinMaxTimes< Timer >
Public Types
-
template<>
usingMillis
= NanoTime::TimeSource<Clock, NanoTime::Milliseconds, uint32_t>
-
template<>
usingMicros
= NanoTime::TimeSource<Clock, NanoTime::Microseconds, TimeType>
Public Functions
-
DelegateCallbackTimer &
initializeUs
(TimerDelegate delegateFunction) Initialise timer in microseconds, with static check.
- Template Parameters
microseconds
: Timer interval in microseconds
- Parameters
delegateFunction
: Function to call when timer triggers
- Return Value
ExtendedCallbackTimer&
: Reference to timer
-
DelegateCallbackTimer &
initializeUs
(uint32_t microseconds, TimerDelegate delegateFunction) Initialise microsecond timer.
- Note
- Delegate callback method
- Parameters
microseconds
: Duration of timer in millisecondsdelegateFunction
: Function to call when timer triggers
-
CallbackTimer &
initializeUs
(TimerCallback callback, void *arg = nullptr) Initialise timer in microseconds (static check) with Timer Callback and optional argument.
-
CallbackTimer &
initializeUs
(InterruptCallback callback = nullptr) Initialise timer in microseconds (static check) with optional Interrupt Callback (no argument)
-
CallbackTimer &
initializeUs
(TimeType microseconds, TimerCallback callback, void *arg = nullptr) Initialise timer in microseconds with Timer Callback and optional argument.
-
CallbackTimer &
initializeUs
(TimeType microseconds, InterruptCallback callback = nullptr) Initialise timer in microseconds with optional Interrupt Callback (no arg)
-
DelegateCallbackTimer &
initializeMs
(TimerDelegate delegateFunction) Initialise hardware timer in milliseconds, with static check.
- Template Parameters
milliseconds
: Timer interval in milliseconds
- Parameters
delegateFunction
: Function to call when timer triggers
- Return Value
ExtendedCallbackTimer&
: Reference to timer
-
DelegateCallbackTimer &
initializeMs
(uint32_t milliseconds, TimerDelegate delegateFunction) Initialise millisecond timer.
- Note
- Delegate callback method
- Parameters
milliseconds
: Duration of timer in millisecondsdelegateFunction
: Function to call when timer triggers
-
CallbackTimer &
initializeMs
(TimerCallback callback, void *arg = nullptr) Initialise hardware timer in milliseconds (static check) with Timer Callback and optional argument.
-
CallbackTimer &
initializeMs
(InterruptCallback callback = nullptr) Initialise hardware timer in milliseconds (static check) and optional Interrupt Callback (no arg)
-
CallbackTimer &
initializeMs
(uint32_t milliseconds, TimerCallback callback, void *arg = nullptr) Initialise hardware timer in milliseconds with Timer Callback and optional argument.
-
CallbackTimer &
initializeMs
(uint32_t milliseconds, InterruptCallback callback = nullptr) Initialise hardware timer in milliseconds with optional Interrupt Callback (no arg)
-
void
setCallback
(TimerDelegate delegateFunction) Set timer trigger function using Delegate callback method.
- Note
- Don’t use this for interrupt timers
- Parameters
delegateFunction
: Function to be called on timer trigger
-
void
setCallback
(TimerCallback callback, void *arg = nullptr) Set timer trigger callback.
- Parameters
callback
: Function to call when timer triggersarg
: Optional argument passed to callback
-
void
setCallback
(InterruptCallback callback) Set timer trigger callback.
- Note
- Provided for convenience where callback argument not required
- Parameters
callback
: Function to call when timer triggers
-
CallbackTimer &
initialize
(TimerCallback callback, void *arg = nullptr) Initialise timer with an interval (static check) and callback.
- Note
- If interval out of range compilation will fail with error
- Template Parameters
unit
: Time unit for intervaltime
: Timer interval
- Parameters
callback
: Callback function to call when timer triggersarg
: Optional argument passed to callback
- Return Value
CallbackTimer&
: Reference to timer
-
CallbackTimer &
initialize
(TimeType time, TimerCallback callback, void *arg = nullptr) Initialise timer with an interval and callback.
- Template Parameters
unit
: Time unit for interval
- Parameters
time
: Timer intervalcallback
: Callback function to call when timer triggersarg
: Optional argument passed to callback
- Return Value
CallbackTimer&
: Reference to timer
-
bool
start
(bool repeating = true) Start timer running.
- Parameters
repeating
: True to restart timer when it triggers, false for one-shot (Default: true)
- Return Value
bool
: True if timer started
-
bool
startOnce
() Start one-shot timer.
- Note
- Timer starts and will run for configured period then stop
- Return Value
bool
: True if timer started
-
void
stop
() Stops timer.
-
bool
restart
() Restart timer.
- Note
- Timer is stopped then started with current configuration
- Return Value
bool
: True if timer started
-
bool
isStarted
() const Check if timer is started.
- Return Value
bool
: True if started
-
TickType
getInterval
() const Get timer interval in clock ticks.
-
bool
checkInterval
(TickType ticks) const Check timer interval is valid.
- Parameters
ticks
: Interval to check
- Return Value
bool
: true if interval is within acceptable range for this timer
-
bool
setInterval
(TickType ticks) Set timer interval in timer ticks.
- Parameters
ticks
: Interval in timer ticks
-
void
setInterval
() Set timer interval in timer ticks (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
ticks
: Interval in ticks
-
void
setInterval
() Set timer interval in specific time unit (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
unit
:time
: Interval to set
-
bool
setInterval
(TimeType time) Set timer interval in timer ticks.
- Template Parameters
unit
:
- Parameters
time
: Interval in given units
-
bool
setIntervalUs
(TimeType microseconds) Set timer interval in microseconds.
-
void
setIntervalUs
() Set timer interval in microseconds (static check)
-
bool
setIntervalMs
(uint32_t milliseconds) Set timer interval in milliseconds.
-
void
setIntervalMs
() Set timer interval in milliseconds (static check)
Public Static Functions
-
static constexpr Millis
millis
() Get a millisecond time source.
-
static constexpr Micros
micros
() Get a microsecond time source.
-
static constexpr uint64_t
usToTicks
() Convert microsecond count into timer ticks.
-
static TickType
usToTicks
(TimeType time) Convert microsecond count into timer ticks.
-
static constexpr uint64_t
ticksToUs
() Convert timer ticks into microseconds.
-
static TimeType
ticksToUs
(TickType ticks) Convert timer ticks into microseconds.
-
static constexpr void
checkInterval
() Check timer interval in ticks is valid (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
ticks
: Timer interval to check
-
static constexpr void
checkInterval
() Check timer interval in specific time unit is valid (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
unit
: Time unit for intervaltime
: Interval to check
-
static constexpr void
checkIntervalMs
() Check timer interval in milliseconds is valid (static check)
-
static constexpr void
checkIntervalUs
() Check timer interval in microseconds is valid (static check)
Protected Types
-
template<>
usingClock
= OsTimerApi::Clock
-
template<>
usingTickType
= uint64_t
-
template<>
usingTimeType
= uint64_t
Protected Functions
-
bool
isArmed
() const
-
TickType
ticks
() const
-
void
arm
(bool repeating)
-
void
disarm
()
-
String
name
() const
-
String
toString
() const
-
operator String
() const
Protected Attributes
-
TimerCallback
func
-
void *
arg
Protected Static Functions
-
static constexpr const char *
typeName
()
-
static constexpr TickType
minTicks
()
-
static constexpr TickType
maxTicks
()
-
template<>
-
class
AutoDeleteTimer
: public DelegateCallbackTimer<OsTimer64Api<AutoDeleteTimer>>¶ - #include <Timer.h>
Auto-delete callback timer class.
Public Types
-
template<>
usingMillis
= NanoTime::TimeSource<Clock, NanoTime::Milliseconds, uint32_t>¶
-
template<>
usingMicros
= NanoTime::TimeSource<Clock, NanoTime::Microseconds, TimeType>¶
Public Functions
-
DelegateCallbackTimer &
initializeUs
(TimerDelegate delegateFunction) Initialise timer in microseconds, with static check.
- Template Parameters
microseconds
: Timer interval in microseconds
- Parameters
delegateFunction
: Function to call when timer triggers
- Return Value
ExtendedCallbackTimer&
: Reference to timer
-
DelegateCallbackTimer &
initializeUs
(uint32_t microseconds, TimerDelegate delegateFunction) Initialise microsecond timer.
- Note
- Delegate callback method
- Parameters
microseconds
: Duration of timer in millisecondsdelegateFunction
: Function to call when timer triggers
-
CallbackTimer &
initializeUs
(TimerCallback callback, void *arg = nullptr) Initialise timer in microseconds (static check) with Timer Callback and optional argument.
-
CallbackTimer &
initializeUs
(InterruptCallback callback = nullptr) Initialise timer in microseconds (static check) with optional Interrupt Callback (no argument)
-
CallbackTimer &
initializeUs
(TimeType microseconds, TimerCallback callback, void *arg = nullptr) Initialise timer in microseconds with Timer Callback and optional argument.
-
CallbackTimer &
initializeUs
(TimeType microseconds, InterruptCallback callback = nullptr) Initialise timer in microseconds with optional Interrupt Callback (no arg)
-
DelegateCallbackTimer &
initializeMs
(TimerDelegate delegateFunction) Initialise hardware timer in milliseconds, with static check.
- Template Parameters
milliseconds
: Timer interval in milliseconds
- Parameters
delegateFunction
: Function to call when timer triggers
- Return Value
ExtendedCallbackTimer&
: Reference to timer
-
DelegateCallbackTimer &
initializeMs
(uint32_t milliseconds, TimerDelegate delegateFunction) Initialise millisecond timer.
- Note
- Delegate callback method
- Parameters
milliseconds
: Duration of timer in millisecondsdelegateFunction
: Function to call when timer triggers
-
CallbackTimer &
initializeMs
(TimerCallback callback, void *arg = nullptr) Initialise hardware timer in milliseconds (static check) with Timer Callback and optional argument.
-
CallbackTimer &
initializeMs
(InterruptCallback callback = nullptr) Initialise hardware timer in milliseconds (static check) and optional Interrupt Callback (no arg)
-
CallbackTimer &
initializeMs
(uint32_t milliseconds, TimerCallback callback, void *arg = nullptr) Initialise hardware timer in milliseconds with Timer Callback and optional argument.
-
CallbackTimer &
initializeMs
(uint32_t milliseconds, InterruptCallback callback = nullptr) Initialise hardware timer in milliseconds with optional Interrupt Callback (no arg)
-
void
setCallback
(TimerDelegate delegateFunction) Set timer trigger function using Delegate callback method.
- Note
- Don’t use this for interrupt timers
- Parameters
delegateFunction
: Function to be called on timer trigger
-
void
setCallback
(TimerCallback callback, void *arg = nullptr) Set timer trigger callback.
- Parameters
callback
: Function to call when timer triggersarg
: Optional argument passed to callback
-
void
setCallback
(InterruptCallback callback) Set timer trigger callback.
- Note
- Provided for convenience where callback argument not required
- Parameters
callback
: Function to call when timer triggers
-
CallbackTimer &
initialize
(TimerCallback callback, void *arg = nullptr) Initialise timer with an interval (static check) and callback.
- Note
- If interval out of range compilation will fail with error
- Template Parameters
unit
: Time unit for intervaltime
: Timer interval
- Parameters
callback
: Callback function to call when timer triggersarg
: Optional argument passed to callback
- Return Value
CallbackTimer&
: Reference to timer
-
CallbackTimer &
initialize
(TimeType time, TimerCallback callback, void *arg = nullptr) Initialise timer with an interval and callback.
- Template Parameters
unit
: Time unit for interval
- Parameters
time
: Timer intervalcallback
: Callback function to call when timer triggersarg
: Optional argument passed to callback
- Return Value
CallbackTimer&
: Reference to timer
-
bool
start
(bool repeating = true) Start timer running.
- Parameters
repeating
: True to restart timer when it triggers, false for one-shot (Default: true)
- Return Value
bool
: True if timer started
-
bool
startOnce
() Start one-shot timer.
- Note
- Timer starts and will run for configured period then stop
- Return Value
bool
: True if timer started
-
void
stop
() Stops timer.
-
bool
restart
() Restart timer.
- Note
- Timer is stopped then started with current configuration
- Return Value
bool
: True if timer started
-
bool
isStarted
() const Check if timer is started.
- Return Value
bool
: True if started
-
TickType
getInterval
() const Get timer interval in clock ticks.
-
bool
checkInterval
(TickType ticks) const Check timer interval is valid.
- Parameters
ticks
: Interval to check
- Return Value
bool
: true if interval is within acceptable range for this timer
-
bool
setInterval
(TickType ticks) Set timer interval in timer ticks.
- Parameters
ticks
: Interval in timer ticks
-
void
setInterval
() Set timer interval in timer ticks (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
ticks
: Interval in ticks
-
void
setInterval
() Set timer interval in specific time unit (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
unit
:time
: Interval to set
-
bool
setInterval
(TimeType time) Set timer interval in timer ticks.
- Template Parameters
unit
:
- Parameters
time
: Interval in given units
-
bool
setIntervalUs
(TimeType microseconds) Set timer interval in microseconds.
-
void
setIntervalUs
() Set timer interval in microseconds (static check)
-
bool
setIntervalMs
(uint32_t milliseconds) Set timer interval in milliseconds.
-
void
setIntervalMs
() Set timer interval in milliseconds (static check)
Public Static Functions
-
static constexpr Millis
millis
() Get a millisecond time source.
-
static constexpr Micros
micros
() Get a microsecond time source.
-
static constexpr uint64_t
usToTicks
() Convert microsecond count into timer ticks.
-
static TickType
usToTicks
(TimeType time) Convert microsecond count into timer ticks.
-
static constexpr uint64_t
ticksToUs
() Convert timer ticks into microseconds.
-
static TimeType
ticksToUs
(TickType ticks) Convert timer ticks into microseconds.
-
static constexpr void
checkInterval
() Check timer interval in ticks is valid (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
ticks
: Timer interval to check
-
static constexpr void
checkInterval
() Check timer interval in specific time unit is valid (static check)
- Note
- On error, compilation fails with error message
- Template Parameters
unit
: Time unit for intervaltime
: Interval to check
-
static constexpr void
checkIntervalMs
() Check timer interval in milliseconds is valid (static check)
-
static constexpr void
checkIntervalUs
() Check timer interval in microseconds is valid (static check)
Protected Types
-
template<>
usingClock
= OsTimerApi::Clock¶
-
template<>
usingTickType
= uint64_t¶
-
template<>
usingTimeType
= uint64_t¶
Protected Functions
-
bool
isArmed
() const
-
TickType
ticks
() const
-
void
arm
(bool repeating)
-
void
disarm
()
-
String
name
() const
-
String
toString
() const
-
operator String
() const
Protected Attributes
-
TimerCallback
func
-
void *
arg
Protected Static Functions
-
static constexpr const char *
typeName
()
-
static constexpr TickType
minTicks
()
-
static constexpr TickType
maxTicks
()
-
template<>
-
typedef std::function<void()>