#include "type.h"
#include "UWater_uart_driver.h"
#include "UWater_DebugPrint.h"
#include "UWater_frame_app.h"

static u8 wait_for_print_buf[PRINT_BUF_SIZE];  //待打印缓冲区
static u8 print_nb_data_flag = FALSE;
static u8 print_program_data_flag = FALSE;
static PRINT_STRUCT printf_struct;
static u8 print_data_state=PRINT_DATA_S0;
static u8 print_temp_data;
static u16 print_temp_save_len;
//启动NB数据打印
void OpenPrintNbDataSwitch(void)
{
	FrameSetMsgApp(MsgOpenIr);
	print_nb_data_flag = TRUE;
}

//关闭NB数据打印
void ClosePrintNbDataSwitch(void)
{
	print_nb_data_flag = FALSE;
}
u8 GetPrintNbDataSwitch(void)
{
    return print_nb_data_flag;
}
//启动程序数据打印
void OpenPrintProgramDataSwitch(void)
{
	print_program_data_flag = TRUE;
}

//关闭程序数据打印
void ClosePrintProgramDataSwitch(void)
{
	print_program_data_flag = FALSE;
}

/***********************启动打印数据********************************
Function:    void PrintNbData (u8 *data_src, u16 len)
Description: 打印数据
Input:     data_src 数据来源指针 ，len 数据长度
 ****************************************************************/
void PrintNbData(u8 *data_src, u16 len)
{
	if(print_nb_data_flag)
	{
		while(len--)
		{
			print_temp_save_len = printf_struct.saved_data_len;//把写指针保存起来
			print_temp_save_len++;
			if (print_temp_save_len >= PRINT_BUF_SIZE)
			print_temp_save_len = 0;
			if (print_temp_save_len != printf_struct.printf_data_len)
			{
				wait_for_print_buf[printf_struct.saved_data_len] = *data_src++;//将数据写入内存缓冲区
				printf_struct.saved_data_len = print_temp_save_len;
			}
		}
	}
}
int Print_Get_Rx_Num(void)
{
	if (printf_struct.saved_data_len >= printf_struct.printf_data_len)
		return (printf_struct.saved_data_len - printf_struct.printf_data_len);
	else 
		return (printf_struct.saved_data_len + PRINT_BUF_SIZE - printf_struct.printf_data_len);
}
unsigned char Print_Get_Rx_Data(void)
{
	unsigned char data;

	data = wait_for_print_buf[printf_struct.printf_data_len];
	printf_struct.printf_data_len++;
	if (printf_struct.printf_data_len >= PRINT_BUF_SIZE)
		printf_struct.printf_data_len = 0;	
	return data; 
}
/***********************启动打印数据********************************
Function:    void PrintProgramData (u8 *data_src, u16 len)
Description: 打印数据
Input:     data_src 数据来源指针 ，len 数据长度
 ****************************************************************/
void PrintProgramData(u8 *data_src, u16 len)
{
    if(print_program_data_flag)
    {
        u16 i;
        if(len < PRINT_BUF_SIZE)
        {
            for(i = 0; i < len; i++)
            {
                wait_for_print_buf[printf_struct.saved_data_len++] = *(data_src + i);
                printf_struct.saved_data_len = printf_struct.saved_data_len % PRINT_BUF_SIZE;
            }
        }
    }
}
//打印状态机是否空闲
u8 PrintDataMachineIfIdle(void)
{
	if(PRINT_DATA_S0 == print_data_state)
    {
        return TASK_IDLE;
    }
    else
    {
        return TASK_BUSY;
    }
}

/*******************************打印数据状态机******************************
Function:      void PrintDataMachine(void)
Description:   打印数据状态机
 ***************************************************************************/
void PrintDataMachine(void)
{
    switch(print_data_state)
    {
        case(PRINT_DATA_S0):
        {
            if(print_nb_data_flag)
            {
                if(Print_Get_Rx_Num()>0)
                {
					print_temp_data = Print_Get_Rx_Data();
                    print_data_state = PRINT_DATA_S1;
                }
            }
        }
        break;

        case(PRINT_DATA_S1):
        {
            if(TASK_IDLE == UartIfIdle(IR_UART_COM))
            {
				UartSend(IR_UART_COM,1,&print_temp_data,&printf_struct.send_msg);
                print_data_state = PRINT_DATA_S2;
            }
        }
        break;
		case(PRINT_DATA_S2):
        {
            if(Print_Get_Rx_Num()>0)
			{
				print_temp_data = Print_Get_Rx_Data();
				print_data_state = PRINT_DATA_S1;
			}
			else
			{
				print_data_state = PRINT_DATA_S0;
			}
        }
        break;
		
        case(PRINT_DATA_DEFAULT):
        {
            print_data_state = PRINT_DATA_S0;
        }
        break;
		default:
		{
			print_data_state = PRINT_DATA_S0;
			break;
		}
    }
}



















