#include "common.h"
#include "data.h"
                    
static uint32 fifo_data_count = 0;              // fifo ݸ
static fifo_struct ai_camera_0_uart_fifo;		// AIͷ0 ڽfifo
static fifo_struct ai_camera_1_uart_fifo;		// AIͷ1 ڽfifo
static fifo_struct ai_camera_2_uart_fifo;		// AIͷ2 ڽfifo

static uint8 ai_camera_0_uart_buffer[64];       // ڽݻ
static uint8 ai_camera_0_fifo_buffer[64];       // fifo 

static uint8 ai_camera_1_uart_buffer[64];       // ڽݻ
static uint8 ai_camera_1_fifo_buffer[64];       // fifo 

static uint8 ai_camera_2_uart_buffer[64];       // ڽݻ
static uint8 ai_camera_2_fifo_buffer[64];       // fifo 

static uint8 uart_data[] = {0x2C, 0x9F, 0x08, 0x5B, 0x5B};

/* AIͷʼ */
void ai_camera_init(void){
	ai_camera_0_init();
	ai_camera_1_init();
	#ifndef AI_CAMERA_MERGE
	ai_camera_2_init();
	#endif
	gpio_init(SUPPLEMENT_LAMP_EN_PIN, GPO, 0, GPO_PUSH_PULL);
}

/* AIͷ0 ʼ */
void ai_camera_0_init(void){
    fifo_init(&ai_camera_0_uart_fifo, FIFO_DATA_8BIT, ai_camera_0_uart_buffer, 64);              // ʼ fifo ػ
    uart_init(AI_CAMERA_0_UART_INDEX, AI_CAMERA_0_UART_BAUDRATE, AI_CAMERA_0_UART_TX_PIN, AI_CAMERA_0_UART_RX_PIN);             // ʼ
    uart_rx_interrupt(AI_CAMERA_0_UART_INDEX, ZF_ENABLE);                                   //  UART_INDEX Ľж
    interrupt_set_priority(AI_CAMERA_0_UART_PRIORITY, 0);                                   // öӦ UART_INDEX жȼΪ 0
}

/* AIͷ1/ߣ֣ ʼ */	
void ai_camera_1_init(void){
    fifo_init(&ai_camera_1_uart_fifo, FIFO_DATA_8BIT, ai_camera_1_uart_buffer, 64);              // ʼ fifo ػ
    
    uart_init(AI_CAMERA_1_UART_INDEX, AI_CAMERA_1_UART_BAUDRATE, AI_CAMERA_1_UART_TX_PIN, AI_CAMERA_1_UART_RX_PIN);             // ʼ
    uart_rx_interrupt(AI_CAMERA_1_UART_INDEX, ZF_ENABLE);                                   //  UART_INDEX Ľж
    interrupt_set_priority(AI_CAMERA_1_UART_PRIORITY, 0);                                   // öӦ UART_INDEX жȼΪ 0
}
#ifndef AI_CAMERA_MERGE
/* AIͷ2֣ ʼ */
void ai_camera_2_init(void){
    fifo_init(&ai_camera_2_uart_fifo, FIFO_DATA_8BIT, ai_camera_2_uart_buffer, 64);              // ʼ fifo ػ
    
    uart_init(AI_CAMERA_2_UART_INDEX, AI_CAMERA_2_UART_BAUDRATE, AI_CAMERA_2_UART_TX_PIN, AI_CAMERA_2_UART_RX_PIN);             // ʼ
    uart_rx_interrupt(AI_CAMERA_2_UART_INDEX, ZF_ENABLE);                                   //  UART_INDEX Ľж
    interrupt_set_priority(AI_CAMERA_2_UART_PRIORITY, 0);                                   // öӦ UART_INDEX жȼΪ 0
}
#endif

/* 
	AIͷ ʶת 
	AI_CAMERA_0: ʶϽXꣻʶȣʶ߶
	AI_CAMERA_1: tool_detection_result_raw
	AI_CAMERA_2: num_detection_result_raw
*/
static void detection_result_trans(_ai_camera_idx_ ai_camera_idx, ...){
	va_list args;
   
	switch(ai_camera_idx){
		case AI_CAMERA_0:{
			va_start(args, 3);  // ʼɱб
			uint8 data_1 = (uint8)va_arg(args, int); // ȡ ʶϽX
			uint8 data_2 = (uint8)va_arg(args, int); // ȡ ʶ
			uint8 data_3 = (uint8)va_arg(args, int); // ȡ ʶ߶
			if(data_1 != 0XFF && data_2 != 0XFF && data_3 != 0XFF){
				int16 detection_box_left_x = data_1-AI_CAMERA_0_OFFSET; // ȡ ʶϽX
				detection_box_width = data_2;  		// ȡ ʶ
				detection_box_height = data_3;  	// ȡ ʶ
				detection_box_center_x = detection_box_left_x + detection_box_width/2;	//  ʶX
			}
			else
				ai_camera_0_init_flag = True;
			
			break;
		}
		case AI_CAMERA_1:{
			va_start(args, 1);  // ʼɱб
			uint8 tool_detection_result_raw = (uint8)va_arg(args, int);	// ȡ ʶԭʼֵ
			if(tool_detection_result_raw != 0XFF){
				detection_result.tool_detection_finsh_flag = True;
				detection_result.tool_raw = tool_detection_result_raw;
				detection_result.tool = (_ai_lable_)tool_detection_result_raw;
			}
			else
				detection_result.ai_camera_init_flag[0] = True;
			
			break;
		}
		case AI_CAMERA_2:{
			va_start(args, 1);  // ʼɱб
			uint8 num_detection_result_raw = (uint8)va_arg(args, int);	// ȡ ʶԭʼֵ
			if(num_detection_result_raw != 0XFF){
				detection_result.num_detection_finsh_flag = True;
				detection_result.num_raw = num_detection_result_raw;
				detection_result.num = num_detection_result_raw;
			}
			else
				detection_result.ai_camera_init_flag[1] = True;
			
			break;
		}
	}
	va_end(args);
}

