/***********************************************************************************************************************
 * Copyright [2020-2023] Renesas Electronics Corporation and/or its affiliates.  All Rights Reserved.
 *
 * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products
 * of Renesas Electronics Corp. and its affiliates ("Renesas").  No other uses are authorized.  Renesas products are
 * sold pursuant to Renesas terms and conditions of sale.  Purchasers are solely responsible for the selection and use
 * of Renesas products and Renesas assumes no liability.  No license, express or implied, to any intellectual property
 * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas
 * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION
 * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT
 * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR
 * DOCUMENTATION.  RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH.  TO THE MAXIMUM
 * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION
 * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING,
 * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS,
 * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY
 * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS.
 **********************************************************************************************************************/

/**********************************************************************************************************************
 * Includes   <System Includes> , "Project Includes"
 *********************************************************************************************************************/
#if defined(__CCRX__) || defined(__ICCRX__) || defined(__RX__)
 #include "r_zmod4xxx_if.h"
#elif defined(__CCRL__) || defined(__ICCRL78__) || defined(__RL78__)
 #include "r_zmod4xxx_if.h"
#else
 #define RM_ZMOD4410_IAQ_2ND_GEN_ULP_CFG_LIB_ENABLE    (1)
#endif

#if RM_ZMOD4410_IAQ_2ND_GEN_ULP_CFG_LIB_ENABLE
 #include "rm_zmod4xxx.h"
 #include "../zmod4xxx_types.h"
 #include "iaq_2nd_gen_ulp.h"
 #include "zmod4410_config_iaq2_ulp.h"

/**********************************************************************************************************************
 * Macro definitions
 *********************************************************************************************************************/

/* Definitions of OAQ 2nd gen Parameter */
 #define RM_ZMOD4410_IAQ_2ND_GEN_ULP_DEFAULT_HUMIDITY       (50.0F)
 #define RM_ZMOD4410_IAQ_2ND_GEN_ULP_DEFAULT_TEMPERATURE    (20.0F)

/**********************************************************************************************************************
 * Local Typedef definitions
 *********************************************************************************************************************/

/**********************************************************************************************************************
 * Exported global functions
 *********************************************************************************************************************/

/**********************************************************************************************************************
 * Private (static) variables and functions
 *********************************************************************************************************************/

static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_open(rm_zmod4xxx_ctrl_t * const      p_api_ctrl,
                                                  rm_zmod4xxx_cfg_t const * const p_cfg);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_measurement_start(rm_zmod4xxx_ctrl_t * const p_api_ctrl);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_measurement_stop(rm_zmod4xxx_ctrl_t * const p_api_ctrl);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_status_check(rm_zmod4xxx_ctrl_t * const p_api_ctrl);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_read(rm_zmod4xxx_ctrl_t * const     p_api_ctrl,
                                                  rm_zmod4xxx_raw_data_t * const p_raw_data);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_temperature_and_humidity_set(rm_zmod4xxx_ctrl_t * const p_api_ctrl,
                                                                          float                      temperature,
                                                                          float                      humidity);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_iaq_1st_gen_data_calculate(rm_zmod4xxx_ctrl_t * const         p_api_ctrl,
                                                                        rm_zmod4xxx_raw_data_t * const     p_raw_data,
                                                                        rm_zmod4xxx_iaq_1st_data_t * const p_zmod4xxx_data);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_data_calculate(rm_zmod4xxx_ctrl_t * const         p_api_ctrl,
                                                            rm_zmod4xxx_raw_data_t * const     p_raw_data,
                                                            rm_zmod4xxx_iaq_2nd_data_t * const p_zmod4xxx_data);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_odor_data_calculate(rm_zmod4xxx_ctrl_t * const      p_api_ctrl,
                                                                 rm_zmod4xxx_raw_data_t * const  p_raw_data,
                                                                 rm_zmod4xxx_odor_data_t * const p_zmod4xxx_data);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_sulfur_odor_data_calculate(
    rm_zmod4xxx_ctrl_t * const             p_api_ctrl,
    rm_zmod4xxx_raw_data_t * const         p_raw_data,
    rm_zmod4xxx_sulfur_odor_data_t * const p_zmod4xxx_data);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_oaq_1st_gen_data_calculate(rm_zmod4xxx_ctrl_t * const         p_api_ctrl,
                                                                        rm_zmod4xxx_raw_data_t * const     p_raw_data,
                                                                        rm_zmod4xxx_oaq_1st_data_t * const p_zmod4xxx_data);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_oaq_2nd_gen_data_calculate(rm_zmod4xxx_ctrl_t * const         p_api_ctrl,
                                                                        rm_zmod4xxx_raw_data_t * const     p_raw_data,
                                                                        rm_zmod4xxx_oaq_2nd_data_t * const p_zmod4xxx_data);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_raq_data_calculate(rm_zmod4xxx_ctrl_t * const     p_api_ctrl,
                                                                rm_zmod4xxx_raw_data_t * const p_raw_data,
                                                                rm_zmod4xxx_raq_data_t * const p_zmod4xxx_data);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_close(rm_zmod4xxx_ctrl_t * const p_api_ctrl);
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_device_error_check(rm_zmod4xxx_ctrl_t * const p_api_ctrl);

