开发一个新的应用,发送节点能够通过两个不同类型的消息分别控制接收节点中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;
}