Skip to content

Commit

Permalink
Expunge integer timers
Browse files Browse the repository at this point in the history
  • Loading branch information
nwf committed Jan 9, 2019
1 parent 87b3ffa commit 5b22e1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
47 changes: 14 additions & 33 deletions app/modules/tmr.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ tmr.delay() -- not changed
tmr.alarm() -- not changed
tmr.stop() -- changed, see below. use tmr.unregister for old functionality
tmr.register(id, interval, mode, function)
tmr.register(ref, interval, mode, function)
bind function with timer and set the interval in ms
the mode can be:
tmr.ALARM_SINGLE for a single run alarm
tmr.ALARM_SEMI for a multiple single run alarm
tmr.ALARM_AUTO for a repating alarm
tmr.register does NOT start the timer
tmr.alarm is a tmr.register & tmr.start macro
tmr.unregister(id)
tmr.unregister(ref)
stop alarm, unbind function and clean up memory
not needed for ALARM_SINGLE, as it unregisters itself
tmr.start(id)
tmr.start(ref)
ret: bool
start a alarm, returns true on success
tmr.stop(id)
tmr.stop(ref)
ret: bool
stops a alarm, returns true on success
this call dose not free any memory, to do so use tmr.unregister
stopped alarms can be started with start
tmr.interval(id, interval)
tmr.interval(ref, interval)
set alarm interval, running alarm will be restarted
tmr.state(id)
tmr.state(ref)
ret: (bool, int) or nil
returns alarm status (true=started/false=stopped) and mode
nil if timer is unregistered
Expand Down Expand Up @@ -73,7 +73,8 @@ static const char* MAX_TIMEOUT_ERR_STR = "Range: 1-"STRINGIFY(MAX_TIMEOUT_DEF);

typedef struct{
os_timer_t os;
sint32_t lua_ref, self_ref;
sint32_t lua_ref; /* Reference to the callback function */
sint32_t self_ref; /* Reference to this structure as userdata */
uint32_t interval;
uint8_t mode;
}timer_struct_t;
Expand All @@ -93,7 +94,6 @@ static uint32_t last_rtc_time=0;
static uint64_t last_rtc_time_us=0;

static sint32_t soft_watchdog = -1;
static timer_struct_t alarm_timers[NUM_TMR];
static os_timer_t rtc_timer;

