Skip to content

Commit

Permalink
Add MGOS_TIMER_RUN_NOW flag
Browse files Browse the repository at this point in the history
CL: Add MGOS_TIMER_RUN_NOW flag

PUBLISHED_FROM=252221bea601eb610c76e646a89f9b958fa12db3
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Jun 11, 2018
1 parent c008ffd commit 57ae93c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 10 additions & 4 deletions fw/include/mgos_timers.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ typedef void (*timer_callback)(void *param);
/* Timer ID type */
typedef uintptr_t mgos_timer_id;

/* Flags for mgos_set*_timer() */

/*
* Flag for mgos_set*_timer().
*
* When it's set, the call is repeated indefinitely; otherwise the call is a
* one-off.
* When set, the callback is invoked at the specified interval.
* Otherwise the call is a one-off.
*/
#define MGOS_TIMER_REPEAT (1 << 0)

/*
* Flag that makes callback execute immediately.
* Only makes sense for repeating timers.
*/
#define MGOS_TIMER_RUN_NOW (1 << 1)

/*
* Setup a timer with `msecs` timeout and `cb` as a callback.
*
Expand Down
10 changes: 7 additions & 3 deletions fw/src/mgos_timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,20 @@ static void mgos_timer_ev(struct mg_connection *nc, int ev, void *ev_data,
(void) nc;
}

mgos_timer_id mgos_set_timer(int msecs, int repeat, timer_callback cb,
mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb,
void *arg) {
struct timer_info *ti = (struct timer_info *) calloc(1, sizeof(*ti));
if (ti == NULL) return MGOS_INVALID_TIMER_ID;
ti->next_invocation = mg_time() + msecs / 1000.0;
if (repeat) {
if (flags & MGOS_TIMER_REPEAT) {
ti->interval_ms = msecs;
} else {
ti->interval_ms = -1;
}
if (flags & MGOS_TIMER_RUN_NOW) {
ti->next_invocation = 1;
} else {
ti->next_invocation = mg_time() + msecs / 1000.0;
}
ti->cb = cb;
ti->cb_arg = arg;
{
Expand Down

0 comments on commit 57ae93c

Please sign in to comment.