无线传感网tinyos代码分析

分析代码的逻辑功能
SenseC.nc 
#include "Timer.h"
Module SenseC
{
  uses {…}
}
Implementation
{
  #define SAMPLING_FREQUENCY 100
  event void Boot.booted() {
  call Timer.startPeriodic(SAMPLING_FREQUENCY);
  }
  event void Timer.fired() {
    call Read.read();      
  }
  event void Read.readDone(error_t result, uint16_t data) {
    if (result == SUCCESS){
        if (data & 0x0004)
        call Leds.led2On();
      else
        call Leds.led2Off();
      if (data & 0x0002)
        call Leds.led1On();
      else
        call Leds.led1Off();
      if (data & 0x0001)
        call Leds.led0On();
      else
        call Leds.led0Off();
    }
  }
}
  event result_t Timer.fired() {
    return call ADC.getData();
  }

SenseC.nc

#include "Timer.h"

Module SenseC

{

  uses {…}

}

Implementation

{

  #define SAMPLING_FREQUENCY 100   (采样周期100ms)

  event void Boot.booted() {

  call Timer.startPeriodic(SAMPLING_FREQUENCY);    (启动计数器)

  }

  event void Timer.fired() {

    call Read.read();                     (计时器到,开始读取数据)

  }

(读取数据完毕,处理数据,即将数据用LED显示)

  event void Read.readDone(error_t result, uint16_t data) {        

    if (result == SUCCESS){

        if (data & 0x0004)

        call Leds.led2On();

      else

        call Leds.led2Off();

      if (data & 0x0002)

        call Leds.led1On();

      else

        call Leds.led1Off();

      if (data & 0x0001)

        call Leds.led0On();

      else

        call Leds.led0Off();

    }

  }

}

  event result_t Timer.fired() {

    return call ADC.getData();

  }

像BlinkC模块一样,SenseC组件使用Boot,Timer和Leds接口。此外,SenseC组件还使用了Read<unit16_t>接口,SenseC周期性的读取采集到的数据并根据数据决定LED的亮灯情况,具体实现过程如下:

1.首先系统初始化,调用Timer,startPeriodic(SAMPLING_FREQUENCY)启动周期性计数器。

2.计数器到,触发事件Timer.fired(),调用Read<unit16_t>接口Read.read()命令读取数据。

3.当数据读取完成,触发事件Read.readone(),然后将数据通过LED显示出来。