Final code

Dependencies:   C12832 PinDetect USBHost mbed wave_player

Fork of MusicPlayer_ThaoLeMinh by Yan Long

Revision:
3:82b68ab94e3e
Parent:
2:087f60c3c445
Child:
4:314af6e95daf
--- a/main.cpp	Mon Dec 12 04:51:19 2016 +0000
+++ b/main.cpp	Tue Dec 13 10:00:51 2016 +0000
@@ -3,6 +3,7 @@
 #include "PinDetect.h"
 #include "USBHostMSD.h"
 #include "C12832.h"
+#include "ReadTime.h"
 #include <vector>
 #include <string>
 
@@ -28,6 +29,8 @@
 PinDetect pb4(p15); //joydown
 PinDetect pb5(p14);//center
 
+Timer timer; //timer
+
 AnalogOut DACout(p18); //set up speaker
 wave_player waver(&DACout); //set up wave player library
 int pos = 0; // index of the song
@@ -35,6 +38,7 @@
  
 bool playing = false; //variable for pause/play 
 bool firstplay = false; //variable for first play
+int total_time = 0;
 vector<string> filenames; //filenames are stored in a vector string
 
 void read_file_names(char *dir) // function that reads in file names from sd cards
@@ -113,6 +117,32 @@
     }
 }
 
+void show_information(const void *argv) {
+    while (1) {
+        int t = timer.read();//playing time
+        int left_time = total_time - t;//left time
+        int bar_len = 21 * t / total_time;//length of bar
+        lcd.cls();
+        lcd.locate(0,0);
+
+        lcd.locate(100,0);
+        lcd.printf("vol: %d", 15 - vol);
+        lcd.locate(0,20);
+        for (int i = 0; i < bar_len; i++) {
+            lcd.printf("_");
+        }
+        int min1 = t / 60;
+        int sec1 = t % 60;
+        int min2 = left_time / 60;
+        int sec2 = left_time % 60;
+        lcd.locate(0,18);
+        lcd.printf("%02d:%02d", min1, sec1);
+        lcd.locate(105,18);
+        lcd.printf("%02d:%02d", min2, sec2);
+        Thread::wait(500);
+    }
+}
+
 
 int main()
 {
@@ -147,7 +177,6 @@
     while(!msc.connect()) {
         lcd.locate(0,0);
         lcd.printf("Insert USB");
-        wait(.5);
     }    
     // Read the songs array, please note that your wav files have to be stored in the same directory
     read_file_names("/msc/music_wav");
@@ -163,10 +192,13 @@
             string fname = a + songname; //retrieves the file name
             FILE *wave_file; 
             lcd.cls();
-            lcd.locate(0,1);
-            lcd.printf("Playing: %s",fname.c_str());
+            total_time = (int)ReadTime(fname.c_str());
             wave_file = fopen(fname.c_str(),"r"); //opens the music file
+            Thread thread(show_information);
+            timer.start();
             waver.play(wave_file);
+            timer.stop();   
+            timer.reset();
             fclose(wave_file);
         }
         firstplay = false;