/**
******************************************************************************************************************************************************************************************
*@file                       app_trace.c
*@author         
*@data                       
*@version                    v1.0
*@par Copyright(c):
*
*@par History:
*     version:author,date,desc
*     v1.0   :create this file
*
*******************************************************************************************************************************************************************************************
*/

#include "sdk_include.h"
#if is_uart_dev(DBG_UART)

#if LOG_SWITCH
#include "common.h"
#endif
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
------------------------------------------------------------------------*/
NO_REATION_RAM(static char print_string[512]);

int block_putchar (int ch) 
{
#if is_uart_dev(DBG_UART)
	if(ch == '\n')
		uart_block_putchar(DBG_UART,'\r');

 	uart_block_putchar(DBG_UART, ch);
#endif	
	return ch;
}


int user_print(char *fmt,...)
{
#if LOG_SWITCH
	if(is_log_enable() == 0)
		return 0;
#endif
	memset(print_string,0,sizeof(print_string));
    va_list ap;
    va_start(ap,fmt);
    vsprintf(print_string, fmt, ap);
    va_end(ap); 
    ui_dbg_uart_writebuf(DBG_UART,(unsigned char *)print_string, strlen(print_string));
    return 0 ;
}

int user_block_print(char *fmt,...)
{
	unsigned char i;
	unsigned char len;
	
#if LOG_SWITCH
	if(is_log_enable() == 0)
		return 0;
#endif

	memset(print_string,0,sizeof(print_string));
    va_list ap;
    va_start(ap,fmt);
    vsprintf(print_string, fmt, ap);
    va_end(ap);
	len = strlen(print_string);
	for(i=0;i<len;i++){
		block_putchar(print_string[i]);
	}
    return 0 ;
}

int user_print_hex(char* lable,unsigned char *buf,unsigned char len)
{
	unsigned char i;

		
#if LOG_SWITCH
	if(is_log_enable() == 0)
		return 0;
#endif

    ui_dbg_uart_writebuf(DBG_UART,(unsigned char *)lable, strlen(lable));
	if(len > 100)
		len = 100;
	
	memset(print_string,0,sizeof(print_string));
	for(i=0;i<len;i++)
	{
		print_string[i*5 + 0]='0';
		print_string[i*5 + 1]='x';

		if((buf[i] & 0xF0) > 0x90){
			print_string[ i*5 + 2 ]= ((buf[i] & 0xF0) >> 4) - 10 + 'A';
		}else{
			print_string[ i*5 + 2 ]= ((buf[i] & 0xF0) >> 4) + '0';
		}

		if((buf[i] & 0x0F) > 0x09){
			print_string[ i*5 + 3 ]= (buf[i] & 0x0F) - 10 + 'A';
		}else{
			print_string[ i*5 + 3 ]= (buf[i] & 0x0F) + '0';
		}
		
		print_string[ i*5 + 4 ]=' ';
	}
	
	print_string[len*5 + 0]='\n';
	print_string[len*5 + 1]='\0';
	ui_dbg_uart_writebuf(DBG_UART,(uint8_t *)print_string, strlen(print_string));	
    return 0 ;
}


int user_block_print_hex(char* lable,char *buf,unsigned char len)
{
	unsigned char i;
#if LOG_SWITCH
	if(is_log_enable() == 0)
		return 0;
#endif
    ui_uart_block_writebuf(DBG_UART,(unsigned char *)lable, strlen(lable));
	if(len > 100)
		len = 100;
	
	memset(print_string,0,sizeof(print_string));
	for(i=0;i<len;i++)
	{

		if((buf[i] & 0xF0) > 0x90){
			print_string[ i*3 + 0 ]= ((buf[i] & 0xF0) >> 4) - 10 + 'A';
		}else{
			print_string[ i*3 + 0 ]= ((buf[i] & 0xF0) >> 4) + '0';
		}

		if((buf[i] & 0x0F) > 0x09){
			print_string[ i*3 + 1 ]= (buf[i] & 0x0F) - 10 + 'A';
		}else{
			print_string[ i*3 + 1 ]= (buf[i] & 0x0F) + '0';
		}
		
		print_string[ i*3 + 2 ]=' ';
	}
	
	print_string[len*3 + 0]='\n';
	print_string[len*3 + 1]='\0';
	
	ui_uart_block_writebuf(DBG_UART,(uint8_t *)print_string, strlen(print_string));	
    return 0 ;
}
#else

extern int block_putchar (int ch)
{
	return 0;
}

int user_block_print(char *fmt,...)
{
	return 0;
}

int user_print(char *fmt,...)
{
	return 0;
}

extern int user_print_hex(char* lable,char *buf,unsigned char len)
{
	return 0;
}
extern int user_print_hex2(char* lable,char *buf,unsigned char len)
{
	return 0;
}
extern int user_block_print_hex(char* lable,char *buf,unsigned char len)
{
	return 0;
}

#endif