/**********************************************************************************************************************
 * Exported global variables
 *********************************************************************************************************************/
rm_zmod4xxx_api_t const g_zmod4xxx_on_zmod4410_iaq_2nd_gen_ulp =
{
    .open                      = rm_zmod4410_iaq_2nd_gen_ulp_open,
    .close                     = rm_zmod4410_iaq_2nd_gen_ulp_close,
    .measurementStart          = rm_zmod4410_iaq_2nd_gen_ulp_measurement_start,
    .measurementStop           = rm_zmod4410_iaq_2nd_gen_ulp_measurement_stop,
    .statusCheck               = rm_zmod4410_iaq_2nd_gen_ulp_status_check,
    .read                      = rm_zmod4410_iaq_2nd_gen_ulp_read,
    .iaq1stGenDataCalculate    = rm_zmod4410_iaq_2nd_gen_ulp_iaq_1st_gen_data_calculate,
    .iaq2ndGenDataCalculate    = rm_zmod4410_iaq_2nd_gen_ulp_data_calculate,
    .odorDataCalculate         = rm_zmod4410_iaq_2nd_gen_ulp_odor_data_calculate,
    .sulfurOdorDataCalculate   = rm_zmod4410_iaq_2nd_gen_ulp_sulfur_odor_data_calculate,
    .oaq1stGenDataCalculate    = rm_zmod4410_iaq_2nd_gen_ulp_oaq_1st_gen_data_calculate,
    .oaq2ndGenDataCalculate    = rm_zmod4410_iaq_2nd_gen_ulp_oaq_2nd_gen_data_calculate,
    .raqDataCalculate          = rm_zmod4410_iaq_2nd_gen_ulp_raq_data_calculate,
    .temperatureAndHumiditySet = rm_zmod4410_iaq_2nd_gen_ulp_temperature_and_humidity_set,
    .deviceErrorCheck          = rm_zmod4410_iaq_2nd_gen_ulp_device_error_check,
};

/***********************************************************************************************************************
 * Functions
 **********************************************************************************************************************/

