#include "HW_Config.h"
#include "uart_printer_drv.h"

#define PRINT_UART 	6

#define GET_PRINTER_PAPER_FLAG 	0x01

unsigned char print_init_data[16];
unsigned int print_init_data_len = 0;

unsigned int get_printer_status_falg = 0;

unsigned int printer_flag_no_paper = 0;
unsigned int printer_flag_err = 0;
unsigned int printer_Rpy = 0;
unsigned int printer_offline = 0;
unsigned int PrinterDivTick =0;

void uart6_rx_task(void)
{
	uart_info_8bit_struct * p_uart_info;
	p_uart_info = &uart_info_8bit[PRINT_UART];
	
	if(p_uart_info->rx_complete){
		p_uart_info->rx_complete = 0;
		
		if(get_printer_status_falg & GET_PRINTER_PAPER_FLAG){
			get_printer_status_falg &=~GET_PRINTER_PAPER_FLAG;
			if(0x04 == p_uart_info->rx_buf[0]){
				printer_flag_no_paper = 0;
				printer_Rpy = 1;
			}else
			if(0x00 == p_uart_info->rx_buf[0]){
				printer_flag_no_paper = 1;
				printer_Rpy = 1;
			}
		}
	}
}

unsigned int printer_is_busy(void)//1:is busy 0:no busy
{
	if((!get_uart_tx_complete_delay_flag(PRINT_UART))||(GET_PRINTER_STATUS()))return 1;
	
	return 0;
}

void printer_reset_int(void)
{
	//系统复位
	print_init_data[0] = 0x10;
	print_init_data[1] = 0x04;
	print_init_data[2] = 0x03;
	
	print_init_data_len = 3;
	
	uart_tx_over(PRINT_UART, print_init_data, print_init_data_len);
}

void printer_set_int(void)
{
	//初始化指令
	print_init_data[0] = 0x1B;
	print_init_data[1] = 0x40;
	//方向指令
	print_init_data[2] = 0x1B;
	print_init_data[3] = 0x63;
	print_init_data[4] = 0x01;
	//字间距0
	print_init_data[5] = 0x1B;
	print_init_data[6] = 0x70;
	print_init_data[7] = 0x00;
	//设置左限0
	print_init_data[8] = 0x1B;
	print_init_data[9] = 0x6C;
	print_init_data[10]= 0x00;
	//设置右限0
	print_init_data[11]= 0x1B;
	print_init_data[12]= 0x51;
	print_init_data[13]= 0x00;
	
	print_init_data_len = 14;
	
	uart_tx_over(PRINT_UART, print_init_data, print_init_data_len);
}

void get_printer_paper_status(void)
{
	//实时查询有无纸
	print_init_data[0] = 0x10;
	print_init_data[1] = 0x04;
	print_init_data[2] = 0x02;
	
	print_init_data_len = 3;
	
	//非实时查询有无纸
	print_init_data[0] = 0x1C;
	print_init_data[1] = 0x76;
	
	print_init_data_len = 2;
	
	uart_tx_over(PRINT_UART, print_init_data, print_init_data_len);
	
	get_printer_status_falg |= GET_PRINTER_PAPER_FLAG;
}

unsigned int set_print_text(char * p_data, unsigned int len)
{
	unsigned int i = 0;
	
	if(printer_is_busy())return 1;
	if((!len)||(len > 16))return 1;
	
	for(i = 0;i<len;i++){
		USART6->TDR = *p_data;
		p_data++;
	}
	uart_info_8bit[PRINT_UART].tx_complete = 1;
	uart_info_8bit[PRINT_UART].tx_complete_delay_flag = 0;
	uart_info_8bit[PRINT_UART].tx_complete_delay_tick= 0;
	
	return 0;
}

unsigned int print_text(char * p_data)
{
	volatile char * pdata;
	
	pdata = p_data;
	
	print_init_data_len = 0;
	while(*pdata){
		pdata++;
		print_init_data_len++;
		if(print_init_data_len > (UART_TX_BUF_MAX-1))break;
	}
	print_init_data_len+=1;
	
	if(printer_is_busy())return 1;
		
	if(print_init_data_len <= UART_TX_BUF_MAX){
		uart_tx_over(PRINT_UART, p_data, print_init_data_len);
		return 0;
	}
	return 1;
}

void printer_init(void)
{
	PRINTER_DRV_ENABLE();
	HAL_Delay(10);
	printer_reset_int();
	HAL_Delay(500);
	printer_set_int();
	HAL_Delay(20);
	print_text("\r\n\r\n\r\n");
	HAL_Delay(5);
}

void printer_task(void)//1S
{
	static unsigned int clear_tick = 0;
	static unsigned int err_tick = 0;
	static unsigned int no_rpy_times = 0; 
	static unsigned int HasStartCheck = 0;
	
	PrinterDivTick++;
	if(PrinterDivTick>9){
		PrinterDivTick = 0;
		if(HasStartCheck){
			HasStartCheck = 0;
			if(printer_Rpy == 0){
				no_rpy_times++;
				if(no_rpy_times >1){
					no_rpy_times = 2;
					printer_offline = 1;
				}
			}else{
				no_rpy_times = 0;
				printer_offline = 0;
			}
			PrcDevPrinterState(printer_offline , printer_flag_no_paper);
		}
		if(!printer_is_busy()){
			//if(0 == get_printer_status_falg){
			if(1){
				get_printer_paper_status();
				printer_Rpy = 0;
				HasStartCheck =1;
			}else{
				//clear_tick++;
				//if(clear_tick > 9){
				//	clear_tick = 0;
					get_printer_status_falg = 0;
				//}
			}
			err_tick = 0;
			printer_flag_err = 0;
		}else{
			err_tick++;
			if(err_tick > 2 ){
				err_tick = 0;
				printer_flag_err = 1;
			}
		}
	}
}

void printer_reset_callback(void)
{
	PrinterDivTick =0;
}	

void uart_printer_task(void)
{
	uart6_rx_task();
}

unsigned int get_printer_flag_no_paper(void)//1:no paper or err
{
	if(printer_flag_no_paper + printer_flag_err)return 1;
	return 0;
}

void set_enable_printer(void)
{
	PRINTER_DRV_ENABLE();
}

void set_disable_printer(void)
{
	PRINTER_DRV_DISABLE();
}
