/**
  ******************************************************************************
  * @file demo_beeper.c
  * @brief This file contains all the functions for the BEEPER demo.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.1.0
  * @date    09/14/2009
  ******************************************************************************
  *
  */

// Includes ------------------------------------------------------------------
#include "demo_beeper.h"

// Private typedef -----------------------------------------------------------
// Private define ------------------------------------------------------------
// Private macro -------------------------------------------------------------
// Private variables ---------------------------------------------------------
// Private function prototypes -----------------------------------------------
// Private functions ---------------------------------------------------------

  // * @addtogroup Demo_Functions

void BEEPERDEMO_Reset(void)
{
    LEDS_OFF();
    BEEP_Cmd(DISABLE);
}

void BEEPERDEMO_Exec(void)
{
    JOYSTICK_ID_T Key;
    uint8_t idx = 0;
    uint8_t idx2;

    LCD_Clear();
    // BEEP calibration 
    disableInterrupts();
		WaitDelay(50);
    LCD_PrintString(LCD_LINE1, ENABLE, DISABLE, "  CALIBRATION");
    LCD_PrintString(LCD_LINE2, ENABLE, DISABLE, "   on going");
    BEEP_AutoLSICalibration();
    enableInterrupts();
    LCD_PrintString(LCD_LINE2, ENABLE, DISABLE, "      OK");
    WaitDelay(200);

    LCD_Clear();
		WaitDelay(50);
    LCD_PrintString(LCD_LINE1, ENABLE, DISABLE, "  BEEPER OFF");
    LCD_PrintString(LCD_LINE2, ENABLE, DISABLE, "   Press Key");

    while (1)
    {

        Key = ReadJoystick();
        if (Key == UP)
        {
            MenuUpdate(Key);
            return;
        }

        Key = ReadButton();
        if (Key == B_PRESSED)
        {
            // Change BEEP frequency 
            switch (idx)
            {
            case 0:
						    BEEP_Cmd(DISABLE);
								WaitDelay(80);
								BEEP_Init(BEEP_Frequency_1KHz);
								BEEP_Cmd(ENABLE);
                LEDS_OFF();
                LED2_ON();
                LCD_PrintString(LCD_LINE1, ENABLE, DISABLE, "  BEEPER 1kHz");
                idx = 1;
                break;
            case 1:
						    BEEP_Cmd(DISABLE);
								WaitDelay(80);
                BEEP_Init(BEEP_Frequency_2KHz);
								BEEP_Cmd(ENABLE);
                LEDS_OFF();
                LED3_ON();
                LCD_PrintString(LCD_LINE1, ENABLE, DISABLE, "  BEEPER 2kHz");
                idx = 2;
                break;
            case 2:
						    BEEP_Cmd(DISABLE);
								WaitDelay(80);
                BEEP_Init(BEEP_Frequency_4KHz);
								BEEP_Cmd(ENABLE);
                LEDS_OFF();
                LED4_ON();
                LCD_PrintString(LCD_LINE1, ENABLE, DISABLE, "  BEEPER 4kHz");
                idx = 3;
                break;
            case 3:
						    
                LEDS_OFF();
								LED2_ON();
                LED3_ON();
                LCD_PrintString(LCD_LINE1, ENABLE, DISABLE, "BEEPER 1-2-4kHz");
                idx = 4;
                idx2 = 1;
                GetTimeOutStartTime();
                break;
            default:
            case 4:
                BEEP_Cmd(DISABLE);
                LEDS_OFF();
                LCD_PrintString(LCD_LINE1, ENABLE, DISABLE, "  BEEPER OFF");
                idx = 0;
                break;
            }
        }
        if (idx == 4)
        {
            if (TimeOut(50) == TRUE)
            {
                LEDS_TOG();
								LEDBICOLOR_TOG();
                SelectBeep(idx2++);
                if (idx2 > 3) idx2 = 0;
                GetTimeOutStartTime();
            }
        }
    }
}

void SelectBeep(uint8_t nb)
{
    if (nb == 0) BEEP_Cmd(DISABLE);
    if (nb == 1) BEEP_Init(BEEP_Frequency_1KHz);
    if (nb == 2) BEEP_Init(BEEP_Frequency_2KHz);
    if (nb == 3) BEEP_Init(BEEP_Frequency_4KHz);
}

/**
  * @brief Measure the LSI frequency using timer IC1 and update the calibration registers.
  * @par Parameters:
  * None
  * @retval 
	* None
  * @par Required preconditions:
  * It is recommanded to use a timer clock frequency of at least 10MHz in order 
	* to obtain a better in the LSI frequency measurement.
  */
void BEEP_AutoLSICalibration(void)
{
  uint32_t lsi_freq_hz = 0x0;
  uint32_t fmaster = 0x0;
  uint16_t ICValue1 = 0x0;
  uint16_t ICValue2 = 0x0;

  // Get master frequency 
  fmaster = CLK_GetClockFreq();

	// Measure the LSI frequency with TIMER Input Capture 1 
	// Enable the LSI measurement: LSI clock connected to timer Input Capture 1 
  AWU->CSR |= AWU_CSR_MSR;

  // Capture only every 8 events!!! 
  TIM2_ICInit(	TIM2_Channel_1, TIM2_ICPolarity_Rising, TIM2_ICSelection_DirectTI, TIM2_ICPSC_Div8, 0x0);

  // Enable TIM2 
  TIM2_Cmd(ENABLE);

  TIM2->SR1 = 0x00;
  TIM2->SR2 = 0x00;

  // Clear CC1 Flag
  TIM2_ClearFlag(TIM2_FLAG_CC1);

  // wait a capture on cc1 
  while ((TIM2->SR1 & (uint8_t)TIM2_FLAG_CC1) != TIM2_FLAG_CC1)
  {
  }
  // Get CCR1 value
  ICValue1 = TIM2_GetCapture1();
  TIM2_ClearFlag(TIM2_FLAG_CC1);

  // wait a capture on cc1 
  while ((TIM2->SR1 & (uint8_t)TIM2_FLAG_CC1) != TIM2_FLAG_CC1)
  {
  }
  // Get CCR1 value
  ICValue2 = TIM2_GetCapture1();
  TIM2_ClearFlag(TIM2_FLAG_CC1);

  // Disable IC1 input capture 
  TIM2->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
  // Reset CCMR1 register 
  TIM2->CCMR1 = 0x00;
  // Disable TIM2 
  TIM2_Cmd(DISABLE);

  // Compute LSI clock frequency 
  lsi_freq_hz = (8 * fmaster) / (ICValue2 - ICValue1);

	// Disable the LSI measurement: LSI clock disconnected from timer Input Capture 1 
  AWU->CSR &= (uint8_t)(~AWU_CSR_MSR);

  // Update the calibration registers 
  BEEP_LSICalibrationConfig(lsi_freq_hz);
}


