www.pudn.com > Audio.rar > waveplayer.c, change:2008-12-19,size:16533b


/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 
* File Name          : waveplayer.c 
* Author             : MCD Application Team 
* Version            : V1.1.1 
* Date               : 06/13/2008 
* Description        : Wave Player driver source file. 
******************************************************************************** 
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 
*******************************************************************************/ 
 
/* Includes ------------------------------------------------------------------*/ 
#include "waveplayer.h" 
#include "lcd.h" 
#include <stdio.h> 
 
/* Private typedef -----------------------------------------------------------*/ 
/* Private define ------------------------------------------------------------*/ 
#define REPLAY  3 
 
/* Private macro -------------------------------------------------------------*/ 
/* Private variables ---------------------------------------------------------*/ 
u8 DemoTitle1[20] = "   EM_STM32E Demo   ";  
u8 DemoTitle2[20] = "     AUDIO_Test     "; 
u8 CmdTitle0[20] = "  Control Buttons:  ";  
u8 CmdTitle1[20] = "    Progress bar    ";  
 
u8 CmdTitle1Playing[20] = "USER>Pause  UP >Vol+"; 
 
u8 CmdTitle2Playing[20] = "SEL>Stop   DOWN>Vol-"; 
 
u8 CmdTitle3Playing[20] = "LEFT>FRWD RIGHT>BACK"; 
 
u8 CmdTitle1Paused[20] =  "USER>Play  UP  >Spkr"; 
 
u8 CmdTitle2Paused[20] =  "SEL>Stop   DOWN>Spkr"; 
 
u8 CmdTitle1Stopped[20] = "    UP > Speaker    ";  
 
u8 CmdTitle2Stopped[20] = "  line 6 is status  "; 
   
u8 StatusTitleStopped[20] = "      Stopped       "; 
 
u8 StatusTitlePlaying[20] = "      Playing       "; 
 
u8 StatusTitlePaused[20] = "       Paused       "; 
 
u8 i2cerr[20] =  "ERROR:I2C com. ->RST"; 
u8 memerr[20] =  "ERROR: Memory  ->RST"; 
u8 fileerr[20] = "ERROR: No Wave File "; 
 
static u8 previoustmp = 50; 
static u8 player = 0; 
/* Private function prototypes -----------------------------------------------*/ 
/* Private functions ---------------------------------------------------------*/ 
 
