Arduino MP3 Shield MP3Player This program comes from http://mbed.org/cookbook/VS1002-MP3-Decoder (Very small modification done to fit latest libs) **Additional small mods to xshige\'s code by mpetersen3 and kbuck3 to add a user interface
VS1002.cpp
00001 #include "VS1002.h" 00002 #include "mbed.h" 00003 #include "TextLCD.h" 00004 AnalogIn rotary1(p19); 00005 DigitalIn pause1(p28); 00006 TextLCD lcd(p10, p18, p24, p23, p22, p21, TextLCD::LCD16x2 ); // rs, e, d0-d3 00007 00008 00009 /* ================================================================== 00010 * Constructor 00011 * =================================================================*/ 00012 VS1002::VS1002( 00013 PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name, 00014 PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, 00015 PinName dreq, PinName dcs, PinName vol) 00016 : 00017 _sd(mmosi, mmiso, ssck, ccs, name), 00018 _spi(mosi, miso, sck), 00019 _CS(cs), 00020 _RST(rst), 00021 _DREQ(dreq), 00022 _DCS(dcs), 00023 _VOL(vol) { 00024 00025 } 00026 00027 /*=================================================================== 00028 * Functions 00029 *==================================================================*/ 00030 00031 void VS1002::cs_low(void) 00032 { 00033 _CS = 0; 00034 } 00035 void VS1002::cs_high(void) 00036 { 00037 _CS = 1; 00038 } 00039 void VS1002::dcs_low(void) 00040 { 00041 _DCS = 0; 00042 } 00043 void VS1002::dcs_high(void) 00044 { 00045 _DCS = 1; 00046 } 00047 void VS1002::sci_en(void) //SCI enable 00048 { 00049 cs_high(); 00050 dcs_high(); 00051 cs_low(); 00052 } 00053 void VS1002::sci_dis(void) //SCI disable 00054 { 00055 cs_high(); 00056 } 00057 void VS1002::sdi_en(void) //SDI enable 00058 { 00059 dcs_high(); 00060 cs_high(); 00061 dcs_low(); 00062 } 00063 void VS1002::sdi_dis(void) //SDI disable 00064 { 00065 dcs_high(); 00066 } 00067 void VS1002::reset(void) //hardware reset 00068 { 00069 wait(0.01); 00070 _RST = 0; 00071 wait(0.01); 00072 _RST = 1; 00073 wait(0.10); 00074 } 00075 void VS1002::power_down(void) //hardware and software reset 00076 { 00077 cs_low(); 00078 reset(); 00079 sci_write(0x00, SM_PDOWN); 00080 wait(0.01); 00081 reset(); 00082 } 00083 void VS1002::sci_initialise(void) 00084 { 00085 _RST = 1; //no reset 00086 _spi.format(8,0); //spi 8bit interface, steady state low 00087 _spi.frequency(1000000); //rising edge data record, freq. 1Mhz 00088 00089 cs_low(); 00090 for(int i=0; i<4; i++) 00091 { 00092 _spi.write(0xFF); //clock the chip a bit 00093 } 00094 cs_high(); 00095 dcs_high(); 00096 wait_us(5); 00097 } 00098 void VS1002::sdi_initialise(void) 00099 { 00100 _spi.format(8,0); 00101 _spi.frequency(7000000); //set to 7MHz 00102 00103 cs_high(); 00104 dcs_high(); 00105 } 00106 void VS1002::sci_write(unsigned char address, unsigned short int data) 00107 { 00108 sci_en(); //enables SCI/disables SDI 00109 00110 while(!_DREQ); //wait unitl data request is high 00111 _spi.write(0x02); //SCI write 00112 _spi.write(address); //register address 00113 _spi.write((data >> 8) & 0xFF); //write out first half of data word 00114 _spi.write(data & 0xFF); //write out second half of data word 00115 00116 sci_dis(); //enables SDI/disables SCI 00117 wait_us(5); 00118 } 00119 void VS1002::sdi_write(unsigned char datum) 00120 { 00121 sdi_en(); 00122 00123 while(!_DREQ); 00124 _spi.write(datum); 00125 00126 sci_dis(); 00127 } 00128 unsigned short int VS1002::read(unsigned short int address) 00129 { 00130 cs_low(); //enables SCI/disables SDI 00131 00132 while(!_DREQ); //wait unitl data request is high 00133 _spi.write(0x03); //SCI write 00134 _spi.write(address); //register address 00135 unsigned short int received = _spi.write(0x00); //write out dummy byte 00136 received <<= 8; 00137 received += _spi.write(0x00); //write out dummy byte 00138 00139 cs_high(); //enables SDI/disables SCI 00140 00141 return received; //return received word 00142 } 00143 void VS1002::sine_test_activate(unsigned char wave) 00144 { 00145 cs_high(); //enables SDI/disables SCI 00146 00147 while(!_DREQ); //wait unitl data request is high 00148 _spi.write(0x53); //SDI write 00149 _spi.write(0xEF); //SDI write 00150 _spi.write(0x6E); //SDI write 00151 _spi.write(wave); //SDI write 00152 _spi.write(0x00); //filler byte 00153 _spi.write(0x00); //filler byte 00154 _spi.write(0x00); //filler byte 00155 _spi.write(0x00); //filler byte 00156 00157 cs_low(); //enables SCI/disables SDI 00158 } 00159 void VS1002::sine_test_deactivate(void) 00160 { 00161 cs_high(); 00162 00163 while(!_DREQ); 00164 _spi.write(0x45); //SDI write 00165 _spi.write(0x78); //SDI write 00166 _spi.write(0x69); //SDI write 00167 _spi.write(0x74); //SDI write 00168 _spi.write(0x00); //filler byte 00169 _spi.write(0x00); //filler byte 00170 _spi.write(0x00); //filler byte 00171 _spi.write(0x00); //filler byte 00172 } 00173 void VS1002::volume(void) 00174 { 00175 #ifdef FIXED_VOL 00176 unsigned char volumize = (0 * 255); // FIXED VOL (not support volume input) 00177 #else 00178 unsigned char volumize = (_VOL * 255); 00179 #endif 00180 while(!_DREQ); 00181 00182 unsigned short int attenuation = ((256 * volumize) + volumize); 00183 sci_write(0x0B, attenuation); 00184 00185 } 00186 00187 void VS1002::play_song(int song_number) 00188 { 00189 /*====== Song Select ======*/ 00190 00191 // char list[10000] = {0}; 00192 char list[1000] = {0}; 00193 char str[16] = {"/sd/"}; 00194 unsigned int startplace = 0; 00195 unsigned int endplace = 0; 00196 unsigned int play = 0; 00197 num_of_files = 0; 00198 00199 DIR *d; 00200 struct dirent *p; 00201 d = opendir("/sd"); 00202 if(d != NULL) 00203 { 00204 while((p = readdir(d)) != NULL) 00205 { 00206 strcat(list, "*"); 00207 strcat(list, p->d_name); 00208 num_of_files++; 00209 } 00210 } 00211 else 00212 { 00213 perror("Could not open directory!"); 00214 } 00215 strcat(list, "*"); //terminating * 00216 if(num_of_files < song_number) 00217 { 00218 return; 00219 } 00220 while(play != song_number) 00221 { 00222 char symbol = list[startplace]; 00223 startplace++; 00224 if(symbol == 0x2A) //0x2A = "*" 00225 { 00226 play++; 00227 } 00228 } 00229 play = 0; 00230 while(play != (song_number+1)) 00231 { 00232 char symbol = list[endplace]; 00233 endplace++; 00234 if(symbol == 0x2A) //0x2A = "*" 00235 { 00236 play++; 00237 } 00238 } 00239 00240 strncat(str, &list[startplace], endplace-startplace); 00241 str[(endplace-startplace)+3] = '\0'; 00242 00243 //printf("list: %s\r\n",list); //debug 00244 00245 /*====== File Transfer ======*/ 00246 00247 // return if not MP3 file 00248 if (!((strstr(str,"MP3")!=NULL)||(strstr(str,"mp3")!=NULL))) return; 00249 // display filename.mp3 to the lcd screen 00250 lcd.printf("Now Playing: %s\r\n",str); 00251 00252 FILE *song; 00253 unsigned char array[512]; 00254 00255 song = fopen(str, "rb"); 00256 00257 if(!song) 00258 { 00259 exit(1); 00260 } 00261 00262 while((!feof(song))&&!pause1) 00263 { 00264 fread(&array, 1, 512, song); 00265 for(int i=0; i<512; i++) 00266 { 00267 #ifndef FS_ONLY 00268 sdi_write(array[i]); 00269 // printf("."); 00270 #endif 00271 } 00272 #ifndef FS_ONLY 00273 volume(); 00274 #endif 00275 } 00276 for(int n=0; n<2048; n++) 00277 { 00278 #ifndef FS_ONLY 00279 sdi_write(0x00); 00280 #endif 00281 } 00282 fclose(song); //close the file 00283 }
Generated on Wed Jul 13 2022 18:07:10 by 1.7.2