/*
 * uart_dev.c
 *
 *  Created on: 2024年6月22日
 *      Author: psx
 */

#include "uart_dev.h"
#include "inflash.h"

/* 使能485发送 */
#define rs485_send_enable 1

static void uart_init(uartIndex_e uart_index, int baud);
static uint8_t uart_putchar(device_handle device, char ch);

device_handle g_bat485_uart3_handle;
device_handle g_gw485_uart4_handle;

static uint8_t bat485_in_buff[200];
static uint8_t gw485_in_buff[300];

uint8_t rs485_out_buff[100];


/**
  * @brief  串口信息初始化，串口号及波特率.
  * @param  uart_index    对应的硬件串口号
  * @param  uart_baudrate 波特率
  */
uart_device_info uart_devices[]={
  [0] = {
      .init          = 0,
      .uart_index    = BAT485_UART_INDEX,
      .uart_baudrate = 9600,
  },
  [1] = {
      .init          = 0,
      .uart_index    = GW485_UART_INDEX,
      .uart_baudrate = 9600,
  },
};

/**
  * @brief  初始化串口设备.
  * @param  uart_index 初始化串口号
  * @param  buff       串口循环buff地址
  * @param  buff_size  串口循环buff对应大小
  * @retval 串口句柄
  */
//#define ELEMENT_OF(x) (sizeof(x) / sizeof((x)[0]))
//device_handle uart_dev_init(uartIndex_e uart_index, uint8_t *buff, int buff_size)
//{
//  int i = 0;
//  for(; i < ELEMENT_OF(uart_devices); i++){
//    if(uart_devices[i].uart_index == uart_index){
//      if(!uart_devices[i].init){
//        InitRingQueue(&uart_devices[i].uart_ring_queue, buff, buff_size);
//        uart_init(uart_index, uart_devices[i].uart_baudrate);
//
//        uart_devices[i].init = 1;
//      }
//      return (device_handle)(&uart_devices[i]);
//    }
//  }
//  return 0;
//}
device_handle uart_dev_init(void)
{
//    int i = 0;
//    for(; i < ELEMENT_OF(uart_devices); i++){
//      if(uart_devices[i].uart_index == uart_index){
//        if(!uart_devices[i].init){
//          InitRingQueue(&uart_devices[i].uart_ring_queue, buff, buff_size);
//          uart_init(uart_index, uart_devices[i].uart_baudrate);
//
//          uart_devices[i].init = 1;
//        }
//        return (device_handle)(&uart_devices[i]);
//      }
//    }
    InitRingQueue(&uart_devices[0].uart_ring_queue, bat485_in_buff, sizeof(bat485_in_buff));
    uart_init(BAT485_UART_INDEX, g_slConfigInfo.baud_485);
    uart_devices[0].init = 1;
    g_bat485_uart3_handle = (device_handle)(&uart_devices[0]);

    InitRingQueue(&uart_devices[1].uart_ring_queue, gw485_in_buff, sizeof(gw485_in_buff));
    uart_init(GW485_UART_INDEX, g_slConfigInfo.baud_485);
    uart_devices[1].init = 1;
    g_gw485_uart4_handle = (device_handle)(&uart_devices[1]);

    return 0;
}


/**
  * @brief  串口硬件初始化.
  * @param  uart_index 串口号
  * @param  baud 波特率
  * @retval None
  */
void uart_init(uartIndex_e uart_index, int baud)
{
    if (uart_index == BAT485_UART_INDEX) {
        BAT_485_Init(uart_devices[0].uart_baudrate);
    } else if (uart_index == GW485_UART_INDEX) {
        GW_485_Init(uart_devices[1].uart_baudrate);
    }
}

/**
  * @brief  发送一个字节.
  * @param  uart_index 串口号
  * @param  ch 待发送字符
  * @retval 1 成功  0失败
  */
uint8_t uart_putchar(device_handle device, char ch)
{
    uart_device_info *device_info = (uart_device_info *)device;
    if((!device) || (!device_info->init))
       return 0;

    if (device_info->uart_index == BAT485_UART_INDEX) {
        USARTx_SendByte(BAT_485, ch);
    } else if(device_info->uart_index == GW485_UART_INDEX) {
        USARTx_SendByte(GW_485, ch);
    }

    return 1;
}

