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

#include "uart_dev.h"
#include "UART.h"


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

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

device_handle g_J1RS485_UART6_handle = 0;
device_handle g_J2RS485_UART7_handle = 0;
device_handle g_J3RS485_USART2_handle = 0;
device_handle g_J4RS485_UART8_handle = 0;
device_handle g_J50RS485_USART3_handle = 0;
device_handle g_LORA_UART4_handle = 0;
device_handle g_Upward_UART5_handle = 0;

#define IN_BUFF_SIZE 128
static uint8_t J1RS485_in_buff[IN_BUFF_SIZE];
static uint8_t J2RS485_in_buff[IN_BUFF_SIZE];
static uint8_t J3RS485_in_buff[IN_BUFF_SIZE];
static uint8_t J4RS485_in_buff[IN_BUFF_SIZE];
static uint8_t J50RS485_in_buff[IN_BUFF_SIZE];
static uint8_t LORA_in_buff[IN_BUFF_SIZE];
static uint8_t Upward_in_buff[IN_BUFF_SIZE];
//uint8_t J1RS485_in_buff[IN_BUFF_SIZE];
//uint8_t J2RS485_in_buff[IN_BUFF_SIZE];
//uint8_t J3RS485_in_buff[IN_BUFF_SIZE];
//uint8_t J4RS485_in_buff[IN_BUFF_SIZE];
//uint8_t J50RS485_in_buff[IN_BUFF_SIZE];
//uint8_t LORA_in_buff[IN_BUFF_SIZE];

//uint8_t rs485_out_buff[100];


/**
  * @brief  串口信息初始化，串口号.
  * @param  uart_index    对应的硬件串口号
  * @param  uart_baudrate 波特率
  */
uart_device_info uart_devices[]={
  [0] = {
      .init          = 0,
      .uart_index    = UART6,
      .uart_num      = ONLYONE,
      .uart_baudrate = 9600,
  },
  [1] = {
      .init          = 0,
      .uart_index    = UART7,
      .uart_num      = ONLYONE,
      .uart_baudrate = 9600,
  },
  [2] = {
      .init          = 0,
      .uart_index    = USART2,
      .uart_num      = ONLYONE,
      .uart_baudrate = 9600,
  },
  [3] = {
      .init          = 0,
      .uart_index    = UART8,
      .uart_num      = ONLYONE,
      .uart_baudrate = 9600,
  },
  [4] = {
      .init          = 0,
      .uart_index    = USART3,
      .uart_num      = J0RS485,
      .uart_baudrate = 9600,
  },
  [5] = {
      .init          = 0,
      .uart_index    = UART4,
      .uart_num      = ONLYONE,
      .uart_baudrate = 9600,
  },
  [6] = {
      .init          = 0,
      .uart_index    = UART5,
      .uart_num      = ONLYONE,
      .uart_baudrate = 115200,
  },
};

