/*!
 * @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 BUZZER
  @{
*/

/** @defgroup BUZZER_Functions Functions
  @{
*/

/* Buuzer init */
void BuzzerInit(void);
/* Measure the LIRC frequency */
uint32_t LIRC_Measure(void);
/* Buuzer ouput frequency configuration */
void BuzzerOutputConfig(BUZZER_FREQUENCY_T freq);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    uint8_t count = 0;

    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_GPIO);
    APM_DelayInit(APM_DELAY_MS);

    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);

    BuzzerInit();

    while(1)
    {
        if(APM_MINI_ReadPBState(BUTTON_KEY1) == RESET)
        {
            switch(count)
            {
                case 0:
                    BuzzerOutputConfig(BUZZER_FREQUENCY_1KHZ);
                    APM_MINI_LEDOn(LED2);
                    APM_MINI_LEDOff(LED3);
                    break;
                case 1:
                    BuzzerOutputConfig(BUZZER_FREQUENCY_2KHZ);
                    APM_MINI_LEDOff(LED2);
                    APM_MINI_LEDOn(LED3);
                    break;
                case 2:
                    BuzzerOutputConfig(BUZZER_FREQUENCY_4KHZ);
                    APM_MINI_LEDOn(LED2);
                    APM_MINI_LEDOn(LED3);
                    break;
                default:
                    break;
            }
            count++;
            if(count == 3)
            {
                count = 0;
            }
            APM_Delay(200);
        }
    }
}

/*!
 * @brief       Measure the LIRC frequency
 *
 * @param       None
 *
 * @retval      LIRC frequency in HZ
 */
uint32_t LIRC_Measure(void)
{
    uint16_t icValue[2];
    uint32_t freqLIRC;
    uint32_t freqMasterClk;
    TMR1_ICConfig_T icConfig;

    freqMasterClk = RCM_GetMasterClockFreq();

    WUPT_EnableLIRCMeasurement();

    icConfig.channel = TMR1_CHANNEL_1;
    icConfig.div = TMR1_IC_DIV_8;
    icConfig.filter = 0;
    icConfig.polarity = TMR1_IC_POLARITY_FALLING;
    icConfig.selection = TMR1_IC_SELECT_DIRECTTI;
    TMR1_ConfigInputCapture(TMR1, &icConfig);
    TMR1_Enable(TMR1);

    while(TMR1_ReadStatusFlag(TMR1, TMR1_FLAG_CH1CC) == RESET);
    icValue[0] = TMR1_ReadCompareCapture(TMR1, TMR1_CHANNEL_1);
    TMR1_ClearStatusFlag(TMR1, TMR1_FLAG_CH1CC);

    while(TMR1_ReadStatusFlag(TMR1, TMR1_FLAG_CH1CC) == RESET);
    icValue[1] = TMR1_ReadCompareCapture(TMR1, TMR1_CHANNEL_1);
    TMR1_ClearStatusFlag(TMR1, TMR1_FLAG_CH1CC);

    TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_1);
    TMR1_Disable(TMR1);

    freqLIRC = (8 * freqMasterClk) / (icValue[1] - icValue[0]);

    WUPT_DisableLIRCMeasurement();

    return freqLIRC;
}

/*!
 * @brief       Buuzer ouput frequency configuration
 *
 * @param       freq:   Frequency
 *
 * @retval      None
 */
void BuzzerOutputConfig(BUZZER_FREQUENCY_T freq)
{
    BUZZER_Disable();
    APM_Delay(20);
    BUZZER_Config(freq);
    BUZZER_Enable();
}

/*!
 * @brief       Buuzer init
 *
 * @param       None
 *
 * @retval      None
 */
void BuzzerInit(void)
{
    uint32_t freqLIRC = 0;

    freqLIRC = LIRC_Measure();
    BUZZER_CalibrationLIRC(freqLIRC);
}

/**@} end of group BUZZER_Functions */
/**@} end of group BUZZER */
/**@} end of group Examples */
