#ifdef __cplusplus
extern "C"
{
#endif

#include "type.h" 
#include "UWater_pt_app.h" 
#include "UWater_gpio_driver.h"
#include "UWater_timer1uS_driver.h"
#include "UWater_adc_driver.h"
#include "UWater_gp22_app.h"
#include "UWater_frame_app.h"
#include "UWater_sys.h"
#include "sys_processor.h"
	

static float s_g_temp_value = 20;//上电默认20℃
static u32 s_temp_adc_result = 0; 
static u8  s_temp_adc_if_done = 0;
	
static u32 s_pt_tickstart = 0;
	
//static u32 s_temp_adc_arr[PT_FILTER_NUM];	//20181025 去掉滑动滤波
//static u8 s_pt_arr_sub = 0;				//20181025 去掉滑动滤波
	
	
	
static u8  s_g_temp_machine_state_num = TEMP_S0; // 电源电压检测处理机状态号
//static float s_g_temp_value_last = 0;
static float s_g_temp_value_now;
float Temp_cal(float temp_result);

#if (TEMP_RM)//用热敏电阻

#else //用IC温度传感器
static float s_g_temp_voltage;
static float s_g_temp_result;
static float s_g_temp_error;
#endif

extern float temp_calculate_last;//获取温度数据
/***************************************************************
函数名称: u32 PtFilterJunZhi(u32 *data, u8 data_len)
功    能：中值 + 算数平均滤波
参    数：u32 *data 需要进行处理的数据数组地址
返回值  ：滤波完成后的值
 ****************************************************************/
//static u32 PtFilterJunZhi(u32 *data, u8 data_len)   //20181025 去掉滑动滤波
//{
//    u8 i, j, len, count;
//    u32 *p;
//    u32 temp = 0, sum = 0;

//    p = data;
//    len = data_len;

//    for (j = 0; j < len - 1; j++)
//    {
//        for (i = 0; i < len - 1 - j; i++)
//        {
//            if (p[i] > p[i + 1])
//            {
//                temp = p[i];
//                p[i] = p[i + 1];
//                p[i + 1] = temp;
//            }
//        }
//    }

//    for (count = 2; count < len - 2; count++)
//    {
//        sum += p[count];
//    }

//    return  (sum / (len - 4));
//}

/***********************温度传感器供电***************************
Function:
Description:
Input:
Return:
Others:
 ****************************************************************/
void TempADCPowEnable(void)
{
     Gpio_WriteOutputIO(TEMP_PWR_GPIOx, TEMP_PWR_GPIO_NUM, GPIO_PIN_SET);//高
}

void TempADCPowDisable(void)
{
    Gpio_WriteOutputIO(TEMP_PWR_GPIOx, TEMP_PWR_GPIO_NUM, GPIO_PIN_RESET);//低
}


/************************************************
Function: u8 TempStartTaskApp(void)
Description: 启动温度测量
Input:
return: 0 启动失败    1 启动成功
Others:
 *************************************************/
u8 TempStartTaskApp(void)
{
    if((s_g_temp_machine_state_num == TEMP_S0)&&(FixedTimeNbIoTUploadIfIdle() == TASK_IDLE))
    {
        TempADCPowEnable();
		s_pt_tickstart = Timer1usGetTick();	//power on delay
        s_g_temp_machine_state_num = TEMP_S2;
        return 1;
    }
    else
        return 0;
}
/************************************************
Function: float TempCheckValueApp(void)
Description: 查询温度数据
Input:
return: 温度数据
Others:
 *************************************************/
float TempCheckValueApp(void)
{
	#if WENDU_FANSUAN_OPEN
	s_g_temp_value =  temp_calculate_last;//反算温度的实时数值
	#else	
	if(s_g_temp_value > 90)
	{
		s_g_temp_value = 36;
	}
	else if(s_g_temp_value < -30)
	{
		s_g_temp_value = -1;
	}
	#endif
    return s_g_temp_value;	
}


void TempInitApp(void)
{
////	u8 i;	//20181025 去掉滑动滤波
    s_g_temp_machine_state_num = TEMP_S0;
       
    stc_gpio_cfg_t stcGpioCfg;

	DDL_ZERO_STRUCT(stcGpioCfg);
    //PF7 
    stcGpioCfg.enDir = GpioDirOut;    ///< 端口方向配置->输出
    stcGpioCfg.enDrv = GpioDrvL;     ///< 端口驱动能力配置->驱动能力
    stcGpioCfg.enPu = GpioPuDisable; ///< 端口上下拉配置->无
    stcGpioCfg.enPd = GpioPdDisable;
    stcGpioCfg.enOD = GpioOdDisable; ///< 端口开漏输出配置->开漏输出关闭
    stcGpioCfg.enCtrlMode = GpioAHB; ///< 端口输入/输出值寄存器总线控制模式配置->AHB
    Gpio_Init(TEMP_PWR_GPIOx, TEMP_PWR_GPIO_NUM, &stcGpioCfg);
    Gpio_WriteOutputIO(TEMP_PWR_GPIOx, TEMP_PWR_GPIO_NUM, GPIO_PIN_RESET);//输出低电平
	
	#if WENDU_FANSUAN_OPEN
	
	#else
    TempStartTaskApp();//上电就立马启动温度采集 添加原因：表计复位后，非检定6s才启动温度采集，导致前6s的温度值是0
	#endif
}

/************************************************
Function: TempPreSleep()
Description: 休眠前处理函数
Input:
return: 
Others:
 *************************************************/
void TempPreSleep(void)
{
     Gpio_WriteOutputIO(TEMP_PWR_GPIOx, TEMP_PWR_GPIO_NUM, GPIO_PIN_RESET);//低
}

/************************************************
Function: TempWakeSleep()
Description: 唤醒处理函数
Input:
return:
Others:
 *************************************************/
void TempWakeSleep(void)
{
    ; //空
}

u8 TempIfSleepApp(void)
{
    if(s_g_temp_machine_state_num == TEMP_S0)
        return TRUE;
    else
        return FALSE;
}


void TempMachineApp(void)
{
    switch (s_g_temp_machine_state_num)
    {
        case TEMP_S0:
        {
            break;
        }
        case TEMP_S1:
        {
            if(Timer1usGetTick() - s_pt_tickstart > POWER_ON_DELAY)
            {
				s_g_temp_machine_state_num = TEMP_S2;
            }
            break;
        }
        case TEMP_S2:
        {
            if(TASK_IDLE == AdcIfIdle())	//温度
            {
//				AdcStartConvert(T_CHECK_ADC, T_CHECK_CHANNEL, T_CHECK_NUM ,T_CHECK_INTERVAL,&s_temp_adc_arr[s_pt_arr_sub],&s_temp_adc_if_done);
				AdcStartConvert(T_CHECK_CHANNEL, T_CHECK_NUM ,T_CHECK_INTERVAL,&s_temp_adc_result,&s_temp_adc_if_done);//20181025 去掉滑动滤波
				s_g_temp_machine_state_num = TEMP_S3;
            }
            break;
        }
        case TEMP_S3:
        {
            if (TRUE == s_temp_adc_if_done)
            {
                if (ADC_OK == AdcGetStatus())
                {
					#if (TEMP_RM)//用热敏传感器
					if(s_temp_adc_result>TEMP_SEGMENT_SMALL)
					{
						s_g_temp_value_now = (float)0.000000004785*s_temp_adc_result*s_temp_adc_result*s_temp_adc_result -(float)0.0000595*s_temp_adc_result*s_temp_adc_result + (float)0.2187*s_temp_adc_result -(float)247.9;//温度传感器建模-20~3
						s_g_temp_value_now = s_g_temp_value_now - (float)0.7;
					}
					else if(s_temp_adc_result>TEMP_SEGMENT_MIDDLE)
					{
						 s_g_temp_value_now = (float)-0.000000002058*s_temp_adc_result*s_temp_adc_result*s_temp_adc_result + (float)0.00001525*s_temp_adc_result*s_temp_adc_result - (float)0.05455*s_temp_adc_result + (float)82.24;//3~43
					}
					else 
					{
						  s_g_temp_value_now = (float)-0.00000005296*s_temp_adc_result*s_temp_adc_result*s_temp_adc_result + (float)0.0001457*s_temp_adc_result*s_temp_adc_result - (float)0.1697*s_temp_adc_result + (float)117.5;//43度以上
					}
					#else
//					s_g_temp_value_now = s_g_temp_result;
					
//					s_temp_adc_result = PtFilterJunZhi(s_temp_adc_arr, PT_FILTER_NUM); //滑动平均滤波
//					s_pt_arr_sub++;
//					s_pt_arr_sub %= PT_FILTER_NUM;
					
					//根据temp_adc_result计算温度值曲线

//					s_g_temp_voltage = ((float)s_g_temp_result)*1.2/10000.0/TMEP_NUM;
//					s_g_temp_voltage = (s_g_temp_voltage - 0.5)/0.01;
//					s_g_temp_error = 0.000244*(125 - s_g_temp_voltage)*(s_g_temp_voltage +40) - 2;
					
					s_g_temp_voltage = (float)250*s_temp_adc_result/(float)4095; //放大100倍
					

					s_g_temp_result = s_g_temp_voltage;
					if(s_g_temp_result >= 50)
					{
						s_g_temp_result -= 50;
						s_g_temp_voltage =  s_g_temp_result;

						if(s_g_temp_result < 125)
						{
							s_g_temp_result = (125 - s_g_temp_result) * (s_g_temp_result + 40);
							s_g_temp_result = s_g_temp_result * (float)0.000244;
							s_g_temp_error = s_g_temp_result - 2;
						}
						else
						{
							s_g_temp_error = 0;
						}
					}
					else
					{
						s_g_temp_result = 50 - s_g_temp_result;
						s_g_temp_voltage = - s_g_temp_result;
						if(s_g_temp_result >= 40)
						{
							s_g_temp_result = (125 + s_g_temp_result) * (40 - s_g_temp_result );
							s_g_temp_result = s_g_temp_result * (float)0.000244;
							s_g_temp_error = s_g_temp_result - 2;
						}
						else
						{
							s_g_temp_error = 0;
						}
					}
					s_g_temp_value_now = s_g_temp_voltage - s_g_temp_error;
					#endif
                    
//					if(s_g_temp_value_last == 0)
//					{
//						;
//					}
//					else if(s_g_temp_value_now >= s_g_temp_value_last)
//					{
//						if((s_g_temp_value_now - s_g_temp_value_last) > 1)
//						{
//							s_g_temp_value_now = s_g_temp_value_last + (float)0.1*(s_g_temp_value_now - s_g_temp_value_last);
//						}
//						else
//						{
//							s_g_temp_value_now = s_g_temp_value_last + (float)0.1*(s_g_temp_value_now - s_g_temp_value_last);
//						}
//					}
//					else
//					{
//						if((s_g_temp_value_last - s_g_temp_value_now) > 1)
//						{
//							s_g_temp_value_now = s_g_temp_value_last + (float)0.1*(s_g_temp_value_now - s_g_temp_value_last);
//						}
//						else
//						{
//							s_g_temp_value_now = s_g_temp_value_last + (float)0.1*(s_g_temp_value_now - s_g_temp_value_last);
//						}
//					}
//					s_g_temp_value_last = s_g_temp_value_now;
					s_g_temp_value = Temp_cal(s_g_temp_value_now);
                }
                TempADCPowDisable();
					
                s_g_temp_machine_state_num = TEMP_S0;
            }
            break;
        }
        default:
            break;
    }
}

float Temp_cal(float temp_result)
{
//    float temp = temp_result ;
    float temp;
    u32 heat_temp = FrameCheckParaApp(FramePara_Tc0);
    if(heat_temp >= 0x80000000)
    {
        heat_temp &= 0x7FFFFFFF;
        temp = temp_result - Gp30DataToFloat(heat_temp);
    }
    else
    {
        temp = temp_result + Gp30DataToFloat(heat_temp);
    }
    #if (TEMP_RM)//热敏电阻
	
	#else//IC传感器才生效
        
        if(temp >= (float)23)
        {
            temp += (float)0.2;
        }
        else if(temp > 15)
        {
            
        }
        else if(temp >= (float)8.5)
        {
    //        temp -= (float)0.3;
        }
        else
        {
            temp -= (float)(0.55 );
        }
    //    if(temp <= 0)
    //    {
    //        temp = temp*(-1);
    //        heat_temp = FloatToGp30Data(temp);
    //        heat_temp = heat_temp|0x80000000;
    //        return heat_temp;
    //    }
    //    else
    //    {
    //        return FloatToGp30Data(temp);
    //    }       
    #endif
    return (temp);
}




#ifdef __cplusplus
}
#endif		//#ifdef __cplusplus
