////////////////////////////////////////////////////////////////////////////////

/// 自动回传模块文件
///
///
/// 包含相关回传功能 
/// @file    device_process.c 
/// @author  zrk 
/// @date    2017-11-23创建
/// @version v0.1 

////////////////////////////////////////////////////////////////////////////////
#include "gpio.h"
#include "app_cfg.h"
#include "ptz_header_file.h"

 PtzAutoReturn g_temp_auto_return;
 PtzAutoReturn g_voltage_auto_return;
 PtzAutoReturn g_speed_auto_return;
 PtzAutoReturn g_angle_auto_return;
 PtzAutoReturn g_current_auto_return;
 PtzAutoReturn g_vref_auto_return;
//extern PtzAutoReturn g_sca830d07_auto_return;
 PtzAutoReturn g_cpu_usage_auto_return;

extern void ptz_uart_dev_send(device_handle device, void *data, int len);

//自动发送电机驱动芯片的模拟电压
static void ptz_vref_auto_return(char dev, char vref_choice)
{
  unsigned short int uint16_vref;
  unsigned char data[7] = {0};
  if(vref_choice == PTZ_HORI)
  {
    uint16_vref = g_ptz.hori_pid.PidUT_uint;
    data[0] = 0xff;
    data[1] = g_ptz.address;
    data[2] = 0xbf;
    data[3] = 0x03;
    data[4] = uint16_vref >> 8;
    data[5] = uint16_vref & 0x00ff;
    data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  }
  else//vref_choice == PTZ_VERT
  {
    uint16_vref = g_ptz.vert_pid.PidUT_uint;
    data[0] = 0xff;
    data[1] = g_ptz.address;
    data[2] = 0xbf;
    data[3] = 0x04;
    data[4] = uint16_vref >> 8;
    data[5] = uint16_vref & 0x00ff;
    data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  }
  //发送数据
  switch(dev)
  {
    case PTZ_UART_422:
      ptz_uart_dev_send(uart_422_handle, data, sizeof(data));
      break;
      
    case PTZ_UART_485:
      ptz_uart_dev_send(uart_485_handle, data, sizeof(data));
      break;
      
    case PTZ_UDP:
      send_udp_data_aim(data,sizeof(data), 
      (struct sockaddr*)&g_vref_auto_return.from, g_vref_auto_return.fromlen);
      break;
  }
  
  //发送调试数据
//  if(g_ptz.debug_data.open_switch == PTZ_DUBUG_DATA_ON_B)
//  {
//    ptz_send_debug_data(data,sizeof(data));
//  }
}


//自动回传发送角度
static void ptz_angle_auto_return(char dev, char angle_choice)
{
  unsigned short int uint16_angle;
  unsigned char data[7] = {0};
  if(angle_choice == PTZ_HORI)
  {
    uint16_angle = (unsigned short int)(g_ptz.hori_angle_actual * 100 + 0.5);
    data[0] = 0xff;
    data[1] = g_ptz.address;
    data[3] = 0x59;
    data[4] = (u_int16_t)(uint16_angle >> 8);
    data[5] = (u_int16_t)(uint16_angle & 0x00ff);
    data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  }
  else//angle_choice == PTZ_VERT
  {
    uint16_angle = (unsigned short int)(g_ptz.vert_angle_actual * 100 + 0.5);
    data[0] = 0xff;
    data[1] = g_ptz.address;
    data[3] = 0x5b;
    data[4] = (u_int16_t)(uint16_angle >> 8);
    data[5] = (u_int16_t)(uint16_angle & 0x00ff);
    data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  }
  //发送数据
  switch(dev)
  {
    case PTZ_UART_422:
      ptz_uart_dev_send(uart_422_handle, data, sizeof(data));
      break;
      
    case PTZ_UART_485:
      ptz_uart_dev_send(uart_485_handle, data, sizeof(data));
      break;
      
    case PTZ_UDP:
      send_udp_data_aim(data, sizeof(data), 
      (struct sockaddr*)&g_angle_auto_return.from, g_angle_auto_return.fromlen);
      break;
  }
  
  //发送调试数据
//  if(g_ptz.debug_data.open_switch == PTZ_DUBUG_DATA_ON_B)
//  {
//    ptz_send_debug_data(data,sizeof(data));
//  }
}

