mbeduino MP3 Sheild player MP3 player that runs on mebeduin with MP3 Shield. Regarding mbeduino, refer to: http://mbed.org/users/okini3939/notebook/mbeduino/ Regarding MP3 Shiled, refer to: http://www.sparkfun.com/commerce/product_info.php?products_id=9736
Dependencies: mbed SDFileSystem
main.cpp@0:67cb2f650c15, 2010-10-12 (annotated)
- Committer:
- xshige
- Date:
- Tue Oct 12 14:11:52 2010 +0000
- Revision:
- 0:67cb2f650c15
- Child:
- 1:c47269f0e9e1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xshige | 0:67cb2f650c15 | 1 | // |
xshige | 0:67cb2f650c15 | 2 | // mbeduino MP3 Shield Player |
xshige | 0:67cb2f650c15 | 3 | // |
xshige | 0:67cb2f650c15 | 4 | // Note: |
xshige | 0:67cb2f650c15 | 5 | // (1)Push reset button on the MP3 Sheild to reboot |
xshige | 0:67cb2f650c15 | 6 | // when this program does not work correctly. |
xshige | 0:67cb2f650c15 | 7 | // (2)Remenber the file system does not support long file/directory name. |
xshige | 0:67cb2f650c15 | 8 | // (3)I have confirmed MP3,Ogg,MIDI work correcly. |
xshige | 0:67cb2f650c15 | 9 | // But other formats(WAV,WMA,AAC) do Not work. I don't know reason why... |
xshige | 0:67cb2f650c15 | 10 | // |
xshige | 0:67cb2f650c15 | 11 | // 2010/10/12 |
xshige | 0:67cb2f650c15 | 12 | // ported on mbeduino with MP3 Shield by xshige |
xshige | 0:67cb2f650c15 | 13 | |
xshige | 0:67cb2f650c15 | 14 | #define mbeduino |
xshige | 0:67cb2f650c15 | 15 | |
xshige | 0:67cb2f650c15 | 16 | #include "mbed.h" |
xshige | 0:67cb2f650c15 | 17 | #include "VS1053.h" |
xshige | 0:67cb2f650c15 | 18 | |
xshige | 0:67cb2f650c15 | 19 | #ifdef mbeduino |
xshige | 0:67cb2f650c15 | 20 | #include "ArduinoShield.h" |
xshige | 0:67cb2f650c15 | 21 | #endif |
xshige | 0:67cb2f650c15 | 22 | |
xshige | 0:67cb2f650c15 | 23 | /* |
xshige | 0:67cb2f650c15 | 24 | |
xshige | 0:67cb2f650c15 | 25 | MP3 Shield Pin Assigment on mbeduino |
xshige | 0:67cb2f650c15 | 26 | |
xshige | 0:67cb2f650c15 | 27 | Arduino MP3 Side: |
xshige | 0:67cb2f650c15 | 28 | RX NC |
xshige | 0:67cb2f650c15 | 29 | TX NC |
xshige | 0:67cb2f650c15 | 30 | |
xshige | 0:67cb2f650c15 | 31 | D2(BSYNC) |
xshige | 0:67cb2f650c15 | 32 | D3(DREQ) |
xshige | 0:67cb2f650c15 | 33 | |
xshige | 0:67cb2f650c15 | 34 | D4(GPIO2) NC |
xshige | 0:67cb2f650c15 | 35 | D5(GPIO3) NC |
xshige | 0:67cb2f650c15 | 36 | |
xshige | 0:67cb2f650c15 | 37 | D9(CS) |
xshige | 0:67cb2f650c15 | 38 | |
xshige | 0:67cb2f650c15 | 39 | D11(MOSI) |
xshige | 0:67cb2f650c15 | 40 | D12(MISO) |
xshige | 0:67cb2f650c15 | 41 | D13(SCK) |
xshige | 0:67cb2f650c15 | 42 | |
xshige | 0:67cb2f650c15 | 43 | NC: means None-Connection so that I cut pin away. |
xshige | 0:67cb2f650c15 | 44 | |
xshige | 0:67cb2f650c15 | 45 | //------------- |
xshige | 0:67cb2f650c15 | 46 | |
xshige | 0:67cb2f650c15 | 47 | this program comming from the following code: |
xshige | 0:67cb2f650c15 | 48 | |
xshige | 0:67cb2f650c15 | 49 | Arduino MP3 Shield MP3Player |
xshige | 0:67cb2f650c15 | 50 | 2010/10/3 |
xshige | 0:67cb2f650c15 | 51 | |
xshige | 0:67cb2f650c15 | 52 | This program comes from http://mbed.org/cookbook/VS1002-MP3-Decoder |
xshige | 0:67cb2f650c15 | 53 | (Very small modification done to fit latest libs) |
xshige | 0:67cb2f650c15 | 54 | |
xshige | 0:67cb2f650c15 | 55 | |
xshige | 0:67cb2f650c15 | 56 | Pin Assigenment for Arduino MP3 Shield |
xshige | 0:67cb2f650c15 | 57 | (VS1053 employed, issued by sparkfun.com) |
xshige | 0:67cb2f650c15 | 58 | |
xshige | 0:67cb2f650c15 | 59 | MP3 Sheild Side | mbed Side |
xshige | 0:67cb2f650c15 | 60 | --------------------------------------- |
xshige | 0:67cb2f650c15 | 61 | D2(BSYNC)------------17 |
xshige | 0:67cb2f650c15 | 62 | D3(DREQ)-------------16 |
xshige | 0:67cb2f650c15 | 63 | |
xshige | 0:67cb2f650c15 | 64 | D9(CS)---------------14 |
xshige | 0:67cb2f650c15 | 65 | |
xshige | 0:67cb2f650c15 | 66 | D11(MOSI)------------11 |
xshige | 0:67cb2f650c15 | 67 | D12(MISO)------------12 |
xshige | 0:67cb2f650c15 | 68 | D13(SCK)-------------13 |
xshige | 0:67cb2f650c15 | 69 | |
xshige | 0:67cb2f650c15 | 70 | GND------------------GND(1) |
xshige | 0:67cb2f650c15 | 71 | 5V-------------------VU(39) |
xshige | 0:67cb2f650c15 | 72 | RESET----------------15 |
xshige | 0:67cb2f650c15 | 73 | |
xshige | 0:67cb2f650c15 | 74 | Volume will not used. (pin20 will not used) |
xshige | 0:67cb2f650c15 | 75 | |
xshige | 0:67cb2f650c15 | 76 | */ |
xshige | 0:67cb2f650c15 | 77 | |
xshige | 0:67cb2f650c15 | 78 | #include "SDFileSystem.h" |
xshige | 0:67cb2f650c15 | 79 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
xshige | 0:67cb2f650c15 | 80 | |
xshige | 0:67cb2f650c15 | 81 | VS1053 mp3( |
xshige | 0:67cb2f650c15 | 82 | // PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, |
xshige | 0:67cb2f650c15 | 83 | // PinName dreq, PinName dcs, PinName vol) |
xshige | 0:67cb2f650c15 | 84 | #ifndef mbeduino |
xshige | 0:67cb2f650c15 | 85 | p11, p12, p13, p14, p15, |
xshige | 0:67cb2f650c15 | 86 | p16, p17, p20); |
xshige | 0:67cb2f650c15 | 87 | #endif |
xshige | 0:67cb2f650c15 | 88 | #ifdef mbeduino |
xshige | 0:67cb2f650c15 | 89 | ARD_D11,ARD_D12,ARD_D13,ARD_D9,p15, |
xshige | 0:67cb2f650c15 | 90 | ARD_D3,ARD_D2,p20); |
xshige | 0:67cb2f650c15 | 91 | |
xshige | 0:67cb2f650c15 | 92 | DigitalOut common_cs(ARD_D10); |
xshige | 0:67cb2f650c15 | 93 | DigitalOut sdFile_cs(p8); |
xshige | 0:67cb2f650c15 | 94 | #endif |
xshige | 0:67cb2f650c15 | 95 | |
xshige | 0:67cb2f650c15 | 96 | |
xshige | 0:67cb2f650c15 | 97 | int main () |
xshige | 0:67cb2f650c15 | 98 | { |
xshige | 0:67cb2f650c15 | 99 | // make debug port Fast |
xshige | 0:67cb2f650c15 | 100 | Serial pc(USBTX, USBRX); |
xshige | 0:67cb2f650c15 | 101 | // pc.baud(9600); |
xshige | 0:67cb2f650c15 | 102 | pc.baud(115200); |
xshige | 0:67cb2f650c15 | 103 | // pc.baud(230400); |
xshige | 0:67cb2f650c15 | 104 | |
xshige | 0:67cb2f650c15 | 105 | #ifdef mbeduino |
xshige | 0:67cb2f650c15 | 106 | // I am not sure that the followig code needed |
xshige | 0:67cb2f650c15 | 107 | common_cs = 1; // init for de-select???? |
xshige | 0:67cb2f650c15 | 108 | sdFile_cs = 1; |
xshige | 0:67cb2f650c15 | 109 | wait_ms(100); |
xshige | 0:67cb2f650c15 | 110 | #endif |
xshige | 0:67cb2f650c15 | 111 | |
xshige | 0:67cb2f650c15 | 112 | /*============================================================ |
xshige | 0:67cb2f650c15 | 113 | * MP3 Initialising |
xshige | 0:67cb2f650c15 | 114 | *==========================================================*/ |
xshige | 0:67cb2f650c15 | 115 | |
xshige | 0:67cb2f650c15 | 116 | mp3._RST = 1; |
xshige | 0:67cb2f650c15 | 117 | mp3.cs_high(); //chip disabled |
xshige | 0:67cb2f650c15 | 118 | mp3.sci_initialise(); //initialise MBED |
xshige | 0:67cb2f650c15 | 119 | mp3.sci_write(0x00,(SM_SDINEW+SM_STREAM)); // set mode reg. |
xshige | 0:67cb2f650c15 | 120 | mp3.sci_write(0x03, 0x9800); |
xshige | 0:67cb2f650c15 | 121 | mp3.sdi_initialise(); |
xshige | 0:67cb2f650c15 | 122 | |
xshige | 0:67cb2f650c15 | 123 | /*============================================================ |
xshige | 0:67cb2f650c15 | 124 | * This is the good part |
xshige | 0:67cb2f650c15 | 125 | *==========================================================*/ |
xshige | 0:67cb2f650c15 | 126 | |
xshige | 0:67cb2f650c15 | 127 | /*====== Song Select ======*/ |
xshige | 0:67cb2f650c15 | 128 | for(int a=1; a<24; a++) |
xshige | 0:67cb2f650c15 | 129 | { |
xshige | 0:67cb2f650c15 | 130 | printf("Song#:%d\r\n",a); |
xshige | 0:67cb2f650c15 | 131 | int song_number = a; |
xshige | 0:67cb2f650c15 | 132 | |
xshige | 0:67cb2f650c15 | 133 | // char list[10000] = {0}; |
xshige | 0:67cb2f650c15 | 134 | char list[1000] = {0}; |
xshige | 0:67cb2f650c15 | 135 | char str[16] = {"/sd/"}; |
xshige | 0:67cb2f650c15 | 136 | unsigned int startplace = 0; |
xshige | 0:67cb2f650c15 | 137 | unsigned int endplace = 0; |
xshige | 0:67cb2f650c15 | 138 | unsigned int play = 0; |
xshige | 0:67cb2f650c15 | 139 | int num_of_files = 0; |
xshige | 0:67cb2f650c15 | 140 | |
xshige | 0:67cb2f650c15 | 141 | DIR *d; |
xshige | 0:67cb2f650c15 | 142 | struct dirent *p; |
xshige | 0:67cb2f650c15 | 143 | d = opendir("/sd"); |
xshige | 0:67cb2f650c15 | 144 | if(d != NULL) |
xshige | 0:67cb2f650c15 | 145 | { |
xshige | 0:67cb2f650c15 | 146 | while((p = readdir(d)) != NULL) |
xshige | 0:67cb2f650c15 | 147 | { |
xshige | 0:67cb2f650c15 | 148 | strcat(list, "*"); |
xshige | 0:67cb2f650c15 | 149 | strcat(list, p->d_name); |
xshige | 0:67cb2f650c15 | 150 | num_of_files++; |
xshige | 0:67cb2f650c15 | 151 | } |
xshige | 0:67cb2f650c15 | 152 | } |
xshige | 0:67cb2f650c15 | 153 | else |
xshige | 0:67cb2f650c15 | 154 | { |
xshige | 0:67cb2f650c15 | 155 | perror("Could not open directory!"); |
xshige | 0:67cb2f650c15 | 156 | } |
xshige | 0:67cb2f650c15 | 157 | strcat(list, "*"); //terminating * |
xshige | 0:67cb2f650c15 | 158 | |
xshige | 0:67cb2f650c15 | 159 | while(play != song_number) |
xshige | 0:67cb2f650c15 | 160 | { |
xshige | 0:67cb2f650c15 | 161 | char symbol = list[startplace]; |
xshige | 0:67cb2f650c15 | 162 | startplace++; |
xshige | 0:67cb2f650c15 | 163 | if(symbol == 0x2A) //0x2A = "*" |
xshige | 0:67cb2f650c15 | 164 | { |
xshige | 0:67cb2f650c15 | 165 | play++; |
xshige | 0:67cb2f650c15 | 166 | } |
xshige | 0:67cb2f650c15 | 167 | } |
xshige | 0:67cb2f650c15 | 168 | play = 0; |
xshige | 0:67cb2f650c15 | 169 | while(play != (song_number+1)) |
xshige | 0:67cb2f650c15 | 170 | { |
xshige | 0:67cb2f650c15 | 171 | char symbol = list[endplace]; |
xshige | 0:67cb2f650c15 | 172 | endplace++; |
xshige | 0:67cb2f650c15 | 173 | if(symbol == 0x2A) //0x2A = "*" |
xshige | 0:67cb2f650c15 | 174 | { |
xshige | 0:67cb2f650c15 | 175 | play++; |
xshige | 0:67cb2f650c15 | 176 | } |
xshige | 0:67cb2f650c15 | 177 | } |
xshige | 0:67cb2f650c15 | 178 | |
xshige | 0:67cb2f650c15 | 179 | strncat(str, &list[startplace], endplace-startplace); |
xshige | 0:67cb2f650c15 | 180 | str[(endplace-startplace)+3] = '\0'; |
xshige | 0:67cb2f650c15 | 181 | |
xshige | 0:67cb2f650c15 | 182 | //printf("list: %s\r\n",list); //debug |
xshige | 0:67cb2f650c15 | 183 | |
xshige | 0:67cb2f650c15 | 184 | /*====== File Transfer ======*/ |
xshige | 0:67cb2f650c15 | 185 | |
xshige | 0:67cb2f650c15 | 186 | printf("Now Playing: %s\r\n",str); |
xshige | 0:67cb2f650c15 | 187 | |
xshige | 0:67cb2f650c15 | 188 | FILE *song; |
xshige | 0:67cb2f650c15 | 189 | unsigned char array[512]; |
xshige | 0:67cb2f650c15 | 190 | |
xshige | 0:67cb2f650c15 | 191 | song = fopen(str, "rb"); |
xshige | 0:67cb2f650c15 | 192 | |
xshige | 0:67cb2f650c15 | 193 | if(!song) |
xshige | 0:67cb2f650c15 | 194 | { |
xshige | 0:67cb2f650c15 | 195 | exit(1); |
xshige | 0:67cb2f650c15 | 196 | } |
xshige | 0:67cb2f650c15 | 197 | |
xshige | 0:67cb2f650c15 | 198 | while(!feof(song)) |
xshige | 0:67cb2f650c15 | 199 | { |
xshige | 0:67cb2f650c15 | 200 | int n=fread(&array, 1, sizeof(array), song); |
xshige | 0:67cb2f650c15 | 201 | mp3.writeStream(array,n); |
xshige | 0:67cb2f650c15 | 202 | } |
xshige | 0:67cb2f650c15 | 203 | mp3.terminateStream(); |
xshige | 0:67cb2f650c15 | 204 | |
xshige | 0:67cb2f650c15 | 205 | fclose(song); //close the file |
xshige | 0:67cb2f650c15 | 206 | |
xshige | 0:67cb2f650c15 | 207 | } // for |
xshige | 0:67cb2f650c15 | 208 | |
xshige | 0:67cb2f650c15 | 209 | printf("Done.\r\n"); |
xshige | 0:67cb2f650c15 | 210 | } |