/*模块组件*/ #include "Timer.h" #include "RadioCountToLeds.h" #include "printf.h" module RadioCountToLedsC @safe() {//外部规范元素 uses { interface Leds; interface Boot; interface Receive; interface AMSend; interface Timer<TMilli> as MilliTimer; interface SplitControl as AMControl; interface Packet; } } implementation { message_t packet; bool locked = FALSE; uint16_t counter = 0; uint8_t k = 0; event void Boot.booted() {//系统成功启动时发出的信号,组件可以假定系统已经正常初始化。 call AMControl.start();//开启无线广播 } event void AMControl.startDone(error_t err) {//通知调用者组件已经启动,并准备好接收其他命令。 if (err == SUCCESS && TOS_NODE_ID == 0) { call MilliTimer.startPeriodic(6000);//设置一个周期定时器重复每dt时间单位。 } else { call AMControl.start();//系统成功启动时发出的信号。 } } event void AMControl.stopDone(error_t err) {//通知调用者组件已停止。 // do nothing } event void MilliTimer.fired() {//当计时器到期(一次性)或重复(周期性)时发出信号 counter = 88; dbg("RadioCountToLedsC", "RadioCountToLedsC: timer fired, counter is %hu.\n", counter); if (locked) { return; } else { radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t)); if (rcm == NULL) { return; } rcm->counter = counter; //if(TOS_NODE_ID == 0) //{ if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)) == SUCCESS) { dbg("RadioCountToLedsC", "RadioCountToLedsC: packet sent.\n", counter); locked = TRUE; } // } } } event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len) { /*接收一个包缓冲区,返回信令组件用于下一次接收的缓冲区。*/ dbg("RadioCountToLedsC", "Received packet of length %hhu.\n", len); if (len != sizeof(radio_count_msg_t)) {return bufPtr;} else { radio_count_msg_t* rcm = (radio_count_msg_t*)payload; if(k<=0){ radio_count_msg_t* wz= (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t)); wz->counter=rcm->counter; printf("data %u\n",wz->counter); printfflush(); call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)); /*发送一个数据有效负载为sizeof(radio_count_msg_t)的数据包到地址addr*/ k++; } if (rcm->counter & 0x1) { call Leds.led0On(); } else { call Leds.led0Off(); } if (rcm->counter & 0x2) { call Leds.led1On(); } else { call Leds.led1Off(); } if (rcm->counter & 0x4) { call Leds.led2On(); } else { call Leds.led2Off(); } return bufPtr; } } event void AMSend.sendDone(message_t* bufPtr, error_t error) { /*响应接受的发送请求时发出的信号*/ if (&packet == bufPtr) { locked = FALSE; } } } /*配置组件*/ #include "printf.h" #include "RadioCountToLeds.h" configuration RadioCountToLedsAppC {} implementation { //mian components MainC, LedsC; App.Leds -> LedsC; App.Boot -> MainC.Boot; //pirntf components PrintfC; components SerialStartC; //radio components ActiveMessageC;//命令包装器 /*该组件提供接口 interface AMPacket interface AMSend[am_id_t] interface LowPowerListening interface Packet interface PacketAcknowledgements interface PacketTimeStamp<T32khz, uint32_t> as PacketTimeStamp32khz interface PacketTimeStamp<TMilli, uint32_t> as PacketTimeStampMilli interface Receive[am_id_t] interface Receive as Snoop[am_id_t] interface SplitControl */ components RadioCountToLedsC as App; /*该组件使用组件 interface SplitControl as AMControl //AMControl为SplitControl的一个实例 interface AMSend interface Boot interface Leds interface Timer<TMilli> as MilliTimer interface Packet interface Receive */ App.Receive -> AMReceiverC; /*如果组件在传递的消息上调用send并返回它,那么缓冲区可能在发送发生之前被重用,覆盖组件的数据。这将导致尘埃可能转而发送它最近收到的数据包*/ App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Packet -> AMSenderC; components new AMReceiverC(AM_RADIO_COUNT_MSG); components new AMSenderC(AM_RADIO_COUNT_MSG); //timer components new TimerMilliC(); /*该组件提供接口 interface Timer<TMilli> */ App.MilliTimer -> TimerMilliC; } /*头文件*/ #ifndef RADIO_COUNT_TO_LEDS_H #define RADIO_COUNT_TO_LEDS_H typedef nx_struct radio_count_msg { nx_uint16_t counter; } radio_count_msg_t; enum { AM_RADIO_COUNT_MSG = 6, }; #endif /*makefile*/ COMPONENT=RadioCountToLedsAppC CFLAGS += -DCC2420_DEF_CHANNEL=14 CFLAGS += -I$(TOSDIR)/lib/printf include $(MAKERULES)