zoukankan      html  css  js  c++  java
  • 第七次实验:CC2530平台上多跳通信的TinyOS编程

    module  P2MM

    {

    uses interface Boot;

    uses interface Timer<TMilli> as Timer0;

    uses interface SplitControl as AMControl;

    uses interface AMPacket;

    uses interface AMSend;

    uses interface Receive;

    uses interface Packet;

    }

    implementation

    {

    typedef nx_struct P2MMsg {nx_uint16_t nodeid[3];nx_uint16_t num;nx_uint16_t counter;}P2MMsg;

    uint8_t counter=0;

    bool busy =FALSE;

    message_t pkt;

    task void test() { }

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

    * 函数名:booted()

    * 功  能:系统启动完毕后自动触发

    * 参  数:无

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

    event void Boot.booted()

    {

    call AMControl.start();

    }

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

    * 函数名:fired()

    * 功  能:定时器触发事件

    * 参  数:无

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

    event void Timer0.fired()

    {

    counter++;

    if (!busy)

    {

    P2MMsg* btrpkt = (P2MMsg*)(call Packet.getPayload(&pkt, sizeof(P2MMsg)));

    btrpkt->num=0;

    btrpkt->nodeid[btrpkt->num] = TOS_NODE_ID;

    btrpkt->counter = counter;

    call AMPacket.setGroup(&pkt,TOS_IEEE_GROUP);    

    if (call AMSend.send(0xFFFF, &pkt, sizeof(P2MMsg)) == SUCCESS)

    busy = TRUE;

         }

    }

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

    * 函数名:startDone(error_t err)

    * 功  能:SplitControl控制启动完成事件

    * 参  数:error_t err

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

    event void AMControl.startDone(error_t err)

    {

    if(err==SUCCESS)

    {

    if(TOS_NODE_ID!=0x1)

    call Timer0.startPeriodic(1000);

    }

    else

    call AMControl.start();

    }

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

    * 函数名:sendDone(message_t* msg, error_t erro)

    * 功  能:AM消息发送完毕事件

    * 参  数:message_t* msg, error_t erro

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

       event void AMSend.sendDone(message_t* msg, error_t erro)

       {

    if (&pkt == msg)

          busy = FALSE;  

       }

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

    * 函数名:receive(message_t* msg, void* payload, uint8_t len)

    * 功  能:Receive接收事件

    * 参  数:message_t* msg, void* payload, uint8_t len

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

    event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len)

    {

    if (len == sizeof(P2MMsg))

       {

         P2MMsg* btrpkt = (P2MMsg*)payload;

    DbgOut(9,"Data is %d,Start is %d,Other is %d,%d ",(uint16_t)btrpkt->counter,

    (uint16_t)btrpkt->nodeid[0],(uint16_t)btrpkt->nodeid[1],(uint16_t)btrpkt->nodeid[2]);

    btrpkt->num++;

    if(TOS_NODE_ID!=0x1){

    if(btrpkt->num<3){

    bool flag=TRUE;

    uint16_t i;

    for(i=0;i<btrpkt->num;i++){

    if(btrpkt->nodeid[i] == TOS_NODE_ID)

    flag=FALSE;

    }

    if(flag){

    btrpkt->nodeid[btrpkt->num] = TOS_NODE_ID;     

    call AMSend.send(0xFFFF, msg, sizeof(P2MMsg));

    }

    }

    }

       }

    }

    event void AMControl.stopDone(error_t err)   { }

    }

    configuration P2MC {}

    #define AM_DATA_TYPE  123

    implementation

    {

    components P2MM as App;

    components MainC;

    App.Boot ->MainC;

    components ActiveMessageC as AM; //消息组件

    App.Packet -> AM.Packet;

       App.AMPacket -> AM.AMPacket;

       App.AMSend -> AM.AMSend[AM_DATA_TYPE];

    App.Receive -> AM.Receive[AM_DATA_TYPE];  

      App.AMControl -> AM.SplitControl;

      components new TimerMilliC () as Timer0;

    App.Timer0 ->Timer0;

    }

    COMPONENT= P2MC

    PFLAGS += -DUART_DEBUG

    PFLAGS += -DUART_BAUDRATE=9600

    include $(MAKERULES)

    好课程内容。

  • 相关阅读:
    剑指offer4:重建二叉树(后序遍历)
    剑指offer3:从尾到头打印链表每个节点的值
    剑指offer2:C++实现的替换空格(字符中的空格替换为“%20”)
    tp5系统变量输出(可以用来传递搜索的参数)
    Ajax实现文件上传的临时垃圾文件回收策略
    php获取当天的开始时间和结束时间
    Think PHP递归获取所有的子分类的ID (删除当前及子分类)
    tp查找某字段,排除某字段,不用一次写那么多
    git-查看历史版本及回滚版本
    dedecms目录结构,非常全
  • 原文地址:https://www.cnblogs.com/LeonNchu/p/10695795.html
Copyright © 2011-2022 走看看