//自动发送转速
static void ptz_speed_auto_return(char dev, char speed_choice)
{
  unsigned short int uint16_speed;
  unsigned char data[7] = {0};
  if(speed_choice == PTZ_HORI)
  {
    uint16_speed = (unsigned short int)(g_ptz.hori_speed_actual * 100 + 0.5);
    data[0] = 0xff;
    data[1] = g_ptz.address;
    data[2] = 0xd0;
    data[3] = 0x03;
    data[4] = (u_int16_t)(uint16_speed >> 8);
    data[5] = (u_int16_t)(uint16_speed & 0x00ff);
    data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  }
  else//speed_choice == PTZ_VERT
  {
    uint16_speed = (unsigned short int)(g_ptz.vert_speed_actual * 100 + 0.5);
    data[0] = 0xff;
    data[1] = g_ptz.address;
    data[2] = 0xd0;
    data[3] = 0x04;
    data[4] = (u_int16_t)(uint16_speed >> 8);
    data[5] = (u_int16_t)(uint16_speed & 0x00ff);
    data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  }
  //发送数据
  switch(dev)
  {
    case PTZ_UART_422:
      ptz_uart_dev_send(uart_422_handle, data, sizeof(data));
      break;
      
    case PTZ_UART_485:
      ptz_uart_dev_send(uart_485_handle, data, sizeof(data));
      break;
      
    case PTZ_UDP:
      send_udp_data_aim(data,sizeof(data), 
      (struct sockaddr*)&g_speed_auto_return.from, g_speed_auto_return.fromlen);
      break;
  }
  
//  //发送调试数据
//  if(g_ptz.debug_data.open_switch == PTZ_DUBUG_DATA_ON_B)
//  {
//    ptz_send_debug_data(data,sizeof(data));
//  }
}

//自动发送温度
static void ptz_temp_auto_return(char dev)
{
  unsigned short int uint16_temp;
  unsigned char data[7] = {0};
  uint16_temp = (unsigned short int)(g_ptz.temperature * 100 + 0.5);
  data[0] = 0xff;
  data[1] = g_ptz.address;
  data[2] = 0xd6;
  data[3] = (u_int16_t)(uint16_temp >> 8);
  data[4] = (u_int16_t)(uint16_temp & 0x00ff);
  data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  //发送数据
  switch(dev)
  {
    case PTZ_UART_422:
      ptz_uart_dev_send(uart_422_handle, data, sizeof(data));
      break;
      
    case PTZ_UART_485:
      ptz_uart_dev_send(uart_485_handle, data, sizeof(data));
      break;
      
    case PTZ_UDP:
      send_udp_data_aim(data,sizeof(data), 
      (struct sockaddr*)&g_temp_auto_return.from, g_temp_auto_return.fromlen);
      break;
  }
  
  //发送调试数据
//  if(g_ptz.debug_data.open_switch == PTZ_DUBUG_DATA_ON_B)
//  {
//    ptz_send_debug_data(data,sizeof(data));
//  }
}

//自动发送电压
static void ptz_voltage_auto_return(char dev)
{
  unsigned short int uint16_voltage;
  unsigned char data[7] = {0};
  uint16_voltage = (unsigned short int)(g_ptz.Voltage * 100 + 0.5);
  data[0] = 0xff;
  data[1] = g_ptz.address;
  data[2] = 0xcd;
  data[3] = (u_int16_t)(uint16_voltage >> 8);
  data[4] = (u_int16_t)(uint16_voltage & 0x00ff);
  data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  //发送数据
  switch(dev)
  {
    case PTZ_UART_422:
      ptz_uart_dev_send(uart_422_handle, data, sizeof(data));
      break;
      
    case PTZ_UART_485:
      ptz_uart_dev_send(uart_485_handle, data, sizeof(data));
      break;
      
    case PTZ_UDP:
      send_udp_data_aim(data,sizeof(data), 
     (struct sockaddr*)&g_voltage_auto_return.from, g_voltage_auto_return.fromlen);
      break;
  }
  
  //发送调试数据
//  if(g_ptz.debug_data.open_switch == PTZ_DUBUG_DATA_ON_B)
//  {
//    ptz_send_debug_data(data,sizeof(data));
//  }
}

