#include "ptz_struct.h"
#include "bsp_os.h"
#include "sock_utils.h"
#include "ptz_type_select.h"
#include "ptz_global_variable.h"

//用于保存云台的各项状态和数据
PtzState g_ptz;

char aux_switch_cfg_read()
{
  Flash_Read((u_int8_t *)&g_ptz.power,AUX_SWITCH_ADDR_BEGIN,sizeof(g_ptz.power));
  if(g_ptz.power.crc == ((~(g_ptz.power.aux_switch_1+g_ptz.power.aux_switch_2+g_ptz.power.aux_switch_3+g_ptz.power.aux_switch_4))&0xFF))
  {
    return 1;
  }
  return 0;
}

char aux_switch_cfg_save()
{
  AuxSwitchPower aux_cfg={0x00};
  char crc=0;
  crc = ~(g_ptz.power.aux_switch_1+g_ptz.power.aux_switch_2+g_ptz.power.aux_switch_3+g_ptz.power.aux_switch_4)&0xFF;
  g_ptz.power.crc=crc;
  Flash_Erase_Sector(0,7);
  Flash_Write_MorePage((u_int8_t *)&g_ptz.power, AUX_SWITCH_ADDR_BEGIN, sizeof(g_ptz.power));
  
  Flash_Read((u_int8_t *)&aux_cfg,AUX_SWITCH_ADDR_BEGIN,sizeof(aux_cfg));
  
  if(aux_cfg.crc == (~(aux_cfg.aux_switch_1+aux_cfg.aux_switch_2+aux_cfg.aux_switch_3+aux_cfg.aux_switch_4)&0xFF))
  {
    return 1;
  }
  return 0;
}

//电源控制初始化
void ptz_aux_switch_init()
{
    //总线时钟使能
  rcu_periph_clock_enable(RCU_GPIOE);
  
  gpio_mode_set(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_3);
  gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  
  gpio_mode_set(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_2);
  gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
  
  if(aux_switch_cfg_read())
  {
    if(g_ptz.power.aux_switch_3 == POWER_ON)
    {
      PHOTO_POWER_PHOTO_ON;
    }
    if(g_ptz.power.aux_switch_3 == POWER_OFF)
    {
      PHOTO_POWER_PHOTO_OFF;
    }
    if(g_ptz.power.aux_switch_4 == POWER_ON)
    {
      FARIR_POWER_FARIR_ON;
    }
    if(g_ptz.power.aux_switch_4 == POWER_OFF)
    {
      FARIR_POWER_FARIR_OFF;
    }
  }else{
    //读取失败
    PHOTO_POWER_PHOTO_OFF;
    g_ptz.power.aux_switch_3 = POWER_OFF;
    
    FARIR_POWER_FARIR_OFF;
    g_ptz.power.aux_switch_4 = POWER_OFF;
  }
}

void ptz_return_clear()
{//清除回传标志
  g_ptz.preset_bit_return.preset_bit_location_cmd  = 0;
  g_ptz.location_return.hori_cmd = 0;
  g_ptz.location_return.vert_cmd = 0;
}

/*
 ******************************************************************************
 *              
 *                            软件版本号说明
 *
 *   1、软件版本号“g_program_version_number”中的内容与bin,hpk文件一致
 *   2、云台类型“g_ptz_type”中内容包括云台型号，硬件版本号，自检或不自检等功能
 *   
 *   软件版本号命名规则：云台型号（缩写）+底板型号（缩写）+软件版本号
 *
 *   24V重型涡轮蜗杆云台为例生成的软件版本：HW17_01A-3S24_BLDC_V10-SV10_21112401
 *
 *  云台型号：HW17_01A
 *  1、云台重量级标识：      L--轻型    M--中型    H--重型  ......
 *  2、云台传动方式标识：    W--涡轮蜗杆    Z--直齿  ......
 *  3、云台发布年份：        17--2017年发布  ......
 *  4、云台型号标识：        01A  ......
 *  底板型号：3S24_BLDC_V10
 *  5、云台硬件接口：        3S--3个网口接口  ......
 *  6、云台底部电压：        24--24V   12--12V  ......
 *  7、云台驱动电机：        BLDC--无刷直流电机   STEP--步进电机  ......
 *  8、底板型号：            V10  ......
 *  软件版本号：SV10_21032401
 *  9、软件版本号固定字：    SV
 *  10、云台软件版本型号：   10--MK60_PTZ   20--Hyt2.0  ......
 *  11、云台软件发布日期：    211119--21年11月19日  ......
 *  12、软件批次号：          01  ......
 *  云台型号与底板型号之间以“-”作为间隔符。
 *
 ******************************************************************************
 */  
  

