/**
  ******************************************************************************
  * @file    stm32f0xx_it.c 
  * @author  MCD Application Team
  * @version V1.0.1
  * @date    29-May-2012
  * @brief   Main Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */ 

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

/** @addtogroup STM320518_EVAL_Demo
  * @{
  */
/** @addtogroup STM32F0XX_IT
  * @brief Interrupts driver modules
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define MESSAGE4   "Tamper to Reset Backup reg  " 
#define MESSAGE5     " LEFT   |  RIGHT  |  SEL  " 
#define MESSAGE6   " RESET  |  START  |  EXIT   " 
#define MESSAGE7   " RESET  |  PAUSE  |  EXIT   " 

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint8_t Index = 0, Counter1 = 0;
__IO uint32_t WaveCounter = 0;
static __IO uint8_t AlarmStatus = 0;
static __IO uint8_t LedCounter = 0;
extern uint8_t Buffer1[];
extern uint8_t Buffer2[];

extern FIL F;
extern UINT BytesWritten;
extern UINT BytesRead;
extern __IO uint8_t SMbusAlertOccurred;
extern WAVE_FormatTypeDef WAVE_Format;
extern uint8_t Key_Button_Pressed;

__IO uint32_t RTCAlarmCount = 0;

__IO uint32_t SubSecFrac = 0;
__IO uint32_t SecondNumb = 60;
extern __IO uint8_t StartEvent;
extern __IO uint8_t MenuApplication;
__IO uint32_t CurrentTimeSec = 0;
RTC_TimeTypeDef RTC_StampTimeStruct;

__IO uint32_t BackupIndex = 0;

uint8_t TabIndex = 0x00;                      /* Variable to choice between Buffer1 and Buffer2 */
uint8_t* Tab;                                 /* Pointer on Buffer1 or Buffer2  */
__IO uint16_t RecordSample = 0x00;            /* Recorded data index in Buffer1 or Buffer2 */
__IO uint8_t WriteOnTheSDCardStatus = 0x00;
__IO uint8_t DOWNBoutonPressed = 0;           /* Stop wave record enable */
extern uint32_t SelectedMaxRecordDataLength;  /* Selected max SD Card size dedicated */
extern __IO uint8_t  VoiceRecStop;            /* Stop Voice Recorder Flag*/

__IO uint8_t RFDemoStatus = 0x00;
uint32_t ICValue1 = 0;
uint32_t ICValue2 = 0;
extern uint32_t RC5_FrameManchestarFormat;
extern uint32_t SIRC12_FramePulseWidthFormat[];
extern __IO uint32_t LCDType;
__IO uint8_t RFProtocol = 0x00;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M0 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
  uint32_t tmp = 318, index = 0;
  
  /* Disable LCD Window mode */
  LCD_WindowModeDisable();

  /* If HSE is not detected at program startup or HSE clock failed during program execution */
  if ((RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) || (RCC_GetITStatus(RCC_IT_CSS) != RESET))
  {
    /* Clear the LCD */
    LCD_Clear(LCD_COLOR_WHITE);
    /* Set the LCD Back Color */
    LCD_SetBackColor(LCD_COLOR_BLUE);
    /* Set the LCD Text Color */
    LCD_SetTextColor(LCD_COLOR_WHITE);
    /* Display " No Clock Detected  " message */
    LCD_DisplayStringLine(LCD_LINE_0, "No HSE Clock         ");
    LCD_DisplayStringLine(LCD_LINE_1, "Detected. STANDBY    ");
    LCD_DisplayStringLine(LCD_LINE_2, "mode in few seconds. ");
    
    LCD_ClearLine(LCD_LINE_4);
    LCD_DisplayStringLine(LCD_LINE_5, "If HSE Clock         ");
    LCD_DisplayStringLine(LCD_LINE_6, "recovers before the  ");
    LCD_DisplayStringLine(LCD_LINE_7, "time out, a System   ");
    LCD_DisplayStringLine(LCD_LINE_8, "Reset is generated.  ");
    LCD_ClearLine(LCD_LINE_9);
    /* Clear Clock Security System interrupt pending bit */
    RCC_ClearITPendingBit(RCC_IT_CSS);
    GPIO_ResetBits(GPIOC, GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12);
    GPIO_ResetBits(GPIOD, GPIO_Pin_2);
    /* Enable HSE */
    RCC_HSEConfig(RCC_HSE_ON);

    /* Set the Back Color */
    LCD_SetBackColor(LCD_COLOR_WHITE);
    /* Set the Text Color */
    LCD_SetTextColor(LCD_COLOR_RED);
    LCD_DrawRect(71, 319, 25, 320);
    LCD_SetBackColor(LCD_COLOR_GREEN);
    LCD_SetTextColor(LCD_COLOR_WHITE);
    /* Wait till HSE is ready */

    while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
    {
      if (index == 0xFFFF)
      {
        /* Check if the LCD is HX8347D Controller */
        if(LCDType != LCD_HX8347D)
        {
          LCD_DisplayChar(LCD_LINE_3, tmp, 0x20);
        }
        else
        {
          LCD_DisplayChar(LCD_LINE_3, 320 - tmp, 0x20);
        }
        tmp -= 16;
        index = 0;
      }
      index++;

      /* Enters the system in STANDBY mode */
      if (tmp < 16)
      {
        LCD_SetBackColor(LCD_COLOR_BLUE);
        LCD_ClearLine(LCD_LINE_3);
        LCD_ClearLine(LCD_LINE_4);
        LCD_ClearLine(LCD_LINE_5);
        LCD_ClearLine(LCD_LINE_6);
        LCD_DisplayStringLine(LCD_LINE_7, " MCU in STANDBY Mode");
        LCD_DisplayStringLine(LCD_LINE_8, "To exit press Tamper");
        /* Enable WakeUp pin */
        PWR_WakeUpPinCmd(PWR_WakeUpPin_2, ENABLE);
        /* Request to enter STANDBY mode */
        PWR_EnterSTANDBYMode();
      }
    }

    /* Generate a system reset */
    NVIC_SystemReset();
  }
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
  /* Decrement the TimingDelay variable */
  Demo_DecrementTimingDelay(); 
}

