#include  <includes.h>
#include "device_photoelectricswitch.h"
#include "ptz_header_file.h"
#include "ptz_type_select.h"
#include "ptz_struct.h"
static BSP_OS_SEM ptz_ps_read_mutex;

#ifdef PTZ_PHOTOELECTRIC_SWITCH  
  float ps_k = 0;  
#endif

/// 初始化所有光电开关
///
/// 初始化所有光电开关
/// @param  无 
/// @return 无
/// @par   修改日志
///        杨鹏程于2017-09-14创建
///        LH修改于2022-05-31
void ps_init()
{
  //配置引脚时钟
  rcu_periph_clock_enable(RCU_GPIOE);
  /*光电开关中断PE7-SW1，PE8-SW2，PE9-SW3*/
  //设置引脚为输入模式:PE7-SW1，PE8-SW2，PE9-SW3
  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_7);
  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_8);
  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
  
  BSP_OS_SemCreate(&ptz_ps_read_mutex,1u,"ptz_ps_read_mutex");
}

/// 读取指定光电开关返回的电平
#define PS_NUM   20
#define PS_RATIO   0.9
char ps_read(char flag)
{
  int i = 0;
  int j = 0;

  BSP_OS_SemWait(&ptz_ps_read_mutex, 0u);
  char temp;
  switch(flag)
  {
    case PS_VERT_SW1:
      j = 0;
      for(i = 0; i < PS_NUM; i++)//对垂直光电开关滤波
      {
        asm("nop");
        temp = PS_VERT_SW1_READ;//光电开关1
        j = j + temp;
      }
      
      ps_k = ((float)(PS_NUM - j))/ PS_NUM;//计算遮挡概率
      
      if(ps_k > PS_RATIO)
      {
        temp = PS_LOW;
      }
      else
      {
        temp = PS_HIGH;
      }
      //temp = PS_VERT_SW1_READ;
      BSP_OS_SemPost(&ptz_ps_read_mutex);
      return temp;
      break;
      
    case PS_VERT_SW2:
      j = 0;
      for(i = 0; i < PS_NUM; i++)//对垂直光电开关滤波
      {
        asm("nop");
        temp = PS_VERT_SW2_READ;//光电开关2
        j = j + temp;
      }
      
      ps_k = ((float)(PS_NUM - j))/ PS_NUM;//计算遮挡概率
      
      if(ps_k > PS_RATIO)
      {
        temp = PS_LOW;
      }
      else
      {
        temp = PS_HIGH;
      }
      //temp = PS_VERT_SW2_READ;
      BSP_OS_SemPost(&ptz_ps_read_mutex);
      return temp;
      break;
      
    case PS_HORI_SW3:
      j = 0;
      for(i = 0; i < PS_NUM; i++)//对水平光电开关滤波
      {
        asm("nop");
        temp = PS_HORI_SW3_READ;//光电开关3
        j = j + temp;
      }
      
      ps_k = ((float)(PS_NUM - j))/ PS_NUM;//计算遮挡概率
      
      if(ps_k > PS_RATIO)
      {
        temp = PS_LOW;
      }
      else
      {
        temp = PS_HIGH;
      }
      //temp = PS_HORI_SW3_READ;
      BSP_OS_SemPost(&ptz_ps_read_mutex);
      return temp;
      break; 
  }
  BSP_OS_SemPost(&ptz_ps_read_mutex);
  return -1;
}


static void ptz_ps_read_task()
{
  while(1)
  {
    g_ptz.hori_ps_sw3_state = ps_read(PS_HORI_SW3);
    g_ptz.vert_ps_sw1_state = ps_read(PS_VERT_SW1);
    g_ptz.vert_ps_sw2_state = ps_read(PS_VERT_SW2);
    OSTimeDlyHMSM(0u, 0u, 0u, 2u);
  }
}



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

void task_ps_read_init(void)
{
  ps_init();
  creat_task_ps_read();
}

void init_photoelectric_switch_module()
{
  task_ps_read_init();

  g_ptz.vert_ps_sw1_down_fall  = 0;
  g_ptz.vert_ps_sw1_up_rise    = 0;
  g_ptz.vert_ps_sw2_up_fall    = 0;
  g_ptz.vert_ps_sw2_down_rise  = 0;
  g_ptz.hori_ps_sw3_right_fall = 0;
  g_ptz.hori_ps_sw3_right_rise = 0;
  g_ptz.hori_ps_sw3_left_fall  = 0;
  g_ptz.hori_ps_sw3_left_rise  = 0;
}