/**
  * @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(USART_TypeDef *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_devices[i], uart_devices[i].uart_baudrate);

        uart_devices[i].init = 1;
      }
      return (device_handle)(&uart_devices[i]);
    }
  }
  return 0;
}

//device_handle uart_dev_init(uart_device_info *uart_device, uint8_t *buff, int buff_size)
//{
//  int i = 0;
//  for(; i < ELEMENT_OF(uart_devices); i++){
//    if(uart_devices[i].uart_index == uart_device->uart_index){
//      if(!uart_devices[i].init){
//        InitRingQueue(&uart_devices[i].uart_ring_queue, buff, buff_size);
//        uart_init(uart_device, uart_devices[i].uart_baudrate);
//
//        uart_devices[i].init = 1;
//      }
//      return (device_handle)(&uart_devices[i]);
//    }
//  }
//  return 0;
//}


//uint8_t uart_all_dev_init(void)
//{
//    InitRingQueue(&uart_devices[0].uart_ring_queue, J1RS485_in_buff, sizeof(J1RS485_in_buff));
//    uart_init(&uart_devices[0], 9600);
//    uart_devices[0].init = 1;
//    g_J1RS485_UART6_handle = (device_handle)(&uart_devices[0]);
//
//    InitRingQueue(&uart_devices[1].uart_ring_queue, J2RS485_in_buff, sizeof(J2RS485_in_buff));
//    uart_init(&uart_devices[1], 9600);
//    uart_devices[1].init = 1;
//    g_J2RS485_UART7_handle = (device_handle)(&uart_devices[1]);
//
//
//    InitRingQueue(&uart_devices[2].uart_ring_queue, J3RS485_in_buff, sizeof(J3RS485_in_buff));
//    uart_init(&uart_devices[2], 9600);
//    uart_devices[2].init = 1;
//    g_J3RS485_USART2_handle = (device_handle)(&uart_devices[2]);
//
//    InitRingQueue(&uart_devices[3].uart_ring_queue, J4RS485_in_buff, sizeof(J4RS485_in_buff));
//    uart_init(&uart_devices[3], 9600);
//    uart_devices[3].init = 1;
//    g_J4RS485_UART8_handle = (device_handle)(&uart_devices[3]);
//
//
//    InitRingQueue(&uart_devices[4].uart_ring_queue, J50RS485_in_buff, sizeof(J50RS485_in_buff));
//    uart_init(&uart_devices[4], 9600);
//    uart_devices[4].init = 1;
//    g_J50RS485_USART3_handle = (device_handle)(&uart_devices[4]);
//
//    InitRingQueue(&uart_devices[5].uart_ring_queue, LORA_in_buff, sizeof(LORA_in_buff));
//    uart_init(&uart_devices[5], 9600);
//    uart_devices[5].init = 1;
//    g_LORA_UART4_handle = (device_handle)(&uart_devices[5]);
//
//    return 0;
//}

uint8_t uart_all_dev_init(void)
{
    g_J1RS485_UART6_handle = uart_dev_init(uart_devices[0].uart_index,
            J1RS485_in_buff, sizeof(J1RS485_in_buff));

    g_J2RS485_UART7_handle = uart_dev_init(uart_devices[1].uart_index,
            J2RS485_in_buff, sizeof(J2RS485_in_buff));

    g_J3RS485_USART2_handle = uart_dev_init(uart_devices[2].uart_index,
            J3RS485_in_buff, sizeof(J3RS485_in_buff));

    g_J4RS485_UART8_handle = uart_dev_init(uart_devices[3].uart_index,
            J4RS485_in_buff, sizeof(J4RS485_in_buff));

    g_J50RS485_USART3_handle = uart_dev_init(uart_devices[4].uart_index,
            J50RS485_in_buff, sizeof(J50RS485_in_buff));

    g_LORA_UART4_handle = uart_dev_init(uart_devices[5].uart_index,
            LORA_in_buff, sizeof(LORA_in_buff));

    g_Upward_UART5_handle = uart_dev_init(uart_devices[6].uart_index,
            Upward_in_buff, sizeof(Upward_in_buff));

    return 0;
}


/**
  * @brief  串口硬件初始化.
  * @param  uart_index 串口号
  * @param  baud 波特率
  * @retval None
  */
void uart_init(uart_device_info *uart_device, int baud)
{
    uart_device->uart_baudrate = baud;
    if (uart_device->uart_index == UART6) {
        J1_485_Init(baud);
    } else if (uart_device->uart_index == UART7) {
        J2_485_Init(baud);
    } else if (uart_device->uart_index == USART2) {
        J3_485_Init(baud);
    } else if (uart_device->uart_index == UART8) {
        J4_485_Init(baud);
    } else if (uart_device->uart_index == USART3) {
        J5_0_485_Init(baud);
        if (uart_device->uart_num == J0RS485) {
            USART_CONNET_J0();
        } else if (uart_device->uart_num == J5RS485) {
            USART_CONNET_J5();
        } else if (uart_device->uart_num == J6RS485) {
            USART_CONNET_J6();
        } else if (uart_device->uart_num == J7RS485) {
            USART_CONNET_J7();
        } else if (uart_device->uart_num == J8RS485) {
            USART_CONNET_J8();
        } else if (uart_device->uart_num == J9RS485) {
            USART_CONNET_J9();
        }
    } else if (uart_device->uart_index == UART5) {
        Upward_UART5_Init(baud);
    } else if (uart_device->uart_index == UART4) {
        LORA_UART4_Init(baud);
    }
}