/******************************************************************************/
/*                 STM32F0xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f0xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @brief  This function handles RTC interrupt request.
  * @param  None
  * @retval None
  */
void RTC_IRQHandler(void)
{
#ifdef APPLICATION_SUBDEMO
  uint8_t i = 0;
  uint32_t tmp = 0;
  
  if(MenuApplication == 1)
  {
    if (RTC_GetITStatus(RTC_IT_TAMP1) != RESET)
    {
      /* Set the LCD Back Color */
      LCD_SetBackColor(White);
      LCD_SetFont(&Font16x24);
      
      /* Clear LCD line 5 to 9 */
      for (i=0; i < 5; i++)
      {  
        /* Clear all the LCD lignes from 3 to 7 */
        LCD_ClearLine(LINE(3+i));
      }
      
      /* Reset Counter */
      BackupIndex = 0 ;
      
      /* Enter to idle */
      StartEvent =0x0;
      
      LCD_SetFont(&Font12x12);
      RTC_Time_InitDisplay();
      
      LCD_SetFont(&Font12x12);
      LCD_ClearLine(LINE(17));
      
      /* Enable Tamper interrupt */
      RTC_ITConfig(RTC_IT_TAMP, DISABLE);
      /* Enable the tamper 1 */
      RTC_TamperCmd(RTC_Tamper_1 , DISABLE);
    }
    
    /* Clear Tamper pin interrupt pending bit */
    RTC_ClearITPendingBit(RTC_IT_TAMP1);
    /* Clear EXTI line 19 */
    EXTI_ClearITPendingBit(EXTI_Line19);
  }
  
  else if(MenuApplication == 2)
  {
    /* Check on the AlarmA falg and on the number of interrupts per Second (60*8) */
    if(RTC_GetITStatus(RTC_IT_ALRA) != RESET) 
    { 
      /* Clear RTC AlarmA Flags */
      RTC_ClearITPendingBit(RTC_IT_ALRA);
      
      /* Generate alarms 480 = 60 * 8 */   
      if(RTCAlarmCount != (8 * SecondNumb))
      {
        /* Increament the counter of Alarma interrupts*/
        RTCAlarmCount++;
        
        
        /* Define the rate of Progress bar */
        tmp = (uint32_t) ((RTCAlarmCount * 100)/ (8 * SecondNumb)); 
        
        /* Set the LCD text color */
        LCD_SetTextColor(Blue);
        
        /* Set the LCD Font */
        LCD_SetFont(&Font16x24);
        /* Check if the LCD is HX8347D Controller */
        if(LCDType != LCD_HX8347D)
        {
          /* Display Char on the LCD : XXX% */
          LCD_DisplayChar(40,110, (tmp / 100) +0x30);
          LCD_DisplayChar(40,88, ((tmp  % 100 ) / 10) +0x30);
          LCD_DisplayChar(40,66, (tmp % 10) +0x30);
          LCD_DisplayChar(40,44, 0x25); 
        }
        else
        {
          /* Display Char on the LCD : XXX% */
          LCD_DisplayChar(40,320 - 110, (tmp / 100) +0x30);
          LCD_DisplayChar(40,320 - 88, ((tmp  % 100 ) / 10) +0x30);
          LCD_DisplayChar(40,320 - 66, (tmp % 10) +0x30);
          LCD_DisplayChar(40,320 - 44, 0x25); 
        }
        
        if(SecondNumb > 13)
        {
          if((tmp % 2) == 0 )
          {  
            /* Set the LCD text color */
            LCD_SetTextColor(White);
            LCD_DrawLine(70 + (tmp/2) , 120 - (tmp/2)  , 100 - tmp  ,Horizontal);
            LCD_DrawLine(170 - (tmp/2) , 120 - (tmp/2)  , 100 - tmp ,Horizontal);
            
            /* Set the LCD Text Color */
            LCD_SetTextColor(Blue);
            LCD_DrawLine(170 - (tmp/2) , 120 - (tmp/2)  , 100 - tmp ,Horizontal);
            
          }
        }      
      }
      else
      {
        StartEvent = 9;
        /* Disable the alarmA */
        RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
      }
    }
    /* Clear the EXTIL line 17 */
    EXTI_ClearITPendingBit(EXTI_Line17);
  }
  else 
  {
#endif /* APPLICATION_SUBDEMO */
    if (RTC_GetITStatus(RTC_IT_ALRA) != RESET)
    {
      /* Clear the Alarm A Pending Bit */
      RTC_ClearITPendingBit(RTC_IT_ALRA);
      
      /* Set the STOPModeStatus variable to be used in the the lowpower sub-demo */
      LowPower_Set_STOPModeStatus();
      
      AlarmStatus = 1;
      /* Clear EXTI line 17 */
      EXTI_ClearITPendingBit(EXTI_Line17);
    }
#ifdef APPLICATION_SUBDEMO
  }
#endif /* APPLICATION_SUBDEMO */
}