/******************************************************************************* 
* Function Name  : ReadKey 
* Description    : Reads key from demoboard. 
* Input          : None 
* Output         : None 
* Return         : Return RIGHT, LEFT, SEL, UP, DOWN or NOKEY 
*******************************************************************************/ 
u8 ReadKey(void) 
{ 
  /* "right" key is pressed */ 
  if(!GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_13)) 
  { 
    while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_13) == Bit_RESET) 
    { 
    }  
    return RIGHT;  
  }	 
  /* "left" key is pressed */ 
  if(!GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_14)) 
  { 
    while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_14) == Bit_RESET) 
    { 
    } 
    return LEFT;  
  } 
  /* "up" key is pressed */ 
  if(!GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_15)) 
  { 
    while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_15) == Bit_RESET) 
    { 
    } 
    return UP;  
  } 
  /* "down" key is pressed */ 
  if(!GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3)) 
  { 
    while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_3) == Bit_RESET) 
    { 
    }  
    return DOWN;  
  } 
  /* "sel" key is pressed */ 
  if(!GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_7)) 
  { 
    while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_7) == Bit_RESET) 
    { 
    }  
    return SEL;  
  } 
  /* "KEY" key is pressed */ 
  if(!GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_8)) 
  { 
    while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_8) == Bit_RESET) 
    { 
    }  
    return KEY;  
  } 
  /* No key is pressed */ 
  else  
  { 
    return NOKEY; 
  } 
} 
 
 
/******************************************************************************* 
* Function Name  : WavePlayer_StartSpeaker 
* Description    : Starts the wave player application. 
* Input          : None 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
void WavePlayer_StartSpeaker(void) 
{ 
  u8 MyKey = 0; 
  u32 err = 0, Counter = 0x0; 
 
  LCD_Clear(White); 
 
  while(ReadKey() != NOKEY) 
  { 
  }   
 
  /* Display the welcome screen and the commands */ 
  LCD_Update(ALL); 
 
  DAC_CODEC_Init(AUDIO_FILE_ADDRESS); 
 
 
  /* Endless loop */ 
  while(1) 
  { 
    /* Check which key is pressed */ 
    MyKey = ReadKey(); 
    
    player = 0; /* initial player */     
 
    if(Counter == 0) 
    {  
      /* Update the displayed progression information */ 
      LCD_Update(PROGRESS); 
      Counter = 0x5FFFF; 
    } 
    Counter--; 
    /* If "UP" pushbutton is pressed */ 
    if(MyKey == UP) 
    { 
      /* Check if the Codec is PLAYING audio file */ 
      if (GetVar_AudioPlayStatus() == AudioPlayStatus_PLAYING) 
      { 
         DAC_CODEC_ControlVolume(VolumeDirection_HIGH, VOLStep); 
         /* Update the display information */ 
		 player = 0; /*NOW PLAYING*/ 
         LCD_Update(VOL); 
      } 
      /* UP bottomn pushed in PAUSE mode => Enable the Speaker device output ---*/ 
      else 
      { 
	    player = 1;	 /*now STOPPED*/ 
        /* Update the display information */ 
        LCD_Update(PLAY);  
        /* Error message display if failure */ 
        if (err != 0) 
        { 
          LCD_DisplayError(err); 
 
          /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */ 
          RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 
 
          /* Clear the LCD */ 
          LCD_Clear(White); 
 
          return;  
        }       
      }  
    } 
 
    /* If "DOWN" pushbutton is pressed */ 
    if(MyKey == DOWN) 
    { 
      /* If the Codec is PLAYING => Decrease Volume*/ 
      if (GetVar_AudioPlayStatus() == AudioPlayStatus_PLAYING) 
      { 
        DAC_CODEC_ControlVolume(VolumeDirection_LOW, VOLStep); 
		player = 0; /*NOW PLAYING*/ 
        /* Update the LCD display */  
        LCD_Update(VOL);  
      } 
      else /* If the Codec is PAUSED => UPDATE PLAY MENU */ 
      { 
        /* Update the LCD display */  
		player = 1; /*NOW STOPPED*/ 
        LCD_Update(PLAY); 
        /* Error message display if failure */ 
        if (err != 0) 
        { 
          LCD_DisplayError(err); 
 
          /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */ 
          RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 
 
          /* Clear the LCD */ 
          LCD_Clear(White); 
 
          return;  
        }   
      } 
    } 
 
    /* If "RIGHT" pushbutton is pressed */ 
    if(MyKey == RIGHT) 
    { 
      /* Check if the Codec is PLAYING audio file */ 
      if (GetVar_AudioPlayStatus() == AudioPlayStatus_PLAYING) 
      { 
        DAC_CODEC_ForwardPlay(STEP_FORWARD);  
        /* Update the display information */ 
        LCD_Update(FRWD);  
      } 
    } 
    /* If "LEFT" pushbutton is pressed */ 
    if(MyKey == LEFT) 
    { 
      /* Check if the Codec is PLAYING audio file */ 
      if (GetVar_AudioPlayStatus() == AudioPlayStatus_PLAYING) 
      { 
        DAC_CODEC_RewindPlay(STEP_BACK); 
        /* Update the display information */ 
        LCD_Update(FRWD);   
      }  
    } 
 
    /* If "SEL" pushbutton is pressed */ 
    if(MyKey == SEL) 
    { 
      /* Update the display information */ 
      LCD_Update(STOP); 
 
      /* Command the Stop of the current audio stream */ 
      SetVar_AudioPlayStatus(AudioPlayStatus_STOPPED); 
 
      DAC_CODEC_Stop(); 
 
      /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */ 
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 
 
      /* Clear the LCD */ 
      LCD_Clear(White); 
 
      return;      
    } 
    /* If "KEY" pushbutton is pressed */ 
    if(MyKey == KEY) 
    { 
       /* If the Codec is Playing => PAUSE */ 
      if (GetVar_AudioPlayStatus() == AudioPlayStatus_PLAYING) 
      { 
        /* Update the display information */ 
        LCD_Update(PAUSE); 
 
        /* Command the Pause of the current stream */ 
        SetVar_AudioPlayStatus(AudioPlayStatus_PAUSED); 
      } 
 
      /* If the Codec is PAUSED => Resume PLAYING */ 
      else if (GetVar_AudioPlayStatus() == AudioPlayStatus_PAUSED) 
      { 
        /* Update the LCD display */  
        LCD_Update(PLAY);  
 
        DAC_CODEC_Play(GetVar_AudioDataIndex()); 
      } 
      /* If the Codec is STOPPED => PLAY from the file start address */ 
      else if (GetVar_AudioPlayStatus() == AudioPlayStatus_STOPPED) 
      { 
        /* Update the display information */ 
        LCD_Update(PLAY); 
 
        err = DAC_CODEC_Init(AUDIO_FILE_ADDRESS); 
   
        /* Error message display if failure */ 
        if (err != 0) 
        { 
          LCD_DisplayError(err); 
 
          /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */ 
          RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 
 
          /* Clear the LCD */ 
          LCD_Clear(White); 
 
          return;  
        }   
           
        DAC_CODEC_Play(GetVar_DataStartAddr()); 
      } 
    } 
  } 
} 
 
