コード例 #1
0
ファイル: tmr.c プロジェクト: dnc40085/nodemcu-firmware
// Lua: tmr.state( id / ref )
static int tmr_state(lua_State* L){
	timer_t tmr = tmr_get(L, 1);

	if(tmr->mode == TIMER_MODE_OFF){
		lua_pushnil(L);
		return 1;
	}
	lua_pushboolean(L, (tmr->mode&TIMER_IDLE_FLAG)==0);
	lua_pushinteger(L, tmr->mode&(~TIMER_IDLE_FLAG));
	return 2;
}
コード例 #2
0
ファイル: tmr.c プロジェクト: Wamoreira/nodemcu-firmware
// Lua: tmr.interval( id / ref, interval )
static int tmr_interval(lua_State* L){
	timer_t tmr = tmr_get(L, 1);

	uint32_t interval = luaL_checkinteger(L, 2);
	luaL_argcheck(L, (interval > 0 && interval <= MAX_TIMEOUT), 2, MAX_TIMEOUT_ERR_STR);
	if(tmr->mode != TIMER_MODE_OFF){	
		tmr->interval = interval;
		if(!(tmr->mode&TIMER_IDLE_FLAG)){
			os_timer_disarm(&tmr->os);
			os_timer_arm(&tmr->os, tmr->interval, tmr->mode==TIMER_MODE_AUTO);
		}
	}
	return 0;
}
コード例 #3
0
ファイル: tmr.c プロジェクト: Wamoreira/nodemcu-firmware
// Lua: tmr.unregister( id / ref )
static int tmr_unregister(lua_State* L){
	timer_t tmr = tmr_get(L, 1);

	if (tmr->self_ref != LUA_REFNIL) {
		luaL_unref(L, LUA_REGISTRYINDEX, tmr->self_ref);
		tmr->self_ref = LUA_NOREF;
	}

	if(!(tmr->mode & TIMER_IDLE_FLAG) && tmr->mode != TIMER_MODE_OFF)
		os_timer_disarm(&tmr->os);
	if(tmr->lua_ref != LUA_NOREF)
		luaL_unref(L, LUA_REGISTRYINDEX, tmr->lua_ref);
	tmr->lua_ref = LUA_NOREF;
	tmr->mode = TIMER_MODE_OFF; 
	return 0;
}
コード例 #4
0
ファイル: tmr.c プロジェクト: Wamoreira/nodemcu-firmware
// Lua: tmr.stop( id / ref )
static int tmr_stop(lua_State* L){
	timer_t tmr = tmr_get(L, 1);

	if (tmr->self_ref != LUA_REFNIL) {
		luaL_unref(L, LUA_REGISTRYINDEX, tmr->self_ref);
		tmr->self_ref = LUA_NOREF;
	}

	//we return false if the timer is idle (of not registered)
	if(!(tmr->mode & TIMER_IDLE_FLAG) && tmr->mode != TIMER_MODE_OFF){
		tmr->mode |= TIMER_IDLE_FLAG;
		os_timer_disarm(&tmr->os);
		lua_pushboolean(L, 1);
	}else{
		lua_pushboolean(L, 0);
	}
	return 1;  
}
コード例 #5
0
ファイル: tmr.c プロジェクト: Wamoreira/nodemcu-firmware
// Lua: tmr.start( id / ref )
static int tmr_start(lua_State* L){
	timer_t tmr = tmr_get(L, 1);

	if (tmr->self_ref == LUA_NOREF) {
		lua_pushvalue(L, 1);
		tmr->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
	}

	//we return false if the timer is not idle
	if(!(tmr->mode&TIMER_IDLE_FLAG)){
		lua_pushboolean(L, 0);
	}else{
		tmr->mode &= ~TIMER_IDLE_FLAG;
		os_timer_arm(&tmr->os, tmr->interval, tmr->mode==TIMER_MODE_AUTO);
		lua_pushboolean(L, 1);
	}
	return 1;
}
コード例 #6
0
ファイル: tmr.c プロジェクト: Wamoreira/nodemcu-firmware
// Lua: tmr.register( id / ref, interval, mode, function )
static int tmr_register(lua_State* L){
	timer_t tmr = tmr_get(L, 1);

	uint32_t interval = luaL_checkinteger(L, 2);
	uint8_t mode = luaL_checkinteger(L, 3);

	luaL_argcheck(L, (interval > 0 && interval <= MAX_TIMEOUT), 2, MAX_TIMEOUT_ERR_STR);
	luaL_argcheck(L, (mode == TIMER_MODE_SINGLE || mode == TIMER_MODE_SEMI || mode == TIMER_MODE_AUTO), 3, "Invalid mode");
	luaL_argcheck(L, (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION), 4, "Must be function");
	//get the lua function reference
	lua_pushvalue(L, 4);
	sint32_t ref = luaL_ref(L, LUA_REGISTRYINDEX);
	if(!(tmr->mode & TIMER_IDLE_FLAG) && tmr->mode != TIMER_MODE_OFF)
		os_timer_disarm(&tmr->os);
	//there was a bug in this part, the second part of the following condition was missing
	if(tmr->lua_ref != LUA_NOREF && tmr->lua_ref != ref)
		luaL_unref(L, LUA_REGISTRYINDEX, tmr->lua_ref);
	tmr->lua_ref = ref;
	tmr->mode = mode|TIMER_IDLE_FLAG;
	tmr->interval = interval;
	os_timer_setfn(&tmr->os, alarm_timer_common, tmr);
	return 0;  
}