/**
  * @brief  This function handles External line 0/1 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI0_1_IRQHandler(void)
{
  if (EXTI_GetITStatus(SEL_BUTTON_EXTI_LINE) != RESET)
  {
    /* SEL function */
    while (STM_EVAL_PBGetState(BUTTON_SEL) == Bit_SET);
    /* SEL function */
    Set_SELStatus();
    /* Clear the EXTI Line 0 */
    EXTI_ClearITPendingBit(SEL_BUTTON_EXTI_LINE);
  }
}

/**
  * @brief  This function handles External lines 9 to 5 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI4_15_IRQHandler(void)
{
#ifdef APPLICATION_SUBDEMO
  RTC_TimeTypeDef  RTC_TimeStructureInit;
  uint8_t index = 0;
  uint32_t tmp = 0, tmp1 = 0;
#endif /* APPLICATION_SUBDEMO */
  
  if (EXTI_GetITStatus(SD_DETECT_EXTI_LINE) != RESET)
  {
    if(GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != RESET) 
    {
      /* Disable TIM6 */
      TIM_Cmd(TIM6, DISABLE);
      
      /* Disable the alarmA */
      RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
      
      LCD_Clear(LCD_COLOR_WHITE);
      /* Set the Back Color */
      LCD_SetBackColor(LCD_COLOR_BLUE);
      /* Set the Text Color */
      LCD_SetTextColor(LCD_COLOR_RED);
      LCD_DisplayStringLine(LCD_LINE_2, "Err: SDCard Removed ");
      LCD_DisplayStringLine(LCD_LINE_3, "Please check SD Card");
      /* Set the Text Color */
      LCD_SetTextColor(LCD_COLOR_WHITE);
      LCD_DisplayStringLine(LCD_LINE_5, "Press JoyStick Up to");
      LCD_DisplayStringLine(LCD_LINE_6, "Restart the Demo... ");
      
      /* Wait until button up is pressed */
      while (Menu_ReadKey() != UP)
      {}
      /* Generate System Reset to load the new option byte values */
      NVIC_SystemReset();
    }
    else
    {
      /* Clear the SD EXTI pending bit */
      EXTI_ClearITPendingBit(SD_DETECT_EXTI_LINE);  
    }
  }
  else
  {
#ifdef APPLICATION_SUBDEMO
    if (MenuApplication == 1) /* Stop watch */
    {
      if((EXTI_GetITStatus(LEFT_BUTTON_EXTI_LINE) != RESET) && (StartEvent == 0x0))
      {
        EXTI_ClearITPendingBit(EXTI_Line19);
        /* Enable Tamper interrupt */
        RTC_ITConfig(RTC_IT_TAMP, ENABLE);
        /* Enable the tamper 1 */
        RTC_TamperCmd(RTC_Tamper_1, ENABLE);
        
        /* Set the LCD Back Color */
        LCD_SetBackColor(White);
        
        /* Clear the LCD line 5 */
        LCD_ClearLine(Line5);
        
        /* Get the current time */
        RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructureInit);
        CurrentTimeSec = (RTC_TimeStructureInit.RTC_Hours * 3600) + (RTC_TimeStructureInit.RTC_Minutes * 60) +
          RTC_TimeStructureInit.RTC_Seconds;     
        
        /* start count */
        StartEvent = 0x1;
      }
      
      else if((EXTI_GetITStatus(RIGHT_BUTTON_EXTI_LINE) != RESET) && (StartEvent !=0x0))
      {
        uint16_t Colorx;
        
        if (BackupIndex < 5)
        {
          if((uint8_t)(BackupIndex% 2) != 0x0)
          { 
            /* Set LCD backcolor*/
            LCD_SetBackColor(Blue2);
            Colorx = White;
          }
          else
          {
            /* Set LCD backcolor*/
            LCD_SetBackColor(Cyan);
            Colorx = Black;
          }
          LCD_SetFont(&Font16x24); 
          
          RTC_GetTime(RTC_Format_BIN, &RTC_StampTimeStruct);
          
          SubSecFrac = 0;
          /* Get the Current sub second and time */
          SubSecFrac = (((256 - (uint32_t)RTC_GetSubSecond()) * 1000) / 256);

          /* Display result on the LCD */
          RTC_Time_Display(LINE(3 + BackupIndex), Colorx, RTC_Get_Time(SubSecFrac , &RTC_StampTimeStruct)); 
        }
        else
        {
          /* The backup register is full with 10 trials */
          /* Set the LCD Back Color */
          LCD_SetBackColor(White);
          LCD_SetFont(&Font12x12);
          /* Set the LCD Text Color */
          LCD_SetTextColor(Red); 
          LCD_DisplayStringLine(LINE(17), (uint8_t *)MESSAGE4);
        }
        
        BackupIndex++;
        /* Set the LCD Back Color */
        LCD_SetBackColor(White);  
      }
      /* Clear the LEFT EXTI  pending bit */
      EXTI_ClearITPendingBit(LEFT_BUTTON_EXTI_LINE);  
      /* Clear the RIGHT EXTI line */
      EXTI_ClearITPendingBit(RIGHT_BUTTON_EXTI_LINE);    
    }
    else if(MenuApplication == 2) /* Lap Timer */
    {
      if (EXTI_GetITStatus(UP_BUTTON_EXTI_LINE) != RESET)
      {
        if(RTCAlarmCount == 0)
        {
          SecondNumb++;
          
          tmp = (uint32_t) (SecondNumb/60);
          tmp1 =   SecondNumb -(tmp*60);
          
          LCD_SetFont(&Font16x24);
          /* Set the LCD text color */
          LCD_SetTextColor(Blue);
          /* Set the LCD Back Color */
          LCD_SetBackColor(White);
          LCD_DisplayStringLine(95,     "         ");
          /* Check if the LCD is HX8347D Controller */
          if(LCDType != LCD_HX8347D)
          {
            /* Display Char on the LCD : XXX% */       
            LCD_DisplayChar(95,294, (tmp / 10) +0x30);
            LCD_DisplayChar(95,278, (tmp  % 10 ) +0x30);
            LCD_DisplayChar(95,262, ':');
            LCD_DisplayChar(95,246, (tmp1 / 10) +0x30);
            LCD_DisplayChar(95,230, (tmp1  % 10 ) +0x30);
          }
          else
          {
            /* Display Char on the LCD : XXX% */       
            LCD_DisplayChar(95, 320 - 294, (tmp / 10) +0x30);
            LCD_DisplayChar(95, 320 - 278, (tmp  % 10 ) +0x30);
            LCD_DisplayChar(95, 320 - 262, ':');
            LCD_DisplayChar(95, 320 - 246, (tmp1 / 10) +0x30);
            LCD_DisplayChar(95, 320 - 230, (tmp1  % 10 ) +0x30);
          }
          
        }
        /* Clear the UP EXTI Line*/
        EXTI_ClearITPendingBit(UP_BUTTON_EXTI_LINE);
      } 
      else if(EXTI_GetITStatus(DOWN_BUTTON_EXTI_LINE) != RESET)
      {
        if(RTCAlarmCount == 0)
        {
          SecondNumb--;
          
          tmp = (uint32_t) (SecondNumb/60);
          tmp1 = SecondNumb -(tmp*60);
          LCD_SetFont(&Font16x24); 
          /* Set the LCD text color */
          LCD_SetTextColor(Blue);
          /* Set the LCD Back Color */
          LCD_SetBackColor(White);
          LCD_DisplayStringLine(95,     "         ");  
          /* Check if the LCD is HX8347D Controller */
          if(LCDType != LCD_HX8347D)
          {
            /* Display Char on the LCD : XXX% */       
            LCD_DisplayChar(95,294, (tmp / 10) +0x30);
            LCD_DisplayChar(95,278, (tmp  % 10 ) +0x30);
            LCD_DisplayChar(95,262, ':');
            LCD_DisplayChar(95,246, (tmp1 / 10) +0x30);
            LCD_DisplayChar(95,230, (tmp1  % 10 ) +0x30);
          }
          else
          {
            /* Display Char on the LCD : XXX% */       
            LCD_DisplayChar(95,320 - 294, (tmp / 10) +0x30);
            LCD_DisplayChar(95,320 - 278, (tmp  % 10 ) +0x30);
            LCD_DisplayChar(95,320 - 262, ':');
            LCD_DisplayChar(95,320 - 246, (tmp1 / 10) +0x30);
            LCD_DisplayChar(95,320 - 230, (tmp1  % 10 ) +0x30);
          }
        }
        /* Clear the DOWN EXTI Line*/
        EXTI_ClearITPendingBit(DOWN_BUTTON_EXTI_LINE);
      } 
      
      else if((EXTI_GetITStatus(LEFT_BUTTON_EXTI_LINE) != RESET) )
      {
        /* Set the LCD Back Color */
        LCD_SetBackColor(White);
        StartEvent = 0;
        /* Reset Counter*/
        RTCAlarmCount = 0;
        
        /* Disable the alarm */
        RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
        /* Check if the LCD is HX8347D Controller */
        if(LCDType != LCD_HX8347D)
        {
          /* Display Char on the LCD : XXX% */
          LCD_DisplayChar(40, 110, 0x30);
          LCD_DisplayChar(40, 88, 0x30);
          LCD_DisplayChar(40, 66, 0x30);
          LCD_DisplayChar(40, 44, 0x25);
        }
        else
        {
          /* Display Char on the LCD : XXX% */
          LCD_DisplayChar(40, 320 - 110, 0x30);
          LCD_DisplayChar(40, 320 - 88, 0x30);
          LCD_DisplayChar(40, 320 -  66, 0x30);
          LCD_DisplayChar(40, 320 - 44, 0x25);
        }
        
        for (index = 0; index < 100; index++)
        {
          if ((index % 2) ==0)
          {
            /* Set the LCD Text Color */
            LCD_SetTextColor(Blue);
            LCD_DrawLine(70 + (index/2) , 120 - (index/2)  , 101 - (index + 1) ,Horizontal);
            /* Set the LCD Text Color */
            LCD_SetTextColor(White);
            LCD_DrawLine(170 - (index/2) , 120 - (index/2)  , 101 - (index + 1) ,Horizontal);
          }
        } 
        /* Displays MESSAGE1 on line 5 */
        LCD_SetFont(&Font12x12);
        /* Set the LCD Back Color */
        LCD_SetBackColor(Cyan);
        /* Set the LCD Text Color */
        LCD_SetTextColor(Black); 
        LCD_DisplayStringLine(LINE(18), (uint8_t *)MESSAGE5);
        /* Set the LCD Back Color */
        LCD_SetBackColor(Blue);
        LCD_SetTextColor(White);
        LCD_DisplayStringLine(LINE(19), (uint8_t *)MESSAGE6);
        LCD_SetFont(&Font16x24);

        /* Set the LCD Text Color */
        LCD_SetTextColor(Black); 
        /* Clear the WAKEUP EXTI  pending bit */
        EXTI_ClearITPendingBit(LEFT_BUTTON_EXTI_LINE);  
      }
      else if (EXTI_GetITStatus(RIGHT_BUTTON_EXTI_LINE) != RESET)
      {
        if(StartEvent == 8)
        {
          StartEvent = 0;
          
          /* Enable the alarmA */
          RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
          
          /* Clear the TAMPER EXTI  pending bit */
          EXTI_ClearITPendingBit(KEY_BUTTON_EXTI_LINE); 
          
          /* Displays MESSAGE1 on line 5 */
          LCD_SetFont(&Font12x12);
          /* Set the LCD Back Color */
          LCD_SetBackColor(Cyan);
          /* Set the LCD Text Color */
          LCD_SetTextColor(Black); 
          LCD_DisplayStringLine(LINE(18), (uint8_t *)MESSAGE5);
          /* Set the LCD Back Color */
          LCD_SetBackColor(Blue);
          LCD_SetTextColor(White);
          LCD_DisplayStringLine(LINE(19), (uint8_t *)MESSAGE6);
          LCD_SetFont(&Font16x24);
          /* Set the LCD Text Color */
          LCD_SetTextColor(Black); 
        }
        else
        {
          /* Displays MESSAGE1 on line 5 */
          LCD_SetFont(&Font12x12);
          /* Set the LCD Back Color */
          LCD_SetBackColor(Cyan);
          /* Set the LCD Text Color */
          LCD_SetTextColor(Black); 
          LCD_DisplayStringLine(LINE(18), (uint8_t *)MESSAGE5);
          /* Set the LCD Back Color */
          LCD_SetBackColor(Blue);
          LCD_SetTextColor(White);
          LCD_DisplayStringLine(LINE(19), (uint8_t *)MESSAGE7);
          LCD_SetFont(&Font16x24);
          /* Set the LCD Back Color */
          LCD_SetBackColor(White);
          
          /* Enable the alarmA */
          RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
          StartEvent = 8;
          /* Clear the LEFT EXTI  pending bit */
          EXTI_ClearITPendingBit(RIGHT_BUTTON_EXTI_LINE); 
        }
      }
      else if(EXTI_GetITStatus(DOWN_BUTTON_EXTI_LINE) != RESET)
      {
        if(RTCAlarmCount == 0)
        {
          SecondNumb--;
          
          tmp = (uint32_t) (SecondNumb/60);
          tmp1 =   SecondNumb -(tmp*60);
          LCD_SetFont(&Font16x24); 
          /* Set the LCD text color */
          LCD_SetTextColor(Blue);
          /* Set the LCD Back Color */
          LCD_SetBackColor(White);
          LCD_DisplayStringLine(95,     "         ");  
          /* Check if the LCD is HX8347D Controller */
          if(LCDType != LCD_HX8347D)
          {
            /* Display Char on the LCD : XXX% */       
            LCD_DisplayChar(95,294, (tmp / 10) +0x30);
            LCD_DisplayChar(95,278, (tmp  % 10 ) +0x30);
            LCD_DisplayChar(95,262, ':');
            LCD_DisplayChar(95,246, (tmp1 / 10) +0x30);
            LCD_DisplayChar(95,230, (tmp1  % 10 ) +0x30);
          }
          else
          {
            /* Display Char on the LCD : XXX% */       
            LCD_DisplayChar(95, 320 - 294, (tmp / 10) +0x30);
            LCD_DisplayChar(95, 320 - 278, (tmp  % 10 ) +0x30);
            LCD_DisplayChar(95, 320 - 262, ':');
            LCD_DisplayChar(95, 320 - 246, (tmp1 / 10) +0x30);
            LCD_DisplayChar(95, 320 - 230, (tmp1  % 10 ) +0x30);
          }
        }
        /* Clear the EXTI Line 11 */
        EXTI_ClearITPendingBit(EXTI_Line8);
      }
    }
    else
    {
#endif /* APPLICATION_SUBDEMO */
      if (EXTI_GetITStatus(KEY_BUTTON_EXTI_LINE) != RESET)
      {
        /* To Exit the CEC sub-menu */
        Key_Button_Pressed = 1;
        
        /* Clear the Button key EXTI Line */
        EXTI_ClearITPendingBit(KEY_BUTTON_EXTI_LINE);
      }
      
      if (EXTI_GetITStatus(UP_BUTTON_EXTI_LINE) != RESET)
      {
        while (STM_EVAL_PBGetState(BUTTON_UP) == Bit_SET)
        {
        }
        /* UP function */
				if(LCDType == LCD_HX8347D)
						Set_DOWNStatus();
				else
						Set_UPStatus();
        /* Clear the button up EXTI Line */
        EXTI_ClearITPendingBit(UP_BUTTON_EXTI_LINE);
      }
      
      if (EXTI_GetITStatus(DOWN_BUTTON_EXTI_LINE) != RESET)
      {
        while (STM_EVAL_PBGetState(BUTTON_DOWN) == Bit_SET)
        {
        }
        
        if (VoiceRecStop == RESET)
        {
          DOWNBoutonPressed = 0x01;
        }
        
        /* DOWN function */
				if(LCDType == LCD_HX8347D)
						Set_UPStatus();
				else
						Set_DOWNStatus();
        
        /* Clear the button down EXTI Line */
        EXTI_ClearITPendingBit(DOWN_BUTTON_EXTI_LINE);
      }
#ifdef APPLICATION_SUBDEMO 
    }
