2016-03-05 10:47:01 +01:00
# Timer Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 22:01:57 +01:00
| 2014-12-12 | [Zeroday ](https://github.com/funshine ) | [dnc40085 ](https://github.com/dnc40085 ) | [tmr.c ](../../app/modules/tmr.c )|
2016-01-06 03:06:00 +01:00
The tmr module allows access to simple timers, the system counter and uptime.
It is aimed at setting up regularly occurring tasks, timing out operations, and provide low-resolution deltas.
2016-01-10 16:04:23 +01:00
What the tmr module is *not* however, is a time keeping module. While most timeouts are expressed in milliseconds or even microseconds, 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.
2016-01-06 03:06:00 +01:00
2016-08-02 22:35:53 +02:00
!!! attention
2019-01-22 22:59:41 +01:00
NodeMCU formerly provided 7 static timers, numbered 0-6, which could be
used instead of OO API timers initiated with [`tmr.create()` ](#tmrcreate ).
After a long period of deprecation, these were removed in 2019 Q1.
2016-01-07 00:33:52 +01:00
2016-08-02 22:35:53 +02:00
## tmr.create()
2019-01-22 22:59:41 +01:00
Creates a dynamic timer object; see below for its method table.
2016-08-02 22:35:53 +02:00
Dynamic timer can be used instead of numeric ID in control functions. Also can be controlled in object-oriented way.
Functions supported in timer object:
2019-01-22 22:59:41 +01:00
- [`t:alarm()` ](#tobjalarm )
- [`t:interval()` ](#tobjinterval )
- [`t:register()` ](#tobjregister )
- [`t:start()` ](#tobjstart )
- [`t:state()` ](#tobjstate )
- [`t:stop()` ](#tobjstop )
- [`t:unregister()` ](#tobjunregister )
2016-08-02 22:35:53 +02:00
#### Parameters
none
#### Returns
`timer` object
#### Example
```lua
local mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function (t) print("expired"); t:unregister() end)
mytimer:start()
```
2016-01-10 16:04:23 +01:00
## tmr.delay()
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
Busyloops the processor for a specified number of microseconds.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
This is in general a **bad** idea, because nothing else gets to run, and the networking stack (and other things) can fall over as a result. The only time `tmr.delay()` may be appropriate to use is if dealing with a peripheral device which needs a (very) brief delay between commands, or similar. *Use with caution!*
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
Also note that the actual amount of time delayed for may be noticeably greater, both as a result of timing inaccuracies as well as interrupts which may run during this time.
#### Syntax
`tmr.delay(us)`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
`us` microseconds to busyloop for
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
2016-01-06 03:06:00 +01:00
`nil`
2016-01-10 16:04:23 +01:00
#### Example
2016-01-06 03:06:00 +01:00
```lua
2016-01-10 16:04:23 +01:00
tmr.delay(100)
2016-01-06 03:06:00 +01:00
```
2016-01-07 00:33:52 +01:00
2019-01-22 22:59:41 +01:00
## tmr.now()
2016-01-06 03:06:00 +01:00
2019-01-22 22:59:41 +01:00
Returns the system counter, which counts in microseconds. Limited to 31 bits, after that it wraps around back to zero. That is essential if you use this function to [debounce or throttle GPIO input ](https://github.com/hackhitchin/esp8266-co-uk/issues/2 ).
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
2019-01-22 22:59:41 +01:00
`tmr.now()`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
2019-01-22 22:59:41 +01:00
none
#### Returns
the current value of the system counter
#### Example
```lua
print(tmr.now())
print(tmr.now())
```
## tmr.softwd()
Provides a simple software watchdog, which needs to be re-armed or disabled before it expires, or the system will be restarted.
#### Syntax
`tmr.softwd(timeout_s)`
#### Parameters
`timeout_s` watchdog timeout, in seconds. To disable the watchdog, use -1 (or any other negative value).
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
`nil`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-06 03:06:00 +01:00
```lua
2019-01-22 22:59:41 +01:00
function on_success_callback()
tmr.softwd(-1)
print("Complex task done, soft watchdog disabled!")
end
tmr.softwd(5)
-- go off and attempt to do whatever might need a restart to recover from
complex_stuff_which_might_never_call_the_callback(on_success_callback)
2016-01-06 03:06:00 +01:00
```
2016-01-07 00:33:52 +01:00
2019-01-22 22:59:41 +01:00
## tmr.time()
2016-01-06 03:06:00 +01:00
2019-01-22 22:59:41 +01:00
Returns the system uptime, in seconds. Limited to 31 bits, after that it wraps around back to zero.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
2019-01-22 22:59:41 +01:00
`tmr.time()`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
none
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
2019-01-22 22:59:41 +01:00
the system uptime, in seconds, possibly wrapped around
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-06 03:06:00 +01:00
```lua
2019-01-22 22:59:41 +01:00
print("Uptime (probably):", tmr.time())
2016-01-06 03:06:00 +01:00
```
2016-01-07 00:33:52 +01:00
2019-01-22 22:59:41 +01:00
## tmr.wdclr()
2016-01-06 03:06:00 +01:00
2019-01-22 22:59:41 +01:00
Feed the system watchdog.
*In general, if you ever need to use this function, you are doing it wrong.*
The event-driven model of NodeMCU means that there is no need to be sitting in hard loops waiting for things to occur. Rather, simply use the callbacks to get notified when somethings happens. With this approach, there should never be a need to manually feed the system watchdog.
#### Syntax
`tmr.wdclr()`
#### Parameters
none
#### Returns
`nil`
2020-01-04 21:48:08 +01:00
## tmr.ccount()
Get value of CPU CCOUNT register which contains CPU ticks. The register is 32-bit and rolls over.
Converting the register's CPU ticks to us is done by dividing it to 80 or 160 (CPU80/CPU160) i.e. `tmr.ccount() / node.getcpufreq()` .
Register arithmetic works without need to account for roll over, unlike `tmr.now()` . Because of same reason when CCOUNT is having its 32nd bit set, it appears in Lua as negative number.
#### Syntax
`tmr.ccount()`
#### Returns
The current value of CCOUNT register.
#### Example
```lua
function timeIt(fnc, cnt)
local function loopIt(f2)
local t0 = tmr.ccount()
for i=1,cnt do
f2()
end
local t1 = tmr.ccount()
return math.ceil((t1-t0)/cnt)
end
assert(type(fnc) == "function", "function to test missing")
cnt = cnt or 1000
local emptyTime = loopIt(function()end)
local deltaCPUTicks = math.abs(loopIt(fnc) - emptyTime)
local deltaUS = math.ceil(deltaCPUTicks/node.getcpufreq())
return deltaCPUTicks, deltaUS
end
print( timeIt(function() tmr.ccount() end) )
```
2019-01-22 22:59:41 +01:00
## Timer Object Methods
### tobj:alarm()
2016-01-06 03:06:00 +01:00
2020-09-05 14:16:57 +02:00
This is a convenience function combining [`tobj:register()` ](#tobjregister ) and [`tobj:start()` ](#tobjstart ) into a single call. This is the reason why this method has the same parameters as `tobj:register()` .
If `tobj:alarm()` is invoked on an already running timer the timer is stopped, new parameters are set and timer is (re)started (similar to call `tobj:start(true)` ).
2019-01-22 22:59:41 +01:00
To free up the resources with this timer when done using it, call [`tobj:unregister()` ](#tobjunregister ) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
2019-01-22 22:59:41 +01:00
`tobj:alarm(interval_ms, mode, func())`
2016-01-10 16:04:23 +01:00
#### Parameters
2016-05-07 11:30:04 +02:00
- `interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
2016-01-10 16:04:23 +01:00
- `mode` timer mode:
2019-01-22 22:59:41 +01:00
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`unregister()` ](#tobjunregister ))
- `tmr.ALARM_SEMI` manually repeating alarm (call [`start()` ](#tobjstart ) to restart)
2016-01-10 16:04:23 +01:00
- `tmr.ALARM_AUTO` automatically repeating alarm
2017-01-03 21:50:56 +01:00
- `func(timer)` callback function which is invoked with the timer object as an argument
2016-01-10 16:04:23 +01:00
2019-01-22 22:59:41 +01:00
#### 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
- [`tobj:create()` ](#tobjcreate )
- [`tobj:register()` ](#tobjregister )
- [`tobj:start()` ](#tobjstart )
- [`tobj:unregister()` ](#tobjunregister )
### tobj:interval()
Changes a registered timer's expiry interval.
#### Syntax
`tobj:interval(interval_ms)`
#### Parameters
- `interval_ms` new timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
2016-01-06 03:06:00 +01:00
`nil`
2016-01-10 16:04:23 +01:00
#### Example
2016-01-06 03:06:00 +01:00
```lua
2017-01-03 21:50:56 +01:00
mytimer = tmr.create()
2019-01-22 22:59:41 +01:00
mytimer:register(10000, tmr.ALARM_AUTO, function() print("hey there") end)
mytimer:interval(3000) -- actually, 3 seconds is better!
2017-01-03 21:50:56 +01:00
mytimer:start()
2016-01-06 03:06:00 +01:00
```
2019-01-22 22:59:41 +01:00
### tobj:register()
2016-01-06 03:06:00 +01:00
2019-01-22 22:59:41 +01:00
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 [`tobj:unregister()` ](#tobjunregister ) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
2019-01-22 22:59:41 +01:00
`tobj:register(interval_ms, mode, func())`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
2019-01-22 22:59:41 +01:00
- `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 [`tobj:unregister()` ](#tobjunregister ))
- `tmr.ALARM_SEMI` manually repeating alarm (call [`tobj:start()` ](#tobjunregister ) to restart)
- `tmr.ALARM_AUTO` automatically repeating alarm
- `func(timer)` callback function which is invoked with the timer object as an argument
Note that registering does *not* start the alarm.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
`nil`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-06 03:06:00 +01:00
```lua
2019-01-22 22:59:41 +01:00
mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end)
mytimer:start()
2016-01-06 03:06:00 +01:00
```
2019-01-22 22:59:41 +01:00
#### See also
- [`tobj:create()` ](#tobjcreate )
- [`tobj:alarm()` ](#tobjalarm )
2016-01-06 03:06:00 +01:00
2019-01-22 22:59:41 +01:00
### tobj:start()
2016-01-10 16:04:23 +01:00
2020-08-29 21:43:50 +02:00
Starts or restarts a previously configured timer. If the timer is running the timer is restarted only when `restart` parameter is `true` . Otherwise `false` is returned signaling error.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
2020-08-29 21:43:50 +02:00
`tobj:start([restart])`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
2020-08-29 21:43:50 +02:00
- `restart` optional boolean parameter forcing to restart already running timer
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
2020-09-05 14:16:57 +02:00
`true` if the timer was (re)started, `false` on error
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-06 03:06:00 +01:00
```lua
2017-01-03 21:50:56 +01:00
mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end)
if not mytimer:start() then print("uh oh") end
2016-01-06 03:06:00 +01:00
```
2016-01-10 16:04:23 +01:00
#### See also
2019-01-22 22:59:41 +01:00
- [`tobj:create()` ](#tobjcreate )
- [`tobj:register()` ](#tobjregister )
- [`tobj:stop()` ](#tobjstop )
- [`tobj:unregister()` ](#tobjunregister )
2016-01-07 00:33:52 +01:00
2019-01-22 22:59:41 +01:00
### tobj:state()
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
Checks the state of a timer.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
2019-01-22 22:59:41 +01:00
`tobj:state()`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
2019-01-22 22:59:41 +01:00
None
2016-01-10 16:04:23 +01:00
#### Returns
(bool, int) or `nil`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
If the specified timer is registered, returns whether it is currently started and its mode. If the timer is not registered, `nil` is returned.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-06 03:06:00 +01:00
```lua
2017-01-03 21:50:56 +01:00
mytimer = tmr.create()
print(mytimer:state()) -- nil
mytimer:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end)
running, mode = mytimer:state()
print("running: " .. tostring(running) .. ", mode: " .. mode) -- running: false, mode: 0
2016-01-06 03:06:00 +01:00
```
2019-01-22 22:59:41 +01:00
### tobj:stop()
2016-01-06 03:06:00 +01:00
2019-01-22 22:59:41 +01:00
Stops a running timer, but does *not* unregister it. A stopped timer can be restarted with [`tobj:start()` ](#tobjstart ).
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
2019-01-22 22:59:41 +01:00
`tobj:stop()`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
2019-01-22 22:59:41 +01:00
None
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
`true` if the timer was stopped, `false` on error
#### Example
2016-01-06 03:06:00 +01:00
```lua
2017-01-03 21:50:56 +01:00
mytimer = tmr.create()
if not mytimer:stop() then print("timer not stopped, not registered?") end
2016-01-06 03:06:00 +01:00
```
2016-01-10 16:04:23 +01:00
#### See also
2019-01-22 22:59:41 +01:00
- [`tobj:register()` ](#tobjregister )
- [`tobj:stop()` ](#tobjstop )
- [`tobj:unregister()` ](#tobjunregister )
2016-01-06 03:06:00 +01:00
2019-01-22 22:59:41 +01:00
### tobj:unregister()
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
Stops the timer (if running) and unregisters the associated callback.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
This isn't necessary for one-shot timers (`tmr.ALARM_SINGLE`), as those automatically unregister themselves when fired.
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
2019-01-22 22:59:41 +01:00
`tobj:unregister()`
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
2019-01-22 22:59:41 +01:00
None
2016-01-06 03:06:00 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
2016-01-06 03:06:00 +01:00
`nil`
2016-01-10 16:04:23 +01:00
#### See also
2019-01-22 22:59:41 +01:00
[`tobj:register()` ](#tobjregister )
2016-01-06 03:06:00 +01:00