tos中定时器的使用

// 顶层配件为空
configuration BlinkAppC{}

implementation
{
// 因为使用MainC中导出的Boot接口
components MainC;

components BlinkM as App,LedsC;
// tos\system\TimerMilliC.nc
components new TimerMilliC() as Timer0;

App->MainC.Boot;
App->LedsC.Leds;
/*
In component `BlinkAppC':
BlinkAppC.nc:15: cannot find `Timer0'
make: *** [exe0] Error 1

App->Timer0.Timer0;

*/
App.Timer0->Timer0; // ok
}

 

 

// 定时器的使用:每1000ms post一个任务,在任务中使用Led闪一次

module BlinkM @safe()
{
// 需要调用Leds接口中的命令来点亮Led,而我们只是使用Leds接口,
// 而Leds接口的真正实现是由LedsC来实现的,在配置中我们要指定它们关系
uses interface Leds;

// tos启动完成后触发的,而Boot接口是由MainC导出的
uses interface Boot;

// 需要使用定时器,在tos\lib\timer\Timer.nc有接口的定义
uses interface Timer<TMilli> as Timer0;
}

// 模块的实现
implementation
{
task void Led0_Task(void)
{
// tos\interfaces\Leds.nc有接口Leds的定义
call Leds.led0Toggle();
}

// 使用接口Timer,要实现接口中的事件
// 而接口中命令则有接口的提供者来实现
event void Timer0.fired()
{
post Led0_Task();
}

// 使用Boot接口,tos启动时就是signal接口Boot的booted事件
// 再由booted事件触发下面一系列动作的
event void Boot.booted()
{
// 启动每一1000ms的周期性定时器
call Timer0.startPeriodic(1000);
}
}

 

源码 https://files.cnblogs.com/files/tinyos/2_Blink.zip

 

posted @ 2017-03-15 23:47  未命名blogs  阅读(223)  评论(0编辑  收藏  举报