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

/*! \brief  Contains public interface to various functions related
 *          to the Phase-lacking (PHLACK) object.
 */

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

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

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief  Defines all the parameters used to initialize PHLACK object
 */
typedef struct _Phlack_ParamsType_
{
    float runFreq_kHz;   /*!< the frequency of running current loop in kHz */
    float Vref;          /*!< the reference voltage used to detect the current */
    float detectFreq_Hz; /*!< the reference frequency used to detect the current */
    float detectTime_ms; /*!< the time used to detect the phase lacking */
    float currentTh_A;   /*!< the current threshold used to determine the phase lacking */
    float IBase_A;       /*!< the base value of phase current in A */
    float fBase_Hz;      /*!< the frequency base value in Hz */
    float VBase_V;       /*!< the base value of voltage in V */
} Phlack_ParamsType;

typedef enum
{
    PHLACK_STATUS_FINISH = 0,
    PHLACK_STATUS_RUNNING,
} Phlack_StatusType;

/*! \brief Defines the PHLACK object
 */
typedef struct _PhlackType_
{
    bool              startCmd;      /*!< Input: True to command to start, will set false when finished */
    _iq               Ia_pu;         /*!< Input: The current of phase A */
    _iq               Ib_pu;         /*!< Input: the current of phase B */
    _iq               Ic_pu;         /*!< Input: the current of phase C */

    _iq               detectFreq_pu; /*!< Parameter: The reference frequency used to detect the current */
    _iq               detectVolt_pu; /*!< Parameter: The reference voltage used to detect the current */
    _iq               currentTh_pu;  /*!< Parameter: The current threshold used to determine the phase lacking */
    uint32_t          scanCountMax;  /*!< Parameter: The max value of count used for timing */

    uint32_t          scanCount;     /*!< Internal variable: The count value used for scan timing */
    Phlack_StatusType status;        /*!< Internal variable: The status of the current process */
    bool              flagA;         /*!< Internal variable: The phase lacking flag of phase A */
    bool              flagB;         /*!< Internal variable: The phase lacking flag of phase B */
    bool              flagC;         /*!< Internal variable: The phase lacking flag of phase C */

    _iq               Vref_pu;       /*!< Output: The reference voltage in P.U. */
    _iq               freqRef_pu;    /*!< Output: The reference frequency in P.U. */
    bool              lackingPhaseA; /*!< Output: The final flag output of lacking phase A */
    bool              lackingPhaseB; /*!< Output: The final flag output of lacking phase B */
    bool              lackingPhaseC; /*!< Output: The final flag output of lacking phase C */
} PhlackType;

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

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

/*! \brief Initialize the PHLACK object
 *
 * This function initalizes Phlack by setting configuration parameters
 *
 * \param[in] obj : pointer to Phlack instance
 * \param[in] pParams : pointer to Phlack configuration parameters
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Phlack_Init(PhlackType *obj, const Phlack_ParamsType *pParams);

/*! \brief Run the PHLACK process
 *
 * This function runs the phase-lacking detection
 *
 * \param[in] obj : pointer to Phlack instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Phlack_Run(PhlackType *obj);

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

#endif /* _PHLACK_H_ */
