Github Repo | C Header | C source | JS source |
---|---|---|---|
cesanta/mongoose-os | mgos_timers.h | mgos_timers.c | api_timer.js |
Mongoose OS supports two types of timers: software timers and hardware timers.
Usage example:
#include "mgos_app.h"
#include "mgos_system.h"
#include "mgos_timers.h"
static void my_timer_cb(void *arg) {
bool val = mgos_gpio_toggle(mgos_sys_config_get_pins_led());
LOG(LL_INFO, ("uptime: %.2lf", mgos_uptime()));
(void) arg;
}
enum mgos_app_init_result mgos_app_init(void) {
mgos_set_timer(1000, MGOS_TIMER_REPEAT, my_timer_cb, NULL);
return MGOS_APP_INIT_SUCCESS;
}
typedef void (*timer_callback)(void *param);
Timer callback
mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb,
void *cb_arg);
Setup a timer with
msecs
timeout andcb
as a callback.
flags
is a bitmask, currently there's only one flag available:MGOS_TIMER_REPEAT
(see above).arg
is a parameter to pass tocb
. Return numeric timer ID.Note that this is a software timer, with fairly low accuracy and high jitter. However, number of software timers is not limited. If you need intervals < 10ms, use mgos_set_hw_timer.
Basic example:
static void my_timer_cb(void *arg) { bool val = mgos_gpio_toggle(mgos_sys_config_get_pins_led()); LOG(LL_INFO, ("uptime: %.2lf", mgos_uptime())); (void) arg; } enum mgos_app_init_result mgos_app_init(void) { mgos_set_timer(1000, MGOS_TIMER_REPEAT, my_timer_cb, NULL); return MGOS_APP_INIT_SUCCESS; }
Example passing unsigned int as an argument to void *
static void my_timer_cb(void *arg) { uint16_t conn_id = (uintptr_t) arg; LOG(LL_INFO, ("my_timer_cb value: %u", conn_id)); } enum mgos_app_init_result mgos_app_init(void) { uint16_t conn_id = 99; mgos_set_timer(15000, 0, my_timer_cb, (void *) (uintptr_t) conn_id); return MGOS_APP_INIT_SUCCESS; }
mgos_timer_id mgos_set_hw_timer(int usecs, int flags, timer_callback cb,
void *cb_arg);
Setup a hardware timer with
usecs
timeout andcb
as a callback.This is similar to mgos_set_timer, but can be used for shorter intervals (note that time unit is microseconds).
Number of hardware timers is limited (ESP8266: 1, ESP32: 4, CC32xx: 4).
Callback is executed in ISR context, with all the implications of that.
void mgos_clear_timer(mgos_timer_id id);
Disable timer with a given timer ID.
double mgos_uptime(void);
Get number of seconds since last reboot
int mgos_strftime(char *s, int size, char *fmt, int time);
Format
time
according to astrftime()
-conformant format. Write the result into thes,size
buffer. Return resulting string length.
Timer.set(milliseconds, flags, handler, userdata)
Setup timer with milliseconds
timeout and handler
as a callback.
flags
can be either 0 or Timer.REPEAT
. In the latter case, the call
will be repeated indefinitely (but can be cancelled with Timer.del()
),
otherwise it's a one-off.
Return value: numeric timer ID.
Example:
// Call every second
Timer.set(1000, Timer.REPEAT, 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'. The strftime reference can be found e.g. here. Example:
let now = Timer.now();
let s = Timer.fmt("Now it's %I:%M%p.", now);
print(s); // Example output: "Now it's 12:01AM."
edit this doc