Skip to content

Commit 944c657

Browse files
committed
Expunge integer timers
1 parent 87b3ffa commit 944c657

File tree

2 files changed

+28
-53
lines changed

2 files changed

+28
-53
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: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,17 @@ 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-
14-
!!! attention
15-
16-
Static timers are deprecated and will be removed later. Use the OO API initiated with [`tmr.create()`](#tmrcreate).
17-
1812
## tmr.alarm()
1913

2014
This is a convenience function combining [`tmr.register()`](#tmrregister) and [`tmr.start()`](#tmrstart) into a single call.
2115

2216
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.
2317

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

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

115109
#### Syntax
116-
`tmr.interval([id/ref], interval_ms)`
110+
`tmr.interval(ref, interval_ms)`
117111

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

122116
#### Returns
@@ -156,10 +150,10 @@ Configures a timer and registers the callback function to call on expiry.
156150
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.
157151

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

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

214208
#### Syntax
215-
`tmr.start([id/ref])`
209+
`tmr.start(ref)`
216210

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

220214
#### Returns
221215
`true` if the timer was started, `false` on error
@@ -237,10 +231,10 @@ if not mytimer:start() then print("uh oh") end
237231
Checks the state of a timer.
238232

239233
#### Syntax
240-
`tmr.state([id/ref])`
234+
`tmr.state(ref)`
241235

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

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

263257
#### Syntax
264-
`tmr.stop([id/ref])`
258+
`tmr.stop(ref)`
265259

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

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

306300
#### Syntax
307-
`tmr.unregister([id/ref])`
301+
`tmr.unregister(ref)`
308302

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

312306
#### Returns
313307
`nil`

0 commit comments

Comments
 (0)