#endif /* APPLICATION_SUBDEMO */
  }
}

/**
  * @brief  This function handles TIM15 overflow and update interrupt request.
  * @param  None
  * @retval None
  */
void TIM15_IRQHandler(void)
{
  /* Clear the TIM15 Update pending bit */
  TIM_ClearITPendingBit(TIM15, TIM_IT_Update);
  
  if (AlarmStatus == 1)
  {
    if ((LedCounter & 0x01) == 0)
    {
      STM_EVAL_LEDOff(LED1);
      STM_EVAL_LEDOff(LED2);
      STM_EVAL_LEDOff(LED3);
      STM_EVAL_LEDOff(LED4);
    }
    else if ((LedCounter & 0x01) == 0x01)
    {
      STM_EVAL_LEDOn(LED1);
      STM_EVAL_LEDOn(LED2);
      STM_EVAL_LEDOn(LED3);
      STM_EVAL_LEDOn(LED4);
    }
    LedCounter++;
    if (LedCounter == 100)
    {
      AlarmStatus = 0;
      LedCounter = 0;
    }
  }
  else
  {
    /* If LedShowStatus is TRUE: enable leds toggling */
    if (Demo_Get_LedShowStatus() != 0)
    {
      switch (Index)
      {
        /* LD1 turned on, LD4 turned off */
      case 0:
        {
          STM_EVAL_LEDOff(LED1);
          STM_EVAL_LEDOn(LED4);
          Index++;
          break;
        }
        /* LD2 turned on, LD1 turned off */
      case 1:
        {
          STM_EVAL_LEDOff(LED2);
          STM_EVAL_LEDOn(LED1);
          Index++;
          break;
        }
        /* LD3 turned on, LD2 turned off */
      case 2:
        {
          STM_EVAL_LEDOff(LED3);
          STM_EVAL_LEDOn(LED2);
          Index++;
          break;
        }
        /* LD4 turned on, LD3 turned off */
      case 3:
        {
          STM_EVAL_LEDOff(LED4);
          STM_EVAL_LEDOn(LED3);
          Index++;
          break;
        }
      default:
        break;
      }
      /* Reset Index to replay leds switch on sequence  */
      if (Index == 4)
      {
        Index = 0;
      }
    }
  }
}

