

/*
 * usr_gpio.c
 *
 *	Created on: 2022年4月11日
 *		Author: amos
 */ 

#include "onechip_include_header.h"



extern uint32_t conter100ms;
extern uint32_t conter10ms;





#define MIN_TEMPERATUARE 5
#define MAX_TEMPERATUARE 120 
#define COOL_WORK_THD 5 
#define HEAT_WORK_THD 2.5
#define COOL_TEST_TIME 30 
#define HEAT_TEST_TIME 6000
#define COOL_TEST_PWR -50
#define HEAT_TEST_PWR 50 
#define THERM_ADC_CHN 5 
#define OVER_HEAT_EDGE 0.5 
#define HEAT_STABLE_TIME 300
#define MAX_REMOVE_GAP  100 
#define EER_NOEXIST_TIME_GAP  500 
#define HEAT_ARRIVE_EDGE 0.5 
#define GOTO_IDLE_GAP  3000 



int32_t tupeExist = 0;



extern void light_band_set(uint8_t mode_select, uint16_t speed_ms, uint32_t rgb_color);

volatile uint32_t g_u32AdcIntFlag;   //adc simple finish flag


uint32_t Read_Tupe(void) {
  return (HAL_GPIO_ReadPin(MINI7_V2_TUBE_SW_GPIO_Port,MINI7_V2_TUBE_SW_Pin)) ? 0 : 1; // 检查PF5引脚值
}




void updatParm_lysis(void) {
	sysinf.lysis.offset = sysinf.lysis_offset;
	sysinf.lysis.overHeat = sysinf.lysis_overHeat;
	sysinf.lysis.overHeatTime =  sysinf.lysis_overHeatTime;
	sysinf.lysis.pid.Kp = sysinf.lysis_Kp;
	sysinf.lysis.pid.Ki = sysinf.lysis_Ki ;
	sysinf.lysis.pid.Kd = sysinf.lysis_Kd;
}



/**
 * @brief	   Motor_Init
 *
 * @param		init key and optic input
 *
 * @return		0正常，1不能加热，2，不能制冷,3 传感器异常
 *
 * @details    上电，通过设置加热制冷，判断温度变化。
 */
uint32_t Therm_Check(void) {
  float firstTemp,heattemp;
  firstTemp = getADC_Chanel_value(1);
  if(firstTemp < MIN_TEMPERATUARE || firstTemp > MAX_TEMPERATUARE){
  		com_pwm_Set(sysinf.lysis.pwdId,100);
		return  THERMOSTAT_SENSER_EER;		
  }  
  com_pwm_Set(sysinf.lysis.pwdId,HEAT_TEST_PWR);
  HAL_Delay(HEAT_TEST_TIME);
  heattemp = getADC_Chanel_value(1);
  com_pwm_Set(sysinf.lysis.pwdId,0);
  if((heattemp - firstTemp) < HEAT_WORK_THD){
		return  THERMOSTAT_HEAT_EER;	
  }
  return THERMOSTAT_NOR;
}
/**
 * @brief	   set_lysis
 *
 * @param		goal 目标温度
 *
 * @return		无
 *
 * @details   设置目标温度。
 */
void set_lysis(float goal) {
   sysinf.lysis.pid.goal = goal;
   if(goal > (sysinf.lysis.overHeat + sysinf.lysis.pid.realtemp )){
		sysinf.lysis.pid_st = PID_OVERHEAT;
		sysinf.lysis.pid.goal += sysinf.lysis.overHeat;
   }else{
		sysinf.lysis.pid_st = PID_ADJ;
   }
   sysinf.lysis.pid.integral = 0;
   sysinf.lysis.pid_stable = 0;
}
void Close_therm(float goal) {  
   sysinf.lysis.pid_st = PID_IDLE;
   sysinf.lysis.pid.integral = 0;
   sysinf.lysis.pid_stable = 0;
  com_pwm_Set(sysinf.lysis.pwdId,0);
}

/**
 * @brief	   Therm_work
 *
 * @param		adjflag 调节标记
 *
 * 
 *
 * @details    温控器调节。
 */
