/**
  ******************************************************************************
  * @file main.c
  * @brief This file contains the main function.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.1.0
  * @date    09/14/2009
  ******************************************************************************
  *
  */

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

  // * @addtogroup DEMO_Functions

// Private typedef -----------------------------------------------------------
// Private define ------------------------------------------------------------
// Evalboard I/Os configuration 
// Private macro -------------------------------------------------------------
// Private variables ---------------------------------------------------------
// Private function prototypes -----------------------------------------------
// Private functions ---------------------------------------------------------

uint8_t State_MelodyCallBack;

// Public functions ----------------------------------------------------------

/**
  * @brief Validation firmware main entry point.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Library functions called:
  */
void main(void)
{
    JOYSTICK_ID_T Key;
    const Melody_TypeDef *pMelody =0;
    uint8_t i;
    uint8_t end_found;

    Init_Application();

    // Display the welcome text on first LCD line 
    LCD_PrintMsg("STM8L10x DEMO");

    // Play melody and display "moving" ST logo in callback function 

    EEPBuffer[0] = 0x00;

    // Check if the first byte correspond to the PWM DEMO melody stored 
    if (EEPBuffer[0] == PWM_DEMO_MELODY)
    {
        // Check if end of melody symbol is present. If not, add it 
        end_found = 0;
        for (i = 1; i < 16; i++)
        {
            if ((EEPBuffer[i] == MELODY_END_CHAR) || (EEPBuffer[i] == MELODY_LOOP_CHAR))
            {
                end_found = 1;
                break;
            }
        }
        if (end_found == 0)
        {
            EEPBuffer[15] = MELODY_END_CHAR;
        }
        // Play melody 
        State_MelodyCallBack = 0;
        PwmPlayMelody(EEPBuffer, 0);
        State_MelodyCallBack = 1;
    }
    else /* This is a pre-defined melody */
    {
        pMelody = MELODIES;
        State_MelodyCallBack = 0;
        PlayMelody(pMelody->Notes);
        State_MelodyCallBack = 1;
    }

    LCD_ClearLine(LCD_LINE2);
    LCD_DisplayLogo(0x93);
    WaitDelay(150);

    // Display the menu 
    MenuDisplay();

    // Executive loop, runs continually after initialization 
    while (1)
    {
        // Menu update managemnt 
        Key = ReadJoystick();

        if ((Key != J_NONE) && (Key != J_RELEASED))
        {
            MenuUpdate(Key);
        }
    }

}
#ifdef ENABLE_MELODY_CALLBACK
void MelodyCallBack(void)
{

    static uint8_t logo_pos = 0x90;
    static int8_t logo_pos_index = 1;

    if (State_MelodyCallBack == 0)
    {
        LCD_ClearLine(LCD_LINE2);
        LCD_DisplayLogo(logo_pos);
        logo_pos += logo_pos_index;
        if (logo_pos > 0x95)
        {
            logo_pos -= 2;
            logo_pos_index = -1;
        }
        if (logo_pos < 0x90)
        {
            logo_pos += 2;
            logo_pos_index = 1;
        }
    }
}
#endif


#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  // Infinite loop 
  while (1)
  {
  }
}
#endif



