User Tools

Site Tools


mbstring extension must be loaded in order to run mPDF
lab12

This is an old revision of the document!


Timers & Events in Contiki

This tutorial will show how to make use of timers in Contiki. It will also give a basic into to events. Contiki provides three kinds of timers:

  • Simple timer: The timer library provides functions for setting, resetting and restarting timers, and for checking if a timer has expired. An application must “manually” check if its timers have expired, meaning that this library does not post an event when the timer expires, so we must implement a routine that checks the timer for expiration.
  • Callback timer: The callback timer library provides the same functions as above, but when the timer expires can callback a C function.
  • Event timer: The same as above, with the difference that instead of calling a function, when the timer expires it post an event signalling the timer expiration.

More information on timer can be found on the Contiki's wiki page on timers.

The Event Timer

The etimer (Event timer) will post an event when the timer is expiring. Since it posts an event you will need to have a process around it to handle the event.

PROCESS_THREAD(etimer_process, ev, data)
{
  static struct etimer et;
  PROCESS_BEGIN();
  /* set timer to expire after 5 seconds */
  etimer_set(&et, CLOCK_SECOND * 5);
  while(1) {
    PROCESS_WAIT_EVENT(); /* Same thing as PROCESS_YIELD */
    if(etimer_expired(&et)) {
      /* Do the work here and restart timer to get it periodic !!! */
      printf("etimer expired.\n"); 
      etimer_restart(&et);
    }
  }
  PROCESS_END();
}

In the above code the process is defined by PROCESS_THREAD which takes a name, an event variable and an event data variable as the arguments. When an etimer expires the event variable will be set to PROCESS_EVENT_TIMER and the event data variable will be set to point to the specific timer. Using that information the above example could also look like:

PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && data == &et);
/* Do the work here and restart timer to get it periodic! */
printf("etimer expired.\n"); 
etimer_restart(&et);

But when waiting for this event in this way – all other events will be ignored so the first approach is more flexible as this will enable handling multiple types of events more easily.

Task 1: Write a new test example for the Sparrow node (ideally place it in /contiki/platform/sparrow/tests) and define an event timer that expires once every second. At expiration, toggle on or off one of the LEDs. Use the LED driver written in ../dev/leds.h
Task 2: Modify the test example to include two processes. First process, named read_temperature will read the node's temperature every 5 seconds. For the temperature sensor, please refer to the driver in ../dev/temperature-sensor.h. The second process called collect_all will display the temperature sampled by the first process and turn on an LED if it exceeds a preset threshold.
lab12.1547190516.txt.gz · Last modified: 2019/01/11 09:08 by dan.tudose