+mp3

Dependencies:   mbed

Fork of VS1053 by SGMP Coperations

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers player.cpp Source File

player.cpp

00001 #include "player.h"
00002 #include"SDFileSystem.h"
00003 
00004 SDFileSystem sd(D11, D12, D13, D9, "sd"); // the pinout on the mbed Cool
00005 vs10xx vs1053(D11, D12, D13, D6, D7, D2, D8);//mosi,miso,sclk,xcs,xdcs,dreq,xreset
00006 Serial aa(USBTX, USBRX);
00007 Serial pc(D1, D0);
00008 Serial lcd(PA_11,PA_12);
00009 Serial bt(PA_15,PB_7);
00010 playerStatetype  playerState;
00011 static unsigned char fileBuf[48000];
00012 unsigned char *bufptr;
00013 char green;
00014 bool startplaysong;
00015 bool playsong;
00016 char datainput;
00017 
00018 char list[20][50];            //song list
00019 
00020 char data ;
00021 
00022 char button,press,test_pop;
00023 uint8_t test,test_get;
00024 int state,size,count_ok,mode_name,mode_button,count_press,page,game_mode,set_mode_loop;
00025 char alphabet [26]  = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
00026 char user_name[10];
00027 
00028 
00029 void Player::begin(void)
00030 {
00031     DirHandle *dir;
00032     struct dirent *ptr;
00033         FileHandle *fp;
00034    
00035     vs1053.reset();
00036     dir = opendir("/sd");
00037     //printf("\r\n**********playing list**********\r\n");
00038     unsigned char i = 0,j=0;
00039     while(((ptr = dir->readdir()) != NULL)&&(i <20))
00040     {
00041         if(strstr(ptr->d_name,".mp3")||strstr(ptr->d_name,".MP3"))
00042         {
00043                             fp =sd.open(ptr->d_name, O_RDONLY);
00044                             if(fp != NULL) 
00045                             {
00046                                 char *byte = ptr->d_name;
00047                                 j=0;
00048                                 while(*byte){
00049                                     list[i][j++]  = *byte++;
00050                                     green=i;
00051                 }
00052                 aa.printf("%2d . %s\r\n", i,list[i++]);
00053                 //fp->close();
00054                             }
00055         }
00056     }
00057         dir->closedir();
00058 }
00059 
00060 
00061 
00062 
00063 
00064 /*  select file by num.  */
00065 void Player::selectfile(char n){
00066 
00067     startplaysong=false;
00068     playerState =PS_STOP;
00069     playsong=false;
00070     green = n;
00071     }
00072     
00073     
00074     
00075 /*  This function stop an audio file.  */
00076 void Player::stop(void){
00077     startplaysong=true;
00078     playerState =PS_STOP;
00079     }
00080     
00081 //--------------------------------------------------------------------//
00082     
00083 void Player::SGMP_First_Page(void){
00084     pc.printf("SGMP\nPress Enter to start\n");
00085     lcd.printf("p001f");
00086     selectfile(3);
00087     playFile(list[green]);
00088     while(1){
00089         
00090         if(bt.readable()) {
00091             button = bt.getc();
00092             if (button == 'E'){
00093                 button = NULL;
00094                 break;
00095             }
00096         }
00097     }
00098 }
00099 
00100 void Player::set_name(void) {
00101     lcd.puts("p002f");
00102     wait_ms(500);
00103     lcd.printf("%c",char(alphabet[state]));
00104     state = 0;
00105     size = 0;
00106     count_ok = 0;
00107     mode_name = 1;
00108     
00109     while(mode_name){
00110         if(bt.readable()) {
00111             button = bt.getc();
00112 //            pc.printf("button = %c \n",button);
00113 //            }}}
00114             switch(button){
00115                 case 'U':                      //UP Button
00116                     if(state == 0){
00117                         state = 25;
00118                     }
00119                     else{
00120                         state--; 
00121                     }
00122                     lcd.printf("%c",char(alphabet[state]));
00123                     pc.printf("alphabet = %c     ",alphabet[state]);
00124                     pc.printf("    name = %s     \n",user_name);
00125 //                    pc.printf("    size = %d     ",size);
00126 //                    pc.printf("    state = %d \n",state);
00127                     wait_ms(600);
00128                     count_ok = 0;
00129                     break;
00130                 case 'D':                     //DOWN Button
00131                     if(state == 25){
00132                         state = 0;
00133                     }
00134                     else{
00135                         state++; 
00136                     }
00137                     lcd.printf("%c",char(alphabet[state]));
00138                     pc.printf("alphabet = %c     ",alphabet[state]);
00139                     pc.printf("    name = %s     \n",user_name);
00140 //                    pc.printf("    size = %d     ",size);
00141 //                    pc.printf("    state = %d \n",state);
00142 //                    pc.printf("DOWN\n");
00143                     wait_ms(600);
00144                     count_ok = 0;
00145                     break;
00146                 case 'O':                     //OK Button
00147                     user_name[size++] = alphabet[state];
00148                     lcd.printf("k%sf",user_name);
00149                     pc.printf("alphabet = %c     ",alphabet[state]);
00150                     pc.printf("    name = %s     \n",user_name);
00151 //                    pc.printf("    size = %d     ",size);
00152 //                    pc.printf("    state = %d \n",state);
00153                     
00154 //                    pc.printf("%c\n",button);
00155                     
00156                     wait_ms(600);
00157                     
00158                     break;
00159                 case 'C':                     //OK Button
00160                     if(size > 0){
00161                         size--;
00162                         user_name[size] = NULL;
00163                     }
00164                     lcd.printf("k%sf",user_name);
00165                     pc.printf("alphabet = %c     ",alphabet[state]);
00166                     pc.printf("    name = %s     \n",user_name);
00167 //                    pc.printf("    size = %d     ",size);
00168 //                    pc.printf("    state = %d \n",state);
00169                     
00170 //                    pc.printf("%c\n",button);
00171                     
00172                     wait_ms(600);
00173                     
00174                     break;
00175                 case 'E':                     //Enter Button
00176                     page = 3;
00177                     lcd.printf("x");
00178                     mode_name = 0;
00179                     
00180 //                    pc.printf("ENTER\n");
00181 //                    wait_ms(600);
00182                     break;
00183                     
00184             }
00185         }
00186 
00187     }
00188     button = NULL;
00189     wait_ms(600);
00190 //    lcd.printf("Name = %s",user_name);
00191 //    pc.printf("alphabet = %c     ",alphabet[state]);
00192     pc.printf("    name = %s     \n",user_name);
00193 //    pc.printf("    size = %d     ",size);
00194 //    pc.printf("    state = %d \n",state);
00195 //    return user_name;
00196     pc.printf("finish your name is : %s \n",user_name);
00197 }
00198 
00199 void Player::set_mode(void){
00200     lcd.puts("p003f");
00201     button = NULL;
00202     wait_ms(600);
00203     set_mode_loop = 1;
00204 //    lcd.printf("%d",game_mode);
00205     while(set_mode_loop){
00206         if(bt.readable()) {
00207             button = bt.getc();
00208             switch(button){
00209                 case 'U':
00210                     if(game_mode == 1){
00211                         game_mode = 3;
00212                     }
00213                     else{
00214                         game_mode--;
00215                     }
00216                     wait_ms(600);
00217                     lcd.printf("%d",game_mode);
00218                     pc.printf("set_mode = %d \n",game_mode);
00219                     break;
00220                 case 'D':
00221                     if(game_mode == 3){
00222                         game_mode = 1;
00223                     }
00224                     else{
00225                         game_mode++;
00226                     }
00227                     wait_ms(600);
00228                     lcd.printf("%d",game_mode);
00229                     pc.printf("set_mode = %d \n",game_mode);
00230                     break;
00231                 case 'E':
00232                     set_mode_loop = 0;
00233                     break;
00234             }
00235         }
00236     }
00237     pc.printf("finish set_mode = %d \n",game_mode);
00238 }
00239     
00240 //--------------------------------------------------------------------//
00241     
00242 void Player::run(void){ 
00243             if(datainput=='0'){
00244                 selectfile(0);
00245                 playFile(list[green]);
00246         
00247                 }
00248             if(datainput=='1'){
00249                 selectfile(1);
00250                 playFile(list[green]);
00251                 
00252                 
00253                 }
00254             if(datainput=='2'){
00255                 selectfile(2);
00256                 playFile(list[green]);
00257             
00258                 }
00259             if(datainput=='3'){
00260                 selectfile(3);
00261                 playFile(list[green]);
00262                 
00263                 }
00264             if(datainput=='4'){
00265                 selectfile(4);
00266                 playFile(list[green]);
00267 
00268                 }
00269             if(datainput=='5'){
00270                 selectfile(5);
00271                 playFile(list[green]);
00272 
00273                 }
00274             else{
00275                 stop();
00276                 }
00277 }
00278 
00279 
00280 
00281 //void Player::read(void){
00282 //  bt.readable();
00283 //  datainput = bt.getc();
00284 //
00285 //    }
00286 
00287     
00288     
00289 /*  This function plays audio file.  */
00290 void Player::playFile(char *file) {
00291     int bytes;        // How many bytes in buffer left
00292     char n;
00293     
00294     playerState = PS_PLAY;
00295     
00296     vs1053.setFreq(24000000);     //hight speed
00297     
00298     FileHandle *fp =sd.open(file, O_RDONLY);
00299     
00300     if(fp == NULL) {
00301         printf("Could not open %s\r\n",file);
00302 
00303     }
00304     else
00305     {
00306         printf("Playing %s ...\r\n",file);
00307         
00308         /* Main playback loop */
00309         while((bytes = fp->read(fileBuf,48000)) > 0) {  
00310        {
00311             if(bt.readable()){
00312                 break;
00313                 }
00314             vs1053.setFreq(24000000); 
00315             bufptr = fileBuf;
00316             // actual audio data gets sent to VS10xx.
00317             while(bytes > 0)
00318             {
00319                 n = (bytes < 32)?bytes:32; //defalt 32 
00320                 vs1053.writeData(bufptr,n);
00321                 bytes -= n;
00322                 bufptr += n;
00323                 if(bt.readable()){
00324                     break;
00325                 }
00326                 
00327             }
00328             uint8_t vol = 0x00;//set vlume
00329             vs1053.setVolume(vol);  //set vlume
00330             
00331             if(playerState != PS_PLAY)         //stop
00332             {
00333                 fp->close();
00334                 vs1053.softReset();
00335             }
00336                 
00337             }
00338         }
00339     }
00340 
00341 }
00342