/******************************************************************************* 
* Function Name  : LCD_Update 
* Description    : Controls the wave player application LCD display messages. 
* Input          : None 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
void LCD_Update(u32 Status) 
{ 
  u8 tmp = 0; 
  u32 counter = 0; 
 
  /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */ 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 
 
  switch (Status) 
  { 
   case PROGRESS: 
         tmp = (u8) ((u32)((GetVar_AudioDataIndex()) * 100) / GetVar_AudioDataLength()); 
         if (tmp == 0) 
         {  
           LCD_SetTextColor(Grey); 
           LCD_ClearLine(Line8); 
           LCD_DrawRect(Line8, 310, 16, 300); 
         } 
         else 
         { 
           LCD_SetTextColor(Grey); 
           LCD_DrawLine(Line8, 310 - (tmp * 3), 16, Vertical); 
         }          
         break; 
   case FRWD: 
         tmp = (u8) ((u32)((GetVar_AudioDataIndex()) * 100) / GetVar_AudioDataLength()); 
 
         LCD_SetTextColor(Grey); 
         LCD_ClearLine(Line8); 
         LCD_DrawRect(Line8, 310, 16, 300); 
         LCD_SetTextColor(Grey); 
 
         for (counter = 0; counter <= tmp; counter++) 
         { 
           LCD_DrawLine(Line8, 310 - (counter * 3), 16, Vertical); 
         }           
         break; 
   case STOP: 
         /* Display the stopped status menu */  
         LCD_SetTextColor(White);  
         LCD_DisplayStringLine(Line3, CmdTitle1Stopped); 
		 LCD_DisplayStringLine(Line4, CmdTitle2Stopped); 
		 LCD_ClearLine(Line5); 
         LCD_SetTextColor(Red); 
         LCD_DisplayStringLine(Line6, StatusTitleStopped); 
         LCD_ClearLine(Line9); 
         LCD_SetTextColor(White); 
         LCD_DisplayChar(Line9, 250, 'v');  
         LCD_DisplayChar(Line9, 235, 'o');  
         LCD_DisplayChar(Line9, 220, 'l');  
         LCD_DisplayChar(Line9, 200, '-');  
         LCD_DisplayChar(Line9, 85, '+');  
         LCD_DrawRect(Line9 + 8, 185, 10, 100);  
         break;  
   case PAUSE:  
         /* Display the paused status menu */  
         LCD_SetTextColor(White); 
         LCD_DisplayStringLine(Line3, CmdTitle1Paused); 
         LCD_DisplayStringLine(Line4, CmdTitle2Paused); 
		 LCD_ClearLine(Line5); 
         LCD_SetTextColor(Red); 
         LCD_DisplayStringLine(Line6, StatusTitlePaused); 
         break; 
   case PLAY: 
         /* Display the Titles */    
         LCD_SetTextColor(White); 
         LCD_DisplayStringLine(Line0, DemoTitle1); 
		 LCD_DisplayStringLine(Line1, DemoTitle2); 
         LCD_DisplayStringLine(Line2, CmdTitle0);  
 
         /* Display the Playing status menu */  
         LCD_SetTextColor(White); 
         LCD_DisplayStringLine(Line3, CmdTitle1Playing); 
         LCD_DisplayStringLine(Line4, CmdTitle2Playing); 
		 LCD_DisplayStringLine(Line5, CmdTitle3Playing); 
         LCD_SetTextColor(Red); 
		 if( player == 0) 
           LCD_DisplayStringLine(Line6, StatusTitlePlaying); 
		 else 
		   LCD_DisplayStringLine(Line6, StatusTitleStopped); 
         LCD_ClearLine(Line9); 
         LCD_SetTextColor(White); 
         LCD_DisplayChar(Line9, 250, 'v');  
         LCD_DisplayChar(Line9, 235, 'o');  
         LCD_DisplayChar(Line9, 220, 'l');  
         LCD_DisplayChar(Line9, 200, '-');  
         LCD_DisplayChar(Line9, 85, '+');  
         LCD_DrawRect(Line9 + 8, 185, 10, 100);  
         break; 
   case ALL:  
         I2S_CODEC_LCDConfig(); 
         /* Display the stopped status menu */  
         LCD_SetTextColor(White);  
         LCD_DisplayStringLine(Line3, CmdTitle1Stopped); 
		 LCD_DisplayStringLine(Line4, CmdTitle2Stopped); 
		 LCD_ClearLine(Line5); 
         LCD_SetTextColor(Red); 
         LCD_DisplayStringLine(Line6, StatusTitleStopped); 
         LCD_ClearLine(Line9); 
         LCD_SetTextColor(White); 
         LCD_DisplayChar(Line9, 250, 'v');  
         LCD_DisplayChar(Line9, 235, 'o');  
         LCD_DisplayChar(Line9, 220, 'l');  
         LCD_DisplayChar(Line9, 200, '-');  
         LCD_DisplayChar(Line9, 85, '+');  
         LCD_DrawRect(Line9 + 8, 185, 10, 100);  
         break; 
  } 
  /* Update the volume bar in all cases except when progress bar is to be apdated */ 
  if (Status != PROGRESS) 
  { 
    /* Compute the current volume percentage */ 
    tmp = (u8) ((u16)((0xFF - GetVar_CurrentVolume()) * 100) / 0xFF) ; 
  
    /* Clear the previuos volume bar */ 
    LCD_SetTextColor(Blue); 
    LCD_DrawLine(Line9 + 10, 185 - previoustmp , 8, Vertical); 
    LCD_DrawLine(Line9 + 10, 185 - previoustmp + 1 , 8, Vertical);     
  
    /* Draw the new volume bar */ 
    LCD_SetTextColor(Red); 
    LCD_DrawLine(Line9 + 10, 185 - tmp , 8, Vertical); 
    LCD_DrawLine(Line9 + 10, 185 - tmp + 1 , 8, Vertical); 
  
    /* save the current position */ 
    previoustmp = tmp; 
  } 
  /* Disable the FSMC that share a pin w/ I2C1 (LBAR) */ 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE); 
} 
 
