//
// Created by Mysteo on 14.07.2023.
//
extern "C" {
    #include "usart.h"
#include "tim.h"

}
#include "uart_bridge.hpp"

UartBridge* bridgePnt;





extern "C" void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){

    if (huart == bridgePnt->getHuart1())
    {
        std::string_view stringView{(const char *) bridgePnt->dataFromUart1, Size};
         bridgePnt->uart1Buf->enqueue(stringView);
    }
    else if (huart == bridgePnt->getHuart2())
    {
        std::string_view stringView{(const char *) bridgePnt->dataFromUart2, Size};
        bridgePnt->uart2Buf->enqueue(stringView);
        HAL_UART_Abort(bridgePnt->getHuart2());
        HAL_UARTEx_ReceiveToIdle_IT(bridgePnt->getHuart2(),(uint8_t*)bridgePnt->dataFromUart2, BUF_SIZE );
    }

}

UartBridge::UartBridge(bool isOn, USART_TypeDef *uart1, USART_TypeDef *uart2, uint16_t baudRate1,
                       uint16_t baudRate2) : turnOn(isOn),
                                                                                            uart1(uart1),
                                                                                            uart2(uart2)
{
    bridgePnt = this;
    UartBridge::uart1Buf = new Circular_Buffer<std::string_view>(MAX_QUEUE);
    UartBridge::uart2Buf = new Circular_Buffer<std::string_view>(MAX_QUEUE);
    uart1Handle.Instance = uart1;
    uart1Handle.Init.BaudRate = baudRate1;
    uart1Handle.Init.WordLength = UART_WORDLENGTH_8B;
    uart1Handle.Init.StopBits = UART_STOPBITS_1;
    uart1Handle.Init.Parity = UART_PARITY_NONE;
    uart1Handle.Init.Mode = UART_MODE_TX_RX;
    uart1Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    uart1Handle.Init.OverSampling = UART_OVERSAMPLING_16;
    uart1Handle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    uart1Handle.Init.ClockPrescaler = UART_PRESCALER_DIV1;
    uart1Handle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    uart2Handle = uart1Handle;
    uart2Handle.Instance = uart2;
    uart2Handle.Init.BaudRate = baudRate2;
}

bool UartBridge::isTurnOn() const {
    return turnOn;
}

void UartBridge::setTurnOn(bool isTurnOn) {
    UartBridge::turnOn = isTurnOn;
}

void UartBridge::uartInit(UART_HandleTypeDef *huart) {
    if (HAL_UART_Init(huart) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_SetTxFifoThreshold(huart, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_SetRxFifoThreshold(huart, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_DisableFifoMode(huart) != HAL_OK)
    {
        Error_Handler();
    }

}

UART_HandleTypeDef* UartBridge::getHuart1()  {
    return &uart1Handle;
}

UART_HandleTypeDef* UartBridge::getHuart2()  {
    return &uart2Handle;
}

void UartBridge::init(void) {
    uartInit(&uart1Handle);
    uartInit(&uart2Handle);

    HAL_UARTEx_ReceiveToIdle_IT(&uart1Handle,(uint8_t*)dataFromUart1, BUF_SIZE );
    HAL_UARTEx_ReceiveToIdle_IT(&uart2Handle,(uint8_t*)dataFromUart2, BUF_SIZE );
    __HAL_UART_CLEAR_FEFLAG(&uart1Handle);
    __HAL_UART_CLEAR_OREFLAG(&uart1Handle);
    __HAL_UART_CLEAR_NEFLAG(&uart1Handle);
    __HAL_UART_CLEAR_PEFLAG(&uart1Handle);
    __HAL_UART_CLEAR_FEFLAG(&uart2Handle);
    __HAL_UART_CLEAR_OREFLAG(&uart2Handle);
    __HAL_UART_CLEAR_NEFLAG(&uart2Handle);
    __HAL_UART_CLEAR_PEFLAG(&uart2Handle);
    __HAL_UART_DISABLE_IT(&uart1Handle, UART_IT_ORE);
    __HAL_UART_DISABLE_IT(&uart1Handle, UART_IT_PE);
    __HAL_UART_DISABLE_IT(&uart1Handle, UART_IT_FE);
    __HAL_UART_DISABLE_IT(&uart1Handle, UART_IT_NE);
    __HAL_UART_DISABLE_IT(&uart2Handle, UART_IT_ORE);
    __HAL_UART_DISABLE_IT(&uart2Handle, UART_IT_PE);
    __HAL_UART_DISABLE_IT(&uart2Handle, UART_IT_FE);
    __HAL_UART_DISABLE_IT(&uart2Handle, UART_IT_NE);
}



extern "C" void USART1_IRQHandler(void)
{
    /* USER CODE BEGIN USART1_IRQn 0 */

    /* USER CODE END USART1_IRQn 0 */
    HAL_UART_IRQHandler(bridgePnt->getHuart1());
    /* USER CODE BEGIN USART1_IRQn 1 */

    /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles USART2 global interrupt / USART2 wake-up interrupt through EXTI line 26.
  */
extern "C" void USART2_IRQHandler(void)
{
    /* USER CODE BEGIN USART2_IRQn 0 */

    /* USER CODE END USART2_IRQn 0 */
    HAL_UART_IRQHandler(bridgePnt->getHuart2());
    /* USER CODE BEGIN USART2_IRQn 1 */

    /* USER CODE END USART2_IRQn 1 */
}

