⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm8_tsl_rc_multichannelkey.c

📁 STM8-触摸例程
💻 C
📖 第 1 页 / 共 3 页
字号:
  * @brief Select I/Os to burst and call IO driver for burst sequence.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey2_Acquisition(void)
{

  KeyIndex = 1;
  TSL_MCKey_SetStructPointer();

  if ( !( (pMCKeyStruct->State.whole == ERROR_STATE) || (pMCKeyStruct->State.whole == DISABLED_STATE) ) )
  {
    for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
    {
      sTouchIO.PORT_ADDR = (GPIO_TypeDef *)(Table_MCKEY_PORTS[CHANNEL_PER_MCKEY + ChannelIndex]);
      sTouchIO.AcqMask = Table_MCKEY_BITS[CHANNEL_PER_MCKEY + ChannelIndex];
      sTouchIO.DriveMask = (u8)(sTouchIO.AcqMask | MCKEY2_DRIVEN_SHIELD_MASK);
      sTouchIO.Measurement = &sMCKeyInfo[1].Channel[ChannelIndex].LastMeas;
      sTouchIO.RejectedNb = &sMCKeyInfo[1].Channel[ChannelIndex].LastMeasRejectNb;
      sTouchIO.Type = MCKEY_TYPE;
      TSL_IO_Acquisition(MCKEY_ACQ_NUM, MCKEY_ADJUST_LEVEL);
    }
  }

}
#endif


/**
  ******************************************************************************
  * @brief After Touch Sensing acquisition, data processing.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_Process(void)
{

  TSL_MCKey_SetStructPointer();

  switch ( pMCKeyStruct->State.whole )
  {
    case IDLE_STATE:
      if ( TSL_MCKey_CheckErrorCondition( ) )
      {
        TSL_MCKey_SetErrorState( );
        break;
      }
      TSL_MCKey_IdleTreatment( );
      TSL_MCKey_CheckDisabled( );
      break;

    case PRE_DETECTED_STATE:
      TSL_MCKey_PreDetectTreatment( );
      break;

    case DETECTED_STATE:
      if ( TSL_MCKey_CheckErrorCondition( ) )
      {
        TSL_MCKey_SetErrorState( );
        break;
      }
      TSL_MCKey_DetectedTreatment( );
      TSL_MCKey_CheckDisabled( );
      break;

    case POST_DETECTED_STATE:
      TSL_MCKey_PostDetectTreatment( );
      break;

    case PRE_CALIBRATION_STATE:
      TSL_MCKey_PreRecalibrationTreatment( );
      break;

    case CALIBRATION_STATE:
      if ( TSL_MCKey_CheckErrorCondition( ) )
      {
        TSL_MCKey_SetErrorState( );
        break;
      }
      TSL_MCKey_CalibrationTreatment( );
      TSL_MCKey_CheckDisabled( );
      break;

    case ERROR_STATE:
      TSL_MCKey_CheckDisabled( );
      break;

    case DISABLED_STATE:
      TSL_MCKey_CheckEnabled( );
      break;

    default:
      for (;;)
      {
        // Infinite loop.
      }
  }

  TSL_TempGlobalSetting.whole |= pMCKeyStruct->Setting.whole;
  TSL_TempGlobalState.whole |= pMCKeyStruct->State.whole;
  pMCKeyStruct->Setting.b.CHANGED = 0;
  pMCKeyStruct->Setting.b.POSCHANGED = 0;

}


/**
  ******************************************************************************
  * @brief Check for MCKey info during Idle state: Verify detection and recalibration.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_IdleTreatment(void)
{

  /* Check all channels to enter in Calibration state if needed */
  for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
  {
    
    /* Ignore channel if too much noise is detected */
    if (pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb > MAX_REJECTED_MEASUREMENTS)
    {
      break;
    }
    
    TSL_MCKey_DeltaCalculation( ChannelIndex );
    
    if ( Delta <= pMCKeyStruct->RecalibrationThreshold )
    {
      TSL_MCKey_SetPreRecalibrationState( );
      return;
    }
    
  }

  /* Check all channels to enter in Pre-detect state if needed */ 
  for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
  {
    
    /* Ignore channel if too much noise is detected */
    if (pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb > MAX_REJECTED_MEASUREMENTS)
    {
      break;
    }
    
    TSL_MCKey_DeltaCalculation( ChannelIndex );
    
    if ( Delta >= pMCKeyStruct->DetectThreshold )
    {
      TSL_MCKey_SetPreDetectState( );
      if ( !DetectionIntegrator )
      {
        pMCKeyStruct->Channel[0].IntegratorCounter++;
        TSL_MCKey_PreDetectTreatment( );
      }
      return;
    }
    
  }

}