#ifdef WAVERECORD_SUBDEMO 
/**
  * @brief  This function handles TIM3 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
  uint16_t SampleNValue = 0;               /* ADC converted value */
  
  /* Clear TIM3 interrupt */
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);

  while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
  {}
  /* Get the ADC conversion value for sample N */
  SampleNValue = (uint16_t)(ADC_GetConversionValue(ADC1));

  /* Select the VoiceRec_Tab destination table */
  if (TabIndex == 0)
  {
    Tab = (uint8_t*)Buffer1;
  }
  else
  {
    Tab = (uint8_t*)Buffer2;
  }
  
  /* Store the converted sample N value */
  Tab[RecordSample++] = (uint8_t)(SampleNValue);
  
  /* If the Buffer Tab destination table is full */
  if (RecordSample == _MAX_SS)
  {
    /* Enable the write on SD Card */
    WriteOnTheSDCardStatus = ENABLE;

    /* Generate an event to start saving in the SD Card*/
    TIM_GenerateEvent(TIM14, TIM_EventSource_CC1);

    /* Change the buffer destination table */
    TabIndex ^= 1 ;
    
    /* Reset RecordSample counter */
    RecordSample = 0;
  }
}

/**
  * @brief  This function handles TIM4 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM14_IRQHandler(void)
{
  uint8_t tmp = 0x00;
  
  if (WriteOnTheSDCardStatus == ENABLE)
  {
    /* Load SD Card with the actual buffer Tab */
    f_write (&F, Tab, _MAX_SS, &BytesWritten);
    /* Increment wave size by 512*/
    WaveCounter += _MAX_SS;

    WriteOnTheSDCardStatus = DISABLE;
  }
  
  if (DOWNBoutonPressed == 0x01)
  {
    /* To exit the wave record demo */
    VoiceRecStop = SET;
    /* Reset the Up bouton variable status */
    DOWNBoutonPressed = 0x00;
  }

  tmp = (uint8_t) ((uint32_t)(WaveCounter * 100) / SelectedMaxRecordDataLength);
  LCD_SetTextColor(LCD_COLOR_MAGENTA);
  LCD_DrawLine(LCD_LINE_8, 310 - ((tmp) * 3), 16, LCD_DIR_VERTICAL);
  
  /* Reset the TIM14 Counter Register value*/
  TIM_SetCounter(TIM14, 0xFFFF);
  
  /* Clear TIM4 interrupt */
  TIM_ClearITPendingBit(TIM14, TIM_IT_CC1); 
}

