TinyOS nesC Sample of BlinkToRadio

BlinkToRadio.h
#ifndef BLINKTORADIO_H
#define BLINKTORADIO_H

enum {
    AM_BLINKTORADIO = 6,
    TIMER_PERIOD_MILLI = 250
};

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

#endif

BlinkToRadioC.nc

#include <Timer.h>
#include "BlinkToRadio.h"
#include "pr.h"
	
module BlinkToRadioC {
    uses interface Boot;
    uses interface Leds;
    uses interface Timer<TMilli> as Timer0;
    uses interface Packet;
    uses interface AMSend;
    uses interface Receive;
    uses interface SplitControl as AMControl;
} implementation {
    uint16_t counter;
    message_t pkt;
    bool busy = FALSE;

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

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

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

    event void Timer0.fired() {
        counter++;
        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(3, &pkt, sizeof (PayloadMsg)) == SUCCESS) {
	        	pr("call send\n");
                busy = TRUE;
            }
        }
    }
						
    event void AMSend.sendDone(message_t * pMsg, error_t err) {
        if (&pkt == pMsg) {
            busy = FALSE;
        }
    }

    event message_t * Receive.receive(message_t * pMsg, void * pPayload, uint8_t payloadLen) {
		pr("in receive\n");
		if (payloadLen == sizeof (PayloadMsg)) {
            PayloadMsg * pPM = (PayloadMsg *)pPayload;
            call Leds.set(pPM->counter);
	    	pr("receive from nodeid:%d,%d\n", pPM->nodeid, pPM->counter);
        }
        return pMsg;
    }
}


BlinkToRadioAppC.nc

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

configuration BlinkToRadioAppC {

} implementation {
    components MainC;
    components LedsC;
    components BlinkToRadioC as App;
    components new TimerMilliC() as Timer0;
    components ActiveMessageC;
    components new AMSenderC(AM_BLINKTORADIO);
    components new AMReceiverC(AM_BLINKTORADIO);

    App.Boot -> MainC;
    App.Leds -> LedsC;
    App.Timer0 -> Timer0;
    App.Packet -> AMSenderC;
 
    App.AMControl -> ActiveMessageC;
    App.AMSend -> AMSenderC;
    App.Receive -> AMReceiverC;
}

Makefile
COMPONENT=BlinkToRadioAppC
CFLAGS += -DCC2420_DEF_CHANNEL=13
CFLAGS += -DCC2420_DEF_RFPOWER=5
CFLAGS += -DENABLE_PR
CFLAGS += -I$(TOSDIR)/lib/printf

CFLAGS += -DTOSH_DATA_LENGTH=128
include $(MAKERULES)



你可能感兴趣的:(TinyOS nesC Sample of BlinkToRadio)