The tmr module allows access to simple timers. It is aimed at setting up regularly occurring tasks and timing out operations.
What the tmr module is *not* however, is a time keeping module. While all timeouts are expressed in milliseconds, the accuracy is limited and compounding errors would lead to rather inaccurate time keeping. Consider using the [rtctime](rtctime.md) module for "wall clock" time.
The resolution of the timers is determined by FreeRTOS' tick rate. The default rate of 100 kHz (resulting in 10 ms resolution) can be changed with `make menuconfig` at item `Component config ---> FreeRTOS ---> Tick rate (Hz)`.
mytimer:register(5000, tmr.ALARM_SINGLE, function (t) print("expired") end)
mytimer:start()
mytimer = nil
```
# tmr Object
## tmr.obj:alarm()
This is a convenience function combining [`tmr.obj:register()`](#tmrobjregister) and [`tmr.obj:start()`](#tmrobjstart) into a single call.
To free up the resources with this timer when done using it, call [`tmr.obj:unregister()`](#tmrobjunregister) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
#### Syntax
`mytmr:alarm(interval_ms, mode, func())`
#### Parameters
-`interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
-`mode` timer mode:
-`tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
-`tmr.ALARM_SEMI` manually repeating alarm (call [`tmr.start()`](#tmrstart) to restart)
-`tmr.ALARM_AUTO` automatically repeating alarm
-`func(timer)` callback function which is invoked with the timer object as an argument
#### Returns
`true` if the timer was started, `false` on error
#### Example
```lua
if not tmr.create():alarm(5000, tmr.ALARM_SINGLE, function()
print("hey there")
end)
then
print("whoopsie")
end
```
#### See also
- [`tmr.create()`](#tmrcreate)
- [`tmr.obj:register()`](#tmrobjregister)
- [`tmr.obj:start()`](#tmrobjstart)
- [`tmr.obj:unregister()`](#tmrobjunregister)
## tmr.obj:interval()
Changes a registered timer's expiry interval.
#### Syntax
`mytmr:interval(interval_ms)`
#### Parameters
-`interval_ms` new timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
mytimer:interval(3000) -- actually, 3 seconds is better!
mytimer:start()
```
## tmr.obj:register()
Configures a timer and registers the callback function to call on expiry.
To free up the resources with this timer when done using it, call [`tmr.obj:unregister()`](#tmrobjunregister) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
#### Syntax
`mytmr:register(interval_ms, mode, func())`
#### Parameters
-`interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
-`mode` timer mode:
-`tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
-`tmr.ALARM_SEMI` manually repeating alarm (call [`tmr.start()`](#tmrunregister) to restart)
-`tmr.ALARM_AUTO` automatically repeating alarm
-`func(timer)` callback function which is invoked with the timer object as an argument