Fork me on GitHub

Timer

This library provides timer API that allows to execute code in the future, repeatedly or once.

C API Reference mgos_timers.h

#include "mgos_timers.h"

View this file on GitHub: mgos_timers.h


typedef void (*timer_callback)(void *param);

Timer callback

typedef uintptr_t mgos_timer_id;

/*
 * Setup timer with `msecs` timeout and `cb` as a callback.
 *
 * `repeat` set to 1 will repeat a call infinitely, otherwise it's a one-off.
 * `arg` is a parameter to pass to `cb`. Return numeric timer ID.
 */
mgos_timer_id mgos_set_timer(int msecs, int repeat, timer_callback cb,
                             void *arg);

Timer ID type

mgos_timer_id mgos_set_timer(int msecs, int repeat, timer_callback cb,
                             void *arg);

Setup timer with msecs timeout and cb as a callback.

repeat set to 1 will repeat a call infinitely, otherwise it's a one-off. arg is a parameter to pass to cb. Return numeric timer ID.

void mgos_clear_timer(mgos_timer_id id);

Disable timer with a given timer ID.

enum mgos_init_result mgos_timers_init(void);

double mgos_uptime(void);

Get number of seconds since last reboot

void mgos_uptime_init(void);

int mgos_strftime(char *s, int size, char *fmt, int time);

JAVASCRIPT API Reference api_timer.js

load("api_timer.js");


Timer.set(milliseconds, repeat, handler)

Setup timer with milliseconds timeout and handler as a callback. If repeat is set to true, the call will be repeated indefinitely, otherwise it's a one-off.

Return value: numeric timer ID.

Example:

// Call every second
Timer.set(1000, true, function() {
  let value = GPIO.toggle(2);
  print(value ? 'Tick' : 'Tock');
}, null);

Timer.now()

Return current time as double value, UNIX epoch (seconds since 1970).

Timer.del(id)

Cancel previously installed timer.

Timer.fmt(fmt, time)

Formats the time 'time' according to the strftime-like format specification 'fmt'