//自动回传电流
static void ptz_current_auto_return(char dev)
{
  unsigned short int uint16_current;
  unsigned char data[7] = {0};
  uint16_current = (unsigned short int)(g_ptz.electric_current * 100 + 0.5);
  data[0] = 0xff;
  data[1] = g_ptz.address;
  data[2] = 0xc8;
  data[3] = (u_int16_t)(uint16_current >> 8);
  data[4] = (u_int16_t)(uint16_current & 0x00ff);
  data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  //发送数据
  switch(dev)
  {
    case PTZ_UART_422:
      ptz_uart_dev_send(uart_422_handle, data, sizeof(data));
      break;
      
    case PTZ_UART_485:
      ptz_uart_dev_send(uart_485_handle, data, sizeof(data));
      break;
      
    case PTZ_UDP:
      send_udp_data_aim(data,sizeof(data), 
     (struct sockaddr*)&g_current_auto_return.from, g_current_auto_return.fromlen);
      break;
  }
  
  //发送调试数据
//  if(g_ptz.debug_data.open_switch == PTZ_DUBUG_DATA_ON_B)
//  {
//    ptz_send_debug_data(data,sizeof(data));
//  }
}

//自动回传CPU占用率
static void ptz_cpu_usage_auto_return(char dev)
{
  unsigned char data[7] = {0};
  data[0] = 0xff;
  data[1] = g_ptz.address;
  data[2] = 0xb0;
  data[3] = 0;
  data[4] = OSCPUUsage;
  data[6] = MotorCalPelcoDSUM(data,sizeof(data));
  //发送数据
  switch(dev)
  {
    case PTZ_UART_422:
      ptz_uart_dev_send(uart_422_handle, data, sizeof(data));
      break;
      
    case PTZ_UART_485:
      ptz_uart_dev_send(uart_485_handle, data, sizeof(data));
      break;
      
    case PTZ_UDP:
      send_udp_data_aim(data,sizeof(data), 
     (struct sockaddr*)&g_cpu_usage_auto_return.from, g_cpu_usage_auto_return.fromlen);
      break;
  }
  
  //发送调试数据
//  if(g_ptz.debug_data.open_switch == PTZ_DUBUG_DATA_ON_B)
//  {
//    ptz_send_debug_data(data,sizeof(data));
//  }
}


