////////////////////////////////////////////////////////////////////////////////

/// 终端打印文件 
///
/// 
/// @file    bsp_ser.c 
/// @author  gkl 
/// @date    2017-05-17 
/// @version v0.1 

////////////////////////////////////////////////////////////////////////////////

// 包含的头文件
#include <bsp_ser.h>
#include "Usart.h"
/// 打印字节
///
/// @param  c: 需要打印的字符   
/// @param  none
/// @return none 
/// @note   修改日志
///         gkl与2017-05-27创建      
static void  term_uart_wrbyte(CPU_INT08U  c)
{
  while(RESET == usart_flag_get(USART0, USART_FLAG_TBE)){}
  //写寄存器
  usart_data_transmit(USART0, c);
  //等待发送完成
  while(RESET == usart_flag_get(USART0, USART_FLAG_TC)){}
}

/// 打印字符串
///
/// @param  *p_str：需打印的字符串指针  
/// @param  none
/// @return none 
/// @note   修改日志
///         gkl与2017-05-27创建
//BSP_OS_SEM ser_mutex;//用于锁调用发生一个字节函数
void  term_uart_wrstr (CPU_CHAR  *p_str)
{
    CPU_BOOLEAN  err;
        
   
//    err = BSP_OS_SemWait(&ser_mutex, 0u);                     /* Obtain access to the serial interface      */
//    if (err != DEF_OK ) {
//        return;
//    } 
    while ((*p_str) != (CPU_CHAR )0u) {
        if (*p_str == ASCII_CHAR_LINE_FEED) {
            term_uart_wrbyte(ASCII_CHAR_CARRIAGE_RETURN);
            term_uart_wrbyte(ASCII_CHAR_LINE_FEED);
            p_str++;
        } else {
            term_uart_wrbyte(*p_str++);
        }
    }
//    BSP_OS_SemPost(&ser_mutex);
}

/// 格式打印
///
/// @param  none  
/// @param  none
/// @return none 
/// @note   修改日志
///         gkl与2017-05-27创建
void  term_printf (CPU_CHAR *format, ...)
{
    static  CPU_CHAR  buffer[200u + 1u];
            va_list   vArgs;
      
	memset(buffer,'0',sizeof(buffer));	
	
    va_start(vArgs, format);
    vsprintf((char *)buffer, (char const *)format, vArgs);
    va_end(vArgs);

    term_uart_wrstr((CPU_CHAR*) buffer);
}

#include "ptz_struct.h"
void ptz_printf(unsigned char *buffer,unsigned short int buff_size)
{//该函数不能在中断中使用
    if(g_ptz.ptz_printf.net_udp_printf_switch == LOCATION_RETURN_ON)
    {
      //发送数据
      send_udp_data_aim(buffer, buff_size, 
                        (struct sockaddr*)&g_ptz.ptz_printf.printf_from, g_ptz.ptz_printf.printf_fromlen);
    }
    
    
    if(g_ptz.ptz_printf.uart_422_printf_switch == LOCATION_RETURN_ON)
    {
      ptz_send_data(PTZ_UART_422, buffer, buff_size);
    }
    
    
    if(g_ptz.ptz_printf.uart_485_printf_switch == LOCATION_RETURN_ON)
    {
      ptz_send_data(PTZ_UART_485, buffer, buff_size);
    }
}


void ptz_printf_init()
{
  g_ptz.ptz_printf.printf_from.sin_family = PF_INET;
  g_ptz.ptz_printf.printf_from.sin_port = htons(8888);
  g_ptz.ptz_printf.printf_from.sin_addr.s_addr = inet_addr("192.168.8.222");
  g_ptz.ptz_printf.printf_fromlen = sizeof(g_ptz.ptz_printf.printf_from);
  
  g_ptz.ptz_printf.net_udp_printf_switch = LOCATION_RETURN_ON;
}