/* ƿ */ 
void supplement_lamp(uint8_t status){
	/* Ƴ */
	if(supplement_lamp_enable_flag == 0)
		gpio_set_level(SUPPLEMENT_LAMP_EN_PIN,0);
	/* ѭ߿ʶ */
	if(supplement_lamp_enable_flag == 1){
		if(status == 0)
			gpio_set_level(SUPPLEMENT_LAMP_EN_PIN,0);
		else
			gpio_set_level(SUPPLEMENT_LAMP_EN_PIN,1);
	}
	/* ѭ߹أʶ */
	if(supplement_lamp_enable_flag == 2){
		if(status == 0)
			gpio_set_level(SUPPLEMENT_LAMP_EN_PIN,1);
		else
			gpio_set_level(SUPPLEMENT_LAMP_EN_PIN,0);
	}	
	/* Ƴ */
	if(supplement_lamp_enable_flag == 3)
		gpio_set_level(SUPPLEMENT_LAMP_EN_PIN,1);
}

//-------------------------------------------------------------------------------------------------------------------
//      UART_INDEX Ľжϴ  UART_INDEX Ӧжϵ  isr.c
// ˵     void
// ز     void
// ʹʾ     uart_rx_interrupt_handler();
//-------------------------------------------------------------------------------------------------------------------
void uart_rx_interrupt_handler_ai_camera_0 (void)
{ 
	uint8 get_data = 0;
	uint32 fifo_data_count = 0;                         // fifo ݸ
		
	uart_query_byte(AI_CAMERA_0_UART_INDEX, &get_data);  		//  ѯʽ ݻ᷵ TRUE ûݻ᷵ FALSE
    fifo_write_buffer(&ai_camera_0_uart_fifo, &get_data, 1);  	// д fifo 
	fifo_data_count = fifo_used(&ai_camera_0_uart_fifo); 		// FIFOѯǰݸ
    fifo_read_buffer(&ai_camera_0_uart_fifo, ai_camera_0_fifo_buffer, &fifo_data_count, FIFO_READ_ONLY);
//	uart_write_buffer(AI_CAMERA_1_UART_INDEX, fifo_get_data_ai_camera_1, fifo_data_count_1);      // ȡݷͳȥ
//	uart_write_buffer(AI_CAMERA_1_UART_INDEX, array3, 1);
	if(ai_camera_0_fifo_buffer[0] != 0x2C) 
	{
			fifo_clear(&ai_camera_0_uart_fifo);
	}
	if(fifo_data_count >= 4)	//֡ͷ֡β
	{	
		if(	ai_camera_0_fifo_buffer[0] == 0x2C && 
			ai_camera_0_fifo_buffer[1] == 0x12 && 
			ai_camera_0_fifo_buffer[fifo_data_count - 2] == 0x5B && 
			ai_camera_0_fifo_buffer[fifo_data_count - 1] == 0x5B )
			// ת
			detection_result_trans(AI_CAMERA_0, ai_camera_0_fifo_buffer[2], ai_camera_0_fifo_buffer[3], ai_camera_0_fifo_buffer[4]);
		if(	ai_camera_0_fifo_buffer[fifo_data_count - 2] == 0x5B && 
			ai_camera_0_fifo_buffer[fifo_data_count - 1] == 0x5B ||
			fifo_data_count > 8)
		{
				fifo_clear(&ai_camera_0_uart_fifo);
		}	
	}
}
//-------------------------------------------------------------------------------------------------------------------
//      UART_INDEX Ľжϴ  UART_INDEX Ӧжϵ  isr.c
// ˵     void
// ز     void
// ʹʾ     uart_rx_interrupt_handler_A();
//-------------------------------------------------------------------------------------------------------------------
void uart_rx_interrupt_handler_ai_camera_1 (void)
{ 	
	uint8 get_data = 0;
	uint32 fifo_data_count = 0;                         // fifo ݸ
		
	uart_query_byte(AI_CAMERA_1_UART_INDEX, &get_data);  		//  ѯʽ ݻ᷵ TRUE ûݻ᷵ FALSE
    fifo_write_buffer(&ai_camera_1_uart_fifo, &get_data, 1);  	// д fifo 
	fifo_data_count = fifo_used(&ai_camera_1_uart_fifo); 		// FIFOѯǰݸ
    fifo_read_buffer(&ai_camera_1_uart_fifo, ai_camera_1_fifo_buffer, &fifo_data_count, FIFO_READ_ONLY);
//	uart_write_buffer(AI_CAMERA_1_UART_INDEX, fifo_get_data_ai_camera_1, fifo_data_count_1);      // ȡݷͳȥ
//	uart_write_buffer(AI_CAMERA_1_UART_INDEX, array3, 1);
	if(ai_camera_1_fifo_buffer[0] != 0x2C) 
	{
		fifo_clear(&ai_camera_1_uart_fifo);
	}
	#ifdef AI_CAMERA_MERGE
	if(fifo_data_count >= 4)	//֡ͷ֡β
	{	
		if(	ai_camera_1_fifo_buffer[0] == 0x2C && 
			ai_camera_1_fifo_buffer[1] == 0x12 && 
			ai_camera_1_fifo_buffer[fifo_data_count - 2] == 0x5B && 
			ai_camera_1_fifo_buffer[fifo_data_count - 1] == 0x5B )
			// ת
			detection_result_trans(AI_CAMERA_1, ai_camera_1_fifo_buffer[2]);
			detection_result_trans(AI_CAMERA_2, ai_camera_1_fifo_buffer[3]);
		if(	ai_camera_1_fifo_buffer[fifo_data_count - 2] == 0x5B && 
			ai_camera_1_fifo_buffer[fifo_data_count - 1] == 0x5B ||
			fifo_data_count > 8)
		{
				fifo_clear(&ai_camera_1_uart_fifo);
		}	
	}
	#endif
	#ifndef AI_CAMERA_MERGE
	if(fifo_data_count >= 4)	//֡ͷ֡β
	{	
		if(	ai_camera_1_fifo_buffer[0] == 0x2C && 
			ai_camera_1_fifo_buffer[1] == 0x12 && 
			ai_camera_1_fifo_buffer[fifo_data_count - 2] == 0x5B && 
			ai_camera_1_fifo_buffer[fifo_data_count - 1] == 0x5B )
			// ת
			detection_result_trans(AI_CAMERA_1, ai_camera_1_fifo_buffer[2]);
		if(	ai_camera_1_fifo_buffer[fifo_data_count - 2] == 0x5B && 
			ai_camera_1_fifo_buffer[fifo_data_count - 1] == 0x5B ||
			fifo_data_count > 8)
		{
				fifo_clear(&ai_camera_1_uart_fifo);
		}	
	}
	#endif
}

