#ifndef __USARTBRIDGETERMINAL_H
#define __USARTBRIDGETERMINAL_H

#include "stm32g4xx_hal.h"
#include "terminal.h"


//Usart configuration
#define USART_BRIDGE_USART            USART3
#define USART_BRIDGE_SPEED            115200 
#define USART_BRIDGE_IRQn             USART3_IRQn
#define USART_BRIDGE_IRQHandler       USART3_IRQHandler
#define USART_BRIDGE_AF               GPIO_AF7_USART3
#define USART_BRIDGE_TX_PIN           GPIO_PIN_10
#define USART_BRIDGE_RX_PIN           GPIO_PIN_11
#define USART_BRIDGE_TX_PORT          GPIOB
#define USART_BRIDGE_RX_PORT          GPIOB
#define USART_BRIDGE_CLK_ENABLE       __USART3_CLK_ENABLE();
#define USART_BRIDGE_TX_PORT_CLK_ENABLE  __HAL_RCC_GPIOB_CLK_ENABLE();
#define USART_BRIDGE_RX_PORT_CLK_ENABLE  __HAL_RCC_GPIOB_CLK_ENABLE();


//-----------------------------------------------------------------------------

// Connect usartBridge objectt with ANSI C Interfaces

//-----------------------------------------------------------------------------


//Task for freertos function
void task_usartbridge(void *pvParameters);

//Ansi C interface for usartBridge object

void configure_usartbridge();
void processing_usartbridge();
void print_usartbridge(const char * str);
void vTaskUsartBridge(void *params);
void HAL_UART_RxCpltCallbackTerminal(void);
void HAL_UART_TxCpltCallbackTerminal(void);

//-----------------------------------------------------------------------------

// Usart bridge class for terminal

//-----------------------------------------------------------------------------

class UsartBridgeTerminal
{
public:

    UsartBridgeTerminal();
    ~UsartBridgeTerminal();

    void configure();
    void print(const char *str);

public:

    void recvByte();
    void sendByte(uint8_t byte);
    void clearTxNoEmpty();

private:

    void InitUsart();

public:
    
    UART_HandleTypeDef uart;
    char               m_uartByte;
    bool               m_state;
    
private:

    uint32_t m_sizeCache;
    uint32_t m_readIndex;
    bool m_isTxNoEmpty;
};


extern UsartBridgeTerminal terminalUsartBridge;


#endif // USARTBRIDGETERMINAL_H

