/*
 * 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 _RAMP_H_
#define _RAMP_H_

/*! \brief Contains public interface to various functions related
 *         to the ramp object
 */

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

#include <stdint.h>
#include "common/iqmath/iqmath.h"

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief Declaration of Ramp Class
 */
typedef struct _RampType_
{
    _iq target; /*!< Input: target input */
    _iq step;   /*!< Parameter: step of ramp */
    _iq output; /*!< Output: output value */

} RampType;

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

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

/*! \brief Initialize the Ramp instance
 *
 * This function initializes the Ramp instance
 *
 * \param[in] obj : pointer to Ramp instance
 * \param[in] Step : step of going up and down
 */
static inline void Ramp_Init(RampType *obj, _iq step)
{
    obj->target = 0;
    obj->output = 0;
    obj->step   = step;
}

/*! \brief Calculates the output
 *
 * This function calculates the output
 *
 * \param[in] obj : pointer to Ramp instance
 */
static inline void Ramp_Calc(RampType *obj)
{
    _iq step = _IQabs(obj->step); /* step must be a positive value */

    if(_IQabs(obj->target - obj->output) > step)
    {
        if(obj->target > obj->output)
        {
            obj->output += step;
        }
        else
        {
            obj->output -= step;
        }
    }
    else
    {
        /* less than one step needed, just jump to the target */
        obj->output = obj->target;
    }
}

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

#endif /* _RAMP_H_ */