#ifndef AI_CAMERA_MERGE
//-------------------------------------------------------------------------------------------------------------------
//      UART_INDEX Ľжϴ  UART_INDEX Ӧжϵ  isr.c
// ˵     void
// ز     void
// ʹʾ     uart_rx_interrupt_handler_A();
//-------------------------------------------------------------------------------------------------------------------
void uart_rx_interrupt_handler_ai_camera_2 (void)
{ 
	uint8 get_data = 0;
	uint32 fifo_data_count = 0;                         // fifo ݸ

	uart_query_byte(AI_CAMERA_2_UART_INDEX, &get_data);  //  ѯʽ ݻ᷵ TRUE ûݻ᷵ FALSE
	fifo_write_buffer(&ai_camera_2_uart_fifo, &get_data, 1);  // д fifo 
	fifo_data_count = fifo_used(&ai_camera_2_uart_fifo);  // FIFOѯǰݸ
	fifo_read_buffer(&ai_camera_2_uart_fifo, ai_camera_2_fifo_buffer, &fifo_data_count, FIFO_READ_ONLY);
//	uart_write_buffer(AI_CAMERA_2_UART_INDEX, fifo_get_data_ai_camera_2, fifo_data_count_2);      // ȡݷͳȥ
	if(ai_camera_2_fifo_buffer[0] != 0x2C)
	{
		fifo_clear(&ai_camera_2_uart_fifo);
	}
	if(fifo_data_count >= 4)	//֡ͷ֡β
	{
		if(	ai_camera_2_fifo_buffer[0] == 0x2C && 
			ai_camera_2_fifo_buffer[1] == 0x12 && 
			ai_camera_2_fifo_buffer[fifo_data_count - 2] == 0x5B && 
			ai_camera_2_fifo_buffer[fifo_data_count - 1] == 0x5B )
			// ת
			detection_result_trans(AI_CAMERA_2, ai_camera_2_fifo_buffer[2]);		
		if(	ai_camera_2_fifo_buffer[fifo_data_count - 2] == 0x5B && 
			ai_camera_2_fifo_buffer[fifo_data_count - 1] == 0x5B ||
			fifo_data_count > 8)
		{
				fifo_clear(&ai_camera_2_uart_fifo);
		}
	}
}
#endif