-
Notifications
You must be signed in to change notification settings - Fork 2.6k
回调定时器(Callback timer)
Callbacks should be used to implement scheduled tasks on microcontrollers. Other means of scheduling tasks, such as polling, require too much overhead for the relatively slow processor that is mounted on a microcontroller - those cycles should be used for more useful tasks.
Scheduled tasks in a mote could be scheduled sensor readings, a timer to put the mote back to sleep, or any other task that should be completed at a certain time.
Contiki has implemented a callback timer to do timer callbacks.
The ctimer library must be initialized before use, and this is done in the Contiki boot-up code. In general you should not have to worry about this function.
This function sets the the timer and defines which method will be called when the timer fires. The function requires a timer struct to use (which is really a handle so you can kill or reset the timer later), the timeout wait time, the function to call, and an argument.
In the example below, no argument was needed, so NULL was passed to the callback.
#include "contiki.h"
#include <stdio.h>
#include "sys/ctimer.h"
PROCESS(callback_example, "Callback example");
AUTOSTART_PROCESSES(&callback_example);
struct ctimer c;
#define MY_TIMEOUT 1 * CLOCK_SECOND
static void
my_timer_callback(void *in)
{
printf("In the callback!");
}
PROCESS_THREAD(callback_example, ev, data)
{
PROCESS_BEGIN();
printf("Starting up!");
ctimer_set(&c, MY_TIMEOUT, my_timer_callback, (void *)NULL);
PROCESS_END();
}