Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
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;
vsky279 marked this conversation as resolved.
Show resolved Hide resolved

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)
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);
}
vsky279 marked this conversation as resolved.
Show resolved Hide resolved
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