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

/*! \brief Contains public interface to various functions related
 *         to the driver of BUTTON module
 */

/******************************************************************************
 * the includes
 *****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "touch_drv.h"

/*! \addtogroup touch_button
 *  @{
 */

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief Touch button event type
 */
typedef enum
{
    BUTTON_STATE_OFF = 0x00,
    BUTTON_STATE_ON,
    BUTTON_STATE_NEED_RECALI,
} ButtonDrv_StateType;

typedef struct
{
    uint8_t phase;
    uint32_t thr;
    uint32_t hyst;
    uint8_t onDebNum;
    uint8_t offDebNum;
    uint16_t onMaxNum;
    uint16_t rawFltCoef;
    uint16_t baseFltPosCoef;
    uint16_t baseFltNegCoef;
} ButtonDrv_ElemCfgType;

typedef struct
{
    ButtonDrv_StateType state;
    int32_t raw;
    int32_t base;
    int32_t diff;
    uint16_t onCnt;
    uint16_t offCnt;
    uint8_t initFlag;
} ButtonDrv_ElemVarType;

typedef struct
{
    uint8_t elementNum;
    uint8_t autoDetectionEn;
    ButtonDrv_ElemCfgType *elemsCfg;
} ButtonDrv_WidgetCfgType;

typedef struct
{
    ButtonDrv_StateType event;
    ButtonDrv_ElemVarType *elemsVar;
} ButtonDrv_WidgetVarType;

/*! \brief Definition of Touch button driver configuration
 */
typedef struct
{
    uint8_t buttonNum;

    ButtonDrv_WidgetCfgType *buttonsCfg;

    ButtonDrv_WidgetVarType *buttonsVar;

    TouchDrv_CfgType *touchDrvCfg;
} ButtonDrv_CfgType;

/*! \brief Definition of Touch button driver class
 */
typedef struct
{
    TouchDrvType *touchDrv;
} ButtonDrvType;

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

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

/*! \brief Initializes the Touch button module
 *
 * This function initializes Touch button driver by user configuration
 *
 * \param[in] obj : pointer to button driver instance
 * \param[in] cfg : pointer to configuration structure
 */
void ButtonDrv_Init(ButtonDrvType *obj, ButtonDrv_CfgType *cfg);

/*! \brief Touch button detection process
 *
 * This function poll Touch button event state
 *
 * \param[in] obj    : pointer to Touch button driver instance
 * \param[in] info   : pointer to button element information
 *
 */
uint8_t ButtonDrv_Process(ButtonDrvType *obj, TouchDrv_InfoType *info);

/*! \brief Getting Touch button event state
 *
 * This function get Touch button event state
 *
 * \param[in] obj           : pointer to Touch button driver instance
 * \param[in] buttonIndex  : touch button index
 * \return touch button event state
 *
 */
uint8_t ButtonDrv_GetButtonEvent(ButtonDrvType *obj, uint8_t buttonIndex);

/*! \brief Get touch button element raw data
 *
 * This function for getting touch button element raw data
 *
 * \param[in] obj           : pointer to Touch Button driver instance
 * \param[in] buttonIndex  : touch button index
 * \param[in] elemIndex    : touch button element index
 * \return touch button raw data
 *
 */
int32_t ButtonDrv_GetButtonElemRawData(ButtonDrvType *obj, uint8_t buttonIndex, uint8_t elemIndex);

/*! \brief Get touch button element base data
 *
 * This function for getting touch button element base data
 *
 * \param[in] obj           : pointer to Touch Button driver instance
 * \param[in] buttonIndex  : touch button index
 * \param[in] elemIndex    : touch button element index
 * \return touch button base data
 *
 */
int32_t ButtonDrv_GetButtonElemBaseData(ButtonDrvType *obj, uint8_t buttonIndex, uint8_t elemIndex);

/*! \brief Get touch button element diff data
 *
 * This function for getting touch button element diff data
 *
 * \param[in] obj           : pointer to Touch Button driver instance
 * \param[in] buttonIndex  : touch button index
 * \param[in] elemIndex    : touch button element index
 * \return touch button diff data
 *
 */
int32_t ButtonDrv_GetButtonElemDiffData(ButtonDrvType *obj, uint8_t buttonIndex, uint8_t elemIndex);

/*! \brief Setting Touch button driver configuration
 *
 * This function set Touch button driver configuration
 *
 * \param[in] obj    : pointer to Touch button driver instance
 * \param[in] cfg    : pointer to configuration data
 * \param[in] len    : configuration data length
 *
 */
void ButtonDrv_SetCfg(void *obj, uint8_t *cfg, uint32_t len);

/*! \brief Getting Touch button driver configuration
 *
 * This function get Touch button driver configuration
 *
 * \param[in] obj    : pointer to Touch button driver instance
 * \param[in] cfg    : pointer to configuration data
 * \param[in] len    : configuration data length
 *
 */
void ButtonDrv_GetCfg(void *obj, uint8_t *cfg, uint32_t len);

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

/*! @}*/

#endif
