Updated tmr module and documentation
* Change upper limit for timer in `tmr_register` and `tmr_interval` to reflect new limit in SDK 1.5.3. * Change documentation for `tmr.alarm()`, `tmr.interval()` and `tmr.register()` to match source. * Improve error reporting to be more descriptive.
This commit is contained in:
parent
65c7e8e061
commit
1744372060
|
@ -60,6 +60,15 @@ tmr.softwd(int)
|
||||||
#define TIMER_MODE_AUTO 1
|
#define TIMER_MODE_AUTO 1
|
||||||
#define TIMER_IDLE_FLAG (1<<7)
|
#define TIMER_IDLE_FLAG (1<<7)
|
||||||
|
|
||||||
|
#define STRINGIFY_VAL(x) #x
|
||||||
|
#define STRINGIFY(x) STRINGIFY_VAL(x)
|
||||||
|
|
||||||
|
// assuming system_timer_reinit() has *not* been called
|
||||||
|
#define MAX_TIMEOUT_DEF 6870947 //SDK 1.5.3 limit (0x68D7A3)
|
||||||
|
|
||||||
|
static const uint32 MAX_TIMEOUT=MAX_TIMEOUT_DEF;
|
||||||
|
static const char* MAX_TIMEOUT_ERR_STR = "Range: 1-"STRINGIFY(MAX_TIMEOUT_DEF);
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
os_timer_t os;
|
os_timer_t os;
|
||||||
sint32_t lua_ref;
|
sint32_t lua_ref;
|
||||||
|
@ -129,16 +138,13 @@ static int tmr_now(lua_State* L){
|
||||||
// Lua: tmr.register( id, interval, mode, function )
|
// Lua: tmr.register( id, interval, mode, function )
|
||||||
static int tmr_register(lua_State* L){
|
static int tmr_register(lua_State* L){
|
||||||
uint32_t id = luaL_checkinteger(L, 1);
|
uint32_t id = luaL_checkinteger(L, 1);
|
||||||
MOD_CHECK_ID(tmr, id);
|
uint32_t interval = luaL_checkinteger(L, 2);
|
||||||
sint32_t interval = luaL_checkinteger(L, 2);
|
|
||||||
uint8_t mode = luaL_checkinteger(L, 3);
|
uint8_t mode = luaL_checkinteger(L, 3);
|
||||||
//validate arguments
|
//Check if provided parameters are valid
|
||||||
const int32_t MAX_TIMEOUT = 0xC49BA5; // assuming system_timer_reinit() has *not* been called
|
MOD_CHECK_ID(tmr, id);
|
||||||
uint8_t args_invalid = (interval <= 0 || interval > MAX_TIMEOUT)
|
luaL_argcheck(L, (interval > 0 && interval <= MAX_TIMEOUT), 2, MAX_TIMEOUT_ERR_STR);
|
||||||
|| (mode != TIMER_MODE_SINGLE && mode != TIMER_MODE_SEMI && mode != TIMER_MODE_AUTO)
|
luaL_argcheck(L, (mode == TIMER_MODE_SINGLE || mode == TIMER_MODE_SEMI || mode == TIMER_MODE_AUTO), 3, "Invalid mode");
|
||||||
|| (lua_type(L, 4) != LUA_TFUNCTION && lua_type(L, 4) != LUA_TLIGHTFUNCTION);
|
luaL_argcheck(L, (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION), 4, "Must be function");
|
||||||
if(args_invalid)
|
|
||||||
return luaL_error(L, "wrong arg range");
|
|
||||||
//get the lua function reference
|
//get the lua function reference
|
||||||
lua_pushvalue(L, 4);
|
lua_pushvalue(L, 4);
|
||||||
sint32_t ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
sint32_t ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||||
|
@ -212,9 +218,8 @@ static int tmr_interval(lua_State* L){
|
||||||
uint8_t id = luaL_checkinteger(L, 1);
|
uint8_t id = luaL_checkinteger(L, 1);
|
||||||
MOD_CHECK_ID(tmr,id);
|
MOD_CHECK_ID(tmr,id);
|
||||||
timer_t tmr = &alarm_timers[id];
|
timer_t tmr = &alarm_timers[id];
|
||||||
sint32_t interval = luaL_checkinteger(L, 2);
|
uint32_t interval = luaL_checkinteger(L, 2);
|
||||||
if(interval <= 0)
|
luaL_argcheck(L, (interval > 0 && interval <= MAX_TIMEOUT), 2, MAX_TIMEOUT_ERR_STR);
|
||||||
return luaL_error(L, "wrong arg range");
|
|
||||||
if(tmr->mode != TIMER_MODE_OFF){
|
if(tmr->mode != TIMER_MODE_OFF){
|
||||||
tmr->interval = interval;
|
tmr->interval = interval;
|
||||||
if(!(tmr->mode&TIMER_IDLE_FLAG)){
|
if(!(tmr->mode&TIMER_IDLE_FLAG)){
|
||||||
|
|
|
@ -19,7 +19,7 @@ To free up the resources with this timer when done using it, call [`tmr.unregist
|
||||||
|
|
||||||
#### Parameters
|
#### Parameters
|
||||||
- `id` timer id (0-6)
|
- `id` timer id (0-6)
|
||||||
- `interval_ms` timer interval in milliseconds. Maximum value is 12884901. In SDKs <= 1.5.0 values >6871948 result in incorrect behaviour.
|
- `interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
|
||||||
- `mode` timer mode:
|
- `mode` timer mode:
|
||||||
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
|
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
|
||||||
- `tmr.ALARM_SEMI` manually repeating alarm (call [`tmr.start()`](#tmrstart) to restart)
|
- `tmr.ALARM_SEMI` manually repeating alarm (call [`tmr.start()`](#tmrstart) to restart)
|
||||||
|
@ -68,7 +68,7 @@ Changes a registered timer's expiry interval.
|
||||||
|
|
||||||
#### Parameters
|
#### Parameters
|
||||||
- `id` timer id (0-6)
|
- `id` timer id (0-6)
|
||||||
- `interval_ms` new timer interval in milliseconds. Maximum value is 12884901. In SDKs <= 1.5.0 values >6871948 result in incorrect behaviour.
|
- `interval_ms` new timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
|
||||||
|
|
||||||
#### Returns
|
#### Returns
|
||||||
`nil`
|
`nil`
|
||||||
|
@ -109,7 +109,7 @@ To free up the resources with this timer when done using it, call [`tmr.unregist
|
||||||
|
|
||||||
#### Parameters
|
#### Parameters
|
||||||
- `id` timer id (0-6)
|
- `id` timer id (0-6)
|
||||||
- `interval_ms` timer interval in milliseconds. Maximum value is 12884901. In SDKs <= 1.5.0 values >6871948 result in incorrect behaviour.
|
- `interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
|
||||||
- `mode` timer mode:
|
- `mode` timer mode:
|
||||||
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
|
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
|
||||||
- `tmr.ALARM_SEMI` manually repeating alarm (call [`tmr.start()`](#tmrunregister) to restart)
|
- `tmr.ALARM_SEMI` manually repeating alarm (call [`tmr.start()`](#tmrunregister) to restart)
|
||||||
|
|
Loading…
Reference in New Issue