#include "hal_esb.h"
#include "bsp_esb.h"
#include "bsp_time.h"
#include "main.h"
//********************* 选择串口 接口 **********************//
static unsigned char CheckLen(void)	//查看收到多少数据
{ 
	return ESB_CheckLen();
}

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

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

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

//********************* 接收 **********************//
#define ESB_RX_CMD_LEN 256
static ESB_Rx_t mESB_Rx[ESB_NUM_OF_R];
static unsigned char cmdDatBuf[ESB_RX_CMD_LEN]; //存放一条完整命令

int ESB_Rx_Regist(unsigned char cmd,ESB_Callback cb) //注册
{
	if(cmd>=ESB_NUM_OF_R) return -1;
	mESB_Rx[cmd].cb = cb;
	mESB_Rx[cmd].cmd = cmd;
	mESB_Rx[cmd].pDat = cmdDatBuf;
	return 0;
}

//********************* 接收协议 **********************//
//协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) 
static void Protocol(unsigned char len) //协议处理
{
	#if DEBUG_ESB
	DEBUG_LOG("Rx_ESB(%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>=ESB_NUM_OF_R) return;
	if(mESB_Rx[cmd].cb){
		for(int i=0;i<len-5;i++) mESB_Rx[cmd].pDat[i] = CheckByte(i+4);
		mESB_Rx[cmd].datLen = len-5;
		mESB_Rx[cmd].cb((ESB_Rx_t*)&mESB_Rx[cmd]);
	}
}

//协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) 
void ESB_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 ESB_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_ESB
	DEBUG_LOG("Tx_ESB:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
	#endif
	SendBuf(buf,L);   //压入发送缓存
}

static ESB_Tx_t mESB_Tx[ESB_NUM_OF_T];

void ESB_Tx_Process(void)
{
	static uint32_t tim=0;
	uint32_t temp = TIME_GetTicks()-tim;
	tim = TIME_GetTicks();
	
	for(int i=0;i<ESB_NUM_OF_T;i++){
		if(temp>=1){
			if(mESB_Tx[i].periodTimecnt>0){mESB_Tx[i].periodTimecnt--;
				if(mESB_Tx[i].nTimes==0&&mESB_Tx[i].periodTimecnt==0&&mESB_Tx[i].cb){
					mESB_Tx[i].cb((ESB_Tx_t*)&mESB_Tx[i]);
				}
			}
		}
		if(mESB_Tx[i].nTimes==1&&mESB_Tx[i].periodTimecnt==0){
			ESB_Send(i,mESB_Tx[i].pDat,mESB_Tx[i].datLen);
			mESB_Tx[i].periodTimecnt = mESB_Tx[i].waitTime;
			mESB_Tx[i].nTimes--;
		}else if(mESB_Tx[i].nTimes>1&&mESB_Tx[i].periodTimecnt==0){
			ESB_Send(i,mESB_Tx[i].pDat,mESB_Tx[i].datLen);
			mESB_Tx[i].periodTimecnt = mESB_Tx[i].periodTime;
			mESB_Tx[i].nTimes--;
		}
	}
}

int ESB_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>=ESB_NUM_OF_T) return -1;
	mESB_Tx[cmd].cmd = cmd;
	mESB_Tx[cmd].pDat = pDat;
	mESB_Tx[cmd].nTimes = nTimes;
	mESB_Tx[cmd].datLen = datLen;
	mESB_Tx[cmd].periodTime = periodTime;
	mESB_Tx[cmd].periodTimecnt = afterTime;
	mESB_Tx[cmd].waitTime = waitTime;
	return 0;
}

int ESB_Tx_Regist(unsigned char cmd,ESB_Callback timeout_cb)
{
	if(cmd>=ESB_NUM_OF_T) return -1;
	mESB_Tx[cmd].cb = timeout_cb;
	return 0;
}

void ESB_Tx_Clear(unsigned char cmd)
{
	if(cmd>=ESB_NUM_OF_T) return;
	mESB_Tx[cmd].nTimes = 0;
	mESB_Tx[cmd].periodTimecnt = 0;
}

void ESB_Initialize(void)
{
	ESB_Init();
	Process_Regist(ESB_Rx_Process);
	Process_Regist(ESB_Tx_Process);
}


