CC2530平台上P2P通信的TinyOS编程

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


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值