Skip to content

Commit

Permalink
Implementing optional parameter restart to tmr:start() (issue #3020)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsky committed Jun 17, 2020
1 parent 1f2e5bb commit 5c85a90
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 12 additions & 5 deletions app/modules/tmr.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,24 @@ static int tmr_register(lua_State* L) {
return 0;
}

// Lua: t:start()
// Lua: t:start( [restart] )
static int tmr_start(lua_State* L){
tmr_t *tmr = (tmr_t *) luaL_checkudata(L, 1, "tmr.timer");
int idle = tmr->mode & TIMER_IDLE_FLAG;
tmr_t tmr = tmr_get(L, 1);
lua_settop(L, 2);
luaL_argcheck(L, lua_isboolean(L, 2) || lua_isnil(L, 2), 2, "boolean expected");
int restart = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : 0;

lua_settop(L, 1); /* ignore any args after the userdata */
if (tmr->self_ref == LUA_NOREF)
tmr->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);

if(idle) {
tmr->mode &= ~TIMER_IDLE_FLAG;
//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;
os_timer_arm(&tmr->os, tmr->interval, tmr->mode==TIMER_MODE_AUTO);
}
lua_pushboolean(L, !idle); /* false if the timer is not idle */
Expand Down
6 changes: 3 additions & 3 deletions docs/modules/tmr.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ mytimer: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
`tobj:start()`
`tobj:start([restart])`

#### Parameters
None
- `restart` optional boolean parameter forcing to restart already running timer

#### Returns
`true` if the timer was started, `false` on error
Expand Down

0 comments on commit 5c85a90

Please sign in to comment.