CC2530平臺上P2P通訊的TinyOS程式設計
阿新 • • 發佈:2018-12-23
開發一個新的應用,傳送節點能夠通過兩個不同型別的訊息分別控制接收節點中LED燈的開和關,並且用串列埠輸出兩個訊息到串列埠除錯助手。以下述順序完成這個新應用的開發。
首先實現週期性傳送訊息控制另一個節點上的LED燈;
然後在上述基礎上程式設計在串列埠除錯助手上輸出接收到的訊息 ;
增加按鍵功能,即能夠通過節點上的按鍵控制另個節點的LED燈。
第一問
模組主鍵
module TestP2PM { uses interface Boot; uses interface Leds; uses interface Timer<TMilli> as Timer0; uses interface SplitControl as AMControl; uses interface AMPacket; uses interface AMSend as AMSend1; uses interface AMSend as AMSend2; uses interface Receive as Receive1; uses interface Receive as Receive2; uses interface Packet; } implementation { #define destAddress 6 typedef nx_struct P2PMsg {nx_uint16_t nodeid; nx_uint16_t counter;}P2PMsg; uint16_t counter=0; bool busy =FALSE; bool flag = FALSE; message_t pkt; task void test() { } /********************************************** * 函式名:booted() * 功 能:系統啟動完畢後自動觸發 * 參 數:無 ***********************************************/ event void Boot.booted() { DbgOut(9,"BOOt"); call AMControl.start(); } /********************************************** * 函式名:fired() * 功 能:定時器觸發事件 * 參 數:無 ***********************************************/ event void Timer0.fired() { if (!busy) { P2PMsg* btrpkt = (P2PMsg*)(call Packet.getPayload(&pkt, sizeof(P2PMsg))); flag = !flag; btrpkt->nodeid = TOS_NODE_ID; if(flag) btrpkt->counter = 0x01; else btrpkt->counter = 0x00; call AMPacket.setGroup(&pkt,TOS_IEEE_GROUP); if (flag && call AMSend1.send(destAddress, &pkt, sizeof(P2PMsg)) == SUCCESS) { busy = TRUE; } else if(call AMSend2.send(destAddress, &pkt, sizeof(P2PMsg)) == SUCCESS){ busy = TRUE; } } } /********************************************** * 函式名:startDone(error_t err) * 功 能:SplitControl控制啟動完成事件 * 參 數:error_t err ***********************************************/ event void AMControl.startDone(error_t err) { if(err==SUCCESS) call Timer0.startPeriodic(1000); else call AMControl.start(); } /********************************************** * 函式名:sendDone(message_t* msg, error_t erro) * 功 能:AM訊息傳送完畢事件 * 參 數:message_t* msg, error_t erro ***********************************************/ event void AMSend1.sendDone(message_t* msg, error_t erro) { if (&pkt == msg) busy = FALSE; } event void AMSend2.sendDone(message_t* msg, error_t erro) { if (&pkt == msg) busy = FALSE; } /********************************************** * 函式名:receive(message_t* msg, void* payload, uint8_t len) * 功 能:Receive接收事件 * 參 數:message_t* msg, void* payload, uint8_t len ***********************************************/ event message_t* Receive1.receive(message_t* msg, void* payload, uint8_t len) { if (len == sizeof(P2PMsg)) { P2PMsg* btrpkt = (P2PMsg*)payload; DbgOut(9,"Receive Id is %d,Data is %d,Length is %d\r\n",(uint16_t)btrpkt->nodeid,(uint16_t)btrpkt->counter,len); call Leds.set(btrpkt->counter); } return msg; } event message_t* Receive2.receive(message_t* msg, void* payload, uint8_t len) { if (len == sizeof(P2PMsg)) { P2PMsg* btrpkt = (P2PMsg*)payload; DbgOut(9,"Receive Id is %d,Data is %d,Length is %d\r\n",(uint16_t)btrpkt->nodeid,(uint16_t)btrpkt->counter,len); call Leds.set(btrpkt->counter); } return msg; } event void AMControl.stopDone(error_t err) { } }
配置主鍵
configuration TestP2PC {} #define AM_DATA_TYPE1 6 #define AM_DATA_TYPE2 123 implementation { components MainC,LedsC; components TestP2PM as App; components ActiveMessageC as AM; //訊息元件 components new TimerMilliC () as Timer0; App.Boot ->MainC; App.Leds ->LedsC; App.Timer0 ->Timer0; App.Packet -> AM.Packet; App.AMPacket -> AM.AMPacket; App.AMSend1 -> AM.AMSend[AM_DATA_TYPE1]; App.Receive1 -> AM.Receive[AM_DATA_TYPE1]; App.AMSend2 -> AM.AMSend[AM_DATA_TYPE2]; App.Receive2 -> AM.Receive[AM_DATA_TYPE2]; App.AMControl -> AM.SplitControl; }
第二問
模組元件
//56 65 module TestP2PM { uses interface Boot; uses interface Leds; uses interface GeneralIO as Led0; uses interface GeneralIO as Key1; //按鍵 uses interface GeneralIO as Key2; uses interface Timer<TMilli> as Timer0; uses interface SplitControl as AMControl; uses interface AMPacket; uses interface AMSend as AMSend1; uses interface AMSend as AMSend2; uses interface Receive as Receive1; uses interface Receive as Receive2; uses interface Packet; } implementation { #define destAddress 5 //GRP=01 NID=06 //#define destAddress 6 //GRP=01 NID=05 typedef nx_struct P2PMsg {nx_uint16_t nodeid; nx_uint16_t counter;}P2PMsg; uint16_t counter=0; bool busy =FALSE; message_t pkt; uint8_t Value1, Value2; //鍵值變數 task void test() { } event void Boot.booted() { call Key1.makeInput(); //設定為輸入 call Key2.makeInput(); //設定為輸入 call Led0.makeOutput(); //設定為輸出 call Led0.clr(); Value1=1; Value2=1; DbgOut(9,"BOOt"); //串列埠輸出 call AMControl.start(); //啟動射頻 } //定時器, event void Timer0.fired() { Value1=call Key1.get();//獲取鍵值 Value2=call Key2.get(); if(Value1==0) { counter = 1; if (!busy) //判斷訊息是否傳送成功 { //獲取載荷結構體 P2PMsg* btrpkt = (P2PMsg*)(call Packet.getPayload(&pkt, sizeof(P2PMsg))); btrpkt->nodeid = TOS_NODE_ID; //資訊ID btrpkt->counter = counter; //資訊內容 call AMPacket.setGroup(&pkt,TOS_IEEE_GROUP); //設定組號 if (call AMSend1.send(destAddress, &pkt, sizeof(P2PMsg)) == SUCCESS) //傳送 { busy = TRUE; } } } if(Value2==0) { counter = 0; if (!busy) //判斷訊息是否傳送成功 { //獲取載荷結構體 P2PMsg* btrpkt = (P2PMsg*)(call Packet.getPayload(&pkt, sizeof(P2PMsg))); btrpkt->nodeid = TOS_NODE_ID; //資訊ID btrpkt->counter = counter; //資訊內容 call AMPacket.setGroup(&pkt,TOS_IEEE_GROUP); //設定組號 if (call AMSend2.send(destAddress, &pkt, sizeof(P2PMsg)) == SUCCESS) //傳送 { busy = TRUE; } } } } //射頻啟動觸發事件 event void AMControl.startDone(error_t err) { if(err==SUCCESS) call Timer0.startPeriodic(10); //開啟定時 10ms else call AMControl.start(); } //AM訊息傳送完畢事件 event void AMSend1.sendDone(message_t* msg, error_t erro) { if (&pkt == msg) busy = FALSE; //清除 } //AM訊息傳送完畢事件 event void AMSend2.sendDone(message_t* msg, error_t erro) { if (&pkt == msg) busy = FALSE; //清除 } //Receive1接收事件 event message_t* Receive1.receive(message_t* msg, void* payload, uint8_t len) { if (len == sizeof(P2PMsg)) { P2PMsg* btrpkt = (P2PMsg*)payload; //獲取射頻載荷 DbgOut(9,"Receive Id is %d,Data is %d,Length is %d\r\n",(uint16_t)btrpkt->nodeid,(uint16_t)btrpkt->counter,len); //設定LED燈 call Led0.set(); } return msg; } //Receive2接收事件 event message_t* Receive2.receive(message_t* msg, void* payload, uint8_t len) { if (len == sizeof(P2PMsg)) { P2PMsg* btrpkt = (P2PMsg*)payload; //獲取射頻載荷 DbgOut(9,"Receive Id is %d,Data is %d,Length is %d\r\n",(uint16_t)btrpkt->nodeid,(uint16_t)btrpkt->counter,len); //設定LED燈 call Led0.clr(); } return msg; } event void AMControl.stopDone(error_t err) { } }
配置主鍵
configuration TestP2PC {}
#define AM_DATA_TYPE1 6
#define AM_DATA_TYPE2 123 //AM訊息型別
implementation
{
components MainC,LedsC;
components TestP2PM as App;
components ActiveMessageC as AM; //訊息元件
components new TimerMilliC () as Timer0;
components HplCC2530GeneralIOC as GPIO;
App.Key1->GPIO.P0_Port[4]; //key1
App.Key2->GPIO.P0_Port[5];
App.Led0->GPIO.P1_Port[0];
App.Boot ->MainC;
App.Leds ->LedsC;
App.Timer0 ->Timer0;
App.Packet -> AM.Packet;
App.AMPacket -> AM.AMPacket;
App.AMSend1 -> AM.AMSend[AM_DATA_TYPE1];
App.AMSend2 -> AM.AMSend[AM_DATA_TYPE2];
App.Receive1 -> AM.Receive[AM_DATA_TYPE1];
App.Receive2 -> AM.Receive[AM_DATA_TYPE2];
App.AMControl -> AM.SplitControl;
}