From 0e89fb24bae3c3dcd446b64f9bbc6d9581c08632 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Tue, 22 Jan 2019 21:59:41 +0000 Subject: [PATCH] Expunge integer timers (#2603) --- app/modules/tmr.c | 64 ++----- docs/modules/tmr.md | 394 +++++++++++++++++++++----------------------- 2 files changed, 210 insertions(+), 248 deletions(-) diff --git a/app/modules/tmr.c b/app/modules/tmr.c index 2d479a7c..b89e2c85 100644 --- a/app/modules/tmr.c +++ b/app/modules/tmr.c @@ -16,7 +16,7 @@ tmr.delay() -- not changed tmr.alarm() -- not changed tmr.stop() -- changed, see below. use tmr.unregister for old functionality -tmr.register(id, interval, mode, function) +tmr.register(ref, interval, mode, function) bind function with timer and set the interval in ms the mode can be: tmr.ALARM_SINGLE for a single run alarm @@ -24,20 +24,20 @@ tmr.register(id, interval, mode, function) tmr.ALARM_AUTO for a repating alarm tmr.register does NOT start the timer tmr.alarm is a tmr.register & tmr.start macro -tmr.unregister(id) +tmr.unregister(ref) stop alarm, unbind function and clean up memory not needed for ALARM_SINGLE, as it unregisters itself -tmr.start(id) +tmr.start(ref) ret: bool start a alarm, returns true on success -tmr.stop(id) +tmr.stop(ref) ret: bool stops a alarm, returns true on success this call dose not free any memory, to do so use tmr.unregister stopped alarms can be started with start -tmr.interval(id, interval) +tmr.interval(ref, interval) set alarm interval, running alarm will be restarted -tmr.state(id) +tmr.state(ref) ret: (bool, int) or nil returns alarm status (true=started/false=stopped) and mode nil if timer is unregistered @@ -73,7 +73,8 @@ static const char* MAX_TIMEOUT_ERR_STR = "Range: 1-"STRINGIFY(MAX_TIMEOUT_DEF); typedef struct{ os_timer_t os; - sint32_t lua_ref, self_ref; + sint32_t lua_ref; /* Reference to the callback function */ + sint32_t self_ref; /* Reference to this structure as userdata */ uint32_t interval; uint8_t mode; }timer_struct_t; @@ -93,7 +94,6 @@ static uint32_t last_rtc_time=0; static uint64_t last_rtc_time_us=0; static sint32_t soft_watchdog = -1; -static timer_struct_t alarm_timers[NUM_TMR]; static os_timer_t rtc_timer; static void alarm_timer_common(void* arg){ @@ -102,12 +102,7 @@ static void alarm_timer_common(void* arg){ if(tmr->lua_ref == LUA_NOREF) return; lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->lua_ref); - if (tmr->self_ref == LUA_REFNIL) { - uint32_t id = tmr - alarm_timers; - lua_pushinteger(L, id); - } else { - lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->self_ref); - } + lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->self_ref); //if the timer was set to single run we clean up after it if(tmr->mode == TIMER_MODE_SINGLE){ luaL_unref(L, LUA_REGISTRYINDEX, tmr->lua_ref); @@ -148,19 +143,13 @@ static int tmr_now(lua_State* L){ } static timer_t tmr_get( lua_State *L, int stack ) { - // Deprecated: static 0-6 timers control by index. - luaL_argcheck(L, (lua_isuserdata(L, stack) || lua_isnumber(L, stack)), 1, "timer object or numerical ID expected"); - if (lua_isuserdata(L, stack)) { - return (timer_t)luaL_checkudata(L, stack, "tmr.timer"); - } else { - uint32_t id = luaL_checkinteger(L, 1); - luaL_argcheck(L, platform_tmr_exists(id), 1, "invalid timer index"); - return &alarm_timers[id]; - } - return 0; + timer_t t = (timer_t)luaL_checkudata(L, stack, "tmr.timer"); + if (t == NULL) + return (timer_t)luaL_error(L, "timer object expected"); + return t; } -// Lua: tmr.register( id / ref, interval, mode, function ) +// Lua: tmr.register( ref, interval, mode, function ) static int tmr_register(lua_State* L){ timer_t tmr = tmr_get(L, 1); @@ -389,8 +378,8 @@ static const LUA_REG_TYPE tmr_dyn_map[] = { { LSTRKEY( "state" ), LFUNCVAL( tmr_state ) }, { LSTRKEY( "interval" ), LFUNCVAL( tmr_interval) }, #ifdef TIMER_SUSPEND_ENABLE - { LSTRKEY( "suspend" ), LFUNCVAL( tmr_suspend ) }, - { LSTRKEY( "resume" ), LFUNCVAL( tmr_resume ) }, + { LSTRKEY( "suspend" ), LFUNCVAL( tmr_suspend ) }, + { LSTRKEY( "resume" ), LFUNCVAL( tmr_resume ) }, #endif { LSTRKEY( "__gc" ), LFUNCVAL( tmr_unregister ) }, { LSTRKEY( "__index" ), LROVAL( tmr_dyn_map ) }, @@ -403,19 +392,10 @@ static const LUA_REG_TYPE tmr_map[] = { { LSTRKEY( "wdclr" ), LFUNCVAL( tmr_wdclr ) }, { LSTRKEY( "softwd" ), LFUNCVAL( tmr_softwd ) }, { LSTRKEY( "time" ), LFUNCVAL( tmr_time ) }, - { LSTRKEY( "register" ), LFUNCVAL( tmr_register ) }, - { LSTRKEY( "alarm" ), LFUNCVAL( tmr_alarm ) }, - { LSTRKEY( "start" ), LFUNCVAL( tmr_start ) }, - { LSTRKEY( "stop" ), LFUNCVAL( tmr_stop ) }, #ifdef TIMER_SUSPEND_ENABLE - { LSTRKEY( "suspend" ), LFUNCVAL( tmr_suspend ) }, - { LSTRKEY( "suspend_all" ), LFUNCVAL( tmr_suspend_all ) }, - { LSTRKEY( "resume" ), LFUNCVAL( tmr_resume ) }, - { LSTRKEY( "resume_all" ), LFUNCVAL( tmr_resume_all ) }, + { LSTRKEY( "suspend_all" ), LFUNCVAL( tmr_suspend_all ) }, + { LSTRKEY( "resume_all" ), LFUNCVAL( tmr_resume_all ) }, #endif - { LSTRKEY( "unregister" ), LFUNCVAL( tmr_unregister ) }, - { LSTRKEY( "state" ), LFUNCVAL( tmr_state ) }, - { LSTRKEY( "interval" ), LFUNCVAL( tmr_interval ) }, { LSTRKEY( "create" ), LFUNCVAL( tmr_create ) }, { LSTRKEY( "ALARM_SINGLE" ), LNUMVAL( TIMER_MODE_SINGLE ) }, { LSTRKEY( "ALARM_SEMI" ), LNUMVAL( TIMER_MODE_SEMI ) }, @@ -425,16 +405,8 @@ static const LUA_REG_TYPE tmr_map[] = { #include "pm/swtimer.h" int luaopen_tmr( lua_State *L ){ - int i; - luaL_rometatable(L, "tmr.timer", (void *)tmr_dyn_map); - for(i=0; i