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
}


你可能感兴趣的:(temp)