#include "device_adc_collect.h"
#include  <includes.h>
#include "gd32f4xx.h"
#include "comm_types.h"
#include "ptz_struct.h"
#include "tmp75.h"
#include "w25q128.h"
#include "agent_hyt.h"
#include "ptz_type_select.h"
#include "pdebug.h"
//发送云台工作电压
void ptz_send_voltage(char dev)
{
  unsigned short int vol = 0;
  unsigned char VData[7]= {0xff,0x00,0x00,0x00,0x00,0x00,0x00};
  vol = ( unsigned short int)(g_ptz.Voltage * 100 + 0.5);
  VData[1] = g_ptz.address;
  VData[2] = 0xcd;
  VData[3] = (unsigned char)(vol >> 8);
  VData[4] = (unsigned char)(vol & 0x00ff);
  VData[6] = MotorCalPelcoDSUM(VData,sizeof(VData));
  ptz_send_data(dev, VData, sizeof(VData));
  //发送调试数据
}

//发送云台工作电流
void ptz_send_current(char dev)
{
  unsigned short int current = 0;
  unsigned char IData[7]= {0xff,0x00,0x00,0x00,0x00,0x00,0x00};
  current = (unsigned short int)(g_ptz.electric_current * 100 + 0.5);
  IData[1] = g_ptz.address;
  IData[2] = 0xc8;
  IData[3] = (unsigned char)(current >> 8);
  IData[4] = (unsigned char)(current & 0x00ff);
  IData[6] = MotorCalPelcoDSUM(IData,sizeof(IData));
  
  ptz_send_data(dev, IData, sizeof(IData));
}  

//发送云台温度
void ptz_send_temperature(char dev)
{
  unsigned short int tem = 0;
  unsigned char TData[7]= {0xff,0x00,0x00,0x00,0x00,0x00,0x00};
  tem = (unsigned short int)(g_ptz.temperature * 100 + 0.5);
  TData[1] = g_ptz.address;
  TData[2] = 0xd6;
  TData[3] = (unsigned char)(tem >> 8);
  TData[4] = (unsigned char)(tem & 0x00ff);
  TData[6] = MotorCalPelcoDSUM(TData,sizeof(TData));
  
  ptz_send_data(dev,TData,sizeof(TData));
  
}

void ADC_Init()
{
    //配置引脚时钟
    rcu_periph_clock_enable(RCU_GPIOC);
    //引脚复用，PC2——电压ADC1-CH12，PC3——电流ADC1-CH13
    gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_2);
    gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
    
    rcu_periph_clock_enable(RCU_ADC1);
    //总线时钟20分频=6MHZ
    adc_clock_config(ADC_ADCCK_PCLK2_DIV4);
    /***********************************************/
    
    //对齐方式      ADC_INSERTED_CHANNEL
    adc_data_alignment_config(ADC1,ADC_DATAALIGN_RIGHT);
    //扫描模式
    adc_special_function_config(ADC1,ADC_SCAN_MODE,ENABLE);
    //外部触发关闭
    adc_external_trigger_config(ADC1,ADC_INSERTED_CHANNEL,DISABLE);
    //采集通道数
    adc_channel_length_config(ADC1,ADC_INSERTED_CHANNEL,2);
    
    //通道注入顺序
    adc_inserted_channel_config(ADC1,0,ADC_CHANNEL_13,ADC_SAMPLETIME_480);//电流
    adc_inserted_channel_config(ADC1,1,ADC_CHANNEL_12,ADC_SAMPLETIME_480);//电压
    
   
    adc_enable(ADC1);
    
    adc_calibration_enable(ADC1);
}


//电压采集
static void ptz_Voltage_collect_adc1_task()
{
  static float adc1_v[5];
  static unsigned char adc1_num;
  int j,k;
  float tem;
  float curadc1;
  uint16_t value_V;
  //软件触发使能
  adc_software_trigger_enable(ADC1,ADC_INSERTED_CHANNEL);
  value_V = ADC_IDATA1(ADC1);
  
  adc1_v[adc1_num] = (float)value_V / 4096.0 * 11 *3.3;//(float)value_V;//
  adc1_num++;
  if(adc1_num >= 5)
  {
    adc1_num = 0;
     for(j = 0; j < 5-1; j++)//采样值由小到大排列
     {
         for(k = 0; k < 5-j-1; k++)
         {
             if(adc1_v[k] > adc1_v[k+1])
             {
                tem      = adc1_v[k];
                adc1_v[k]   = adc1_v[k+1];
                adc1_v[k+1] = tem;
             }
          }
       }
      for(uint8_t i = 1; i < 5 - 1; i++)
      {
        curadc1 = curadc1 + adc1_v[i];
      }
    curadc1 = curadc1 /((float)(5 - 2));//去掉一个最大值和一个最小值求平均值
    g_ptz.Voltage =  curadc1;
    memset(adc1_v,0,sizeof(adc1_v));
//    pdebug(DEBUG_LEVEL_INFO,"get ptz Voltage: %.3fV",g_ptz.Voltage);
  }
  
}

