/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.2
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "main.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup ADC_Channel_2
  @{
*/

/** @defgroup ADC_Channel_2_Functions Functions
  @{
*/

/* ADC Init */
void ADC_Init(ADC_CHANNEL_T channel);
/* ADC Measure */
void ADC_Measure(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_GPIO);

    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);

    while (1)
    {
       if(APM_MINI_ReadPBState(BUTTON_KEY1) == BIT_RESET)
       {
           ADC_Init(ADC_CHANNEL_5);
           ADC_Measure();
       }
       else
       {
           ADC_Init(ADC_CHANNEL_4);
           ADC_Measure();
       }
    }
}

/*!
 * @brief       ADC Calibration
 *
 * @param       None
 *
 * @retval      None
 */
void ADC_Calibration(void)
{
    uint8_t i = 0;
    int8_t offset = 0;
    uint16_t adcData = 0;

    ADC_SetOffset(0);
    ADC_EnableCompensation();
    ADC_ClearStatusFlag(ADC_FLAG_CC);
    ADC_Enable();

    for (i = 0; i < 10; i++)
    {
        ADC_StartConversion();

        while (ADC_ReadStatusFlag(ADC_FLAG_CC) == RESET);

        ADC_ClearStatusFlag(ADC_FLAG_CC);
    }

    ADC_Disable();

    adcData = ADC_ReadData();
    offset = (int8_t)(0x800 - adcData);
    ADC_SetOffset(offset);

    ADC_DisableCompensation();
    ADC_ClearStatusFlag(ADC_FLAG_CC);
}

/*!
 * @brief       ADC Initialization
 *
 * @param       channel : Specifies the channel
 *
 * @retval      None
 */
void ADC_Init(ADC_CHANNEL_T channel)
{
    ADC_Config_T adcConfig;
    GPIO_Config_T gpioConfig;

    /* ADC GPIO configuration */
    gpioConfig.intEn = GPIO_EINT_DISABLE;
    gpioConfig.mode = GPIO_MODE_IN_FLOATING;
    gpioConfig.speed = GPIO_SPEED_10MHz;

    /* ADC_CHANNEL_4 */
    if (channel == ADC_CHANNEL_4)
    {
        gpioConfig.pin = GPIO_PIN_3;
        GPIO_Config(GPIOD, &gpioConfig);
    }
    /* ADC_CHANNEL_5 */
    else
    {
        gpioConfig.pin = GPIO_PIN_5;
        GPIO_Config(GPIOD, &gpioConfig);
    }

    ADC_SetMode(ADC_MODE_SINGLE_END);

    /* ADC configuration */
    ADC_ConfigStructInit(&adcConfig);
    adcConfig.channel = channel;
    adcConfig.convMode = ADC_CONV_MODE_CONTINUOUS;
    adcConfig.interrupt = ADC_INT_NONE;
    ADC_Config(&adcConfig);

    /* ADC Calibration */
    ADC_Calibration();

    ADC_Enable();

    ADC_StartConversion();
}

/*!
 * @brief       ADC measure
 *
 * @param       None
 *
 * @retval      None
 */
void ADC_Measure(void)
{
    uint8_t i = 0;
    uint16_t adcData = 0;
    uint16_t voltage = 0;

    for (i = 0; i < 10; i++)
    {
        while (ADC_ReadStatusFlag(ADC_FLAG_CC) == RESET);

        ADC_ClearStatusFlag(ADC_FLAG_CC);

        adcData = ADC_ReadData();

        /* voltage(mV) =  adcData * (3300mV / 4095) */
        voltage = (adcData * 3300) / 4095;

        /* voltage > 3100mv */
        if (voltage > 3100)
        {
            APM_MINI_LEDOn(LED2);
            APM_MINI_LEDOff(LED3);
        }
        /* voltage < 800mv */
        else if (voltage < 800)
        {
            APM_MINI_LEDOff(LED2);
            APM_MINI_LEDOn(LED3);
        }
        /* 800mv <= voltage <= 3100 */
        else
        {
            APM_MINI_LEDOn(LED2);
            APM_MINI_LEDOn(LED3);
        }
    }
}

/**@} end of group ADC_Channel_2_Functions */
/**@} end of group ADC_Channel_2 */
/**@} end of group Examples */
