Skip to content

Commit 5b22e1f

Browse files
committed
Expunge integer timers
1 parent 87b3ffa commit 5b22e1f

File tree

2 files changed

+31
-50
lines changed

2 files changed

+31
-50
lines changed

app/modules/tmr.c

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ tmr.delay() -- not changed
1616
tmr.alarm() -- not changed
1717
tmr.stop() -- changed, see below. use tmr.unregister for old functionality
1818
19-
tmr.register(id, interval, mode, function)
19+
tmr.register(ref, interval, mode, function)
2020
bind function with timer and set the interval in ms
2121
the mode can be:
2222
tmr.ALARM_SINGLE for a single run alarm
2323
tmr.ALARM_SEMI for a multiple single run alarm
2424
tmr.ALARM_AUTO for a repating alarm
2525
tmr.register does NOT start the timer
2626
tmr.alarm is a tmr.register & tmr.start macro
27-
tmr.unregister(id)
27+
tmr.unregister(ref)
2828
stop alarm, unbind function and clean up memory
2929
not needed for ALARM_SINGLE, as it unregisters itself
30-
tmr.start(id)
30+
tmr.start(ref)
3131
ret: bool
3232
start a alarm, returns true on success
33-
tmr.stop(id)
33+
tmr.stop(ref)
3434
ret: bool
3535
stops a alarm, returns true on success
3636
this call dose not free any memory, to do so use tmr.unregister
3737
stopped alarms can be started with start
38-
tmr.interval(id, interval)
38+
tmr.interval(ref, interval)
3939
set alarm interval, running alarm will be restarted
40-
tmr.state(id)
40+
tmr.state(ref)
4141
ret: (bool, int) or nil
4242
returns alarm status (true=started/false=stopped) and mode
4343
nil if timer is unregistered
@@ -73,7 +73,8 @@ static const char* MAX_TIMEOUT_ERR_STR = "Range: 1-"STRINGIFY(MAX_TIMEOUT_DEF);
7373

7474
typedef struct{
7575
os_timer_t os;
76-
sint32_t lua_ref, self_ref;
76+
sint32_t lua_ref; /* Reference to the callback function */
77+
sint32_t self_ref; /* Reference to this structure as userdata */
7778
uint32_t interval;
7879
uint8_t mode;
7980
}timer_struct_t;
@@ -93,7 +94,6 @@ static uint32_t last_rtc_time=0;
9394
static uint64_t last_rtc_time_us=0;
9495

9596
static sint32_t soft_watchdog = -1;
96-
static timer_struct_t alarm_timers[NUM_TMR];
9797
static os_timer_t rtc_timer;
9898

