text: /**
    ******************************************************************************
    * @file    Audio/Audio_playback_and_record/Src/explorer.c 
    * @author  MCD Application Team
    * @brief   This file provides SD drive configuration
    ******************************************************************************
    * @attention
    *
    * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
    * All rights reserved.</center></h2>
    *
    * Redistribution and use in source and binary forms, with or without 
    * modification, are permitted, provided that the following conditions are met:
    *
    * 1. Redistribution of source code must retain the above copyright notice, 
    *    this list of conditions and the following disclaimer.
    * 2. Redistributions in binary form must reproduce the above copyright notice,
    *    this list of conditions and the following disclaimer in the documentation
    *    and/or other materials provided with the distribution.
    * 3. Neither the name of STMicroelectronics nor the names of other 
    *    contributors to this software may be used to endorse or promote products 
    *    derived from this software without specific written permission.
    * 4. This software, including modifications and/or derivative works of this 
    *    software, must execute solely and exclusively on microcontroller or
    *    microprocessor devices manufactured by or for STMicroelectronics.
    * 5. Redistribution and use of this software other than as permitted under 
    *    this license is void and will automatically terminate your rights under 
    *    this license. 
    *
    * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
    * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
    * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
    * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
    * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
    * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    *
    ******************************************************************************
    */
  
  /* Includes ------------------------------------------------------------------*/
  #include "main.h"
  
  /* Private typedef -----------------------------------------------------------*/
  extern FATFS SDFatFs; /* File system object for SD card logical drive */
  extern char SDPath[4]; /* SD card logical drive path */
  
  FILELIST_FileTypeDef FileList;
  uint16_t NumObs = 0;
  
  /* Private define ------------------------------------------------------------*/
  /* Private macro -------------------------------------------------------------*/
  /* Private variables ---------------------------------------------------------*/
  /* Private function prototypes -----------------------------------------------*/
  /* Private functions ---------------------------------------------------------*/
  
  /**
    * @brief  Initializes the SD Storage.
    * @param  None
    * @retval Status
    */
  uint8_t AUDIO_StorageInit(void)
  {
    /* Link the SD disk I/O driver */
    if((f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK))
    {
      /* FatFs Initialization Error */
      LCD_ErrLog("Cannot Initialize FatFs! \n");
      return 1;
    }
    else
    {
      LCD_DbgLog ("INFO : FatFs Initialized! \n");
      return 0;
    }
  }
  
  /**
    * @brief  Copies disk content in the explorer list.
    * @param  None
    * @retval Operation result
    */
  FRESULT AUDIO_StorageParse(void)
  {
    FRESULT res = FR_OK;
    FILINFO fno;
    DIR dir;
    char *fn;
    
    res = f_opendir(&dir, SDPath);
    FileList.ptr = 0;
    
    if(res == FR_OK)
    {
      while(SD_status(0x00) != STA_NOINIT)
      {
        res = f_readdir(&dir, &fno);
        if(res != FR_OK || fno.fname[0] == 0)
        {
          break;
        }
        if(fno.fname[0] == '.')
        {
          continue;
        }
  
        fn = fno.fname;
        
        if(FileList.ptr < FILEMGR_LIST_DEPDTH)
        {
          if((fno.fattrib & AM_DIR) == 0)
          {
            if((strstr(fn, "wav")) || (strstr(fn, "WAV")))
            {
              strncpy((char *)FileList.file[FileList.ptr].name, (char *)fn, FILEMGR_FILE_NAME_SIZE);
              FileList.file[FileList.ptr].type = FILETYPE_FILE;
              FileList.ptr++;
            }
          }
        }   
      }
    }
    NumObs = FileList.ptr;
    f_closedir(&dir);
    return res;
  }
  
  
  /**
    * @brief  Shows audio file (*.wav) on the root
    * @param  None
    * @retval None
    */
  uint8_t AUDIO_ShowWavFiles(void)
  {
    uint8_t i = 0;
    uint8_t line_idx = 0;
    if(AUDIO_StorageInit() == FR_OK)
    {
      if(AUDIO_StorageParse() ==  FR_OK)
      {
        if(FileList.ptr > 0)
        {
          BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
          LCD_UsrLog("audio file(s) [ROOT]:\n\n");
          
          for(i = 0; i < FileList.ptr; i++)
          {
            line_idx++;
            LCD_DbgLog("   |__");
            LCD_DbgLog((char *)FileList.file[i].name);
            LCD_DbgLog("\n");
          }
          BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
          LCD_UsrLog("\nEnd of files list.\n");
          return 0;
        }
        return 1;
      }
      return 2;
    }
    else
    {
      return 3;
    }
  }
  
  /**
    * @brief  Gets Wav Object Number.
    * @param  None
    * @retval None
    */
  uint16_t AUDIO_GetWavObjectNumber(void)
  {
    return NumObs;
  }
  
  /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  