//电流采集
static void ptz_Current_collect_adc1_task()
{
  static float adc1_i[5];
  static unsigned char adc0_num;
  int j,k;
  float tem;
  float curadc0;
  uint16_t value_I;
  
  adc_software_trigger_enable(ADC1,ADC_INSERTED_CHANNEL);
  value_I = ADC_IDATA0(ADC1);
  
  adc1_i[adc0_num] = (((float)value_I / 4096.0  * 3.3)-3.3/2) / 0.132;//(float)value_I;//
  
  adc0_num ++;
  if(adc0_num >= 5)
  {
    adc0_num = 0;
     for(j = 0; j < 5-1; j++)//采样值由小到大排列
     {
         for(k = 0; k < 5-j-1; k++)
         {
             if(adc1_i[k] > adc1_i[k+1])
             {
                tem      = adc1_i[k];
                adc1_i[k]   = adc1_i[k+1];
                adc1_i[k+1] = tem;
             }
          }
       }
      for(uint8_t i = 1; i < 5 - 1; i++)
      {
        curadc0 = curadc0 + adc1_i[i];
      }
    curadc0 = curadc0 /((float)(5 - 2));//去掉一个最大值和一个最小值求平均值
    g_ptz.electric_current =  curadc0;
    memset(adc1_i,0,sizeof(adc1_i));
//    pdebug(DEBUG_LEVEL_INFO,"get ptz Electric_Current: %.3fA",g_ptz.electric_current);
  }
  
}

//温度采集
static void ptz_temperature_collect_tmp75_task()
{
  static float tmp75[5];
  static unsigned char tmp75_num;
  float curtmp75;
  float tem;
  int j,k;
  tmp75[tmp75_num] = tmp75_read_temp();
  tmp75_num ++;
  if(tmp75_num >= 5)
  {
    tmp75_num = 0;
    for(j = 0; j < 5-1; j++)//采样值由小到大排列
    {
       for(k = 0; k < 5-j-1; k++)
       {
           if(tmp75[k] > tmp75[k+1])
           {
              tem        = tmp75[k];
              tmp75[k]   = tmp75[k+1];
              tmp75[k+1] = tem;
           }
        }
     }
    for(uint8_t i = 1; i < 5 - 1; i++)
    {
      curtmp75 = curtmp75 + tmp75[i];//去掉一个最大值和一个最小值
    }
    curtmp75 = curtmp75 / ((float)(5 - 2));
    g_ptz.temperature = curtmp75;
    memset(tmp75,0,sizeof(tmp75));
//    pdebug(DEBUG_LEVEL_INFO,"get ptz Temperature: %.3f℃",g_ptz.temperature);
  }
  
}


//采集任务
static char ptz_data_collect_task()
{
  int i=0;
  while(1)
  {
    //云台不自检关闭，打开采集任务
    #ifndef PTZ_NO_SELF_CHECK 
        ptz_Voltage_collect_adc1_task();
    #endif
    OSTimeDlyHMSM(0u, 0u, 0u, 50u); 
    ptz_Current_collect_adc1_task();
    OSTimeDlyHMSM(0u, 0u, 0u, 50u); 
    if(i >= 30)
    {//降低温度采集频率
      ptz_temperature_collect_tmp75_task();
      i=0;
    }
    i++;
  }
}




static OS_STK  task_data_collect_stk[TASK_PTZ_DATA_COLLECT_STK_SIZE];
static  void  creat_task_data_collect(void)
{ 
   CPU_INT08U  task_err;   
   CPU_INT08U  name_err;
   
   task_err = OSTaskCreateExt((void (*)(void *)) ptz_data_collect_task,           
		   (void           *) 0,
		   (OS_STK         *)&task_data_collect_stk[TASK_PTZ_DATA_COLLECT_STK_SIZE - 1],
		   (INT8U           ) TASK_PTZ_DATA_COLLECT_PRIO,                   
		   (INT16U          ) TASK_PTZ_DATA_COLLECT_PRIO,
		   (OS_STK         *)&task_data_collect_stk[0],
		   (INT32U          ) TASK_PTZ_DATA_COLLECT_STK_SIZE,
		   (void           *) 0,
		   (INT16U          )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));     
    #if (OS_TASK_NAME_EN > 0)
    OSTaskNameSet(TASK_PTZ_DATA_COLLECT_PRIO, "ptz_data_collect_task", &name_err);
    #endif
       
}


void init_data_collect_module(void)
{
  ADC_Init();//AD采集引脚初始化
  temp75_gpio_init();//温度传感器初始化
  creat_task_data_collect();
}

