Implementing optional parameter `restart` to tmr:start() (#3111)

This commit is contained in:
Lukáš Voborský 2020-08-29 21:43:50 +02:00 committed by GitHub
parent 87030a87ea
commit d72ea91ed0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 65 deletions

View File

@ -125,16 +125,23 @@ static int tmr_register(lua_State* L) {
return 0; return 0;
} }
// Lua: t:start() // Lua: t:start( [restart] )
static int tmr_start(lua_State* L){ static int tmr_start(lua_State* L){
tmr_t *tmr = (tmr_t *) luaL_checkudata(L, 1, "tmr.timer"); tmr_t *tmr = (tmr_t *) luaL_checkudata(L, 1, "tmr.timer");
int idle = tmr->mode & TIMER_IDLE_FLAG; lua_settop(L, 2);
luaL_argcheck(L, lua_isboolean(L, 2) || lua_isnil(L, 2), 2, "boolean expected");
int restart = lua_toboolean(L, 2);
lua_settop(L, 1); /* ignore any args after the userdata */ lua_settop(L, 1); /* ignore any args after the userdata */
if (tmr->self_ref == LUA_NOREF) if (tmr->self_ref == LUA_NOREF)
tmr->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); tmr->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
if(idle) { //we return false if the timer is not idle
int idle = tmr->mode&TIMER_IDLE_FLAG;
if(!(idle || restart)){
lua_pushboolean(L, 0);
}else{
if (!idle) {os_timer_disarm(&tmr->os);}
tmr->mode &= ~TIMER_IDLE_FLAG; tmr->mode &= ~TIMER_IDLE_FLAG;
os_timer_arm(&tmr->os, tmr->interval, tmr->mode==TIMER_MODE_AUTO); os_timer_arm(&tmr->os, tmr->interval, tmr->mode==TIMER_MODE_AUTO);
} }

View File

@ -273,13 +273,13 @@ mytimer:start()
### tobj:start() ### tobj:start()
Starts or restarts a previously configured timer. Starts or restarts a previously configured timer. If the timer is running the timer is restarted only when `restart` parameter is `true`. Otherwise `false` is returned signaling error.
#### Syntax #### Syntax
`tobj:start()` `tobj:start([restart])`
#### Parameters #### Parameters
None - `restart` optional boolean parameter forcing to restart already running timer
#### Returns #### Returns
`true` if the timer was started, `false` on error `true` if the timer was started, `false` on error