temp

#ifndef BLINKTORADIO_H
#define BLINKTORADIO_H
#define MOTE_ID 1

enum {
    AM_BLINKTORADIO = 6,
    #if (MOTE_ID == 2)
    TIMER_PERIOD_MILLI = 250
    #elif (MOTE_ID == 3)
    TIMER_PERIOD_MILLI = 1500
    #endif
};

typedef nx_struct PayloadMsg {
    nx_uint16_t nodeId;
    nx_uint16_t counter;
} PayloadMsg, payloadMsg_t;

#endif

#include <Timer.h>
#include "BlinkToRadio.h"
#include "pr.h"
    
module BlinkToRadioC {
    uses interface Boot;

    #if (MOTE_ID == 1)
    uses interface Leds;
    #endif

    #if (MOTE_ID != 1)
    uses interface Timer<TMilli> as Timer0;
    #endif

    uses interface Packet;
    uses interface AMPacket;

    #if (MOTE_ID != 1)
    uses interface AMSend;
    #else
    uses interface Receive;
    #endif

    uses interface SplitControl as AMControl;
} implementation {
    message_t pkt;
    bool busy = FALSE;

    event void Boot.booted() {
        call AMControl.start();
    }

    event void AMControl.startDone(error_t err) {
        if (err == SUCCESS) {
            #if (MOTE_ID != 1)
            call Timer0.startPeriodic(TIMER_PERIOD_MILLI);
            #endif
            pr("start done\n");
        }
        else {
            call AMControl.start();
        }
    }

    event void AMControl.stopDone(error_t err) {
        pr("stop done\n");
    }

    #if (MOTE_ID != 1)
    event void Timer0.fired() {
        static uint16_t counter = 0;

        if (!busy) {
            PayloadMsg * pPM = 
                (PayloadMsg *)(call Packet.getPayload(&pkt, sizeof (PayloadMsg)));
            if (pPM == NULL) {
                pr("can not creatbtr\n");
                return;
            }
            pPM->nodeId = TOS_NODE_ID;
            pPM->counter = counter;
            if (call AMSend.send(1, &pkt, sizeof (PayloadMsg)) == SUCCESS) {
                pr("call send\n");
                busy = TRUE;
            }
        }
        counter++;
    }
    #endif

    #if (MOTE_ID != 1)                
    event void AMSend.sendDone(message_t * pMsg, error_t err) {
        if (&pkt == pMsg) {
            busy = FALSE;
        }
    }
    #endif

    #if (MOTE_ID == 1)
    event message_t * Receive.receive(message_t * pMsg, void * pPayload, uint8_t payloadLen) {
        static uint8_t lock2 = 1, lock3 = 1;    //state start with lock
        static uint16_t dataBuf = 0;

        pr("in receive\n");
        if (payloadLen == sizeof (PayloadMsg)) {    //payload has not missing
            am_addr_t srcAddr = call AMPacket.source(pMsg);
            PayloadMsg * pPM = (PayloadMsg *)pPayload;
            if (srcAddr == 2) {
                if (pPM->counter == 0) {
                    lock2 = !lock2;
                }
                if (lock2 == 0) {        //reset LEDBuff only when lock2 is unlocked
                    dataBuf = pPM->counter;
                    call Leds.set(dataBuf);    
                }
                pr("receive from nodeid: %d, %d\n", pPM->nodeId, pPM->counter);
            } else if (srcAddr == 3) {
                if (pPM->counter == 0) {
                    lock3 = !lock3;
                }
                if (lock3 == 0) {
                    if (pPM->counter & 1) {
                        call Leds.set(0);
                    } else {
                        call Leds.set(dataBuf);
                    } 
                }
                pr("receive from nodeid: %d, %d\n", pPM->nodeId, pPM->counter);                
            }
        }
        return pMsg;
    }
    #endif
}



#include <Timer.h>
#include "BlinkToRadio.h"

configuration BlinkToRadioAppC {

} implementation {
    components MainC;
    components LedsC;
    components BlinkToRadioC as App;

    #if (MOTE_ID != 1)
    components new TimerMilliC() as Timer0;
    #endif

    components ActiveMessageC as AM;

    #if (MOTE_ID != 1)
    components new AMSenderC(AM_BLINKTORADIO);
    #else
    components new AMReceiverC(AM_BLINKTORADIO);
    #endif

    App.Boot -> MainC;

    #if (MOTE_ID == 1)
    App.Leds -> LedsC;
    #endif

    #if (MOTE_ID != 1)
    App.Timer0 -> Timer0;
    #endif

    #if (MOTE_ID != 1)
    App.Packet -> AMSenderC;
    #endif

    App.AMPacket -> AM.AMPacket;
 
    App.AMControl -> AM.SplitControl;

    #if (MOTE_ID != 1)
    App.AMSend -> AMSenderC;
    #else
    App.Receive -> AMReceiverC;
    #endif
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值