/**
  ******************************************************************************
  * @brief Check MCKey info during PRE DETECT state: Verify detection integrator and detection exclusion.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_PreDetectTreatment(void)
{

  u8 ConditionLeaveDetect = 1; /* Per default we go back to idle state */

  /* Go back in idle state if ALL channels are below the detect threshold */
  for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
  {
    TSL_MCKey_DeltaCalculation( ChannelIndex );
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
       (Delta >= pMCKeyStruct->DetectThreshold))
    {
      ConditionLeaveDetect = 0;
      break;
    }
  }

  if ( ConditionLeaveDetect )
  {
    TSL_MCKey_BackToIdleState( );
    return;
  }
  else
  {
    TSL_MCKey_DES( );
    pMCKeyStruct->Channel[0].IntegratorCounter--;
    if ( !pMCKeyStruct->Channel[0].IntegratorCounter )
    {
      TSL_MCKey_Position( );
      TSL_MCKey_SetDetectedState( );
    }
  }

}


/**
  ******************************************************************************
  * @brief Check MCKey info during DETECTED state:
  * Verify detection timeout, end of detection and detection exclusion.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_DetectedTreatment(void)
{

  u8 ConditionLeaveDetect = 1;

  /* Go in post-detect state if ALL channels are below or equal the end-detect threshold */
  for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
  {
    TSL_MCKey_DeltaCalculation( ChannelIndex );
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (Delta > pMCKeyStruct->EndDetectThreshold))
    {
      ConditionLeaveDetect = 0;
      break;
    }
  }
  
  if ( ConditionLeaveDetect )
  {
    TSL_MCKey_SetPostDetectState( );
    if ( !EndDetectionIntegrator )
    {
      pMCKeyStruct->Channel[0].IntegratorCounter++;
      TSL_MCKey_PostDetectTreatment( );
    }
    return;
  }

  /* Detection TimeOut only if there is no change in the slider/wheel position */
  if (TSL_MCKey_Position() == 0x00)
  {
    TSL_MCKey_DetectionTimeout( );
  }

}


/**
  ******************************************************************************
  * @brief Check MCKey info during POST DETECT state: Verify end of detection.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_PostDetectTreatment(void)
{

  u8 ConditionLeaveDetect = 1;

  /* Go in idle state if ALL channels are below or equal the end-detect threshold */
  for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
  {
    TSL_MCKey_DeltaCalculation( ChannelIndex );
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (Delta > pMCKeyStruct->EndDetectThreshold))
    {
      ConditionLeaveDetect = 0;
      break;
    }
  }

  if ( ConditionLeaveDetect )
  {
    pMCKeyStruct->Channel[0].IntegratorCounter--;
    if ( !pMCKeyStruct->Channel[0].IntegratorCounter )
    {
      TSL_MCKey_SetIdleState( );
    }
  }
  else
  {
    // No reset of DTO counter.
    TSL_MCKey_BackToDetectedState( );
  }
}


/**
  ******************************************************************************
  * @brief Check MCKey info during PRE RECALIBRATION state: Verify condition for recalibration.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_PreRecalibrationTreatment(void)
{

  u8 ConditionLeaveCalib = 1;

  /* Go in calibration state if at least ONE channel is below the calibration threshold */
  for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
  {
    TSL_MCKey_DeltaCalculation( ChannelIndex );
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (Delta <= pMCKeyStruct->RecalibrationThreshold))
    {
      ConditionLeaveCalib = 0;
      break;
    }
  }

  if ( !ConditionLeaveCalib )
  {
    pMCKeyStruct->Channel[0].IntegratorCounter--;
    if ( !pMCKeyStruct->Channel[0].IntegratorCounter )
    {
      TSL_MCKey_SetCalibrationState( );
    }
  }
  else
  {
    TSL_MCKey_BackToIdleState( );
  }

}


/**
  ******************************************************************************
  * @brief During calibration, calculates the new reference.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_CalibrationTreatment(void)
{

  for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
  {
    if (pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS)
    {
      pMCKeyStruct->Channel[ChannelIndex].Reference += pMCKeyStruct->Channel[ChannelIndex].LastMeas;
    }
    else
    {
      return;
    }
  }
  
  pMCKeyStruct->Counter--;
  
  if ( !pMCKeyStruct->Counter )
  {
    for ( ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++ )
    {
      // Warning: Must be divided by MCKEY_CALIBRATION_COUNT_DEFAULT !!!
      pMCKeyStruct->Channel[ChannelIndex].Reference = (pMCKeyStruct->Channel[ChannelIndex].Reference >> 3);
    }
    TSL_MCKey_SetIdleState( );
  }

}


/**
  ******************************************************************************
  * @brief Calculates the position on the MCKey. 
  * @par Parameters:
  * None
  * @retval u8 Returns 0x00 if no change, 0xFF is new position detected
  * @par Required preconditions:
  * None
  ******************************************************************************
  */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -