@@ -16,28 +16,28 @@ tmr.delay() -- not changed
16
16
tmr.alarm() -- not changed
17
17
tmr.stop() -- changed, see below. use tmr.unregister for old functionality
18
18
19
- tmr.register(id , interval, mode, function)
19
+ tmr.register(ref , interval, mode, function)
20
20
bind function with timer and set the interval in ms
21
21
the mode can be:
22
22
tmr.ALARM_SINGLE for a single run alarm
23
23
tmr.ALARM_SEMI for a multiple single run alarm
24
24
tmr.ALARM_AUTO for a repating alarm
25
25
tmr.register does NOT start the timer
26
26
tmr.alarm is a tmr.register & tmr.start macro
27
- tmr.unregister(id )
27
+ tmr.unregister(ref )
28
28
stop alarm, unbind function and clean up memory
29
29
not needed for ALARM_SINGLE, as it unregisters itself
30
- tmr.start(id )
30
+ tmr.start(ref )
31
31
ret: bool
32
32
start a alarm, returns true on success
33
- tmr.stop(id )
33
+ tmr.stop(ref )
34
34
ret: bool
35
35
stops a alarm, returns true on success
36
36
this call dose not free any memory, to do so use tmr.unregister
37
37
stopped alarms can be started with start
38
- tmr.interval(id , interval)
38
+ tmr.interval(ref , interval)
39
39
set alarm interval, running alarm will be restarted
40
- tmr.state(id )
40
+ tmr.state(ref )
41
41
ret: (bool, int) or nil
42
42
returns alarm status (true=started/false=stopped) and mode
43
43
nil if timer is unregistered
@@ -73,7 +73,8 @@ static const char* MAX_TIMEOUT_ERR_STR = "Range: 1-"STRINGIFY(MAX_TIMEOUT_DEF);
73
73
74
74
typedef struct {
75
75
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 */
77
78
uint32_t interval ;
78
79
uint8_t mode ;
79
80
}timer_struct_t ;
@@ -93,7 +94,6 @@ static uint32_t last_rtc_time=0;
93
94
static uint64_t last_rtc_time_us = 0 ;
94
95
95
96
static sint32_t soft_watchdog = -1 ;
96
- static timer_struct_t alarm_timers [NUM_TMR ];
97
97
static os_timer_t rtc_timer ;
98
98
99
99
static void alarm_timer_common (void * arg ){
@@ -102,12 +102,7 @@ static void alarm_timer_common(void* arg){
102
102
if (tmr -> lua_ref == LUA_NOREF )
103
103
return ;
104
104
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 );
111
106
//if the timer was set to single run we clean up after it
112
107
if (tmr -> mode == TIMER_MODE_SINGLE ){
113
108
luaL_unref (L , LUA_REGISTRYINDEX , tmr -> lua_ref );
@@ -148,19 +143,13 @@ static int tmr_now(lua_State* L){
148
143
}
149
144
150
145
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 ;
161
150
}
162
151
163
- // Lua: tmr.register( id / ref, interval, mode, function )
152
+ // Lua: tmr.register( ref, interval, mode, function )
164
153
static int tmr_register (lua_State * L ){
165
154
timer_t tmr = tmr_get (L , 1 );
166
155
@@ -389,8 +378,8 @@ static const LUA_REG_TYPE tmr_dyn_map[] = {
389
378
{ LSTRKEY ( "state" ), LFUNCVAL ( tmr_state ) },
390
379
{ LSTRKEY ( "interval" ), LFUNCVAL ( tmr_interval ) },
391
380
#ifdef TIMER_SUSPEND_ENABLE
392
- { LSTRKEY ( "suspend" ), LFUNCVAL ( tmr_suspend ) },
393
- { LSTRKEY ( "resume" ), LFUNCVAL ( tmr_resume ) },
381
+ { LSTRKEY ( "suspend" ), LFUNCVAL ( tmr_suspend ) },
382
+ { LSTRKEY ( "resume" ), LFUNCVAL ( tmr_resume ) },
394
383
#endif
395
384
{ LSTRKEY ( "__gc" ), LFUNCVAL ( tmr_unregister ) },
396
385
{ LSTRKEY ( "__index" ), LROVAL ( tmr_dyn_map ) },
@@ -403,19 +392,10 @@ static const LUA_REG_TYPE tmr_map[] = {
403
392
{ LSTRKEY ( "wdclr" ), LFUNCVAL ( tmr_wdclr ) },
404
393
{ LSTRKEY ( "softwd" ), LFUNCVAL ( tmr_softwd ) },
405
394
{ LSTRKEY ( "time" ), LFUNCVAL ( tmr_time ) },
406
- { LSTRKEY ( "register" ), LFUNCVAL ( tmr_register ) },
407
- { LSTRKEY ( "alarm" ), LFUNCVAL ( tmr_alarm ) },
408
- { LSTRKEY ( "start" ), LFUNCVAL ( tmr_start ) },
409
- { LSTRKEY ( "stop" ), LFUNCVAL ( tmr_stop ) },
410
395
#ifdef TIMER_SUSPEND_ENABLE
411
- { LSTRKEY ( "suspend" ), LFUNCVAL ( tmr_suspend ) },
412
- { LSTRKEY ( "suspend_all" ), LFUNCVAL ( tmr_suspend_all ) },
413
- { LSTRKEY ( "resume" ), LFUNCVAL ( tmr_resume ) },
414
- { LSTRKEY ( "resume_all" ), LFUNCVAL ( tmr_resume_all ) },
396
+ { LSTRKEY ( "suspend_all" ), LFUNCVAL ( tmr_suspend_all ) },
397
+ { LSTRKEY ( "resume_all" ), LFUNCVAL ( tmr_resume_all ) },
415
398
#endif
416
- { LSTRKEY ( "unregister" ), LFUNCVAL ( tmr_unregister ) },
417
- { LSTRKEY ( "state" ), LFUNCVAL ( tmr_state ) },
418
- { LSTRKEY ( "interval" ), LFUNCVAL ( tmr_interval ) },
419
399
{ LSTRKEY ( "create" ), LFUNCVAL ( tmr_create ) },
420
400
{ LSTRKEY ( "ALARM_SINGLE" ), LNUMVAL ( TIMER_MODE_SINGLE ) },
421
401
{ LSTRKEY ( "ALARM_SEMI" ), LNUMVAL ( TIMER_MODE_SEMI ) },
@@ -425,16 +405,8 @@ static const LUA_REG_TYPE tmr_map[] = {
425
405
426
406
#include "pm/swtimer.h"
427
407
int luaopen_tmr ( lua_State * L ){
428
- int i ;
429
-
430
408
luaL_rometatable (L , "tmr.timer" , (void * )tmr_dyn_map );
431
409
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
- }
438
410
last_rtc_time = system_get_rtc_time (); // Right now is time 0
439
411
last_rtc_time_us = 0 ;
440
412
0 commit comments