/*
 * uart.c
 *
 *  Created on: 2019. 5. 27.
 *      Author: parkyj
 */

#include "main.h"

UARTQUEUE TerminalQueue;
UARTQUEUE UbxQueue;

void InitUartQueue(pUARTQUEUE pQueue)
{
    pQueue->data = pQueue->head = pQueue->tail = 0;
}

#if 0 // PYJ.2019.06.20_BEGIN -- 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    pUARTQUEUE pQueue;
    pQueue = (huart->Instance == USART1 ? &TerminalQueue:&UbxQueue);
    pQueue->head++;
    if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
    pQueue->data++;
    if (pQueue->data >= QUEUE_BUFFER_LENGTH)
        GetDataFromUartQueue(huart);
    HAL_UART_Receive_IT(huart, pQueue->Buffer + pQueue->head, 1);
}
#endif // PYJ.2019.06.20_END -- 
void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
{
    pUARTQUEUE pQueue = (huart->Instance == USART1 ? &TerminalQueue:&UbxQueue);
    if (pQueue->data >= QUEUE_BUFFER_LENGTH)
        GetDataFromUartQueue(huart);
    pQueue->Buffer[pQueue->head++] = data;
    if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
    pQueue->data++;
}
void GetDataFromUartQueue(UART_HandleTypeDef *huart)
{
//    UART_HandleTypeDef *dst = (huart->Instance == USART1 ? &hTpb22:&hTerminal);
//    pUARTQUEUE pQueue = (huart->Instance == USART6 ? &TerminalQueue:&UbxQueue);
     pUARTQUEUE pQueue = (huart->Instance == USART1 ? &TerminalQueue:&UbxQueue);

    if(huart->Instance == USART1)
   {
     printf("%c",*(pQueue->Buffer + pQueue->tail));
//        printf("%c",*(pQueue->Buffer + pQueue->tail));
    
    } else{
//        for(uint8_t i = 0; i < 4; i++)
//            HAL_UART_Transmit(&hTerminal, buf[i], 1, 3000);
//    	HAL_UART_Transmit(&hTerminal, pQueue->Buffer + pQueue->tail, 1, 3000);
//          printf("%c",*(pQueue->Buffer + pQueue->tail));
//          printf("%c",*(pQueue->Buffer + pQueue->tail));
    }
    pQueue->tail++;
    if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
    pQueue->data--;
//    if(pQueue->data == 0){
//        if(huart->Instance == USART1)
//            printf("Usart1\r\n");
//        else
//            printf("Usart6\r\n");            
//    }
    HAL_Delay(1);
}