#endif /* WAVERECORD_SUBDEMO */

#ifdef THERMOMETER_SUBDEMO
/**
  * @brief  This function handles I2C1 Error interrupt request.
  * @param  None
  * @retval None
  */
void I2C1_IRQHandler(void)
{
  /* Check on I2C1 SMBALERT flag and clear it */
  if (I2C_GetITStatus(LM75_I2C, I2C_IT_ALERT))
  {
    I2C_ClearITPendingBit(LM75_I2C, I2C_IT_ALERT);
    SMbusAlertOccurred++;
  }
  
  /* Check on I2C1 Time out flag and clear it */
  if (I2C_GetITStatus(LM75_I2C, I2C_IT_TIMEOUT))
  {
    I2C_ClearITPendingBit(LM75_I2C, I2C_IT_TIMEOUT);
  }
  
  /* Check on I2C1 Arbitration Lost flag and clear it */
  if (I2C_GetITStatus(LM75_I2C, I2C_IT_ARLO))
  {
    I2C_ClearITPendingBit(LM75_I2C, I2C_IT_ARLO);
  }

  /* Check on I2C1 PEC error flag and clear it */
  if (I2C_GetITStatus(LM75_I2C, I2C_IT_PECERR))
  {
    I2C_ClearITPendingBit(LM75_I2C, I2C_IT_PECERR);
  }
  
  /* Check on I2C1 Overrun/Underrun error flag and clear it */
  if (I2C_GetITStatus(LM75_I2C, I2C_IT_OVR))
  {
    I2C_ClearITPendingBit(LM75_I2C, I2C_IT_OVR);
  }
  
  /* Check on I2C1 Bus error flag and clear it */
  if (I2C_GetITStatus(LM75_I2C, I2C_IT_BERR))
  {
    I2C_ClearITPendingBit(LM75_I2C, I2C_IT_BERR);
  }
}
#endif /* THERMOMETER_SUBDEMO */

