BlinkB

A simple Insense program which blinks the red green and blue LEDs at specified timer intervals.

/**
* A component for each colour of LED. Each component flash a LED at a 
* different interval : 0.25, 0.5, and 1 second.
*/

// Interface for the blink component with an IN channel for the timer ticks
type blinkI is interface (	in bool timerRecv	)
	
//An enum for the possible LED values 
type ledType is enum (red, green, blue, other)

component blink presents blinkI {

	timeout = 0.0
	led = other
	state = true

	constructor(ledType colour ; real duration) {
		timeout := duration
		led := colour
		
		setTimer(timerRecv , timeout , true)
	}
	
	
	behaviour {
	
		
		receive tick from timerRecv
		
		if (led == red) then{
			setRedLedPower(state)
			state := !state
		}
		
		if (led == green) then{
			setGreenLedPower(state)
			state := !state
		}
		
		if (led == blue) then{
			setBlueLedPower(state)
			state := !state
		}
	}
}
/**********************************************************************/
r = new blink(red, 0.25)
g = new blink(green, 0.5)
b = new blink(blue, 1.0)

The corresponding nesC program for TinyOS is shown below.


configuration BlinkAppC {
}
implementation {
  components MainC, BlinkC,  LedsC;
  components new ThreadC(100) as NullThread;
  components new ThreadC(100) as TinyThread0;
  components new ThreadC(100) as TinyThread1;
  components new ThreadC(100) as TinyThread2;

  MainC.Boot  NullThread;
  BlinkC.TinyThread0 -> TinyThread0;
  BlinkC.TinyThread1 -> TinyThread1; 
  BlinkC.TinyThread2 -> TinyThread2;

  BlinkC.Leds -> LedsC;
}

/************************************************/

module BlinkC {
  uses {
    interface Boot;
    interface Thread as NullThread;
    interface Thread as TinyThread0;
    interface Thread as TinyThread1;
    interface Thread as TinyThread2;
    interface Leds;
  }
}

implementation {
  event void Boot.booted() {
    //call NullThread.start(NULL);
    call TinyThread0.start(NULL);
    call TinyThread1.start(NULL);
    call TinyThread2.start(NULL);
  }

  event void NullThread.run(void* arg) {
    for(;;){
    }
  }  
  event void TinyThread0.run(void* arg) {
    for(;;){
      call Leds.led0Toggle();
      call TinyThread0.sleep(200);
    }
  }
  event void TinyThread1.run(void* arg) {
    for(;;){
      call Leds.led1Toggle();
      call TinyThread1.sleep(1000);
    }
  }
  event void TinyThread2.run(void* arg) {
    for(;;){
      call Leds.led2Toggle();
      call TinyThread2.sleep(1000);
    }
  }
}