Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2014-12-12 | Zeroday | dnc40085 | tmr.c |
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.
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 module for «wall clock» time.
NodeMCU provides 7 static timers, numbered 0-6, and dynamic timer creation function tmr.create()
.
!!! attention
Static timers are deprecated and will be removed later. Use the OO API initiated with [`tmr.create()`](#tmrcreate).
This is a convenience function combining tmr.register()
and tmr.start()
into a single call.
To free up the resources with this timer when done using it, call tmr.unregister()
on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
tmr.alarm([id/ref], interval_ms, mode, func())
id
/ref
timer id (0-6) or object, obsolete for OO API (→ tmr.create()
)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()
)tmr.ALARM_SEMI
manually repeating alarm (call tmr.start()
to restart)tmr.ALARM_AUTO
automatically repeating alarmfunc(timer)
callback function which is invoked with the timer object as an argumenttrue
if the timer was started, false
on error
if not tmr.create():alarm(5000, tmr.ALARM_SINGLE, function()
print("hey there")
end)
then
print("whoopsie")
end
Creates a dynamic timer object.
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:
none
timer
object
local mytimer = tmr.create()
-- oo calling
mytimer:register(5000, tmr.ALARM_SINGLE, function (t) print("expired"); t:unregister() end)
mytimer:start()
-- with self parameter
tmr.register(mytimer, 5000, tmr.ALARM_SINGLE, function (t) print("expired"); tmr.unregister(t) end)
tmr.start(mytimer)
Busyloops the processor for a specified number of microseconds.
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!
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.
tmr.delay(us)
us
microseconds to busyloop for
nil
tmr.delay(100)
Changes a registered timer’s expiry interval.
tmr.interval([id/ref], interval_ms)
id
/ref
timer id (0-6) or object, obsolete for OO API (→ tmr.create()
)interval_ms
new timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).nil
mytimer = tmr.create()
mytimer:register(10000, tmr.ALARM_AUTO, function() print("hey there") end)
mytimer:interval(3000) -- actually, 3 seconds is better!
mytimer:start()
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.
tmr.now()
none
the current value of the system counter
print(tmr.now())
print(tmr.now())
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.unregister()
on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
tmr.register([id/ref], interval_ms, mode, func())
id
/ref
timer id (0-6) or object, obsolete for OO API (→ tmr.create()
)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()
)tmr.ALARM_SEMI
manually repeating alarm (call tmr.start()
to restart)tmr.ALARM_AUTO
automatically repeating alarmfunc(timer)
callback function which is invoked with the timer object as an argumentNote that registering does not start the alarm.
nil
mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end)
mytimer:start()
Provides a simple software watchdog, which needs to be re-armed or disabled before it expires, or the system will be restarted.
tmr.softwd(timeout_s)
timeout_s
watchdog timeout, in seconds. To disable the watchdog, use -1 (or any other negative value).
nil
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)
Starts or restarts a previously configured timer.
tmr.start([id/ref])
id
/ref
timer id (0-6) or object, obsolete for OO API (→ tmr.create()
)
true
if the timer was started, false
on error
mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end)
if not mytimer:start() then print("uh oh") end
Checks the state of a timer.
tmr.state([id/ref])
id
/ref
timer id (0-6) or object, obsolete for OO API (→ tmr.create()
)
(bool, int) or nil
If the specified timer is registered, returns whether it is currently started and its mode. If the timer is not registered, nil
is returned.
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
Stops a running timer, but does not unregister it. A stopped timer can be restarted with tmr.start()
.
tmr.stop([id/ref])
id
/ref
timer id (0-6) or object, obsolete for OO API (→ tmr.create()
)
true
if the timer was stopped, false
on error
mytimer = tmr.create()
if not mytimer:stop() then print("timer not stopped, not registered?") end
Returns the system uptime, in seconds. Limited to 31 bits, after that it wraps around back to zero.
tmr.time()
none
the system uptime, in seconds, possibly wrapped around
print("Uptime (probably):", tmr.time())
Stops the timer (if running) and unregisters the associated callback.
This isn’t necessary for one-shot timers (tmr.ALARM_SINGLE
), as those automatically unregister themselves when fired.
tmr.unregister([id/ref])
id
/ref
timer id (0-6) or object, obsolete for OO API (→ tmr.create()
)
nil
tmr.unregister(0)
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.
tmr.wdclr()
none
nil