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

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

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

/*! \addtogroup touch_slider
 *  @{
 */

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
 * the defines
 *****************************************************************************/
#define TOUCH_SLIDER_INVALIDE_COOR  (0x7FFF)
#define TOUCH_SLIDER_NEED_RECALI    (0x7FFE)
#define TOUCH_SLIDER_COOR_PITCH     (100)

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

/*! \brief Touch slider event type
 */
typedef enum
{
    SLIDER_STATE_OFF = 0x00,
    SLIDER_STATE_ON,
    SLIDER_STATE_NEED_RECALI,
} SliderDrv_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;
} SliderDrv_ElemCfgType;

typedef struct
{
    SliderDrv_StateType state;
    int32_t raw;
    int32_t base;
    int32_t diff;
    uint16_t onCnt;
    uint16_t offCnt;
    uint8_t initFlag;
} SliderDrv_ElemVarType;

typedef struct
{
    uint8_t elementNum;
    uint8_t autoDetectionEn;
    SliderDrv_ElemCfgType *elemsCfg;
} SliderDrv_WidgetCfg;

typedef struct
{
    uint16_t coor;
    uint8_t event;
    SliderDrv_ElemVarType *elemsVar;
} SliderDrv_WidgetVarType;

/*! \brief Definition of Touch slider driver configuration
 */
typedef struct
{
    uint8_t slidersNum;

    SliderDrv_WidgetCfg *slidersCfg;

    SliderDrv_WidgetVarType *slidersVar;

    TouchDrv_CfgType *touchDrvCfg;
} SliderDrv_CfgType;

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

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

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

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

/*! \brief Polling Touch slider event state
 *
 * This function poll Touch slider event state
 *
 * \param[in] obj    : pointer to Touch slider driver instance
 * \param[in] cfg    : pointer to configuration structure
 * \param[out] state : state[0] for slider sensor0 state, 1 means sensor0 is in proximity state;
 *                     state[1] for slider sensor1 state, 1 means sensor1 is in proximity state;
 *                     state[2] for slider event state, refer to SliderDrv_Event;
 *
 */
uint8_t SliderDrv_Process(SliderDrvTypeType *obj, TouchDrv_InfoType *info);

uint16_t SliderDrv_GetSliderCoor(SliderDrvTypeType *obj, uint8_t sliderIndex);

/*! \brief Getting Touch slider event state
 *
 * This function get Touch slider event state
 *
 * \param[in] obj           : pointer to Touch slider driver instance
 * \param[in] sliderIndex  : touch slider index
 * \param[in] elemIndex  : touch slider elem index
 * \return touch slider event state
 *
 */
uint16_t SliderDrv_GetSliderState(SliderDrvTypeType *obj, uint8_t sliderIndex, uint8_t elemIndex);

/*! \brief Getting Touch slider event state
 *
 * This function get Touch slider event state
 *
 * \param[in] obj           : pointer to Touch slider driver instance
 * \param[in] buttonIndex  : touch slider index
 * \return touch slider event state
 *
 */
uint16_t SliderDrv_GetSliderEvent(SliderDrvTypeType *obj, uint8_t sliderIndex);

/*! \brief Get touch slider element raw data
 *
 * This function for getting touch slider element raw data
 *
 * \param[in] obj           : pointer to Touch slider driver instance
 * \param[in] sliderIndex  : touch slider index
 * \param[in] elemIndex    : touch slider element index
 * \return touch slider raw data
 *
 */
int32_t SliderDrv_GetSliderElemRawData(SliderDrvTypeType *obj, uint8_t sliderIndex, uint8_t elemIndex);

/*! \brief Get touch slider element base data
 *
 * This function for getting touch slider element base data
 *
 * \param[in] obj           : pointer to Touch slider driver instance
 * \param[in] sliderIndex  : touch slider index
 * \param[in] elemIndex    : touch slider element index
 * \return touch slider base data
 *
 */
int32_t SliderDrv_GetSliderElemBaseData(SliderDrvTypeType *obj, uint8_t sliderIndex, uint8_t elemIndex);

/*! \brief Get touch slider element diff data
 *
 * This function for getting touch slider element diff data
 *
 * \param[in] obj           : pointer to Touch slider driver instance
 * \param[in] sliderIndex  : touch slider index
 * \param[in] elemIndex    : touch slider element index
 * \return touch slider diff data
 *
 */
int32_t SliderDrv_GetSliderElemDiffData(SliderDrvTypeType *obj, uint8_t sliderIndex, uint8_t elemIndex);

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

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



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

/*! @}*/

#endif
