Sample application for vs1053b library (rev. lkw4hi)

Dependencies:   mbed

main.cpp

Committer:
christi_s
Date:
2010-12-16
Revision:
0:e1919041e50b
Child:
1:62552e038f69

File content as of revision 0:e1919041e50b:

/* mbed MP3 Shield Player - Testapplication for VLSI VS1053b Lib
 * Copyright (c) 2010 Christian Schmiljun
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


#include "mbed.h"
#include "VS1053.h"

// Volume [ 0x0000 (loud) .. 0xFEFE (quiet) ]
#define VOLUME -42.0f
// Switch for local filesystem or SDCard
#define USE_SDCARD

#define DEBUG
// ----------------------------------------------------------------------------
//
// Pin Assigenment for SDCard on Cool Components Workshop Board
//
// SDCard             |  mbed Side
// ---------------------------------------
//  mosi-----------------5
//  miso-----------------6
//  sclk-----------------7
//  cs-------------------8
//
// ----------------------------------------------------------------------------
#ifdef USE_SDCARD
#include "SDFileSystem.h"

SDFileSystem sd(p5, p6, p7, p8, "sd");
#define DIR_NAME "/sd/Musik"
#else
LocalFileSystem sd("local");
#define DIR_NAME "/local"
#endif




// ----------------------------------------------------------------------------
//
// Pin Assigenment for Arduino MP3 Shield
// (VS1053 employed, issued by sparkfun.com)
//
// MP3 Sheild Side   |  mbed Side
// ---------------------------------------
//  RX-------------------10 (optional)
//  TX-------------------09 (optional)
//  D2(BSYNC)------------17
//  D3(DREQ)-------------16
//
//  D9(CS)---------------14
//
//  D11(MOSI)------------11
//  D12(MISO)------------12
//  D13(SCK)-------------13
//
//  GND------------------GND(1)
//  5V-------------------VU(39)
//  RESET----------------15
//
// ----------------------------------------------------------------------------
VS1053 mp3( p11, p12, p13, p14, p15, p16, p17);

// Serial for Debug
Serial pc(USBTX, USBRX);

void setVolume(void) {
    if (pc.readable())
    {
        unsigned char c = pc.getc();
// scanf ("%x",&i);
        if (c =='+')
            mp3.setVolume(mp3.getVolume() + 0.5);
        else if (c == '-')
        {        
            mp3.setVolume(mp3.getVolume() - 0.5);
        }
        else if (c =='a')
        {            
            mp3.setBalance(mp3.getBalance() + 0.5);
        }
        else if (c == 'y')
        {
            mp3.setBalance(mp3.getBalance() - 0.5);
        } 
        else if (c == '1')
        {
            mp3.setPlaySpeed(1);       
        }
        else if (c == '2')
        {
            mp3.setPlaySpeed(2);       
        }
        else if (c == '3')
        {
            mp3.setPlaySpeed(3);       
        }
        else if (c == '4')
        {
            mp3.setPlaySpeed(4);       
        }             
    }
}

int main () {
    // ------------------------------------------------------------------------
    //  MP3 Initialising
    // ------------------------------------------------------------------------
    printf("Initialize mp3 Codec...\r\n");
    mp3.initialize();
    printf("mp3 Codec is initialized\r\n");


    mp3.setVolume(VOLUME);
    pc.attach(setVolume);                    


    // ------------------------------------------------------------------------
    //  Play mp3 file
    // ------------------------------------------------------------------------    

    //while (1)
    {
        DIR *d;
        struct dirent *p;
        d = opendir(DIR_NAME);
        if(d != NULL) 
        {
            while ((p = readdir(d)) != NULL) {                          
                char str[160] = DIR_NAME;
                
                //check extension
                char * extension =strrchr( p->d_name,'.') + 1;
                bool isAudioFile = false; 
                if (strcmp(extension, "mp3") == 0)
                    isAudioFile = true;
                else if (strcmp(extension, "MP3") == 0)
                    isAudioFile = true;
                else if (strcmp(extension, "mp4") == 0)
                    isAudioFile = true;
                else if (strcmp(extension, "MP4") == 0)
                    isAudioFile = true;
                if (isAudioFile ) 
                {                                                                                         
                    printf("Now Playing: %s\r\n", p->d_name);
                    
                    sprintf(str, "%s/%s", DIR_NAME, p->d_name);
                    printf("Path %s\r\n", str);
                    
                    FILE *song;
            
                    unsigned char array[4*1024];
                                   
                    song = fopen(str, "rb");
                    //song = fopen("/sd/Musik/01.mp3", "rb");
                
                    if (!song) {
                        printf("Couldn't open %s\r\n", str);
                        continue;
                        //exit(1);
                    }
                
                    while (!feof(song)) {
                        int n=fread(&array, 1, sizeof(array), song);
                        mp3.writeStream(array,n);
                    }
                
                    mp3.terminateStream();
                    fclose(song);  //close the file
                    
                    printf("End of song.");
                    wait(1.0);
                }
                else
                {
                     printf("File ignored. Extension: %s\r\n", extension);                
                }

            }
            closedir(d);
        } 
        else 
        {
            error("Could not open directory!");
    //        break;
        }
                
     }
    printf("Done.\r\n");

}