#include "Basechecker.h"
#include "base/utility.h"
#include "hardware/power.h"
#include "base/delay.h"
#include "hardware/gpio_cfg.h"
Checker_RunCfg_st checker_runcfg;
uint8_t Checker_FacBuf[CHECKER_FAC_BUF_LEN];
volatile uint8_t ThreeBindResult_flag = 1;
volatile uint16_t Checker_First_Set_Voltage = 88;
/*
@brief 标记执行结果
@param rtv 执行返回值 1表示失败 0 表示成功
@param taskindex 任务项目ID号
*/
void Checker_MaskResult(uint8_t rtv,uint8_t taskindex)
{
	uint8_t uc_index = taskindex >> 3;
	uint8_t uc_bitindex = taskindex & 0x07;
	if(taskindex >= CHECKER_MAXRT_COUNT)
	{
		return;
	}
	checker_runcfg.excue_rtv = rtv;
	uc_bitindex = (0x01<<uc_bitindex) & 0xFF;
	if(rtv == 0)
	{
		checker_runcfg.Task_Result[uc_index] &= (~uc_bitindex);
	}else{
		checker_runcfg.Task_Result[uc_index] |= uc_bitindex;
	}
	CurrentSampleR_Def;
}
/*
@brief 标记任务序列执行
@param taskindex 任务序列号
*/
void Checker_Excueindex(uint8_t taskindex)
{
	uint8_t uc_index = taskindex >> 3;
	uint8_t uc_bitindex = taskindex & 0x07;
	if(taskindex >= CHECKER_MAXRT_COUNT)
	{
		return;
	}
	uc_bitindex = (0x01<<uc_bitindex) & 0xFF;
  checker_runcfg.Task_Excute[uc_index] |= uc_bitindex;
	
}


/*
@brief  保存测试结果值
@param  *pus_rtv 测量结果缓存
@param  count 缓存个数
*/
void Checker_SetRtv(uint16_t* pus_rtv, uint8_t count)
{
	while(count > 0)
	{
		checker_runcfg.Test_Rtv[checker_runcfg.rtv_index++] = *pus_rtv;
		pus_rtv++;
		count--;
	}
}

/*
brief 准备测试用电压
@param 设置电压值，最多支持10个参数
*/
void Checker_PowerPrapare(void)
{
	uint8_t uc_index = 0;
	uint8_t uc_rtv = 0;
	uint16_t us_h_v,us_m_v;
	if(checker_runcfg.power_prapare_exe > 0 || checker_runcfg.param_count == 0){
		Checker_MaskResult(uc_rtv,checker_runcfg.task_info.runindex);
		return ;
	}
	Checker_First_Set_Voltage = checker_runcfg.params[0];
	Bubble_Sort_u16(checker_runcfg.params,checker_runcfg.param_count);
	POWER_OFF;
	while(uc_index < checker_runcfg.param_count)
	{
		us_h_v = checker_runcfg.params[uc_index];
		uc_index++;
		if(us_h_v < 55)
		{
			uc_rtv = 1;
			continue;
		}
		us_m_v = (us_h_v>>1) > 50 ? (us_h_v>>1):50;
		uc_rtv |= PowerCalibration_set(us_h_v,us_m_v);
		
	}
	PowerCalibration_set(POWER_DEF_V,POWER_DEF_V-10);
	Checker_MaskResult(uc_rtv,checker_runcfg.task_info.runindex);
}

/*
@brief 延时
@pram1 延时时间 0.1S
@param 使能总线电流监控
@rtv1  波动AD值
*/
void Checker_WaitDelay(void)
{
	uint32_t ul_delaytime =  checker_runcfg.params[0];
	uint8_t us_monito_flag = checker_runcfg.params[1];
	uint16_t us_temp;
	if(us_monito_flag == 0)
	{
		while(ul_delaytime > 0)
		{
			delay_os_ms(100);
			ul_delaytime--;
		}
	}else{
		
		Power_SetSampleRange_Seep(Checker_MonitorBuf_Sample_R,ADC_SPEED_MIDLE);
		delay_ms(1);
		us_temp = AD_CurMonitor(ul_delaytime*1000);
		CurrentSampleR_Def;
	}
	Power_SetSampleCurrentRange(Current_Max);
	Checker_MaskResult(0,checker_runcfg.task_info.runindex);
	Checker_SetRtv(&us_temp,checker_runcfg.rtv_count);
}

/*
@brief 电阻检测
@param0 电阻检测模式 0 4线制 1 2线 1-4，2 2线 2-3 通 
@rtv0 电阻检测值
*/
void Checker_ResistorSample(void)
{
	/*
			1 通道1-4通，过桥丝
			2 通道2-3通，过桥丝
			3 通道1-3通，测阻抗
			4 通道2-4通，测阻抗
	*/
	uint16_t aus_sample[5];
	uint16_t us_resistor;
	uint8_t uc_index = 0;
	AD_SampleResistor(aus_sample);
	for(uc_index = 0; uc_index < 5; uc_index++)
	{
		aus_sample[uc_index] -= board_st.resistor_diff;
	}
	switch(checker_runcfg.params[0])
	{
		case 0: us_resistor = aus_sample[0];break;
		case 1: us_resistor = aus_sample[1];break;
		case 2: us_resistor = aus_sample[2];break;
		case 3: us_resistor = aus_sample[3];break;
		case 4: us_resistor = aus_sample[4];break;
    case 5: us_resistor = board_st.resistor_diff;break;
		default : us_resistor = 0;break;
	}
	Checker_MaskResult(0,checker_runcfg.task_info.runindex);
	Checker_SetRtv(&us_resistor,checker_runcfg.rtv_count);
}

/*
@brief 判断是否需要注码
@rtv 0 不需要注码  1 注码
*/
uint8_t JQXT_Test_IsNeedBind(void)
{
  uint8_t uc_index = 0;
  
  if(checker_runcfg.uid_len == 0 || checker_runcfg.pwd_len == 0)
  {
      return 0;
  }
  for(uc_index = 0; uc_index < checker_runcfg.uid_len ; uc_index++)
  {
      if(checker_runcfg.writeuid[uc_index] != 0)
      {
          break;
      }
  }
  if(uc_index != checker_runcfg.uid_len)
  {
      return 1;
  }
  
  for(uc_index = 0; uc_index < checker_runcfg.pwd_len ; uc_index++)
  {
      if(checker_runcfg.writepwd[uc_index] != 0)
      {
          break;
      }
  }
  if(uc_index != checker_runcfg.pwd_len)
  {
      return 1;
  }
  return 0;
}
/*
检测执行函数是否正常
*/
uint8_t Checker_FunIsValid(CheckerTask* taskArray,uint16_t run_index)
{
	uint16_t index = 0;
	while((*taskArray !=0) && (index < CHECKER_MAXID_COUNT))
	{
		index++;
	}
	if(run_index >= index)
	{
		return 1;
	}
	return 0;
}
/*
@brief 缓存空间检测
@rtv 1 失败 0 成功
*/
uint8_t Checker_FacBufCheck()
{
	uint8_t uc_len = Checker_FacBuf[0];
	uint8_t uc_crc = Checker_FacBuf[1];
	if(uc_len > (CHECKER_FAC_BUF_LEN-2) || uc_len == 0)
	{
		return  1;
	}
	if(uc_crc != CheckCRC_8(Checker_FacBuf+2,uc_len))
	{
		return 1;
	}
	return 0;
}
