tinyos2.x/tos/interfaces/中提供了组件Random.nc,其提供了两个命令:async command uint32_t rand32()、async command uint16_t rand16();调用的时候直接int16/32_t num=call Random.rand16/32()即可。
给出简单的Blink例子
1.更改BlinkC.nc
#include <Timer.h>
module BlinkC{
uses interface Timer<TMilli> as Timer0;
// uses interface Timer<TMilli> as Timer1;
// uses interface Timer<TMilli> as Timer2;
uses interface Leds;
uses interface Boot;
uses interface Random;
}
implementation{
event void Boot.booted(){
call Timer0.startPeriodic(250);
// call Timer1.startPeriodic(1000);
// call Timer2.startPeriodic(500);
}
event void Timer0.fired(){
int16_t i=call Random.rand16();
call Leds.set(i);
}
/* event void Timer1.fired(){
call Leds.led1Toggle();
}
event void Timer2.fired(){
call Leds.led2Toggle();
}
*/
}
2.更改BlinkAppC.nc
configuration BlinkAppC{
}
implementation{
components MainC,LedsC,RandomC;
components BlinkC as App;
components new TimerMilliC() as Timer0;
// components new TimerMilliC() as Timer1;
// components new TimerMilliC() as Timer2;
App->MainC.Boot;
App.Timer0->Timer0;
// App.Timer1->Timer1;
// App.Timer2->Timer2;
App.Leds->LedsC;
App.Random->RandomC;
}