/******************************************************************************* 
* Function Name  : LCD_DisplayError 
* Description    : Displays error message on the LCD screen and prompt user to  
*                  reset the application. 
* Input          : err: Error code. 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
void LCD_DisplayError(u32 err) 
{ 
  I2S_CODEC_LCDConfig(); 
 
  /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */ 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 
 
  LCD_SetTextColor(Red);  
 
  /* Clear the LCD */ 
  LCD_Clear(White); 
 
 
  /* The memory initialazation failed */ 
  if (err == 1) 
  { 
    LCD_DisplayStringLine(Line7, memerr);  
  } 
 
  /* The audio file initialization failed (wrong audio format or wrong file) */ 
  if (err == 2) 
  { 
    LCD_DisplayStringLine(Line7, fileerr);  
  } 
 
  /* I2C communication failure occured */ 
  if (err == 3) 
  { 
    LCD_DisplayStringLine(Line7, i2cerr);  
  } 
 
  LCD_DisplayStringLine(Line8, "Push Reset to       "); 
  LCD_DisplayStringLine(Line9, "exit.               "); 
 
  /* Disable the FSMC that share a pin w/ I2C1 (LBAR) */ 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE); 
 
  while(ReadKey() == NOKEY) 
  { 
  } 
} 
 
/******************************************************************************* 
* Function Name  : I2S_CODEC_LCDConfig 
* Description    : Initialize the LCD device and display the welcome screen. 
* Input          : None 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
void I2S_CODEC_LCDConfig(void) 
{ 
  /* Set the text and the background color */ 
  LCD_SetBackColor(Blue); 
  LCD_SetTextColor(White); 
  LCD_Clear(White); 
 
  /* Display the Titles */   
  LCD_DisplayStringLine(Line0, DemoTitle1); 
  LCD_DisplayStringLine(Line1, DemoTitle2); 
  LCD_DisplayStringLine(Line2, CmdTitle0);  
  LCD_DisplayStringLine(Line7, CmdTitle1); 
} 
 
 
 
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/