//====直流无刷-L6235D-直齿-中型云台-24V
#ifdef PTZ_LIGHT_GEAR_L6235D_AS5047D
    unsigned char g_program_version_number[50] = "MZ22_01A-3S24_BLDC_V20-SV20_22052001";
    unsigned char g_ptz_type[60] = "HY-MZYT-01A_/_3S24_BLDC_V20_L6235/_NC_/_NO_//";
#endif    

//====直流无刷-L6235D-涡轮蜗杆-中型云台-24V
#ifdef PTZ_MEDIUM_WORM_L6235D_AS5047D
    unsigned char g_program_version_number[50] = "MW22_01A-3S24_BLDC_V20-SV20_22052001";
    unsigned char g_ptz_type[60] = "HY-MWYT-01A_/_3S24_BLDC_V20_L6235/_NC_/_NO_//";
#endif    
    
//====直流无刷-L6235D-涡轮蜗杆-重型云台-24V
#ifdef PTZ_HEAVY_WORM_L6235D_AS5047D
    unsigned char g_program_version_number[50] = "HW22_01A-3S24_BLDC_V20-SV20_22052001";
    unsigned char g_ptz_type[60] = "HY-HWYT-01A_/_3S24_BLDC_V20_L6235/_NC_/_NO_//";
#endif     
    
//====直流无刷-L6235D-涡轮蜗杆-轻型云台-24V
#ifdef PTZ_SUPER_LIGHT_WORM_L6235D_AS5047D_24V
    unsigned char g_program_version_number[50] = "LW22_01B-3S24_BLDC_V20-SV20_22052001";
    unsigned char g_ptz_type[60] = "HY-LWYT-01B_/_3S24_BLDC_V20_L6235/_NC_/_NO_//";
#endif     

//====直流无刷-L6235D-涡轮蜗杆-轻型云台-12V
#ifdef PTZ_SUPER_LIGHT_WORM_L6235D_AS5047D_12V
    unsigned char g_program_version_number[50] = "LW22_01A-3S24_BLDC_V20-SV20_22052001";
    unsigned char g_ptz_type[60] = "HY-LWYT-01A_/_3S24_BLDC_V20_L6235/_NC_/_NO_//";
#endif
    
    
    
    
//====步进电机-TMC2160-涡轮蜗杆-超轻型云台-24V
#ifdef PTZ_SUPER_LIGHT_WORM_STEP_TMC2160_AS5047D_24V    
    
    unsigned char g_program_version_number[50] = "LW21_02B-3S24_STEP_V20-SV20_25032201";
    unsigned char g_ptz_type[60] = "HY-LWYT-02B_/_3S24_STEP_V20_TMC/_NC_/_NO_//";
    
#endif
//====步进电机-TMC2160-涡轮蜗杆-中型云台-24V
#ifdef PTZ_MEDIUM_WORM_STEP_TMC2160_AS5047D_24V    
    
    unsigned char g_program_version_number[50] = "MW22_02A-3S24_STEP_V20-SV20_23013101";
    unsigned char g_ptz_type[60] = "HY-MWYT-02A_/_3S24_STEP_V20_TMC/_NC_/_NO_//";
    
#endif  