uint32_t Pid_work(void) {
  static uint32_t chrConter = 0;
  lysis_ctr_def *ptherm = &sysinf.lysis;
  float temp = getADC_Chanel_value(1);  
  
  ptherm->pid.realtemp = temp + ptherm->offset;
  switch(ptherm->pid_st){
	case PID_OVERHEAT:{
		PIDRealize(&ptherm->pid);
		
        com_pwm_Set(ptherm->pwdId,ptherm->pid.variate);
		if (ptherm->pid.realtemp  > (ptherm->pid.goal - OVER_HEAT_EDGE)){
			ptherm->pid_st = PID_OVERHEATLAST;
			chrConter = 0;		   
		}
	}break;
	case PID_OVERHEATLAST:{
  		chrConter ++;
		if (chrConter  > ptherm->overHeatTime){
			ptherm->pid_st = PID_ADJ;
			/*PID功率值维持不变*/
			ptherm->pid.integral = (ptherm->overHeat*ptherm->pid.Kp)/ptherm->pid.Ki;	
			ptherm->pid.integral += (ptherm->pid.variate)/ptherm->pid.Ki;	
			ptherm->pid.goal -= ptherm->overHeat;
			chrConter = 0;
		}
	}break;
	case PID_ADJ:{
		PIDRealize(&ptherm->pid);
		if ((ptherm->pid.realtemp  > (ptherm->pid.goal - OVER_HEAT_EDGE)) && (ptherm->pid.realtemp  < (ptherm->pid.goal + OVER_HEAT_EDGE))){
			if(chrConter < HEAT_STABLE_TIME){
				chrConter ++;
			}
			else{
				/*上报加热稳定*/
				ptherm->pid_stable = 1;
			}
		}else{
			if(chrConter){chrConter--;}
			else{ptherm->pid_stable = 0;}
		}
	}break;
	default:
		ptherm->pid_st = PID_IDLE;
		ptherm->pid.variate = 0;
		break;
  }  
  com_pwm_Set(sysinf.lysis.pwdId,ptherm->pid.variate);
//   com_pwm_Set(sysinf.lysis.pwdId,80);
  return 0;
}

