#include "UART.h"
#include <string.h>
#include "oled.h"
#include <stdio.h>

// 用于存储接收数据的数组
unsigned char rxBuffer[ARRAY_SIZE];
unsigned int rxIndex = 0;  // 接收数据的索引
unsigned int rx_flag=0;
extern unsigned int  CONNECT_FLEG;//连接标志
// 初始化串口
void Serial_Init() {
    SCON = 0x50;  // 设置为模式1，8位数据，可变波特率
    TMOD &= 0x0F;  // 清除定时器1模式位
    TMOD |= 0x20;  // 设置定时器1为8位自动重装模式
    TH1 = TL1 = 256 - (11059200 / 12 / 32) / BAUDRATE;  // 设置波特率
    TR1 = 1;       // 启动定时器1
    ES = 1;        // 开启串口中断
    EA = 1;        // 开启全局中断
}

void Delay(unsigned int xms)
{
	unsigned char i, j;
	while(xms--)
	{
		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}

void buff_memset(void)
{
	memset(rxBuffer,0,64);
	rxIndex=0;
	rx_flag = 0;
}
unsigned int  WIFI_CheckAck(char* src, char* dest, int timeout)
{
	char *check = NULL;
	//清空缓冲
	 buff_memset();
	
	 UART_sentString(src);
	 timeout=timeout/10;//10ms处理一次
	 while(timeout--)
	 {
		Delay(10);//延时1ms
		if(rx_flag==1)
		 {
			check = strstr(rxBuffer,dest);
			if(check != NULL)
			{
				buff_memset();//数据有误 清空		
				return 1; 
			}
		 }
	 }
	 buff_memset();//数据有误 清空	
	 return 0;//超时	
}

sbit LED = P2^5;
sbit motor1 = P2^6;
sbit motor2 = P2^7;

void CONNECT_TX_Control(void)
{
	char *ptr = NULL;
	int number;
	if (CONNECT_FLEG) 
	{
		if(rx_flag)
		{
			if(strstr(rxBuffer,"192.168.0")!=NULL)
			{
				ptr = strrchr((char*)rxBuffer,',');           // 以最后一个,为准后面为数据
				
				if(strlen(ptr)-3==1)                      //判断接收到为一位数据   ,*\r\n
						{
							number = ptr[1]-'0';
							switch(number)
							{
							 case 1: LED = 0 ; break;
							 case 2: LED = 1; break;
							 case 3:OLED_Show(); break;
							 case 4:OLED_Clear(); break;
							 case 5:motor1=0;motor2=1; break;
							 case 6:motor1=1;motor2=0; break;
							 case 7:motor1=0;motor2=0 ; break;
							 default:  return;
							}
						}							
			}
			else if(strstr((char *)rxBuffer,"RECV:test-app")!=NULL)
			{
				ptr = strrchr((char*)rxBuffer,',');           // 以最后一个,为准后面为数据
				
				if(strlen(ptr)-3==1)                      //判断接收到为一位数据   ,*\r\n
						{
							number = ptr[1]-'0';
							switch(number)
							{
							 case 1: LED = 0 ; break;
							 case 2: LED = 1; break;
							 case 3:OLED_Show(); break;
							 case 4:OLED_Clear(); break;
							 case 5:motor1=0;motor2=1; break;
							 case 6:motor1=1;motor2=0; break;
							 case 7:motor1=0;motor2=0 ; break;
							 default:  return;
							}
						}
			}
		}
		buff_memset();//数据有误 清空	
	}
}
void CONNECT_TO_TCP(unsigned char* id,unsigned char *password)
{
	unsigned char buf[50];
	WIFI_CheckAck("+++",id,1500); //确保其退出透传
	//while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); 
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));
	while(!WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1000)!=0);
	while(!WIFI_CheckAck("AT+CIPSTART=TCP,192.168.0.223,2347\r\n","+CIPSTART:1",3000));
	while(!WIFI_CheckAck("AT+CIPSEND\r\n","OK",1000));
	UART_sentString(":Connected\r\n");
	CONNECT_FLEG = 1;
}
void CONNECT_TO_UDP(unsigned char* id,unsigned char *password)
{
	unsigned char buf[50];

	WIFI_CheckAck("+++",id,1500); //确保其退出透传
	//while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); 
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));
	while(!WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1000)!=0);
	while(!WIFI_CheckAck("AT+CIPSTART=UDP,192.168.0.146,2345,1112,0\r\n","+CIPSTART:1",3000)); //192.168.0.150,2345为IP地址 2345 端口号1112 是模块设置的端口号 0 UDP固定目标模式 
	while(!WIFI_CheckAck("AT+CIPSEND\r\n","OK",1000));
	UART_sentString(":Connected\r\n");
	CONNECT_FLEG = 1;
}
void CONNECT_TO_MQTT(unsigned char* id,unsigned char *password)
{
	unsigned char buf[50];
	while(!WIFI_CheckAck("AT+MQTTCLEAN\r\n","OK",1000));
	//while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); //STA模式
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));//连接WIFI
	while(!WIFI_CheckAck("AT+MQTTLONGCLIENTID=WF-TEST2\r\n","OK",1000)!=0);//配置 MQTT 客户端所需的客户端 ID、用户名和密码
	while(!WIFI_CheckAck("AT+MQTTCONN=broker.emqx.io,1883,0\r\n","MQTTCONNECTED:",2000)!=0);//连接 MQTT 服务器
	while(!WIFI_CheckAck("AT+MQTTSUB=test-app,0\r\n","OK",2000)!=0);//订阅主题 test-app
	while(!WIFI_CheckAck("AT+MQTTPUBRAW=test-w,32,0,0\r\n","OK",2000)!=0);
	UART_sentString("MQTT_Connected\r\n");
	buff_memset();
	CONNECT_FLEG = 1;
}

void UART_SendByte(unsigned char Byte)
{
	SBUF=Byte;
	while(!TI);
	TI=0;
}

void UART_sentString(char *str)
{
	 
	 while(*str){
		 UART_SendByte(*str);
		 str++;
	 }
}
// 串口接收中断服务程序
void Serial_ISR() interrupt 4 {
    if (RI) {
        RI = 0;  // 清除接收中断标志
		
		
		rxBuffer[rxIndex++] = SBUF;  // 存储接收到的数据
        if(rxBuffer[rxIndex-1]=='\n')
		{
			rx_flag=1;
		}
		if(rxIndex==64)
		{
			rxIndex=0;
			rx_flag=1;
		}
		
    }
}




