BlinkToRadio.h
BlinkToRadioC.nc
Makefile
pr.h
#ifndef BLINKTORADIO_H
#define BLINKTORADIO_H
#define MOTE_ID 1 //only need to modify here
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
BlinkToRadioAppC.nc
#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
}
BlinkToRadioC.nc
#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 = 0, lock3 = 0; //state start with unlock; it doesn't support bit type
static uint8_t isFirstPkt2 = 1, isFirstPkt3 = 1; //special judge
static uint16_t dataBuf = 0;
pr("in receive\n");
if (payloadLen == sizeof (PayloadMsg)) { //payload is not missed
am_addr_t srcAddr = call AMPacket.source(pMsg);
PayloadMsg * pPM = (PayloadMsg *)pPayload;
if (srcAddr == 2) {
if (pPM->counter == 0 && isFirstPkt2 == 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 && isFirstPkt3 == 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);
}
isFirstPkt2 = 0;
isFirstPkt3 = 0;
}
return pMsg;
}
#endif
}
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)
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 */