uint32_t get_lysis_st(float *temp,unsigned int *st) {

	*temp = sysinf.lysis.pid.realtemp;
	*st = sysinf.lysis.thermostat_st;
	return 0;
}
uint32_t lysis_work(void) {
  float temp;
  static uint32_t last10ms;
  lysis_ctr_def *ptherm = &sysinf.lysis;

  temp = getADC_Chanel_value(1);
  ptherm->pid.realtemp = temp + ptherm->offset;
  switch(ptherm->thermostat_st){
	case THERMOSTAT_PWRON:{
		
  		static float firstTemp;
		if(ptherm->firstin){
			ptherm->firstin = false;
			updateLysisState(SYS_STATE_PREPARE);sysinf.lysisState = SYS_STATE_PREPARE; 
			firstTemp = getADC_Chanel_value(1);
			if(firstTemp < MIN_TEMPERATUARE || firstTemp > MAX_TEMPERATUARE){
			 	error_set(ERROR_TEMPSENSOR_EER);
				ptherm->thermostat_st = THERMOSTAT_ERROR;
				ptherm->firstin = true;	
			 }else{
				 set_lysis(firstTemp+30);
				 last10ms = conter10ms;
			 }  
			
		}else{
			float heattemp = getADC_Chanel_value(1); 
			if((heattemp - firstTemp) > HEAT_WORK_THD){
					ptherm->thermostat_st = THERMOSTAT_WARM;
					ptherm->firstin = true;
					ptherm->pid_st = PID_IDLE;
			  }

			if((conter10ms-last10ms)> HEAT_TEST_TIME){
				/*超时判断*/
				ptherm->thermostat_st = THERMOSTAT_ERROR;
				updateLysisState(SYS_STATE_ERROR_HW);
				error_set(ERROR_HEATFUC_EER);
				ptherm->firstin = true;
			}
		}	
	}break;
	case THERMOSTAT_NOTICE:{
		if(ptherm->firstin){
			/*上电或者预热状态存在管管，停止加热*/			
			updateLysisState(SYS_STATE_ERROR_HM);
			set_lysis( 0);
			ptherm->firstin = false;
		}else{			
		    if(Read_Tupe() == 0){
			/*管管移除，进入到预热状态*/
				ptherm->thermostat_st = THERMOSTAT_WARM;
				error_clear();
				ptherm->firstin = true;
			}
		}	    	
	}break;
		
	case THERMOSTAT_WARM:{
		if(ptherm->firstin){
			
			updateLysisState(SYS_STATE_PREPARE);
			ptherm->firstin = false;
			if(ptherm->idletemp < 0.1){
				/*预热参数配置为0，无需要预热*/
				ptherm->thermostat_st = THERMOSTAT_IDLE;
				ptherm->firstin = true;
			}else{				
				set_lysis(ptherm->idletemp);
			}
		}else{
			/*预热到位，进入空闲状态*/
			if(ptherm->pid.realtemp > (ptherm->idletemp - HEAT_ARRIVE_EDGE)){
				ptherm->thermostat_st = THERMOSTAT_IDLE;
				ptherm->firstin = true;
			}
//		    if(Read_Tupe()){
//				/*预热插管管，进入提示状态*/
//				ptherm->thermostat_st = THERMOSTAT_NOTICE;
//				ptherm->firstin = TRUE;
//			}
		}
	}break;
	case THERMOSTAT_IDLE:{
		if(ptherm->firstin){			
			updateLysisState(SYS_STATE_READY);
			ptherm->firstin = false;
			Close_therm(0);
			ptherm->recGoalFlag = 0;
			
		}else{	
		    if(ptherm->recGoalFlag){
				set_lysis(ptherm->pid.goal);	
				ptherm->recGoalFlag = 0;
			}
			if(ptherm->pid_stable){
				ptherm->thermostat_st = THERMOSTAT_INSERT;
				ptherm->firstin = true;	
			}
//		    if(Read_Tupe()){
//				/*预热插管管，进入提示状态*/
//				ptherm->thermostat_st = THERMOSTAT_NOTICE;
//				ptherm->firstin = TRUE;
//			}
		}
	}break;
	case THERMOSTAT_INSERT:{
		if(ptherm->firstin){			
			updateLysisState(SYS_STATE_CHECK);
			ptherm->firstin = false;
		}else{	
		    if(Read_Tupe()){
				ptherm->thermostat_st = THERMOSTAT_WORK;
				ptherm->firstin = true;		
				}
		}
	}break;
	case THERMOSTAT_WORK:{
		if(ptherm->firstin){	
			updateLysisState(SYS_STATE_RUNING);
			ptherm->firstin = false;
			ptherm->pid_stable_time = 0;
		}else{
//		    if(Read_Tupe()){
//				last10ms = conter10ms;
//			}else{
//				if((conter10ms-last10ms)> MAX_REMOVE_GAP){
//					Close_therm(0);
//					ptherm->eerst = THERMOSTAT_TUBENOEXIST_EER;
//					ptherm->thermostat_st = THERMOSTAT_ERROR;
//					error_set(ERROR_TUPE_MOVE); 
//					last10ms = conter10ms;	
//					ptherm->firstin = TRUE;			
//				}
//			}
//			if(ptherm->pid_stable)
			{
				ptherm->pid_stable_time ++;
				if(ptherm->pid_stable_time > ptherm->worktime){
					ptherm->thermostat_st = THERMOSTAT_END;
					ptherm->firstin = true;		
				}
			}
		}
	}break;
	case THERMOSTAT_END:{
		if(ptherm->firstin){
			updateLysisState(SYS_STATE_WORKEND);
			ptherm->firstin = false;
			Close_therm(0);
			last10ms = conter10ms;
		}else{			
			if((conter10ms-last10ms)> GOTO_IDLE_GAP){
//			if(Read_Tupe() == 0){
			/*管管移除，进入到预热状态*/
				ptherm->thermostat_st = THERMOSTAT_WARM;
				ptherm->firstin = true;
			}
		}
	}break;
	case THERMOSTAT_ERROR:{
		if(ptherm->firstin){
			ptherm->firstin = false;
			Close_therm(0);
		}else{	
			if(ptherm->eerst == THERMOSTAT_TUBENOEXIST_EER){
				if((conter10ms-last10ms)> EER_NOEXIST_TIME_GAP){			
					ptherm->thermostat_st = THERMOSTAT_IDLE;
					error_clear();
					ptherm->firstin = true;		
				}
			}	
		}
	}break;
		
  }
  Pid_work();
  return 0;
}

uint32_t Close_Therm_work(void) {
  lysis_ctr_def *ptherm = &sysinf.lysis;
  ptherm->thermostat_st = THERMOSTAT_END;
  ptherm->firstin = true;
  return 0;
}

