The function does not behave as you might think. Usually when you are required to provide a callback function, the callback is executed when an event occurs; which is what happens here. The callback function is executed after the timer expires.
The documentation for tmr.alarm()
says that the function is a combination of tmr.register()
and tmr.start()
. The tmr.register()
documentation says
Configures a timer and registers the callback function to call on expiry.
So, your answer is that "Something to do" will run, until it's finished, 2 seconds after the tmr.alarm()
function is called.
tmr.alarm()
(and tmr.register()
which it is based upon) can take a mode
parameter. I'll describe their behavior and how each is affected by the execution time of the callback function.
tmr.ALARM_SINGLE
: Run the callback function only once n seconds after the call to tmr.alarm()
finishes. Completely independent of the execution time of the callback function.
tmr.ALARM_AUTO
: Run the callback function repeated every n seconds after the call to tmr.alarm()
finishes. It is important to note that the next interval starts as soon as the previous finishes, regardless of the execution time of the callback. So if the callback take 0.5s to finish executing and the timer is 2s, then the next call will occur 1.5s after the callback function finishes.
tmr.ALARM_SEMI
: Run the callback function n seconds after the call to tmr.alarm()
finishes. Unlike tmr.ALARM_AUTO
the next interval is not automatically started, but will only start after you call tmr.start()
; which you should probably do within the callback function. This means you can set the timer to be dependent on the execution time of the callback function. If the timer is 2s and you restart the timer at the end of your callback function, then the next time the callback will run will be 2s from then.
As you might be able to tell, you don't want the execution time of the callback function to be greater than the timer period, callbacks will keep stacking on top of each other, never finishing. The callbacks should be simple and quick to execute, perhaps scheduling additional work as another task.