Timer¶
- class
A queued Timer class, recurring or single shot.
A timer can call a function at regular intervals or after a defined delay. You can use the timer to do periodic tasks, like house-keeping functions or display updates.
Queued callback
The timer uses the Application Run Loop to schedule the callback handler function. This means your callback are not executed inside a hardware interrupt context. This is very convenient since your can do any kind of heavy lifting in your callback handler, and your code is not pre-empted.
Presicion
You should note that the timer are not guaranteed to be precisely accurate, it might fire later than your defined interval (or delay). The timer will not fire before your defined interval though. If you use any blocking
wait
statements in your code, you might contribute to loss in precision for timers.If you want precise hardware timer interrupts consider the mbed Ticker class, but you should be aware of the hazards when using hardware interrupts.
Example
Create a reocurring timer that fires each second:
Timer timr(1000); timr.setCallback<MyClass>(this, &MyClass::callback); timr.start();
The member function
callback
will now be called every second. If you want to use a single shot callback with a delay, Timer has a convenience static function:Timer delay = Timer::callOnce<MyClass>(100, this, &MyClass::callback);
Now
delay
is a running timer that callscallback
only one time. Note that the timer object (delay
) should not be deallocated. Deallocating the object will cause the timer to shut down.Time slices
Say you set an interval of 1000 ms, and your callback takes 300 ms to execute. Then timer will delay for 700 ms and not 1000 ms. It is up to you to ensure your callback do not take longer to execute, than the timer interval.
Public Functions
-
Timer::
Timer
()¶ Contruct an empty (zero-timeout) re-occurring timer.
After calling this contructor, you must set the time out and callback function. Then start the timer.
-
Timer::
Timer
(uint32_t intervalOrTimeoutMs, bool snglShot)¶ Create a new timer with with an interval or timeout time.
All newly created timers are stopped as default. You must also attach callback handler to the timer, before it can start.
- Parameters
intervalOrTimeoutMs
-The timers time interval before it fires, in milliseconds
snglShot
-Set this to
true
if the timer should only fire once. Defaultfalse
-
void
mono::Timer::
start
()¶ Start the timer and put into running state.
Note: You must set a callback handler, before starting the timer.
-
void
Timer::
Start
()¶
-
void
mono::Timer::
stop
()¶ Stop the timer, any pending callback will not be executed.
-
void
Timer::
Stop
()¶
-
bool
Timer::
SingleShot
()¶
const See if the timer is single shot.
-
bool
Timer::
Running
()¶
const See if the timer is currently running
-
void
Timer::
setInterval
(uint32_t newIntervalMs)¶ Set a new timer interval.
- Parameters
newIntervalMs
-The timer interval in milliseconds
- template <typename Owner>
-
void mono::Timer::setCallback(Owner * obj, void(Owner::*)(void) memPtr)
Sets a C++ callback member function to the timer.
- Parameters
obj
-A pointer to the callback member function context (the
this
pointer)memPtr
-A pointer to the member function, that is the callback
-
void
mono::Timer::
setCallback
(void (*cFunction)(void))¶ Sets a callback handler C function to the timer.
- Parameters
cFunction
-A pointer to the C function, that is the callback
Public Static Functions
- template <typename Owner>
-
static Timer* mono::Timer::callOnce(uint32_t delayMs, Owner * obj, void(Owner::*)(void) memPtr)
Create a single shot timer with a delay and callback function.
The timer object is created on the HEAP, which allows it to exists across stack frame contexts. You can safely create a
callOnce(...)
timer, and return from a function. Even if you do not have a reference to the timer object, it will still run and fire. The timer deallocates itself after it has fired. It cannot be reused.- Return
- A pointer to the single shot timer
- Parameters
delayMs
-Delay time before the timer fires, in milliseconds.
obj
-A pointer to the callbacks function member context (the
this
pointer)memPtr
-A pointer to the callback member function
-
static Timer *
mono::Timer::
callOnce
(uint32_t delayMs, void (*memPtr)(void))¶ Create a single shot timer with a delay and callback function.
The timer object is created on the HEAP, which allows it to exists across stack frame contexts. You can safely create a
callOnce(...)
timer, and return from a function. Even if you do not have a reference to the timer object, it will still run and fire. The timer deallocates itself after it has fired. It cannot be reused.- Return
- A pointer to the single shot timer
- Parameters
delayMs
-Delay time before the timer fires, in milliseconds.
memPtr
-A pointer to the callback C function
Protected Functions
-
virtual void
Timer::
taskHandler
()¶ This is the method that gets called by the run loop.
NOTE that this is not an interrupt function, you can do stuff that take some time.
-