#include "hal_uart.h"
#include "bsp_uart.h"
#include "bsp_time.h"
#include "main.h"

//********************* 选择串口 接口 **********************//
static unsigned char CheckLen(void)	//查看收到多少数据
{ 
	return UART0_CheckLen();
}

static unsigned char CheckByte(int dex)	//查看数据
{ 
	return UART0_CheckByte(dex); 
}

static void Discard(unsigned int dex) //丢弃RX数据几位  
{ 
	UART0_Discard(dex);
}

static void SendBuf(unsigned char *p,int L) //发送
{ 
	UART0_SendBuff(p,L);
}

//********************* 接收 **********************//
#define UART0_RX_CMD_LEN 256
static UART0_Rx_t mUART0_Rx[UART0_NUM_OF_R];
static unsigned char cmdDatBuf[UART0_RX_CMD_LEN]; //存放一条完整命令

int UART0_Rx_Regist(unsigned char cmd,UART0_Callback cb) //注册
{
	if(cmd>=UART0_NUM_OF_R) return -1;
	mUART0_Rx[cmd].cb = cb;
	mUART0_Rx[cmd].cmd = cmd;
	mUART0_Rx[cmd].pDat = cmdDatBuf;
	return 0;
}

//********************* 接收协议 **********************//
//协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) 
static void Protocol(unsigned char len) //协议处理
{
	#if DEBUG_UART0
	DEBUG_LOG("Rx_UART0(%d):",CheckByte(1)); for(int i=0;i<len;i++){DEBUG_LOG("%02X ",CheckByte(i));} DEBUG_LOG("\r\n");
	#endif
	uint8_t cmd = CheckByte(3);
	if(len<5||cmd>=UART0_NUM_OF_R) return;
	if(mUART0_Rx[cmd].cb){
		for(int i=0;i<len-5;i++) mUART0_Rx[cmd].pDat[i] = CheckByte(i+4);
		mUART0_Rx[cmd].datLen = len-5;
		mUART0_Rx[cmd].cb((UART0_Rx_t*)&mUART0_Rx[cmd]);
	}
}

//协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) 
void UART0_Rx_Process(void)
{	
	static unsigned char R=0; 
    static unsigned char L=0; 

	//接收缓存有数据，就全部处理
    while(1){  
        switch( R ){  
        case  0:    if( CheckLen()<3 )return; else{  
                        if(CheckByte(0)!=0xAA){ Discard(1);}
                        else{unsigned char LF=CheckByte(2);LF=~LF; L=CheckByte(1); if((LF==L)&&(L>=5)){ R++;}else { Discard(1);}}
                    }   break;  
        // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验
        case  1:    if( CheckLen()<L) { return; }else{  //DEBUG_LOG("Rx:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",CheckByte(i));} DEBUG_LOG("\r\n");
						unsigned char i,ver=0;
						for(i=0;i<L-1;i++){  ver += CheckByte(i); }
                        if(CheckByte(L-1)==ver){ Protocol(L);Discard(L);//丢弃整条命令
                        } else Discard(1); 
						R=0;
					}	break;    
        default:    R=0;break; }  
    }
}

//********************* 发送协议 **********************//
//协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) 
void UART0_Send(unsigned char cmd,unsigned char *pDat,unsigned char datLen) //协议发送
{	
	unsigned char buf[255];
	unsigned char ver=0; 
	unsigned char i,L=0,Len=datLen+5;  
	
	buf[L]=0xAA;	ver+=buf[L++];		// 头
	buf[L]=Len;		ver+=buf[L++]; 		//1位长度
	buf[L]=~Len;	ver+=buf[L++];	 	// 1位长度反码
	buf[L]=cmd; 	ver+=buf[L++];    	//1命令
	for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
	buf[L++]=ver;      //效验
	#if DEBUG_UART0
	DEBUG_LOG("Tx_UART0:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
	#endif
	SendBuf(buf,L);   //压入发送缓存
}

static UART0_Tx_t mUART0_Tx[UART0_NUM_OF_T];

void UART0_Tx_Process(void)
{
	static uint32_t tim=0;
	uint32_t temp = TIME_GetTicks()-tim;
	tim = TIME_GetTicks();
	
	for(int i=0;i<UART0_NUM_OF_T;i++){
		if(temp>=1){
			if(mUART0_Tx[i].periodTimecnt>0){mUART0_Tx[i].periodTimecnt--;
				if(mUART0_Tx[i].nTimes==0&&mUART0_Tx[i].periodTimecnt==0&&mUART0_Tx[i].cb){
					mUART0_Tx[i].cb((UART0_Tx_t*)&mUART0_Tx[i]);
				}
			}
		}
		if(mUART0_Tx[i].nTimes==1&&mUART0_Tx[i].periodTimecnt==0){
			UART0_Send(i,mUART0_Tx[i].pDat,mUART0_Tx[i].datLen);
			mUART0_Tx[i].periodTimecnt = mUART0_Tx[i].waitTime;
			mUART0_Tx[i].nTimes--;
		}else if(mUART0_Tx[i].nTimes>1&&mUART0_Tx[i].periodTimecnt==0){
			UART0_Send(i,mUART0_Tx[i].pDat,mUART0_Tx[i].datLen);
			mUART0_Tx[i].periodTimecnt = mUART0_Tx[i].periodTime;
			mUART0_Tx[i].nTimes--;
		}
	}
}

int UART0_Tx_Send(unsigned char cmd,unsigned char *pDat,unsigned char datLen,uint8_t nTimes,uint32_t periodTime,uint32_t afterTime,uint32_t waitTime)
{
	if(cmd>=UART0_NUM_OF_T) return -1;
	mUART0_Tx[cmd].cmd = cmd;
	mUART0_Tx[cmd].pDat = pDat;
	mUART0_Tx[cmd].nTimes = nTimes;
	mUART0_Tx[cmd].datLen = datLen;
	mUART0_Tx[cmd].periodTime = periodTime;
	mUART0_Tx[cmd].periodTimecnt = afterTime;
	mUART0_Tx[cmd].waitTime = waitTime;
	return 0;
}

int UART0_Tx_Regist(unsigned char cmd,UART0_Callback timeout_cb)
{
	if(cmd>=UART0_NUM_OF_T) return -1;
	mUART0_Tx[cmd].cb = timeout_cb;
	return 0;
}

void UART0_Tx_Clear(unsigned char cmd)
{
	if(cmd>=UART0_NUM_OF_T) return;
	mUART0_Tx[cmd].nTimes = 0;
	mUART0_Tx[cmd].periodTimecnt = 0;
}

void UART0_Initialize(void)
{
	UART0_Init(115200);
	Process_Regist(UART0_Rx_Process);
	Process_Regist(UART0_Tx_Process);
}


