/*
 * digital_filters.c
 *
 *  Created on: 13 нояб. 2024 г.
 *      Author: Evgeniy_Sokolov
 */



////////////////////////////////////////////////////////////////////
unsigned int filter_digital_input(unsigned int prev_valus, unsigned int *c_plus, unsigned int max_wait, unsigned int flag)
{

   if (flag)
   {
        if ((*c_plus)>=max_wait)
        {
          return 1;
        }
        else
        {
            (*c_plus)++;
            return (prev_valus);
        }
   }
   else
   {
       if ((*c_plus)==0)
       {
         return 0;
       }
       else
       {
           (*c_plus)--;
           return (prev_valus);
       }
   }
}
///////////////////////////////////////////////////////////////////
//TODO: may be move to detect_errors.c
unsigned int pause_detect_error(unsigned int *c_err, unsigned int max_wait,unsigned int flag)
{
   if (flag)
   {
        if ((*c_err)>=max_wait)
        {
          return 1;
        }
        else
        {
            (*c_err)++;
            return 0;
        }
   }
   else
   {
      (*c_err) = 0;
      return 0;

   }



}



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



unsigned int filter_err_count(unsigned int *counter, unsigned int max_errors, unsigned int err, unsigned int cmd)
{
    if (cmd==1)
    {
        (*counter) = 0;
        return 0;
    }

    if (err)
    {
       if ((*counter)>=max_errors)
          return 1;
       else
           (*counter)++;

       return 0;
    }

    if (err==0)
    {
       if ((*counter)==0)
          return 0;
       else
           (*counter)--;

       return 0;
    }
    return 0;
}



