#include "app_scan.h"
#include "bsp_rs2251.h"
#include "main.h"

#include "bsp_74hc595.h"
#include "bsp_adc.h"
#include "resi_math_lib.h"
#include "string.h"
#include "mx_serial.h"
#include "app_soft.h"
#include "bsp_can.h"
#include "systick.h"

#include "lib_cal_creep_resistance.h"
#include "app_calibration.h"
#include "mozen_protocol.h"
#include "app_mozen_handler.h"
#include "flash_port.h"
#include "bsp_flash.h"
enum scan_ad {
	changge_low,
	changge_low1,
	changge_low2,
	changge_low3,
	getAD_L0,
	getAD_L1,
	getAD_L2,
	getAD_L3,
	getAD_L4,
	getAD_L5,
	getAD_L6,
	getAD_L7,
	fast_l,
	changge_hig,
	changge_hig1,
	changge_hig2,
	changge_hig3,
	getAD_H0,
	getAD_H1,
	getAD_H2,
	getAD_H3,
	getAD_H4,
	getAD_H5,
	getAD_H6,
	getAD_H7,
	get_break
}state;
uint8_t hc595_ranks[] = {7,6,5,4,3,2,1,0,15,14,13,12};//初始化默认
int flag_send = 0;
volatile uint8_t scan_num = 0;

static uint16_t cailed_sensor_data[MT_AX_NUM][MT_AY_NUM];//放16bit的数组
static uint8_t frame8bit[MT_AX_NUM][MT_AY_NUM];//放8bit的数组

adc_value_frame_struct temp_adc_value;

uint64_t task_tick = 0;

uint8_t serial_tx_buffer[AX_NUM][AY_NUM] = {0};//原始数组

cal_creep_resistance_t creep_strength_value;
uint8_t Matrix_Stab[AX_NUM][AY_NUM] = {0};
uint8_t Matrix_Creep[AX_NUM][AY_NUM] = {0};
uint8_t Matrix_Display[AX_NUM][AY_NUM] = {0};
mx_frame_struct mx_sensor_frame;

int  print_counter = 0;


void Hc4051Delay(uint32_t num)
{
    while(num --);
}

void usart_event_handler(void)
{
    static uint16_t recv_len = 0;
    static uint8_t rx_buffer[1024] = {0x00};
    
	recv_len = usart_rx_recv(USART_3_TR, rx_buffer, sizeof(rx_buffer));

    if(recv_len > 0)
    {
		uint16_t i;
		for (i = 0; i < recv_len; i++) 
		{
			mozen_protocol_feed_byte(&g_mozen_prot, rx_buffer[i], 0);
		}    
   
    }

        
}


/*
* @function: 发送数据
* @param: frame: 数据帧
* @param: len: 数据长度
* @return: 0:成功 -1:失败
*/
static int mozen_uart_tx(const uint8_t *frame, uint16_t len)
{
    if ((frame == NULL) || (len == 0U)) {
        return -1;
    }

    usart_tx_push(USART_3_TR, (uint8_t *)frame,len);
    
    return 0;
}

void app_user_map_init(void){
	mx_sensor_frame.trantype = 0x01;
	mx_sensor_frame.type = 0x01;
	mx_sensor_frame.datalen = sizeof(serial_tx_buffer);
	creep_strength_value.x_max = AX_NUM;
	creep_strength_value.y_max = AY_NUM;

	creep_strength_value.matrix_stab = (uint8_t*)Matrix_Stab;
	creep_strength_value.matrix_creep = (uint8_t*)Matrix_Creep;
	creep_strength_value.matrix_display = (uint8_t*)Matrix_Display;
	
	app_mozen_init(mozen_uart_tx);
	flash_port_ops_t flash_ops = 
    {
        .read = flash_read,
        .write = flash_write,
        .erase = flash_erase,   
        .is_erased = bsp_flash_is_erased 
    };
	app_calibration_init(&flash_ops); 
}


uint64_t time_tmr1;
void AD_Sacn(){
//	if(state >= get_break){
//		return;
//	}
	switch(state){
		case changge_low: 
			REF_OUT(1);//低扫
			ic_74hc595_clean_0(32);
			break;
		case changge_low1: 
			break;
		case changge_low2: 
			break;
		case changge_low3: 
			break;
		case getAD_L0:
			temp_adc_value.sensor_iqr_ref_value[scan_num][0] = GetAdcValue(1);
			temp_adc_value.sensor_iqr_ref_value[scan_num][8] = GetAdcValue(0);
			Choose_AX_Channel(4);
			break;
		case getAD_L1:
			temp_adc_value.sensor_iqr_ref_value[scan_num][1] = GetAdcValue(1);
			temp_adc_value.sensor_iqr_ref_value[scan_num][9] = GetAdcValue(0);
			Choose_AX_Channel(2);			
			break;
		case getAD_L2:
			temp_adc_value.sensor_iqr_ref_value[scan_num][2] = GetAdcValue(1);
			temp_adc_value.sensor_iqr_ref_value[scan_num][10] = GetAdcValue(0);
			Choose_AX_Channel(1);			
			break;
		case getAD_L3:
			temp_adc_value.sensor_iqr_ref_value[scan_num][3] = GetAdcValue(1);
			temp_adc_value.sensor_iqr_ref_value[scan_num][11] = GetAdcValue(0);	
			Choose_AX_Channel(7);			
			break;
		case getAD_L4:
			temp_adc_value.sensor_iqr_ref_value[scan_num][7] = GetAdcValue(1);
			Choose_AX_Channel(5);			
			break;
		case getAD_L5:
			temp_adc_value.sensor_iqr_ref_value[scan_num][6] = GetAdcValue(1);
			Choose_AX_Channel(3);			
			break;
		case getAD_L6:
			temp_adc_value.sensor_iqr_ref_value[scan_num][5] = GetAdcValue(1);
			Choose_AX_Channel(0);			
			break;
		case getAD_L7:
			temp_adc_value.sensor_iqr_ref_value[scan_num][4] = GetAdcValue(1);
			Choose_AX_Channel(6);
			state = fast_l;
			break;
		case fast_l:
			REF_OUT(1);//低扫
			ic_74hc595_clean_0(32);
			break;
		case changge_hig:
			REF_OUT(0);
			ic_74hc595_io_write_1(hc595_ranks[scan_num]);
			break;
		case changge_hig1:
			
			break;
		case changge_hig2:
			
			break;
		case changge_hig3:
			
			break;
		case getAD_H0: 
			temp_adc_value.sensor_iqr_raw_value[0] = GetAdcValue(1);
			temp_adc_value.sensor_iqr_raw_value[8] = GetAdcValue(0);
			Choose_AX_Channel(4);
			break;
		case getAD_H1:
			temp_adc_value.sensor_iqr_raw_value[1] = GetAdcValue(1);
			temp_adc_value.sensor_iqr_raw_value[9] = GetAdcValue(0);	
			Choose_AX_Channel(2);
			break;
		case getAD_H2:
			temp_adc_value.sensor_iqr_raw_value[2] = GetAdcValue(1);
			temp_adc_value.sensor_iqr_raw_value[10] = GetAdcValue(0);
			Choose_AX_Channel(1);
			break;
		case getAD_H3: 
			temp_adc_value.sensor_iqr_raw_value[3] = GetAdcValue(1);
			temp_adc_value.sensor_iqr_raw_value[11] = GetAdcValue(0);
			Choose_AX_Channel(7);
			break;
		case getAD_H4: 
			temp_adc_value.sensor_iqr_raw_value[7] = GetAdcValue(1);
			Choose_AX_Channel(5);
			break;
		case getAD_H5:
			temp_adc_value.sensor_iqr_raw_value[6] = GetAdcValue(1);
			Choose_AX_Channel(3);
			break;
		case getAD_H6:
			temp_adc_value.sensor_iqr_raw_value[5] = GetAdcValue(1);
			Choose_AX_Channel(0);
			break;
		case getAD_H7: 
			temp_adc_value.sensor_iqr_raw_value[4] = GetAdcValue(1);
			Choose_AX_Channel(6);
			break;
		case get_break:
			return;
			
	}
	state++;
}

void IRQ_Scan(adc_value_frame_struct *adc_raw_value){
	static  math_resi_cal_t math_resi_cal = {0};
	static  int math_scan_num = 0;
	__disable_irq();
		math_scan_num = scan_num;
		memcpy(adc_raw_value->sensor_raw_value,adc_raw_value->sensor_iqr_raw_value,24);
		memcpy(adc_raw_value->sensor_ref_value,adc_raw_value->sensor_iqr_ref_value[math_scan_num],24);
		
		
		scan_num++;
		if(scan_num == AX_NUM)
		{
			scan_num = 0;
			flag_send= 1;
			print_counter++;
			if(print_counter == 10)
			{
				print_counter = 0;
			}
		}
		if(print_counter >= 9){
			state = changge_low;	
		}
		else
			state = fast_l;
	__enable_irq();

	math_resi_cal.sensor_adc_value = adc_raw_value->sensor_raw_value;
	math_resi_cal.resi_adc_value = adc_raw_value->sensor_ref_value;
	math_resi_cal.math_number = AX_NUM;
	
	
	
	math_resi_cali_once(&math_resi_cal,
						adc_raw_value->sensor_resi_output,
						AX_NUM,
						SCAN_LOW_LEVEL);

	math_display_resi(adc_raw_value->sensor_resi_output,
						adc_raw_value->sensor_display_output,
						AX_NUM,
						RESI_BACKWARDS,
						SINGLE_BYTE);

	memcpy(adc_raw_value->sensor_voltage[math_scan_num],adc_raw_value->sensor_display_output,AX_NUM);

}

void scan_deal(void){
	if(state >= get_break)
	{
		IRQ_Scan(&temp_adc_value);	
	}

}



void TMR1_OVF_TMR10_IRQHandler(void)
{
  // 检查溢出中断标志位，避免误触发
  if(tmr_flag_get(TMR1, TMR_OVF_FLAG) != RESET)
  {
		AD_Sacn();
    // 必须清除中断标志位，否则会无限触发中断
    tmr_flag_clear(TMR1, TMR_OVF_FLAG);
  }
}
int counter_fps;
int counter_fps1;
void adc_deal(void){
	if(flag_send == 1){
		flag_send = 0;
		memcpy(frame8bit,temp_adc_value.sensor_voltage,144);
		mx_sensor_frame.data = (uint8_t *)frame8bit;
		mx_serial_tx_frame(USART_3_TR,&mx_sensor_frame);
		counter_fps++;
		
	}
	if((get_system_tick() - task_tick) >= 1000){
		task_tick = get_system_tick();
		counter_fps1 = counter_fps;
		counter_fps = 0;
	}

}





