#include "mbed.h"

#define Ting_TxStart 	0
#define Ting_TxRun 		1
#define Ting_TxEnd 		2
#define Ting_RxStart 	3
#define Ting_RxRun 		4
#define Ting_RxEnd 		5
//---------------------------------------------------//
void Ting_Config(void);
void Ting_state(char *Ting_state);
//---------------------------------------------------//
char *USART_RX_BUF;								//接收缓冲
char State=0;											//状态标志
char RX_Data = 0;									//RX接收标志
char Ting_buf1[]="AT,OK";
char Ting_buf2[]="AT,SENDING";
char Ting_buf3[]="AT,SENDED";
char Ting_buf4[]="OK";
//---------------------------------------------------//
DigitalOut led1(LED1);
Serial pc(SERIAL_TX, SERIAL_RX);
//---------------------------------------------------//
int main() 
{
	Ting_Config();
  while (true) 
	{
		Ting_state(&State);
  }
}
//--------------------------------------------------------------------------------------------------------//        
void Ting_Config(void)                                            //LoRa初始化
{
	pc.printf("AT+RST\r\n");	   																		//复位 
	pc.scanf("%s",USART_RX_BUF);	
	if(strcmp((char*)USART_RX_BUF,(char*)Ting_buf1)==0)
	{
		led1 = !led1;
		wait(0.1);
	}
	pc.printf("AT+ADDR=0001\r\n");	 																//配置自身地址
	pc.scanf("%s",USART_RX_BUF);
	if(strcmp((char*)USART_RX_BUF,(char*)Ting_buf1)==0)
	{
		led1 = !led1;
		wait(0.1);
	}
	pc.printf("AT+DEST=0002\r\n");										 							//配置发送地址
	pc.scanf("%s",USART_RX_BUF);
	if(strcmp((char*)USART_RX_BUF,(char*)Ting_buf1)==0)
	{
		led1 = !led1;
		wait(0.1);
	}
	pc.printf("AT+CFG=433000000,20,6,10,1,1,0,0,0,0,3000,8,4\r\n");//配置参数
	pc.scanf("%s",USART_RX_BUF);
	if(strcmp((char*)USART_RX_BUF,(char*)Ting_buf4)==0)
	{
		led1 = !led1;
		wait(0.1);
	}			
}	 
//-------------------------------------------------------------------------------------------------------//
void Ting_state(char *Ting_state)																	//LoRa状态
{
	switch(*Ting_state)																							
	{
		case Ting_TxStart:																						//配置为发送状态
		{
			pc.printf("AT+SEND=08\r\n");    								 						//发送字节为8个字节		
			*Ting_state=Ting_TxRun;
			break;
		}
		case Ting_TxRun:																							//向LoRa载入数据
		{
			pc.scanf("%s",USART_RX_BUF);
			if(strcmp((char*)USART_RX_BUF,(char*)Ting_buf1)==0)
			{
				pc.printf("SendData");      					          					//发送数据为SendData
				*Ting_state=Ting_TxEnd;	
			}
			else
			{
				*Ting_state=Ting_TxStart;
			}
			break;
		}
		case Ting_TxEnd:																							//等待发送完成
		{
			pc.scanf("%s",USART_RX_BUF);
			if(strcmp((char*)USART_RX_BUF,(char*)Ting_buf2)==0)
			{
				pc.scanf("%s",USART_RX_BUF);
				if(strcmp((char*)USART_RX_BUF,(char*)Ting_buf3)==0)
					*Ting_state=Ting_TxStart;
			}					
			break;
		}		
		case Ting_RxStart:
		{
			pc.printf("AT+RX\r\n");    								 									//进入接收模式
			*Ting_state=Ting_RxRun;
			break;			
		}
		case Ting_RxRun:
		{
			pc.scanf("%s",USART_RX_BUF);
			{
				*Ting_state=Ting_RxEnd;	
				RX_Data = 1;																							//接收数据存入缓存，打开标志位
			}			
		}
		case Ting_RxEnd:
		{
			RX_Data = 0;
			//MCU发送给PC处理
			*Ting_state=Ting_TxStart;
		}
	}
}

