/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _DEBOUNCE_H_
#define _DEBOUNCE_H_

/*! \brief  Contains public interface to various functions related
 *          to the debounce (DEBOUNCE) object
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include <stdint.h>
#include <stdbool.h>
#include "common/iqmath/iqmath.h"
#include "common/sort/compare.h"

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

typedef enum
{
    DEBOUNCE_TYPE_OVER,  /*!< Over the threshold is active */
    DEBOUNCE_TYPE_UNDER, /*!< Under the threshold is active */
} Debounce_ModeType;

/*! \brief  Defines all the parameters used to initialize DEBOUNCE object
 */
typedef struct _Debounce_ParamsType_
{
    uint32_t          debounceConfirmCntMax; /*!< The max value of the debounce count */
    uint32_t          debounceRehabCntMax;   /*!< The max value of the debounce count */
    _iq               threshold;             /*!< The threshold of the input compare */
    Debounce_ModeType compType;              /*!< The compare type */
} Debounce_ParamsType;

/*! \brief Defines the DEBOUNCE object
 */
typedef struct _DebounceType_
{
    _iq input; /*!< Input: The input value of the debounce */

    uint32_t countConfirmMax; /*!< Parameters: The max value of the debounce count */
    uint32_t countRehabMax;   /*!< Parameters: The max value of the debounce count */
    _iq      threshold;       /*!< Parameters: The threshold of the input compare */

    uint32_t count;         /*!< Internal variable: The count of the debouncing process */
    bool (*comp)(_iq, _iq); /*!< Internal Variable: The compare function pointer */

    bool isActive; /*!< Output: Is active after debounce */
} DebounceType;

/*******************************************************************************
 * the globals
 ******************************************************************************/

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/

/*! \brief  Initialize the DEBOUNCE object
 *
 * This function initialize the debounce instance.
 *
 * \param[in] obj : pointer to Debounce instance
 * \param[in] pParams : The pointer to initial parameters
 */
static inline void Debounce_Init(DebounceType *obj, const Debounce_ParamsType *pParams)
{
    obj->countConfirmMax = pParams->debounceConfirmCntMax;
    obj->countRehabMax   = pParams->debounceRehabCntMax;
    obj->threshold       = pParams->threshold;
    if(pParams->compType == DEBOUNCE_TYPE_UNDER)
    {
        obj->comp = Compare_SmallerThan;
    }
    else if(pParams->compType == DEBOUNCE_TYPE_OVER)
    {
        obj->comp = Compare_GreaterThan;
    }
    obj->count    = 0;
    obj->isActive = false;
}

/*! \brief  Run the DEBOUNCE process
 *
 * This function is the calculation of debounce process.
 *
 * \param[in] obj :pointer to Debounce instance
 */
static inline void Debounce_Run(DebounceType *obj)
{
    if(obj->comp(obj->input, obj->threshold))
    {
        if(obj->isActive)
        {
            obj->count = 0;
        }
        else if(++obj->count >= obj->countConfirmMax)
        {
            obj->isActive = true;
            obj->count    = 0;
        }
    }
    else
    {
        if(!obj->isActive)
        {
            obj->count = 0;
        }
        else if(++obj->count >= obj->countRehabMax)
        {
            obj->isActive = false;
            obj->count    = 0;
        }
    }
}

/*! \brief  Reset the DEBOUNCE object
 *
 * This function resets the debounce instance to intial state.
 *
 * \param[in] obj : pointer to Debounce instance
 */
static inline void Debounce_Reset(DebounceType *obj)
{
    obj->count    = 0;
    obj->isActive = false;
}

#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* _DEBOUNCE_H_ */
