My first attempt of Thread based mp3 player with mbed RTOS *player thread *volume control thread

Dependencies:   mbed VS1053b

Files at this revision

API Documentation at this revision

Comitter:
takashikojo
Date:
Mon Mar 19 12:00:14 2012 +0000
Parent:
0:82078eeba8ba
Commit message:
Thread based mp3 player with mbed RTOS

Changed in this revision

Thread_mp3/main_mp3.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Thread_mp3/main_mp3.cpp	Mon Mar 19 11:32:57 2012 +0000
+++ b/Thread_mp3/main_mp3.cpp	Mon Mar 19 12:00:14 2012 +0000
@@ -104,7 +104,7 @@
 
 void statisticsOutput();
 
-void setVolume(void const *) {
+void SetVolume(void const *) {
     while (1) {
         if (pc.readable()) {
             unsigned char c = pc.getc();
@@ -178,8 +178,6 @@
     }
 }
 
-
-
 void statisticsOutput() {
     printf("Statistics\r\n");
     printf("Buffer - Size: %i, Free: %i, Loaded: %i\r\n", mp3.bufferLength(), mp3.bufferFree(), mp3.bufferCount());
@@ -188,6 +186,43 @@
 
 DigitalOut led2(LED2); // Ticker for mp3
 
+void playFile(char *fname) {
+    FILE *song;
+    static char array[2048];
+
+    song = fopen(fname, "rb");
+    if (!song) {
+        printf("Couldn't open %s\r\n", fname);
+        return ;
+    }
+
+    int count = 0;
+    bool test = true;
+
+    while (!feof(song) && !next) {
+        int n=fread(&array, 1, sizeof(array), song);
+        while (mp3.bufferFree() < n) {
+            led2 = 1 ;
+            Thread::wait(100) ;
+            led2 = 0 ;
+        }
+        mp3.bufferPutStream(array,n);
+        if (count > 2 && test) {
+            test = false;
+            mp3.play();
+        }
+        count++;
+    }
+    if (next) {
+        mp3.stop();
+        next = false;
+    } else {
+        mp3.terminateStream();
+    }
+    fclose(song);  //close the file
+}
+
+
 void main_mp3 (void const *argument) {
 
     // ------------------------------------------------------------------------
@@ -196,11 +231,9 @@
     printf("Initialize mp3 Codec...\r\n");
     mp3.initialize();
     printf("mp3 Codec is initialized\r\n");
-
-
+    
     mp3.setVolume(VOLUME);
-    // pc.attach(&setVolume);
-    Thread t(setVolume , NULL, osPriorityNormal, (DEFAULT_STACK_SIZE));
+    Thread t(SetVolume , NULL, osPriorityNormal, (DEFAULT_STACK_SIZE));
 
     // ------------------------------------------------------------------------
     //  Play mp3 file
@@ -232,43 +265,7 @@
                     sprintf(str, "%s/%s", DIR_NAME, p->d_name);
                     printf("Path %s\r\n", str);
 
-                    FILE *song;
-
-                    static char array[2048];
-
-                    song = fopen(str, "rb");
-                    //song = fopen("/sd/Musik/01.mp3", "rb");
-
-                    if (!song) {
-                        printf("Couldn't open %s\r\n", str);
-                        continue;
-                        //exit(1);
-                    }
-
-                    int count = 0;
-                    bool test = true;
-
-                    while (!feof(song) && !next) {
-                        int n=fread(&array, 1, sizeof(array), song);
-                        while (mp3.bufferFree() < n) {
-                            led2 = 1 ;
-                            Thread::wait(100) ;
-                            led2 = 0 ;
-                        }
-                        mp3.bufferPutStream(array,n);
-                        if (count > 2 && test) {
-                            test = false;
-                            mp3.play();
-                        }
-                        count++;
-                    }
-                    if (next) {
-                        mp3.stop();
-                        next = false;
-                    } else {
-                        mp3.terminateStream();
-                    }
-                    fclose(song);  //close the file
+                    playFile(str) ; /*** Play the mp3 file ***/
 
                     printf("End of song.\r\n");
                     wait(1.0);