#ifdef HDMICEC_SUBDEMO
/**
  * @brief  This function handles CEC interrupt request.
  * @param  None
  * @retval None
  */

void CEC_IRQHandler(void)
{
  /* Process the CEC Interrupts */
  HDMI_CEC_ProcessIRQSrc();
}
#endif /* HDMICEC_SUBDEMO */

#ifdef INFRALED_SUBDEMO
/**
  * @brief  This function handles TIM17 interrupt request.
  * @param  None
  * @retval None
  */
void TIM16_IRQHandler(void)
{
  if(RFDemoStatus == RC5DEMO)
  {
    RC5_Encode_SignalGenerate(RC5_FrameManchestarFormat);
  }
  
  if(RFDemoStatus == SIRCDEMO)
  {
    SIRC12_Encode_SignalGenerate(SIRC12_FramePulseWidthFormat);
  }
  
  /* Clear TIM16 update interrupt */
  TIM_ClearITPendingBit(TIM16, TIM_IT_Update);
}
#endif /* INFRALED_SUBDEMO */

#if defined (INFRALED_SUBDEMO) || defined (HDMICEC_SUBDEMO)
/**
  * @brief  This function handles TIM14 overflow and update interrupt request.
  * @param  None
  * @retval None
  */
void TIM2_IRQHandler(void)
{
  /* Clear the TIM2 Update pending bit */
  TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  
  if(RFDemoStatus == RC5DEMO)
  {
    /* - Timer Falling Edge Event:
    The Timer interrupt is used to measure the period between two 
    successive falling edges (The whole pulse duration).
    
    - Timer Rising Edge Event:  
    It is also used to measure the duration between falling and rising 
    edges (The low pulse duration).
    The two durations are useful to determine the bit value. Each bit is 
    determined according to the last bit. 
    
    Update event:InfraRed decoders time out event.
    ---------------------------------------------
    It resets the InfraRed decoders packet.
    - The Timer Overflow is set to 3.6 ms .*/
    /* IC1 Interrupt*/
    if((TIM_GetFlagStatus(IR_TIM, TIM_FLAG_CC2) != RESET))
    {
      TIM_ClearFlag(IR_TIM, TIM_FLAG_CC2);
      /* Get the Input Capture value */
      ICValue2 = TIM_GetCapture2(IR_TIM);
      /* RC5 */
      RC5_DataSampling( ICValue2 - ICValue1 , 0);
      
    }  /* IC2 Interrupt */   
    else  if((TIM_GetFlagStatus(IR_TIM, TIM_FLAG_CC1) != RESET))
    {
      TIM_ClearFlag(IR_TIM, TIM_FLAG_CC1);
      /* Get the Input Capture value */
      ICValue1 = TIM_GetCapture1(IR_TIM);
      RC5_DataSampling(ICValue1 , 1);
    } 
    /* Checks whether the IR_TIM flag is set or not.*/
    else if ((TIM_GetFlagStatus(IR_TIM, TIM_FLAG_Update) != RESET))
    { 
      /* Clears the IR_TIM's pending flags*/
      TIM_ClearFlag(IR_TIM, TIM_FLAG_Update);
      
      RC5_ResetPacket(); 
    }
  }
  if (RFDemoStatus == SIRCDEMO)
  {
    /*The Timer interrupt is used to measure the different period between 
    two successive falling edges in order to identify the frame bits.
    
    We measure the low pulse duration:
    - If the period measured is equal to T = 1200 µs and the low pulse 
    duration is equal to T/2 = 600µs => the bit is logic '0'. 
    - If the period measured is equal to 3T/2 = 1800µs and the low pulse 
    duration is equal to T = 1200µs => the bit is logic '1'.
    - If the whole period measured is equal to 3000µs and the low pulse 
    duration is equal to 2400µs => the bit is ‘start bit’.
    
    Update event:InfraRed decoders time out event
    ----------------------------------------------
    It resets the InfraRed decoders packet.                 
    - The Timer Overflow is set to 4 ms.  */
    
    
    /* IC1 Interrupt */
    if((TIM_GetFlagStatus(IR_TIM, TIM_FLAG_CC2) != RESET))
    {
      TIM_ClearFlag(IR_TIM, TIM_FLAG_CC2);
      /* Get the Input Capture value */
      ICValue2 = TIM_GetCapture2(IR_TIM);
      SIRC_DataSampling(ICValue1, ICValue2); 
    }  /* IC2 Interrupt*/   
    else  if((TIM_GetFlagStatus(IR_TIM, TIM_FLAG_CC1) != RESET))
    {
      TIM_ClearFlag(IR_TIM, TIM_FLAG_CC1);
      /* Get the Input Capture value */
      ICValue1 = TIM_GetCapture1(IR_TIM);
    } 
    /* Checks whether the IR_TIM flag is set or not. */
    else if ((TIM_GetFlagStatus(IR_TIM, TIM_FLAG_Update) != RESET))
    { 
      /* Clears the IR_TIM's pending flags*/
      TIM_ClearFlag(IR_TIM, TIM_FLAG_Update);
      
      SIRC_ResetPacket();
    }
  }
  else
  {}
}
#endif /* INFRALED_SUBDEMO, HDMICEC_SUBDEMO */


/**
  * @}
  */ 

/**
  * @}
  */ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
