tinyos

How to fire a onshot timer multiple times in TinyOs?


I am trying to write a simple program in TinyOS to implement 2 timers, one a periodic timer, and the other a oneshot timer. The periodic timer has to fire every 2 seconds and the oneshot timer should fire at 5th, 7th and 9th second respectively. I have written the program, but the oneshot timer doesn't work. Please help me find the issue.

#include "Timer.h"

module MyTimerC @safe()
{
  uses interface Timer<TMilli> as Timer0;
  uses interface Timer<TMilli> as Timer1;
  uses interface Leds;
  uses interface Boot;
}
implementation
{
  event void Boot.booted()
  {
    call Timer0.startOneShot( 5120 );
    call Timer0.startOneShot( 7168 );
    call Timer0.startOneShot( 9216 );
    call Timer1.startPeriodic( 2048 );
 }


  task void TogLed0()
  {
    dbg("MyTimerC", "LED 0 Toggle \n");
    call Leds.led0Toggle();

  }
  task void TogLed1()
  {
    dbg("MyTimerC", "LED 1 Toggle \n");
    call Leds.led1Toggle();

  }

  event void Timer0.fired()
  {
    dbg("MyTimerC", "One shot Timer 0 fired @ %s \n", sim_time_string());
    call Leds.led2Toggle();

  }


  event void Timer1.fired()
  {
    dbg("MyTimerC", "Periodic Timer 1 fired @ %s.\n", sim_time_string());
    post TogLed0();
    post TogLed1();
  }    


}

Solution

  • You're only supposed to call startOneShot once on a timer - you should only call startOneShot again on the same timer after it has fired. I would suggest either using 3 separate timers for your one shots, or call startOneShot(5 seconds) on boot, then when it fires, call startOneShot again for 2 seconds, then a third time. Use a counter to keep track of how many times it has fired.