Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:8ef1e4409f20, 2011-01-12 (annotated)
- Committer:
- christi_s
- Date:
- Wed Jan 12 18:42:46 2011 +0000
- Revision:
- 2:8ef1e4409f20
- Parent:
- 1:62552e038f69
update for library rev. lkw4hi
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| christi_s | 0:e1919041e50b | 1 | /* mbed MP3 Shield Player - Testapplication for VLSI VS1053b Lib |
| christi_s | 0:e1919041e50b | 2 | * Copyright (c) 2010 Christian Schmiljun |
| christi_s | 0:e1919041e50b | 3 | * |
| christi_s | 0:e1919041e50b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| christi_s | 0:e1919041e50b | 5 | * of this software and associated documentation files (the "Software"), to deal |
| christi_s | 0:e1919041e50b | 6 | * in the Software without restriction, including without limitation the rights |
| christi_s | 0:e1919041e50b | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| christi_s | 0:e1919041e50b | 8 | * copies of the Software, and to permit persons to whom the Software is |
| christi_s | 0:e1919041e50b | 9 | * furnished to do so, subject to the following conditions: |
| christi_s | 0:e1919041e50b | 10 | * |
| christi_s | 0:e1919041e50b | 11 | * The above copyright notice and this permission notice shall be included in |
| christi_s | 0:e1919041e50b | 12 | * all copies or substantial portions of the Software. |
| christi_s | 0:e1919041e50b | 13 | * |
| christi_s | 0:e1919041e50b | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| christi_s | 0:e1919041e50b | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| christi_s | 0:e1919041e50b | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| christi_s | 0:e1919041e50b | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| christi_s | 0:e1919041e50b | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| christi_s | 0:e1919041e50b | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| christi_s | 0:e1919041e50b | 20 | * THE SOFTWARE. |
| christi_s | 0:e1919041e50b | 21 | */ |
| christi_s | 0:e1919041e50b | 22 | |
| christi_s | 0:e1919041e50b | 23 | |
| christi_s | 0:e1919041e50b | 24 | #include "mbed.h" |
| christi_s | 1:62552e038f69 | 25 | #include "defines.h" |
| christi_s | 0:e1919041e50b | 26 | #include "VS1053.h" |
| christi_s | 0:e1919041e50b | 27 | |
| christi_s | 0:e1919041e50b | 28 | // Volume [ 0x0000 (loud) .. 0xFEFE (quiet) ] |
| christi_s | 0:e1919041e50b | 29 | #define VOLUME -42.0f |
| christi_s | 0:e1919041e50b | 30 | // Switch for local filesystem or SDCard |
| christi_s | 0:e1919041e50b | 31 | #define USE_SDCARD |
| christi_s | 0:e1919041e50b | 32 | |
| christi_s | 0:e1919041e50b | 33 | // ---------------------------------------------------------------------------- |
| christi_s | 0:e1919041e50b | 34 | // |
| christi_s | 0:e1919041e50b | 35 | // Pin Assigenment for SDCard on Cool Components Workshop Board |
| christi_s | 0:e1919041e50b | 36 | // |
| christi_s | 0:e1919041e50b | 37 | // SDCard | mbed Side |
| christi_s | 0:e1919041e50b | 38 | // --------------------------------------- |
| christi_s | 0:e1919041e50b | 39 | // mosi-----------------5 |
| christi_s | 0:e1919041e50b | 40 | // miso-----------------6 |
| christi_s | 0:e1919041e50b | 41 | // sclk-----------------7 |
| christi_s | 0:e1919041e50b | 42 | // cs-------------------8 |
| christi_s | 0:e1919041e50b | 43 | // |
| christi_s | 0:e1919041e50b | 44 | // ---------------------------------------------------------------------------- |
| christi_s | 0:e1919041e50b | 45 | #ifdef USE_SDCARD |
| christi_s | 0:e1919041e50b | 46 | #include "SDFileSystem.h" |
| christi_s | 0:e1919041e50b | 47 | |
| christi_s | 0:e1919041e50b | 48 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
| christi_s | 0:e1919041e50b | 49 | #define DIR_NAME "/sd/Musik" |
| christi_s | 0:e1919041e50b | 50 | #else |
| christi_s | 0:e1919041e50b | 51 | LocalFileSystem sd("local"); |
| christi_s | 0:e1919041e50b | 52 | #define DIR_NAME "/local" |
| christi_s | 0:e1919041e50b | 53 | #endif |
| christi_s | 0:e1919041e50b | 54 | |
| christi_s | 0:e1919041e50b | 55 | |
| christi_s | 0:e1919041e50b | 56 | |
| christi_s | 0:e1919041e50b | 57 | |
| christi_s | 0:e1919041e50b | 58 | // ---------------------------------------------------------------------------- |
| christi_s | 0:e1919041e50b | 59 | // |
| christi_s | 0:e1919041e50b | 60 | // Pin Assigenment for Arduino MP3 Shield |
| christi_s | 0:e1919041e50b | 61 | // (VS1053 employed, issued by sparkfun.com) |
| christi_s | 0:e1919041e50b | 62 | // |
| christi_s | 0:e1919041e50b | 63 | // MP3 Sheild Side | mbed Side |
| christi_s | 0:e1919041e50b | 64 | // --------------------------------------- |
| christi_s | 0:e1919041e50b | 65 | // RX-------------------10 (optional) |
| christi_s | 0:e1919041e50b | 66 | // TX-------------------09 (optional) |
| christi_s | 0:e1919041e50b | 67 | // D2(BSYNC)------------17 |
| christi_s | 0:e1919041e50b | 68 | // D3(DREQ)-------------16 |
| christi_s | 0:e1919041e50b | 69 | // |
| christi_s | 0:e1919041e50b | 70 | // D9(CS)---------------14 |
| christi_s | 0:e1919041e50b | 71 | // |
| christi_s | 0:e1919041e50b | 72 | // D11(MOSI)------------11 |
| christi_s | 0:e1919041e50b | 73 | // D12(MISO)------------12 |
| christi_s | 0:e1919041e50b | 74 | // D13(SCK)-------------13 |
| christi_s | 0:e1919041e50b | 75 | // |
| christi_s | 0:e1919041e50b | 76 | // GND------------------GND(1) |
| christi_s | 0:e1919041e50b | 77 | // 5V-------------------VU(39) |
| christi_s | 0:e1919041e50b | 78 | // RESET----------------15 |
| christi_s | 0:e1919041e50b | 79 | // |
| christi_s | 0:e1919041e50b | 80 | // ---------------------------------------------------------------------------- |
| christi_s | 2:8ef1e4409f20 | 81 | const int VS1053B_BUFFER_SIZE = (16 * 1024 + 1); |
| christi_s | 2:8ef1e4409f20 | 82 | char VS1053_BUFFER[VS1053B_BUFFER_SIZE]; |
| christi_s | 2:8ef1e4409f20 | 83 | char* VS1053B_BUFFER_POINTER = VS1053_BUFFER; |
| christi_s | 2:8ef1e4409f20 | 84 | VS1053 mp3( p11, p12, p13, p14, p15, p16, p17, VS1053_BUFFER, VS1053B_BUFFER_SIZE); |
| christi_s | 0:e1919041e50b | 85 | |
| christi_s | 0:e1919041e50b | 86 | // Serial for Debug |
| christi_s | 0:e1919041e50b | 87 | Serial pc(USBTX, USBRX); |
| christi_s | 0:e1919041e50b | 88 | |
| christi_s | 1:62552e038f69 | 89 | Ticker timer; |
| christi_s | 1:62552e038f69 | 90 | Ticker timer2; |
| christi_s | 1:62552e038f69 | 91 | |
| christi_s | 1:62552e038f69 | 92 | DigitalIn _DREQ(p16); |
| christi_s | 1:62552e038f69 | 93 | bool next = false; |
| christi_s | 1:62552e038f69 | 94 | |
| christi_s | 1:62552e038f69 | 95 | void statisticsOutput(); |
| christi_s | 1:62552e038f69 | 96 | |
| christi_s | 0:e1919041e50b | 97 | void setVolume(void) { |
| christi_s | 0:e1919041e50b | 98 | if (pc.readable()) |
| christi_s | 0:e1919041e50b | 99 | { |
| christi_s | 0:e1919041e50b | 100 | unsigned char c = pc.getc(); |
| christi_s | 1:62552e038f69 | 101 | // scanf ("%x",&i); |
| christi_s | 1:62552e038f69 | 102 | switch (c) |
| christi_s | 0:e1919041e50b | 103 | { |
| christi_s | 1:62552e038f69 | 104 | case '1': |
| christi_s | 1:62552e038f69 | 105 | case '2': |
| christi_s | 1:62552e038f69 | 106 | case '3': |
| christi_s | 1:62552e038f69 | 107 | case '4': |
| christi_s | 1:62552e038f69 | 108 | mp3.setPlaySpeed(c - 48); |
| christi_s | 1:62552e038f69 | 109 | break; |
| christi_s | 1:62552e038f69 | 110 | case '+': |
| christi_s | 1:62552e038f69 | 111 | mp3.setVolume(mp3.getVolume() + 0.5); |
| christi_s | 1:62552e038f69 | 112 | break; |
| christi_s | 1:62552e038f69 | 113 | case '-': |
| christi_s | 1:62552e038f69 | 114 | mp3.setVolume(mp3.getVolume() - 0.5); |
| christi_s | 1:62552e038f69 | 115 | break; |
| christi_s | 1:62552e038f69 | 116 | case 'k': |
| christi_s | 1:62552e038f69 | 117 | mp3.setVolume(mp3.getBalance() + 0.5); |
| christi_s | 1:62552e038f69 | 118 | break; |
| christi_s | 1:62552e038f69 | 119 | case 'l': |
| christi_s | 1:62552e038f69 | 120 | mp3.setVolume(mp3.getBalance() - 0.5); |
| christi_s | 1:62552e038f69 | 121 | break; |
| christi_s | 1:62552e038f69 | 122 | case 'a': |
| christi_s | 1:62552e038f69 | 123 | mp3.setTrebleFrequency(mp3.getTrebleFrequency() + 1000); |
| christi_s | 1:62552e038f69 | 124 | break; |
| christi_s | 1:62552e038f69 | 125 | case 'y': |
| christi_s | 1:62552e038f69 | 126 | mp3.setTrebleFrequency(mp3.getTrebleFrequency() - 1000); |
| christi_s | 1:62552e038f69 | 127 | break; |
| christi_s | 1:62552e038f69 | 128 | case 's': |
| christi_s | 1:62552e038f69 | 129 | mp3.setTrebleAmplitude(mp3.getTrebleAmplitude() - 1); |
| christi_s | 1:62552e038f69 | 130 | break; |
| christi_s | 1:62552e038f69 | 131 | case 'x': |
| christi_s | 1:62552e038f69 | 132 | mp3.setTrebleAmplitude(mp3.getTrebleAmplitude() + 1); |
| christi_s | 1:62552e038f69 | 133 | break; |
| christi_s | 1:62552e038f69 | 134 | case 'd': |
| christi_s | 1:62552e038f69 | 135 | mp3.setBassFrequency(mp3.getBassFrequency() + 10); |
| christi_s | 1:62552e038f69 | 136 | break; |
| christi_s | 1:62552e038f69 | 137 | case 'c': |
| christi_s | 1:62552e038f69 | 138 | mp3.setBassFrequency(mp3.getBassFrequency() - 10); |
| christi_s | 1:62552e038f69 | 139 | break; |
| christi_s | 1:62552e038f69 | 140 | case 'f': |
| christi_s | 1:62552e038f69 | 141 | mp3.setBassAmplitude(mp3.getBassAmplitude() - 1); |
| christi_s | 1:62552e038f69 | 142 | break; |
| christi_s | 1:62552e038f69 | 143 | case 'v': |
| christi_s | 1:62552e038f69 | 144 | mp3.setBassAmplitude(mp3.getBassAmplitude() + 1); |
| christi_s | 1:62552e038f69 | 145 | break; |
| christi_s | 1:62552e038f69 | 146 | case 'z': |
| christi_s | 1:62552e038f69 | 147 | timer.attach(&statisticsOutput, 1); |
| christi_s | 1:62552e038f69 | 148 | break; |
| christi_s | 1:62552e038f69 | 149 | case 'u': |
| christi_s | 1:62552e038f69 | 150 | timer.detach(); |
| christi_s | 1:62552e038f69 | 151 | break; |
| christi_s | 1:62552e038f69 | 152 | case 'i': |
| christi_s | 1:62552e038f69 | 153 | statisticsOutput(); |
| christi_s | 1:62552e038f69 | 154 | break; |
| christi_s | 1:62552e038f69 | 155 | case 'h': |
| christi_s | 1:62552e038f69 | 156 | mp3.play(); |
| christi_s | 1:62552e038f69 | 157 | break; |
| christi_s | 1:62552e038f69 | 158 | case 'j': |
| christi_s | 1:62552e038f69 | 159 | mp3.pause(); |
| christi_s | 1:62552e038f69 | 160 | break; |
| christi_s | 1:62552e038f69 | 161 | case 'n': |
| christi_s | 1:62552e038f69 | 162 | next = true; |
| christi_s | 1:62552e038f69 | 163 | break; |
| christi_s | 1:62552e038f69 | 164 | default: |
| christi_s | 1:62552e038f69 | 165 | break; |
| christi_s | 1:62552e038f69 | 166 | } |
| christi_s | 0:e1919041e50b | 167 | } |
| christi_s | 0:e1919041e50b | 168 | } |
| christi_s | 0:e1919041e50b | 169 | |
| christi_s | 1:62552e038f69 | 170 | |
| christi_s | 1:62552e038f69 | 171 | |
| christi_s | 1:62552e038f69 | 172 | void statisticsOutput() |
| christi_s | 1:62552e038f69 | 173 | { |
| christi_s | 1:62552e038f69 | 174 | printf("Statistics\r\n"); |
| christi_s | 2:8ef1e4409f20 | 175 | printf("Buffer - Size: %i, Free: %i, Loaded: %i\r\n", mp3.bufferLength(), mp3.bufferFree(), mp3.bufferCount()); |
| christi_s | 1:62552e038f69 | 176 | printf("DREQ: %#x\r\n", _DREQ.read()); |
| christi_s | 1:62552e038f69 | 177 | } |
| christi_s | 1:62552e038f69 | 178 | |
| christi_s | 1:62552e038f69 | 179 | int main () { |
| christi_s | 1:62552e038f69 | 180 | |
| christi_s | 0:e1919041e50b | 181 | // ------------------------------------------------------------------------ |
| christi_s | 0:e1919041e50b | 182 | // MP3 Initialising |
| christi_s | 0:e1919041e50b | 183 | // ------------------------------------------------------------------------ |
| christi_s | 0:e1919041e50b | 184 | printf("Initialize mp3 Codec...\r\n"); |
| christi_s | 0:e1919041e50b | 185 | mp3.initialize(); |
| christi_s | 0:e1919041e50b | 186 | printf("mp3 Codec is initialized\r\n"); |
| christi_s | 0:e1919041e50b | 187 | |
| christi_s | 0:e1919041e50b | 188 | |
| christi_s | 0:e1919041e50b | 189 | mp3.setVolume(VOLUME); |
| christi_s | 1:62552e038f69 | 190 | pc.attach(&setVolume); |
| christi_s | 0:e1919041e50b | 191 | |
| christi_s | 0:e1919041e50b | 192 | // ------------------------------------------------------------------------ |
| christi_s | 0:e1919041e50b | 193 | // Play mp3 file |
| christi_s | 0:e1919041e50b | 194 | // ------------------------------------------------------------------------ |
| christi_s | 0:e1919041e50b | 195 | |
| christi_s | 0:e1919041e50b | 196 | //while (1) |
| christi_s | 0:e1919041e50b | 197 | { |
| christi_s | 0:e1919041e50b | 198 | DIR *d; |
| christi_s | 0:e1919041e50b | 199 | struct dirent *p; |
| christi_s | 0:e1919041e50b | 200 | d = opendir(DIR_NAME); |
| christi_s | 0:e1919041e50b | 201 | if(d != NULL) |
| christi_s | 0:e1919041e50b | 202 | { |
| christi_s | 0:e1919041e50b | 203 | while ((p = readdir(d)) != NULL) { |
| christi_s | 0:e1919041e50b | 204 | char str[160] = DIR_NAME; |
| christi_s | 0:e1919041e50b | 205 | |
| christi_s | 0:e1919041e50b | 206 | //check extension |
| christi_s | 0:e1919041e50b | 207 | char * extension =strrchr( p->d_name,'.') + 1; |
| christi_s | 0:e1919041e50b | 208 | bool isAudioFile = false; |
| christi_s | 0:e1919041e50b | 209 | if (strcmp(extension, "mp3") == 0) |
| christi_s | 0:e1919041e50b | 210 | isAudioFile = true; |
| christi_s | 0:e1919041e50b | 211 | else if (strcmp(extension, "MP3") == 0) |
| christi_s | 0:e1919041e50b | 212 | isAudioFile = true; |
| christi_s | 0:e1919041e50b | 213 | else if (strcmp(extension, "mp4") == 0) |
| christi_s | 0:e1919041e50b | 214 | isAudioFile = true; |
| christi_s | 0:e1919041e50b | 215 | else if (strcmp(extension, "MP4") == 0) |
| christi_s | 0:e1919041e50b | 216 | isAudioFile = true; |
| christi_s | 0:e1919041e50b | 217 | if (isAudioFile ) |
| christi_s | 0:e1919041e50b | 218 | { |
| christi_s | 0:e1919041e50b | 219 | printf("Now Playing: %s\r\n", p->d_name); |
| christi_s | 0:e1919041e50b | 220 | |
| christi_s | 0:e1919041e50b | 221 | sprintf(str, "%s/%s", DIR_NAME, p->d_name); |
| christi_s | 0:e1919041e50b | 222 | printf("Path %s\r\n", str); |
| christi_s | 0:e1919041e50b | 223 | |
| christi_s | 0:e1919041e50b | 224 | FILE *song; |
| christi_s | 0:e1919041e50b | 225 | |
| christi_s | 2:8ef1e4409f20 | 226 | char array[2048]; |
| christi_s | 0:e1919041e50b | 227 | |
| christi_s | 0:e1919041e50b | 228 | song = fopen(str, "rb"); |
| christi_s | 0:e1919041e50b | 229 | //song = fopen("/sd/Musik/01.mp3", "rb"); |
| christi_s | 0:e1919041e50b | 230 | |
| christi_s | 0:e1919041e50b | 231 | if (!song) { |
| christi_s | 0:e1919041e50b | 232 | printf("Couldn't open %s\r\n", str); |
| christi_s | 0:e1919041e50b | 233 | continue; |
| christi_s | 0:e1919041e50b | 234 | //exit(1); |
| christi_s | 1:62552e038f69 | 235 | } |
| christi_s | 1:62552e038f69 | 236 | |
| christi_s | 1:62552e038f69 | 237 | int count = 0; |
| christi_s | 1:62552e038f69 | 238 | bool test = true; |
| christi_s | 1:62552e038f69 | 239 | |
| christi_s | 1:62552e038f69 | 240 | while (!feof(song) && !next) { |
| christi_s | 0:e1919041e50b | 241 | int n=fread(&array, 1, sizeof(array), song); |
| christi_s | 1:62552e038f69 | 242 | while (mp3.bufferFree() < n) |
| christi_s | 1:62552e038f69 | 243 | ; |
| christi_s | 1:62552e038f69 | 244 | mp3.bufferPutStream(array,n); |
| christi_s | 1:62552e038f69 | 245 | if (count > 2 && test) |
| christi_s | 1:62552e038f69 | 246 | { |
| christi_s | 1:62552e038f69 | 247 | test = false; |
| christi_s | 1:62552e038f69 | 248 | mp3.play(); |
| christi_s | 1:62552e038f69 | 249 | } |
| christi_s | 1:62552e038f69 | 250 | count++; |
| christi_s | 1:62552e038f69 | 251 | } |
| christi_s | 1:62552e038f69 | 252 | if (next) |
| christi_s | 1:62552e038f69 | 253 | { |
| christi_s | 1:62552e038f69 | 254 | mp3.stop(); |
| christi_s | 1:62552e038f69 | 255 | next = false; |
| christi_s | 0:e1919041e50b | 256 | } |
| christi_s | 1:62552e038f69 | 257 | else |
| christi_s | 1:62552e038f69 | 258 | { |
| christi_s | 1:62552e038f69 | 259 | mp3.terminateStream(); |
| christi_s | 1:62552e038f69 | 260 | } |
| christi_s | 0:e1919041e50b | 261 | fclose(song); //close the file |
| christi_s | 0:e1919041e50b | 262 | |
| christi_s | 1:62552e038f69 | 263 | printf("End of song.\r\n"); |
| christi_s | 0:e1919041e50b | 264 | wait(1.0); |
| christi_s | 0:e1919041e50b | 265 | } |
| christi_s | 0:e1919041e50b | 266 | else |
| christi_s | 0:e1919041e50b | 267 | { |
| christi_s | 0:e1919041e50b | 268 | printf("File ignored. Extension: %s\r\n", extension); |
| christi_s | 0:e1919041e50b | 269 | } |
| christi_s | 0:e1919041e50b | 270 | |
| christi_s | 0:e1919041e50b | 271 | } |
| christi_s | 0:e1919041e50b | 272 | closedir(d); |
| christi_s | 0:e1919041e50b | 273 | } |
| christi_s | 0:e1919041e50b | 274 | else |
| christi_s | 0:e1919041e50b | 275 | { |
| christi_s | 0:e1919041e50b | 276 | error("Could not open directory!"); |
| christi_s | 0:e1919041e50b | 277 | // break; |
| christi_s | 0:e1919041e50b | 278 | } |
| christi_s | 0:e1919041e50b | 279 | |
| christi_s | 0:e1919041e50b | 280 | } |
| christi_s | 0:e1919041e50b | 281 | printf("Done.\r\n"); |
| christi_s | 0:e1919041e50b | 282 | |
| christi_s | 0:e1919041e50b | 283 | } |