/*******************************************************************************************************************//**
 * @brief Initialize the IAQ 2nd Gen. ULP library
 *
 * @retval FSP_SUCCESS              Successfully started.
 * @retval FSP_ERR_ASSERTION        Null pointer passed as a parameter.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_open (rm_zmod4xxx_ctrl_t * const      p_api_ctrl,
                                                   rm_zmod4xxx_cfg_t const * const p_cfg)
{
    rm_zmod4xxx_instance_ctrl_t * p_ctrl = (rm_zmod4xxx_instance_ctrl_t *) p_api_ctrl;
    int8_t lib_err = 0;
    rm_zmod4xxx_lib_extended_cfg_t * p_lib    = p_ctrl->p_zmod4xxx_lib;
    iaq_2nd_gen_ulp_handle_t       * p_handle = (iaq_2nd_gen_ulp_handle_t *) p_lib->p_handle;

    FSP_PARAMETER_NOT_USED(p_cfg);

    /* Set default temperature and humidity */
    p_lib->temperature = RM_ZMOD4410_IAQ_2ND_GEN_ULP_DEFAULT_TEMPERATURE;
    p_lib->humidity    = RM_ZMOD4410_IAQ_2ND_GEN_ULP_DEFAULT_HUMIDITY;

    /* Initialize the library */
    lib_err = init_iaq_2nd_gen_ulp(p_handle);
    FSP_ERROR_RETURN(0 == lib_err, FSP_ERR_ASSERTION);

    return FSP_SUCCESS;
}

