//
// Created by Mysteo on 14.07.2023.
//
extern "C" {
    #include "usart.h"
}
#include "uart_bridge.hpp"

UartBridge* bridgePnt;


uint8_t data[2];



extern "C" void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){

    if (huart == bridgePnt->getHuart1())
    {
        bridgePnt->uart1Buf->enqueue(bridgePnt->dataFromUart1);
        HAL_UART_Receive_IT(bridgePnt->getHuart1(), (uint8_t*)&bridgePnt->dataFromUart1, 1);
    }
    else if (huart == bridgePnt->getHuart2())
    {
        bridgePnt->uart2Buf->enqueue(bridgePnt->dataFromUart2);
        HAL_UART_Receive_IT(bridgePnt->getHuart2(), (uint8_t*)&bridgePnt->dataFromUart2, 1);
    }

}

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<uint8_t>(1024);
    UartBridge::uart2Buf = new Circular_Buffer<uint8_t>(1024);
    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_UART_Receive_IT(&uart1Handle, (uint8_t*)&dataFromUart1, 1);
    HAL_UART_Receive_IT(&uart2Handle, (uint8_t*)&dataFromUart2, 1);
}



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 */
}
