#include "ptz_header_file.h"
#include "device_heatresistor.h"
#include "tmp75.h"
#include "agent_hyt.h"

///加热电阻
HeatResistor heat_resistor;

//加热电阻开关
void ptz_heat_resistor_switch(PTZ_DATA_PACK *pack)
{
  if(pack->command[1] == 1)
  {//打开电阻加热
    heat_resistor.pwm_duty_cycle = ((float)((pack->data[0] << 8) | pack->data[1])) / 100.0;
    heat_resistor.heat_switch = PTZ_HEAT_RESISTOR_ON;
  }
  else
  {
    heat_resistor.pwm_duty_cycle = 0;
    heat_resistor.heat_switch = PTZ_HEAT_RESISTOR_OFF_1;
  }
}

//加热电阻初始化
static void ptz_heat_resistor_init()
{
  //总线时钟使能
  rcu_periph_clock_enable(RCU_GPIOE);

  gpio_mode_set(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0| GPIO_PIN_1);
  gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, GPIO_PIN_0| GPIO_PIN_1);
  
  PTZ_HEAT_RESISTOR_A_OFF;
  PTZ_HEAT_RESISTOR_B_OFF;
}

//电阻加热任务
static void ptz_heat_resistor_task()
{
  while(1)
  {
    switch(heat_resistor.heat_switch)
    {
      case PTZ_HEAT_RESISTOR_ON:
        //加热时间清0
        heat_resistor.heat_timer_ms  = 0;
        heat_resistor.heat_timer_s   = 0;
        heat_resistor.heat_timer_min = 0;
        //PWM波控制总周期
        heat_resistor.pwm_t = PTZ_HEAT_RESISTOR_PWM_T;
        //计算允许最大的占空比
        heat_resistor.pwm_duty_cycle_max = 
          PTZ_HEAT_RESISTOR_I_MAX * PTZ_HEAT_RESISTOR_R / PTZ_HEAT_RESISTOR_V_MAX;
        //判断输入占空比是否满足要求
        if(heat_resistor.pwm_duty_cycle > heat_resistor.pwm_duty_cycle_max)
        {
          heat_resistor.pwm_duty_cycle = heat_resistor.pwm_duty_cycle_max;
        }
        //根据占空比计算电阻通电和关电的时间
        heat_resistor.pwm_power_on_t = 
          (unsigned short int)(heat_resistor.pwm_duty_cycle * heat_resistor.pwm_t + 0.5);
        heat_resistor.pwm_power_off_t = heat_resistor.pwm_t - heat_resistor.pwm_power_on_t;
        //根据占空比计算电阻两端实际电压
        heat_resistor.resistor_v = 
          heat_resistor.pwm_duty_cycle * PTZ_HEAT_RESISTOR_V_MAX;
        //根据电阻两端实际电压计算电阻电流
        heat_resistor.resistor_i = heat_resistor.resistor_v / PTZ_HEAT_RESISTOR_R;
        heat_resistor.heat_switch = PTZ_HEAT_RESISTOR_ON_PWM;
        OSTimeDlyHMSM(0u, 0u, 0u, 20u);
        break;
      
      case PTZ_HEAT_RESISTOR_ON_PWM:
        //加热PWM输出
        PTZ_HEAT_RESISTOR_A_ON;
        PTZ_HEAT_RESISTOR_B_ON;
        OSTimeDlyHMSM(0u, 0u, 0u, heat_resistor.pwm_power_on_t);
        PTZ_HEAT_RESISTOR_A_OFF;
        PTZ_HEAT_RESISTOR_B_OFF;
        OSTimeDlyHMSM(0u, 0u, 0u, heat_resistor.pwm_power_off_t);
        //加热时间计算
        heat_resistor.heat_timer_ms = 
          heat_resistor.heat_timer_ms + heat_resistor.pwm_t;
        if(heat_resistor.heat_timer_ms >= 1000)
        {
          heat_resistor.heat_timer_s ++;
          heat_resistor.heat_timer_ms = heat_resistor.heat_timer_ms - 1000;
        }
        if(heat_resistor.heat_timer_s >= 60)
        {
          heat_resistor.heat_timer_min ++;
          heat_resistor.heat_timer_s = heat_resistor.heat_timer_s - 60;
        }
        //加热保护机制，云台本来温度就合适，防止加热电阻被误打开
        if(g_ptz.temperature >= PTZ_HEAT_RESISTOR_TEMP)
        {
          heat_resistor.heat_switch = PTZ_HEAT_RESISTOR_OFF_1;//关闭加热
        }
        break;
      
      case PTZ_HEAT_RESISTOR_OFF_1:
        PTZ_HEAT_RESISTOR_A_OFF;
        PTZ_HEAT_RESISTOR_B_OFF;
        heat_resistor.heat_timer_ms  = 0;
        heat_resistor.heat_timer_s   = 0;
        heat_resistor.heat_timer_min = 0;
        heat_resistor.heat_switch = PTZ_HEAT_RESISTOR_OFF_0;
        OSTimeDlyHMSM(0u, 0u, 0u, 20u);
        break;
        
      default://PTZ_HEAT_RESISTOR_OFF_0
        OSTimeDlyHMSM(0u, 0u, 0u, 1000u);
        break;
    }

    OSTimeDlyHMSM(0u, 0u, 0u, 50u);
  }
}



static OS_STK  task_heat_resistor_stk[TASK_PTZ_HEAT_RESISTOR_STK_SIZE];
static  void  creat_task_heat_resistor(void)
{ 
   CPU_INT08U  task_err;   
   CPU_INT08U  name_err;
   
   task_err = OSTaskCreateExt((void (*)(void *)) ptz_heat_resistor_task,           
		   (void           *) 0,
		   (OS_STK         *)&task_heat_resistor_stk[TASK_PTZ_HEAT_RESISTOR_STK_SIZE - 1],
		   (INT8U           ) TASK_PTZ_HEAT_RESISTOR_PRIO,                   
		   (INT16U          ) TASK_PTZ_HEAT_RESISTOR_PRIO,
		   (OS_STK         *)&task_heat_resistor_stk[0],
		   (INT32U          ) TASK_PTZ_HEAT_RESISTOR_STK_SIZE,
		   (void           *) 0,
		   (INT16U          )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));     
    #if (OS_TASK_NAME_EN > 0)
    OSTaskNameSet(TASK_PTZ_HEAT_RESISTOR_PRIO, "ptz_heat_resistor_task", &name_err);
    #endif
       
    if ((task_err == OS_ERR_NONE) && (name_err == OS_ERR_NONE)) {
        pdebug(DEBUG_LEVEL_INFO,"create ptz_heat_resistor_task  success...\n\r");
	} else { 
        pdebug(DEBUG_LEVEL_FATAL,"create ptz_heat_resistor_task  failed...\n\r");
	}
}

void init_heat_resistor_module()
{
  ptz_heat_resistor_init();
  creat_task_heat_resistor();
  
}
