TinyOS nesC example 3

pr.h

#ifndef MY_PR_H
#define MY_PR_H

#ifdef ENABLE_PR


#ifndef TOSSIM
#include "printf.h"

#define pr(fmt, args...) do { printf(fmt, ##args); printfflush(); } while (0)
#else

#define pr(fmt, args...) dbg("Sim", fmt, ##args)

#endif // TOSSIM

#else

#define pr(fmt, args...) 

#endif // ENABLE_PR


#endif /* PR_H */

BlinkToRadio.h
#ifndef BLINKTORADIO_H
#define BLINKTORADIO_H

enum {
    AM_BLINKTORADIO = 6,
    TIMER_PERIOD_MILLI = 250
};

typedef nx_struct BlinkToRadioMsg {
    nx_uint16_t nodeid;
    nx_uint16_t counter;
} BlinkToRadioMsg;

#endif
BlinkToRadioC.nc
/**
 * Implementation of the BlinkToRadio application.  A counter is
 * incremented and a radio message is sent whenever a timer fires.
 * Whenever a radio message is received, the three least significant
 * bits of the counter in the message payload are displayed on the
 * LEDs.  Program two motes with this application.  As long as they
 * are both within range of each other, the LEDs on both will keep
 * changing.  If the LEDs on one (or both) of the nodes stops changing
 * and hold steady, then that node is no longer receiving any messages
 * from the other node.
 */
#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;

    void setLeds(uint16_t val) {
        if (val & 0x01)
            call Leds.led0On();
        else 
            call Leds.led0Off();
        if (val & 0x02)
            call Leds.led1On();
        else
            call Leds.led1Off();
        if (val & 0x04)
            call Leds.led2On();
        else
            call Leds.led2Off();
    }

    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) {
            BlinkToRadioMsg * btrpkt = 
	    (BlinkToRadioMsg *)(call Packet.getPayload(&pkt, sizeof (BlinkToRadioMsg)));
            if (btrpkt == NULL) {
	        pr("can not creatbtr\n");
	        return;
            }
            btrpkt->nodeid = TOS_NODE_ID;
            btrpkt->counter = counter;
            if (call AMSend.send(3, &pkt, sizeof (BlinkToRadioMsg)) == SUCCESS) {
	        pr("call send\n");
                busy = TRUE;
            }
        }
    }
						
    event void AMSend.sendDone(message_t * msg, error_t err) {
        if (&pkt == msg) {
            busy = FALSE;
        }
    }

    event message_t * Receive.receive(message_t * msg, void * payload, uint8_t len) {
	pr("in receive\n");
	if (len == sizeof (BlinkToRadioMsg)) {
            BlinkToRadioMsg * btrpkt = (BlinkToRadioMsg *)payload;
            setLeds(btrpkt->counter);
	    pr("receive from nodeid:%d,%d\n", btrpkt->nodeid, btrpkt->counter);
        }
        return msg;
    }
}

BlinkToRadioAppC.nc
/**
 * Application file for the BlinkToRadio application.  A counter is
 * incremented and a radio message is sent whenever a timer fires.
 * Whenever a radio message is received, the three least significant
 * bits of the counter in the message payload are displayed on the
 * LEDs.  Program two motes with this application.  As long as they
 * are both within range of each other, the LEDs on both will keep
 * changing.  If the LEDs on one (or both) of the nodes stops changing
 * and hold steady, then that node is no longer receiving any messages
 * from the other node.
 */
#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 example 3)