Skip to content

Commit c6444ec

Browse files
committed
Expunge integer timers
1 parent 87b3ffa commit c6444ec

File tree

2 files changed

+159
-197
lines changed

2 files changed

+159
-197
lines changed

app/modules/tmr.c

Lines changed: 18 additions & 46 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

@@ -389,8 +378,8 @@ static const LUA_REG_TYPE tmr_dyn_map[] = {
389378
{ LSTRKEY( "state" ), LFUNCVAL( tmr_state ) },
390379
{ LSTRKEY( "interval" ), LFUNCVAL( tmr_interval) },
391380
#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 ) },
394383
#endif
395384
{ LSTRKEY( "__gc" ), LFUNCVAL( tmr_unregister ) },
396385
{ LSTRKEY( "__index" ), LROVAL( tmr_dyn_map ) },
@@ -403,19 +392,10 @@ static const LUA_REG_TYPE tmr_map[] = {
403392
{ LSTRKEY( "wdclr" ), LFUNCVAL( tmr_wdclr ) },
404393
{ LSTRKEY( "softwd" ), LFUNCVAL( tmr_softwd ) },
405394
{ 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 ) },
410395
#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 ) },
415398
#endif
416-
{ LSTRKEY( "unregister" ), LFUNCVAL( tmr_unregister ) },
417-
{ LSTRKEY( "state" ), LFUNCVAL( tmr_state ) },
418-
{ LSTRKEY( "interval" ), LFUNCVAL( tmr_interval ) },
419399
{ LSTRKEY( "create" ), LFUNCVAL( tmr_create ) },
420400
{ LSTRKEY( "ALARM_SINGLE" ), LNUMVAL( TIMER_MODE_SINGLE ) },
421401
{ LSTRKEY( "ALARM_SEMI" ), LNUMVAL( TIMER_MODE_SEMI ) },
@@ -425,16 +405,8 @@ static const LUA_REG_TYPE tmr_map[] = {
425405

426406
#include "pm/swtimer.h"
427407
int luaopen_tmr( lua_State *L ){
428-
int i;
429-
430408
luaL_rometatable(L, "tmr.timer", (void *)tmr_dyn_map);
431409

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-
}
438410
last_rtc_time=system_get_rtc_time(); // Right now is time 0
439411
last_rtc_time_us=0;
440412

0 commit comments

Comments
 (0)