/*******************************************************************************************************************//**
 * @brief  Calculate the IAQ 2nd Gen. ULP data with the library API.
 *
 * @retval FSP_SUCCESS                            Successfully results are read.
 * @retval FSP_ERR_ASSERTION                      Null pointer passed as a parameter.
 * @retval FSP_ERR_SENSOR_IN_STABILIZATION        Module is stabilizing.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_data_calculate (rm_zmod4xxx_ctrl_t * const         p_api_ctrl,
                                                             rm_zmod4xxx_raw_data_t * const     p_raw_data,
                                                             rm_zmod4xxx_iaq_2nd_data_t * const p_zmod4xxx_data)
{
    rm_zmod4xxx_instance_ctrl_t    * p_ctrl    = (rm_zmod4xxx_instance_ctrl_t *) p_api_ctrl;
    rm_zmod4xxx_lib_extended_cfg_t * p_lib     = p_ctrl->p_zmod4xxx_lib;
    iaq_2nd_gen_ulp_handle_t       * p_handle  = (iaq_2nd_gen_ulp_handle_t *) p_lib->p_handle;
    iaq_2nd_gen_ulp_results_t      * p_results = (iaq_2nd_gen_ulp_results_t *) p_lib->p_results;
    zmod4xxx_dev_t                 * p_device  = (zmod4xxx_dev_t *) p_lib->p_device;
    uint16_t                 i;
    int8_t                   lib_err = 0;
    iaq_2nd_gen_ulp_inputs_t algorithm_input;

    /* Calculate IAQ 2nd Gen. ULP data from ADC data */
    algorithm_input.adc_result       = &p_raw_data->adc_data[0];
    algorithm_input.humidity_pct     = p_lib->humidity;
    algorithm_input.temperature_degc = p_lib->temperature;
    lib_err = calc_iaq_2nd_gen_ulp(p_handle, p_device, &algorithm_input, p_results);
    FSP_ERROR_RETURN(0 <= lib_err, FSP_ERR_ASSERTION);

    /* Set Data */
    for (i = 0; i < 13; i++)
    {
        p_zmod4xxx_data->rmox[i] = p_results->rmox[i];
    }

    for (i = 0; i < 3; i++)
    {
        p_zmod4xxx_data->log_nonlog_rcda[i] = p_results->log_nonlog_rcda[i];
    }

    p_zmod4xxx_data->iaq  = p_results->iaq;
    p_zmod4xxx_data->tvoc = p_results->tvoc;
    p_zmod4xxx_data->etoh = p_results->etoh;
    p_zmod4xxx_data->eco2 = p_results->eco2;
    FSP_ERROR_RETURN(IAQ_2ND_GEN_ULP_STABILIZATION != lib_err, FSP_ERR_SENSOR_IN_STABILIZATION);

    return FSP_SUCCESS;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_close (rm_zmod4xxx_ctrl_t * const p_api_ctrl)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_measurement_start (rm_zmod4xxx_ctrl_t * const p_api_ctrl)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_measurement_stop (rm_zmod4xxx_ctrl_t * const p_api_ctrl)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_status_check (rm_zmod4xxx_ctrl_t * const p_api_ctrl)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_read (rm_zmod4xxx_ctrl_t * const     p_api_ctrl,
                                                   rm_zmod4xxx_raw_data_t * const p_raw_data)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);
    FSP_PARAMETER_NOT_USED(p_raw_data);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_temperature_and_humidity_set (rm_zmod4xxx_ctrl_t * const p_api_ctrl,
                                                                           float                      temperature,
                                                                           float                      humidity)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);
    FSP_PARAMETER_NOT_USED(temperature);
    FSP_PARAMETER_NOT_USED(humidity);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_iaq_1st_gen_data_calculate (rm_zmod4xxx_ctrl_t * const         p_api_ctrl,
                                                                         rm_zmod4xxx_raw_data_t * const     p_raw_data,
                                                                         rm_zmod4xxx_iaq_1st_data_t * const p_zmod4xxx_data)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);
    FSP_PARAMETER_NOT_USED(p_raw_data);
    FSP_PARAMETER_NOT_USED(p_zmod4xxx_data);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_odor_data_calculate (rm_zmod4xxx_ctrl_t * const      p_api_ctrl,
                                                                  rm_zmod4xxx_raw_data_t * const  p_raw_data,
                                                                  rm_zmod4xxx_odor_data_t * const p_zmod4xxx_data)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);
    FSP_PARAMETER_NOT_USED(p_raw_data);
    FSP_PARAMETER_NOT_USED(p_zmod4xxx_data);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_sulfur_odor_data_calculate (
    rm_zmod4xxx_ctrl_t * const             p_api_ctrl,
    rm_zmod4xxx_raw_data_t * const         p_raw_data,
    rm_zmod4xxx_sulfur_odor_data_t * const p_zmod4xxx_data)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);
    FSP_PARAMETER_NOT_USED(p_raw_data);
    FSP_PARAMETER_NOT_USED(p_zmod4xxx_data);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_oaq_1st_gen_data_calculate (rm_zmod4xxx_ctrl_t * const         p_api_ctrl,
                                                                         rm_zmod4xxx_raw_data_t * const     p_raw_data,
                                                                         rm_zmod4xxx_oaq_1st_data_t * const p_zmod4xxx_data)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);
    FSP_PARAMETER_NOT_USED(p_raw_data);
    FSP_PARAMETER_NOT_USED(p_zmod4xxx_data);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_oaq_2nd_gen_data_calculate (rm_zmod4xxx_ctrl_t * const         p_api_ctrl,
                                                                         rm_zmod4xxx_raw_data_t * const     p_raw_data,
                                                                         rm_zmod4xxx_oaq_2nd_data_t * const p_zmod4xxx_data)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);
    FSP_PARAMETER_NOT_USED(p_raw_data);
    FSP_PARAMETER_NOT_USED(p_zmod4xxx_data);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_raq_data_calculate (rm_zmod4xxx_ctrl_t * const     p_api_ctrl,
                                                                 rm_zmod4xxx_raw_data_t * const p_raw_data,
                                                                 rm_zmod4xxx_raq_data_t * const p_zmod4xxx_data)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);
    FSP_PARAMETER_NOT_USED(p_raw_data);
    FSP_PARAMETER_NOT_USED(p_zmod4xxx_data);

    return FSP_ERR_UNSUPPORTED;
}

/*******************************************************************************************************************//**
 * @brief  Unsupported API.
 *
 * @retval FSP_ERR_UNSUPPORTED                    Operation mode is not supported.
 **********************************************************************************************************************/
static fsp_err_t rm_zmod4410_iaq_2nd_gen_ulp_device_error_check (rm_zmod4xxx_ctrl_t * const p_api_ctrl)
{
    FSP_PARAMETER_NOT_USED(p_api_ctrl);

    return FSP_ERR_UNSUPPORTED;
}

#endif
