#include "Uart.h"

void UartDataRecvSet(uint8_t val);
uint8_t UartDataRecvGet(void);
void Uart2_Data_Send(uint8_t* data,uint8_t size);
void Uart1_Data_Send(uint8_t* data,uint8_t size);
int _write (int file, uint8_t *ptr, uint16_t len);
void Uart_dataCheck(uint8_t Usart_Num ,uint8_t* cnt);
void QueueCheck(uint8_t Usart_Num,uint8_t* header,uint8_t* tail);
void UartDataBufferCheck(void);




uint8_t UartDataisReved;
uint8_t rx1_data[1];
uint8_t rx2_data[1];

uint8_t count_in1 = 0, count_out1 = 0;
uint8_t count_in2 = 0, count_out2 = 0;

uint8_t buf[USART_CNT][buf_size] = {0,};
uint8_t Uart_RxData[USART_CNT][buf_size] = {0,};
uint8_t Uart_Rxcnt = 0;
typedef struct{
    uint8_t Rx_Header;
    uint8_t Rx_Tail;
    uint8_t buf[USART_CNT][buf_size];
}BlueUsart_t;
    
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1)
    {
        buf[USART1_CNT][count_in1] = rx1_data[0];
        if(++count_in1>=100){ count_in1 = 0; }
        HAL_UART_Receive_IT(&huart1,&rx1_data[0],1);
    }
    if(huart->Instance == USART2) 
    {
        buf[USART2_CNT][count_in2] = rx2_data[0];
        if(++count_in2>=100){ count_in2 = 0; }
        HAL_UART_Receive_IT(&huart2,&rx2_data[0],1);
    }
    
}
void QueueCheck(uint8_t Usart_Num,uint8_t* header,uint8_t* tail){

     if(*tail != *header){ 
        Uart_RxData[Usart_Num][Uart_Rxcnt++] = buf[Usart_Num][(*tail)++];
        printf("%02x ",Uart_RxData[Usart_Num][Uart_Rxcnt - 1]);
        if(*tail>= 100){ *tail = 0; }
        UartTimerCnt = 0;
        UartDataRecvSet(Usart_Num + 1);
    }
}
void UartDataBufferCheck(void){
    QueueCheck(USART1_CNT,&count_in1,&count_out1);
    QueueCheck(USART2_CNT,&count_in2,&count_out2);
}
void UartDataRecvSet(uint8_t val){
    UartDataisReved = val;
}
uint8_t UartDataRecvGet(void){
    return UartDataisReved;
}


void Uart2_Data_Send(uint8_t* data,uint8_t size){
    HAL_UART_Transmit(&huart2, data,size, 10); 
}
void Uart1_Data_Send(uint8_t* data,uint8_t size){
    HAL_UART_Transmit(&huart1, data,size, 10); 
}

int _write (int file, uint8_t *ptr, uint16_t len) 
{
    HAL_UART_Transmit (&huart1, ptr, len, 10); 
    return len;
}
void Uart_dataCheck(uint8_t Usart_Num ,uint8_t* cnt){

    etError crccheck = 0;
#if 0
    for(uint8_t i = 0; i < (* cnt); i++){
        printf("%02x ",buf[i]);
    }
        printf("\r\n");
#endif  

    crccheck = STH30_CheckCrc(&Uart_RxData[Usart_Num][blucell_type],Uart_RxData[Usart_Num][blucell_length],Uart_RxData[Usart_Num][Uart_RxData[Usart_Num][blucell_length] + 1]);
    if(crccheck == CHECKSUM_ERROR){
        for(uint8_t i = 0; i < (*cnt); i++){
            printf("%02x ",Uart_RxData[Usart_Num][i]);
        }
     //   printf("Original CRC : %02x RecvCRC : %02x \r\n",crccheck,Uart_RxData[Usart_Num][Uart_RxData[Usart_Num][blucell_length] + 1]);
    }
    else if(crccheck == NO_ERROR){
        RGB_Controller_Func(&Uart_RxData[Usart_Num][blucell_stx]);\
        UartDataRecvSet(0);
    }
    else{
         printf("What Happen?\r\n");
        /*NOP*/
    }

    *cnt = 0;
    
    memset(Uart_RxData[Usart_Num],0x00,buf_size);
}