/**
  * @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;

    USARTx_SendByte_str(device_info->uart_index, ch);


    return 1;
}

/**
  * @brief  J1RS485发送使能.
  * @param
  * @retval
  */
void J1RS485_tx_enabla(void)
{
    rt_pin_write(J1_DE, write);
}

/**
  * @brief  J1RS485发送关闭.
  * @param
  * @retval
  */
void J1RS485_tx_disenabla(void)
{
    rt_pin_write(J1_DE, read);
}

/**
  * @brief  J2RS485发送使能.
  * @param
  * @retval
  */
void J2RS485_tx_enabla(void)
{
    rt_pin_write(J2_DE, write);
}

/**
  * @brief  J2RS485发送关闭.
  * @param
  * @retval
  */
void J2RS485_tx_disenabla(void)
{
    rt_pin_write(J2_DE, read);
}

/**
  * @brief  J3RS485发送使能.
  * @param
  * @retval
  */
void J3RS485_tx_enabla(void)
{
    rt_pin_write(J3_DE, write);
}

/**
  * @brief  J3RS485发送关闭.
  * @param
  * @retval
  */
void J3RS485_tx_disenabla(void)
{
    rt_pin_write(J3_DE, read);
}
/**
  * @brief  J4RS485发送使能.
  * @param
  * @retval
  */
void J4RS485_tx_enabla(void)
{
    rt_pin_write(J4_DE, write);
}

/**
  * @brief  J4RS485发送关闭.
  * @param
  * @retval
  */
void J4RS485_tx_disenabla(void)
{
    rt_pin_write(J4_DE, read);
}
/**
  * @brief  J50RS485发送使能.
  * @param
  * @retval
  */
void J50RS485_tx_enabla(void)
{
    rt_pin_write(USART3_DE, write);
}

/**
  * @brief  J50RS485发送关闭.
  * @param
  * @retval
  */
void J50RS485_tx_disenabla(void)
{
    rt_pin_write(USART3_DE, read);
}

/**
  * @brief  发送字符串.
  * @param  uart_index 串口号
  * @param  str 待发送字符串地址
  * @retval None
  */
void uart_sendstr(device_handle device,char *str)
{
#if rs485_send_enable
  if(device == g_J1RS485_UART6_handle){
      J1RS485_tx_enabla();
  } else if (device == g_J2RS485_UART7_handle) {
      J2RS485_tx_enabla();
  } else if (device == g_J3RS485_USART2_handle) {
      J3RS485_tx_enabla();
  } else if (device == g_J4RS485_UART8_handle) {
      J4RS485_tx_enabla();
  } else if (device == g_J50RS485_USART3_handle) {
      J50RS485_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_J1RS485_UART6_handle){
        J1RS485_tx_disenabla();
    } else if (device == g_J2RS485_UART7_handle) {
        J2RS485_tx_disenabla();
    } else if (device == g_J3RS485_USART2_handle) {
        J3RS485_tx_disenabla();
    } else if (device == g_J4RS485_UART8_handle) {
        J4RS485_tx_disenabla();
    } else if (device == g_J50RS485_USART3_handle) {
        J50RS485_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_J1RS485_UART6_handle){
      J1RS485_tx_enabla();
  } else if (device == g_J2RS485_UART7_handle) {
      J2RS485_tx_enabla();
  } else if (device == g_J3RS485_USART2_handle) {
      J3RS485_tx_enabla();
  } else if (device == g_J4RS485_UART8_handle) {
      J4RS485_tx_enabla();
  } else if (device == g_J50RS485_USART3_handle) {
      J50RS485_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_J1RS485_UART6_handle){
        J1RS485_tx_disenabla();
    } else if (device == g_J2RS485_UART7_handle) {
        J2RS485_tx_disenabla();
    } else if (device == g_J3RS485_USART2_handle) {
        J3RS485_tx_disenabla();
    } else if (device == g_J4RS485_UART8_handle) {
        J4RS485_tx_disenabla();
    } else if (device == g_J50RS485_USART3_handle) {
        J50RS485_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);
}