static void alarm_timer_common(void* arg){
Expand All @@ -102,12 +102,7 @@ static void alarm_timer_common(void* arg){
if(tmr->lua_ref == LUA_NOREF)
return;
lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->lua_ref);
if (tmr->self_ref == LUA_REFNIL) {
uint32_t id = tmr - alarm_timers;
lua_pushinteger(L, id);
} else {
lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->self_ref);
}
lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->self_ref);
//if the timer was set to single run we clean up after it
if(tmr->mode == TIMER_MODE_SINGLE){
luaL_unref(L, LUA_REGISTRYINDEX, tmr->lua_ref);
Expand Down Expand Up @@ -148,19 +143,13 @@ static int tmr_now(lua_State* L){
}

static timer_t tmr_get( lua_State *L, int stack ) {
// Deprecated: static 0-6 timers control by index.
luaL_argcheck(L, (lua_isuserdata(L, stack) || lua_isnumber(L, stack)), 1, "timer object or numerical ID expected");
if (lua_isuserdata(L, stack)) {
return (timer_t)luaL_checkudata(L, stack, "tmr.timer");
} else {
uint32_t id = luaL_checkinteger(L, 1);
luaL_argcheck(L, platform_tmr_exists(id), 1, "invalid timer index");
return &alarm_timers[id];
}
return 0;
timer_t t = (timer_t)luaL_checkudata(L, stack, "tmr.timer");
if (t == NULL)
return (timer_t)luaL_error(L, "timer object expected");
return t;
}

// Lua: tmr.register( id / ref, interval, mode, function )
// Lua: tmr.register( ref, interval, mode, function )
static int tmr_register(lua_State* L){
timer_t tmr = tmr_get(L, 1);

Expand Down Expand Up @@ -425,16 +414,8 @@ static const LUA_REG_TYPE tmr_map[] = {

#include "pm/swtimer.h"
int luaopen_tmr( lua_State *L ){
int i;

luaL_rometatable(L, "tmr.timer", (void *)tmr_dyn_map);

for(i=0; i<NUM_TMR; i++){
alarm_timers[i].lua_ref = LUA_NOREF;
alarm_timers[i].self_ref = LUA_REFNIL;
alarm_timers[i].mode = TIMER_MODE_OFF;
os_timer_disarm(&alarm_timers[i].os);
}
last_rtc_time=system_get_rtc_time(); // Right now is time 0
last_rtc_time_us=0;

Expand Down
34 changes: 17 additions & 17 deletions docs/en/modules/tmr.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ It is aimed at setting up regularly occurring tasks, timing out operations, and

What the tmr module is *not* however, is a time keeping module. While most timeouts are expressed in milliseconds or even microseconds, the accuracy is limited and compounding errors would lead to rather inaccurate time keeping. Consider using the [rtctime](rtctime.md) module for "wall clock" time.

NodeMCU provides 7 static timers, numbered 0-6, and dynamic timer creation function [`tmr.create()`](#tmrcreate).

!!! attention

Static timers are deprecated and will be removed later. Use the OO API initiated with [`tmr.create()`](#tmrcreate).
NodeMCU formerly provided 7 static timers, numbered 0-6, which could be
used instead of OO API timers initiated with [`tmr.create()`](#tmrcreate).
After a long period of deprecation, these were removed in 2019 Q1.

## tmr.alarm()

Expand All @@ -22,10 +22,10 @@ This is a convenience function combining [`tmr.register()`](#tmrregister) and [`
To free up the resources with this timer when done using it, call [`tmr.unregister()`](#tmrunregister) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.

#### Syntax
`tmr.alarm([id/ref], interval_ms, mode, func())`
`tmr.alarm(ref, interval_ms, mode, func())`

#### Parameters
- `id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
- `ref` timer object
- `interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
- `mode` timer mode:
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
Expand Down Expand Up @@ -113,10 +113,10 @@ tmr.delay(100)
Changes a registered timer's expiry interval.

#### Syntax
`tmr.interval([id/ref], interval_ms)`
`tmr.interval(ref, interval_ms)`

#### Parameters
- `id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
- `ref` timer object
- `interval_ms` new timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).

#### Returns
Expand Down Expand Up @@ -156,10 +156,10 @@ Configures a timer and registers the callback function to call on expiry.
To free up the resources with this timer when done using it, call [`tmr.unregister()`](#tmrunregister) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.

#### Syntax
`tmr.register([id/ref], interval_ms, mode, func())`
`tmr.register(ref, interval_ms, mode, func())`

#### Parameters
- `id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
- `ref` timer object
- `interval_ms` timer interval in milliseconds. Maximum value is 6870947 (1:54:30.947).
- `mode` timer mode:
- `tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
Expand Down Expand Up @@ -212,10 +212,10 @@ complex_stuff_which_might_never_call_the_callback(on_success_callback)
Starts or restarts a previously configured timer.

#### Syntax
`tmr.start([id/ref])`
`tmr.start(ref)`

#### Parameters
`id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
`ref` timer object

#### Returns
`true` if the timer was started, `false` on error
Expand All @@ -237,10 +237,10 @@ if not mytimer:start() then print("uh oh") end
Checks the state of a timer.

#### Syntax
`tmr.state([id/ref])`
`tmr.state(ref)`

#### Parameters
`id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
`ref` timer object

#### Returns
(bool, int) or `nil`
Expand All @@ -261,10 +261,10 @@ print("running: " .. tostring(running) .. ", mode: " .. mode) -- running: false,
Stops a running timer, but does *not* unregister it. A stopped timer can be restarted with [`tmr.start()`](#tmrstart).

#### Syntax
`tmr.stop([id/ref])`
`tmr.stop(ref)`

#### Parameters
`id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
`ref` timer object

#### Returns
`true` if the timer was stopped, `false` on error
Expand Down Expand Up @@ -304,10 +304,10 @@ Stops the timer (if running) and unregisters the associated callback.
This isn't necessary for one-shot timers (`tmr.ALARM_SINGLE`), as those automatically unregister themselves when fired.

#### Syntax
`tmr.unregister([id/ref])`
`tmr.unregister(ref)`

#### Parameters
`id`/`ref` timer id (0-6) or object, obsolete for OO API (→ [`tmr.create()`](#tmrcreate))
`ref` timer object

#### Returns
`nil`
Expand Down

0 comments on commit 5b22e1f

Please sign in to comment.