/**
  * @brief  bat485发送使能.
  * @param
  * @retval
  */
void bat485_tx_enabla(void)
{
    GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, write);
}

/**
  * @brief  bat485发送关闭.
  * @param
  * @retval
  */
void bat485_tx_disenabla(void)
{
    GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, read);
}

/**
  * @brief  gw485发送使能.
  * @param
  * @retval
  */
void gw485_tx_enabla(void)
{
    GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, write);
}

/**
  * @brief  gw485发送关闭.
  * @param
  * @retval
  */
void gw485_tx_disenabla(void)
{
    GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, read);
}

/**
  * @brief  发送字符串.
  * @param  uart_index 串口号
  * @param  str 待发送字符串地址
  * @retval None
  */
void uart_sendstr(device_handle device,char *str)
{
#if rs485_send_enable
  if(device == g_bat485_uart3_handle){
      bat485_tx_enabla();
  }
  else if (device == g_gw485_uart4_handle) {
      gw485_tx_enabla();
  }
#endif

    while ((*str) != (char )0u) {
        if (*str == ASCII_CHAR_LINE_FEED){
            uart_putchar(device, (ASCII_CHAR_CARRIAGE_RETURN));
            uart_putchar(device, ASCII_CHAR_LINE_FEED);
            str++;
        }else{
            uart_putchar(device, *str++);
        }
    }

#if rs485_send_enable
   if(device == g_bat485_uart3_handle){
       bat485_tx_disenabla();
   }
   else if (device == g_gw485_uart4_handle) {
       gw485_tx_disenabla();
   }
#endif
}



/**
  * @brief  串口多字节发送.
  * @param  device 串口句柄
  * @param  data 待发送数据
  * @param  len  待发送数据长度
  * @retval None
  */
void uart_dev_write(device_handle device, void *data, int len)
{
#if rs485_send_enable
  if(device == g_bat485_uart3_handle){
      bat485_tx_enabla();
  }
  else if (device == g_gw485_uart4_handle) {
      gw485_tx_enabla();
  }
#endif

   for (int i = 0; i < len; i++) {
       uart_putchar(device, ((u_int8_t *)data)[i]);
   }

#if rs485_send_enable
   if(device == g_bat485_uart3_handle){
       bat485_tx_disenabla();
   }
   else if (device == g_gw485_uart4_handle) {
       gw485_tx_disenabla();
   }
#endif
}

/**
  * @brief  判断串口设备循环buff是否有数据.
  * @param  device 串口句柄
  * @retval 0 空   1有数据
  */
int uart_dev_char_present(device_handle device)
{
  uart_device_info *device_info = (uart_device_info *)device;

  if((!device) || (!device_info->init))
    return 0;

  return !RingQueueEmpty(&device_info->uart_ring_queue);
}

/**
  * @brief  从串口设备循环buff读取一个数据.
  * @param  device 串口句柄
  * @retval 读取到的字符
  */
char uart_dev_in_char(device_handle device)
{
  uart_device_info *device_info = (uart_device_info *)device;
  char c = 0;

  if (uart_dev_char_present(device))
      OutRingQueue(&device_info->uart_ring_queue, (u_int8_t*)&c);
  return c;
}

/**
  * @brief  判断循环buff是否有数据.
  * @param  ring_queue 循环buff地址
  * @retval 0 空   1有数据
  */
int ring_queue_dev_char_present(RingQueue *ring_queue)
{
    return !RingQueueEmpty(ring_queue);
}

/**
  * @brief  从循环buff读取一个数据.
  * @param  ring_queue 循环buff地址
  * @retval 读取到的字符
  */
char ring_queue_dev_in_char(RingQueue *ring_queue)
{
  char c = 0;
  if (ring_queue_dev_char_present(ring_queue))
      OutRingQueue(ring_queue, (u_int8_t*)&c);
  return c;
}

/**
  * @brief  查看循环buff中的数据个数.
  * @param  ring_queue 循环buff地址
  * @retval 循环buff中的个数
  */
int ring_queue_length(device_handle device)
{
    uart_device_info *device_info = (uart_device_info *)device;
    return RingQueueLength(&device_info->uart_ring_queue);
}