9999
static void alarm_timer_common(void* arg){
@@ -102,12 +102,7 @@ static void alarm_timer_common(void* arg){
102102
if(tmr->lua_ref == LUA_NOREF)
103103
return;
104104
lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->lua_ref);
105-
if (tmr->self_ref == LUA_REFNIL) {
106-
uint32_t id = tmr - alarm_timers;
107-
lua_pushinteger(L, id);
108-
} else {
109-
lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->self_ref);
110-
}
105+
lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->self_ref);
111106
//if the timer was set to single run we clean up after it
112107
if(tmr->mode == TIMER_MODE_SINGLE){
113108
luaL_unref(L, LUA_REGISTRYINDEX, tmr->lua_ref);
@@ -148,19 +143,13 @@ static int tmr_now(lua_State* L){
148143
}
149144

150145
static timer_t tmr_get( lua_State *L, int stack ) {
151-
// Deprecated: static 0-6 timers control by index.
152-
luaL_argcheck(L, (lua_isuserdata(L, stack) || lua_isnumber(L, stack)), 1, "timer object or numerical ID expected");
153-
if (lua_isuserdata(L, stack)) {
154-
return (timer_t)luaL_checkudata(L, stack, "tmr.timer");
155-
} else {
156-
uint32_t id = luaL_checkinteger(L, 1);
157-
luaL_argcheck(L, platform_tmr_exists(id), 1, "invalid timer index");
158-
return &alarm_timers[id];
159-
}
160-
return 0;
146+
timer_t t = (timer_t)luaL_checkudata(L, stack, "tmr.timer");
147+
if (t == NULL)
148+
return (timer_t)luaL_error(L, "timer object expected");
149+
return t;
161150
}
162151

163-
// Lua: tmr.register( id / ref, interval, mode, function )
152+
// Lua: tmr.register( ref, interval, mode, function )
164153
static int tmr_register(lua_State* L){
165154
timer_t tmr = tmr_get(L, 1);
166155

@@ -425,16 +414,8 @@ static const LUA_REG_TYPE tmr_map[] = {
425414

426415
#include "pm/swtimer.h"
427416
int luaopen_tmr( lua_State *L ){
428-
int i;
429-
430417
luaL_rometatable(L, "tmr.timer", (void *)tmr_dyn_map);
431418

432-
for(i=0; i<NUM_TMR; i++){
433-
alarm_timers[i].lua_ref = LUA_NOREF;
434-
alarm_timers[i].self_ref = LUA_REFNIL;
435-
alarm_timers[i].mode = TIMER_MODE_OFF;
436-
os_timer_disarm(&alarm_timers[i].os);
437-
}
438419
last_rtc_time=system_get_rtc_time(); // Right now is time 0
439420
last_rtc_time_us=0;
440421

docs/en/modules/tmr.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ It is aimed at setting up regularly occurring tasks, timing out operations, and
99

1010
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.
1111

12-
NodeMCU provides 7 static timers, numbered 0-6, and dynamic timer creation function [`tmr.create()`](#tmrcreate).
13-
1412
!!! attention
1513

16-
Static timers are deprecated and will be removed later. Use the OO API initiated with [`tmr.create()`](#tmrcreate).
14+
NodeMCU formerly provided 7 static timers, numbered 0-6, which could be
15+
used instead of OO API timers initiated with [`tmr.create()`](#tmrcreate).
16+
After a long period of deprecation, these were removed in 2019 Q1.
1717

1818
## tmr.alarm()
1919

@@ -22,10 +22,10 @@ This is a convenience function combining [`tmr.register()`](#tmrregister) and [`
2222
To free up the resources with this timer when done using it, call [`tmr.unregister()`](#tmrunregister) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
2323

2424
#### Syntax
25-
`tmr.alarm([id/ref], interval_ms, mode, func())`
25+
`tmr.alarm(ref, interval_ms, mode, func())`
2626

2727
#### Parameters
28-
- `id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
28+
- `ref` timer object
2929
- `interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
3030
- `mode` timer mode:
3131
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
@@ -113,10 +113,10 @@ tmr.delay(100)
113113
Changes a registered timer's expiry interval.
114114

115115
#### Syntax
116-
`tmr.interval([id/ref], interval_ms)`
116+
`tmr.interval(ref, interval_ms)`
117117

118118
#### Parameters
119-
- `id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
119+
- `ref` timer object
120120
- `interval_ms` new timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
121121

122122
#### Returns
@@ -156,10 +156,10 @@ Configures a timer and registers the callback function to call on expiry.
156156
To free up the resources with this timer when done using it, call [`tmr.unregister()`](#tmrunregister) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
157157

158158
#### Syntax
159-
`tmr.register([id/ref], interval_ms, mode, func())`
159+
`tmr.register(ref, interval_ms, mode, func())`
160160

161161
#### Parameters
162-
- `id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
162+
- `ref` timer object
163163
- `interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
164164
- `mode` timer mode:
165165
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
@@ -212,10 +212,10 @@ complex_stuff_which_might_never_call_the_callback(on_success_callback)
212212
Starts or restarts a previously configured timer.
213213

214214
#### Syntax
215-
`tmr.start([id/ref])`
215+
`tmr.start(ref)`
216216

217217
#### Parameters
218-
`id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
218+
`ref` timer object
219219

220220
#### Returns
221221
`true` if the timer was started, `false` on error
@@ -237,10 +237,10 @@ if not mytimer:start() then print("uh oh") end
237237
Checks the state of a timer.
238238

239239
#### Syntax
240-
`tmr.state([id/ref])`
240+
`tmr.state(ref)`
241241

242242
#### Parameters
243-
`id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
243+
`ref` timer object
244244

245245
#### Returns
246246
(bool, int) or `nil`
@@ -261,10 +261,10 @@ print("running: " .. tostring(running) .. ", mode: " .. mode) -- running: false,
261261
Stops a running timer, but does *not* unregister it. A stopped timer can be restarted with [`tmr.start()`](#tmrstart).
262262

263263
#### Syntax
264-
`tmr.stop([id/ref])`
264+
`tmr.stop(ref)`
265265

266266
#### Parameters
267-
`id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
267+
`ref` timer object
268268

269269
#### Returns
270270
`true` if the timer was stopped, `false` on error
@@ -304,10 +304,10 @@ Stops the timer (if running) and unregisters the associated callback.
304304
This isn't necessary for one-shot timers (`tmr.ALARM_SINGLE`), as those automatically unregister themselves when fired.
305305

306306
#### Syntax
307-
`tmr.unregister([id/ref])`
307+
`tmr.unregister(ref)`
308308

309309
#### Parameters
310-
`id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
310+
`ref` timer object
311311

312312
#### Returns
313313
`nil`

0 commit comments

Comments
 (0)