///自动回传云台数据 
static void ptz_auto_return_task()
{
  while(1)
  {
    //网络自动回传
    //模拟电压
    if(g_vref_auto_return.udp_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_vref_auto_return.udp_timer = 
        g_vref_auto_return.udp_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_vref_auto_return.udp_timer >= g_vref_auto_return.udp_time &&
         g_vref_auto_return.udp_timer >= PTZ_VREF_AUTO_RETURN_TIME_MIN)
      {
        g_vref_auto_return.udp_timer = 0;
        ptz_vref_auto_return(PTZ_UDP, PTZ_HORI);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
        ptz_vref_auto_return(PTZ_UDP, PTZ_VERT);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    
    //角度
    if(g_angle_auto_return.udp_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_angle_auto_return.udp_timer = 
        g_angle_auto_return.udp_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_angle_auto_return.udp_timer >= g_angle_auto_return.udp_time &&
         g_angle_auto_return.udp_timer >= PTZ_ANGLE_AUTO_RETURN_TIME_MIN)
      {
        g_angle_auto_return.udp_timer = 0;
        ptz_angle_auto_return(PTZ_UDP, PTZ_HORI);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
        ptz_angle_auto_return(PTZ_UDP, PTZ_VERT);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    //转速
    if(g_speed_auto_return.udp_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_speed_auto_return.udp_timer = 
        g_speed_auto_return.udp_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_speed_auto_return.udp_timer >= g_speed_auto_return.udp_time &&
         g_speed_auto_return.udp_timer >= PTZ_SPEED_AUTO_RETURN_TIME_MIN)
      {
        g_speed_auto_return.udp_timer = 0;
        ptz_speed_auto_return(PTZ_UDP, PTZ_HORI);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
        ptz_speed_auto_return(PTZ_UDP, PTZ_VERT);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    //温度
    if(g_temp_auto_return.udp_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_temp_auto_return.udp_timer = 
        g_temp_auto_return.udp_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_temp_auto_return.udp_timer >= g_temp_auto_return.udp_time &&
         g_temp_auto_return.udp_timer >= PTZ_TEMP_AUTO_RETURN_TIME_MIN)
      {
        g_temp_auto_return.udp_timer = 0;
        ptz_temp_auto_return(PTZ_UDP);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    //电压
    if(g_voltage_auto_return.udp_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_voltage_auto_return.udp_timer = 
        g_voltage_auto_return.udp_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_voltage_auto_return.udp_timer >= g_voltage_auto_return.udp_time &&
         g_voltage_auto_return.udp_timer >= PTZ_VOLTAGE_AUTO_RETURN_TIME_MIN)
      {
        g_voltage_auto_return.udp_timer = 0;
        ptz_voltage_auto_return(PTZ_UDP);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    //电流
    if(g_current_auto_return.udp_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_current_auto_return.udp_timer = 
        g_current_auto_return.udp_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_current_auto_return.udp_timer >= g_current_auto_return.udp_time &&
         g_current_auto_return.udp_timer >= PTZ_CURRENT_AUTO_RETURN_TIME_MIN)
      {
        g_current_auto_return.udp_timer = 0;
        ptz_current_auto_return(PTZ_UDP);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    
    //倾角传感器sca830d07
//    if(g_sca830d07_auto_return.udp_swtich == PTZ_AUTO_RETURN_ON)
//    {
//      g_sca830d07_auto_return.udp_timer = 
//        g_sca830d07_auto_return.udp_timer + PTZ_AUTO_RETURN_TASK_TIME;
//      if(g_sca830d07_auto_return.udp_timer >= g_sca830d07_auto_return.udp_time &&
//         g_sca830d07_auto_return.udp_timer >= PTZ_SCA830D07_AUTO_RETURN_TIME_MIN)
//      {
//        g_sca830d07_auto_return.udp_timer = 0;
//        ptz_sca830d07_auto_return(PTZ_UDP);
//        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
//      }
//    }
    
    //CPU_usage
    if(g_cpu_usage_auto_return.udp_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_cpu_usage_auto_return.udp_timer = 
        g_cpu_usage_auto_return.udp_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_cpu_usage_auto_return.udp_timer >= g_cpu_usage_auto_return.udp_time &&
         g_cpu_usage_auto_return.udp_timer >= PTZ_CURRENT_AUTO_RETURN_TIME_MIN)
      {
        g_cpu_usage_auto_return.udp_timer = 0;
        ptz_cpu_usage_auto_return(PTZ_UDP);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    
    //串口自动回传
    //模拟电压
    if(g_vref_auto_return.uart_422_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_vref_auto_return.uart_422_timer = 
        g_vref_auto_return.uart_422_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_vref_auto_return.uart_422_timer >= g_vref_auto_return.uart_422_time &&
         g_vref_auto_return.uart_422_timer >= PTZ_VREF_AUTO_RETURN_TIME_MIN + 25)//串口实时回传角度，适当增加时间
      {
        g_vref_auto_return.uart_422_timer = 0;
        ptz_vref_auto_return(PTZ_UART_422, PTZ_HORI);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
        ptz_vref_auto_return(PTZ_UART_422, PTZ_VERT);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    
    //角度
    if(g_angle_auto_return.uart_422_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_angle_auto_return.uart_422_timer = 
        g_angle_auto_return.uart_422_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_angle_auto_return.uart_422_timer >= g_angle_auto_return.uart_422_time &&
         g_angle_auto_return.uart_422_timer >= PTZ_ANGLE_AUTO_RETURN_TIME_MIN + 25)//串口实时回传角度，适当增加时间
      {
        g_angle_auto_return.uart_422_timer = 0;
        ptz_angle_auto_return(PTZ_UART_422, PTZ_HORI);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
        ptz_angle_auto_return(PTZ_UART_422, PTZ_VERT);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    //转速
    if(g_speed_auto_return.uart_422_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_speed_auto_return.uart_422_timer = 
        g_speed_auto_return.uart_422_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_speed_auto_return.uart_422_timer >= g_speed_auto_return.uart_422_time &&
         g_speed_auto_return.uart_422_timer >= PTZ_SPEED_AUTO_RETURN_TIME_MIN + 100)
      {
        g_speed_auto_return.uart_422_timer = 0;
        ptz_speed_auto_return(PTZ_UART_422, PTZ_HORI);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
        ptz_speed_auto_return(PTZ_UART_422, PTZ_VERT);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    //温度
    if(g_temp_auto_return.uart_422_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_temp_auto_return.uart_422_timer = 
        g_temp_auto_return.uart_422_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_temp_auto_return.uart_422_timer >= g_temp_auto_return.uart_422_time &&
         g_temp_auto_return.uart_422_timer >= PTZ_TEMP_AUTO_RETURN_TIME_MIN * 2)
      {
        g_temp_auto_return.uart_422_timer = 0;
        ptz_temp_auto_return(PTZ_UART_422);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    //电压
    if(g_voltage_auto_return.uart_422_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_voltage_auto_return.uart_422_timer = 
        g_voltage_auto_return.uart_422_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_voltage_auto_return.uart_422_timer >= g_voltage_auto_return.uart_422_time &&
         g_voltage_auto_return.uart_422_timer >= PTZ_VOLTAGE_AUTO_RETURN_TIME_MIN * 2)
      {
        g_voltage_auto_return.uart_422_timer = 0;
        ptz_voltage_auto_return(PTZ_UART_422);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    //电流
    if(g_current_auto_return.uart_422_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_current_auto_return.uart_422_timer = 
        g_current_auto_return.uart_422_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_current_auto_return.uart_422_timer >= g_current_auto_return.uart_422_time &&
         g_current_auto_return.uart_422_timer >= PTZ_CURRENT_AUTO_RETURN_TIME_MIN * 2)
      {
        g_current_auto_return.uart_422_timer = 0;
        ptz_current_auto_return(PTZ_UART_422);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    
    //倾角传感器sca830d07
//    if(g_sca830d07_auto_return.uart_422_swtich == PTZ_AUTO_RETURN_ON)
//    {
//      g_sca830d07_auto_return.uart_422_timer = 
//        g_sca830d07_auto_return.uart_422_timer + PTZ_AUTO_RETURN_TASK_TIME;
//      if(g_sca830d07_auto_return.uart_422_timer >= g_sca830d07_auto_return.uart_422_time &&
//         g_sca830d07_auto_return.uart_422_timer >= PTZ_SCA830D07_AUTO_RETURN_TIME_MIN)
//      {
//        g_sca830d07_auto_return.uart_422_timer = 0;
//        ptz_sca830d07_auto_return(PTZ_UDP);
//        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
//      }
//    }
    
    //CPU_USAGE
    if(g_cpu_usage_auto_return.uart_422_swtich == PTZ_AUTO_RETURN_ON)
    {
      g_cpu_usage_auto_return.uart_422_timer = 
        g_cpu_usage_auto_return.uart_422_timer + PTZ_AUTO_RETURN_TASK_TIME;
      if(g_cpu_usage_auto_return.uart_422_timer >= g_cpu_usage_auto_return.uart_422_time &&
         g_cpu_usage_auto_return.uart_422_timer >= PTZ_CURRENT_AUTO_RETURN_TIME_MIN * 2)
      {
        g_cpu_usage_auto_return.uart_422_timer = 0;
        ptz_cpu_usage_auto_return(PTZ_UART_422);
        OSTimeDlyHMSM(0u, 0u, 0u, 1u);
      }
    }
    
    OSTimeDlyHMSM(0u, 0u, 0u, PTZ_AUTO_RETURN_TASK_TIME);
  }
  
}


///实时回传打开和关闭
void ptz_auto_return_off_on(char dev, PTZ_DATA_PACK *pack)
{
  if(dev == PTZ_UDP)
  {
    switch(pack->command[0])
    {
//      case 0xbe://云台倾角传感器角度回传
//       switch(pack->command[1])
//       {
//         case PTZ_AUTO_RETURN_ON://打开
//            //保存回传目标
//            g_sca830d07_auto_return.fromlen = ptz_fromlen;
//            memcpy(&g_sca830d07_auto_return.from,&ptz_from,ptz_fromlen);
//            //提取回传间隔时间
//            g_sca830d07_auto_return.udp_time = 
//              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
//            //计时器清0
//            g_sca830d07_auto_return.udp_timer = 0;
//            //打开回传
//            g_sca830d07_auto_return.udp_swtich = PTZ_AUTO_RETURN_ON;
//            break;
//            
//          default://关闭
//            g_sca830d07_auto_return.udp_swtich = PTZ_AUTO_RETURN_OFF;
//            break;
//        }
//        break;
      
      case 0xbf://L6235D模拟电压输入等级
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //保存回传目标
            g_vref_auto_return.fromlen = ptz_fromlen;
            memcpy(&g_vref_auto_return.from,&ptz_from,ptz_fromlen);
            //提取回传间隔时间
            g_vref_auto_return.udp_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_vref_auto_return.udp_timer = 0;
            //打开回传
            g_vref_auto_return.udp_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_vref_auto_return.udp_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
     
      case 0xc1://电流
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //保存回传目标
            g_current_auto_return.fromlen = ptz_fromlen;
            memcpy(&g_current_auto_return.from,&ptz_from,ptz_fromlen);
            //提取回传间隔时间
            g_current_auto_return.udp_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_current_auto_return.udp_timer = 0;
            //打开回传
            g_current_auto_return.udp_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_current_auto_return.udp_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
        
      case 0xcc://电压
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //保存回传目标
            g_voltage_auto_return.fromlen = ptz_fromlen;
            memcpy(&g_voltage_auto_return.from,&ptz_from,ptz_fromlen);
            //提取回传间隔时间
            g_voltage_auto_return.udp_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_voltage_auto_return.udp_timer = 0;
            //打开回传
            g_voltage_auto_return.udp_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_voltage_auto_return.udp_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
        
      case 0xd4://温度
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //保存回传目标
            g_temp_auto_return.fromlen = ptz_fromlen;
            memcpy(&g_temp_auto_return.from,&ptz_from,ptz_fromlen);
            //提取回传间隔时间
            g_temp_auto_return.udp_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_temp_auto_return.udp_timer = 0;
            //打开回传
            g_temp_auto_return.udp_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_temp_auto_return.udp_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
        
      case 0xdc://转速
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //保存回传目标
            g_speed_auto_return.fromlen = ptz_fromlen;
            memcpy(&g_speed_auto_return.from,&ptz_from,ptz_fromlen);
            //提取回传间隔时间
            g_speed_auto_return.udp_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_speed_auto_return.udp_timer = 0;
            //打开回传
            g_speed_auto_return.udp_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_speed_auto_return.udp_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
        
      case 0xe1://角度
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //保存回传目标
            g_angle_auto_return.fromlen = ptz_fromlen;
            memcpy(&g_angle_auto_return.from,&ptz_from,ptz_fromlen);
            //提取回传间隔时间
            g_angle_auto_return.udp_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_angle_auto_return.udp_timer = 0;
            //打开回传
            g_angle_auto_return.udp_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_angle_auto_return.udp_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
       
      case 0xb0://单片机占有率
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //保存回传目标
            g_cpu_usage_auto_return.fromlen = ptz_fromlen;
            memcpy(&g_cpu_usage_auto_return.from,&ptz_from,ptz_fromlen);
            //提取回传间隔时间
            g_cpu_usage_auto_return.udp_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_cpu_usage_auto_return.udp_timer = 0;
            //打开回传
            g_cpu_usage_auto_return.udp_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_cpu_usage_auto_return.udp_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
    }      
  }
  
  if(dev == PTZ_UART_422)
  {
    switch(pack->command[0])
    {
      case 0xbf://电流
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://vref
            //提取回传间隔时间
            g_vref_auto_return.uart_422_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_vref_auto_return.uart_422_timer = 0;
            //打开回传
            g_vref_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_vref_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
      case 0xc1://电流
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //提取回传间隔时间
            g_current_auto_return.uart_422_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_current_auto_return.uart_422_timer = 0;
            //打开回传
            g_current_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_current_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
        
      case 0xcc://电压
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //提取回传间隔时间
            g_voltage_auto_return.uart_422_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_voltage_auto_return.uart_422_timer = 0;
            //打开回传
            g_voltage_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_voltage_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
        
      case 0xd4://温度
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //提取回传间隔时间
            g_temp_auto_return.uart_422_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_temp_auto_return.uart_422_timer = 0;
            //打开回传
            g_temp_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_temp_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
        
      case 0xdc://转速
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //提取回传间隔时间
            g_speed_auto_return.uart_422_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_speed_auto_return.uart_422_timer = 0;
            //打开回传
            g_speed_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_speed_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
        
      case 0xe1://角度
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //提取回传间隔时间
            g_angle_auto_return.uart_422_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_angle_auto_return.uart_422_timer = 0;
            //打开回传
            g_angle_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_angle_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
       
      case 0xb0://单片机占有率
       switch(pack->command[1])
       {
         case PTZ_AUTO_RETURN_ON://打开
            //提取回传间隔时间
            g_cpu_usage_auto_return.uart_422_time = 
              (unsigned short int)((pack->data[0] << 8) | pack->data[1]);
            //计时器清0
            g_cpu_usage_auto_return.uart_422_timer = 0;
            //打开回传
            g_cpu_usage_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_ON;
            break;
            
          default://关闭
            g_cpu_usage_auto_return.uart_422_swtich = PTZ_AUTO_RETURN_OFF;
            break;
        }
        break;
    }      
  }
}




/// 创建自动回传任务

static OS_STK task_ptz_auto_return_stk[TASK_PTZ_AUTO_RETURN_STK_SIZE];
static void  task_create_ptz_auto_return (void)
{ 

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


void init_auto_return_module()
{   	
  task_create_ptz_auto_return();//创建自动回传任务	
}