Yan commited

Dependencies:   C12832 PinDetect USBHost mbed wave_player

Fork of MusicPlayer_ThaoLeMinh by Thao Le Minh

Files at this revision

API Documentation at this revision

Comitter:
LyCosine
Date:
Tue Dec 13 10:00:51 2016 +0000
Parent:
2:087f60c3c445
Commit message:
Yan revised

Changed in this revision

ReadTime.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ReadTime.h	Tue Dec 13 10:00:51 2016 +0000
@@ -0,0 +1,85 @@
+#include<stdio.h>
+#include<string.h>
+#include<stdlib.h>
+
+char ToHex(int num) {
+    if (num < 0 && num > 15) {
+        return '!';
+    }
+    else if (num < 10) {
+        return num + 48;
+    }
+    else {
+        switch (num) {
+        case 10: return 'A';
+        case 11: return 'B';
+        case 12: return 'C';
+        case 13: return 'D';
+        case 14: return 'E';
+        case 15: return 'F';
+        }
+    }
+    return '?';
+}
+
+int ToDec(char *str) {
+    int num = 0;
+    bool flag = true;
+    int len = strlen(str);
+    for (int i = 0; i < len; i++) {
+        if (flag && str[i] == '0') {
+            continue;
+        }
+        flag = false;
+        if (str[i] > '9') {
+            num = 16 * num + (str[i] - 55);
+        }
+        else {
+            num = 16 * num + (str[i] - '0');
+        }
+    }
+    return num;
+}
+
+double ReadTime(const char *path) {
+    char size[9];
+    char byteRate[9];
+    unsigned char ch[100];
+    FILE *file;
+
+    file = fopen(path,"rb");
+    
+    for (int i = 0; i < 58; i++) {
+        ch[i] = fgetc(file);
+    }
+
+    for (int i = 7, j = 0; i >= 4; i--, j += 2) {
+        if (ch[i] < 16) {
+            size[j] = '0';
+            size[j + 1] = ToHex(ch[i]);
+        }
+        else {
+            size[j] = ToHex(ch[i] / 16);
+            size[j + 1] = ToHex(ch[i] % 16);
+        }
+    }
+    size[8] = '\0';
+
+    for (int i = 31, j = 0; i > 27; i--, j += 2) {
+        if (ch[i] < 16) {
+            byteRate[j] = '0';
+            byteRate[j + 1] = ToHex(ch[i]);
+        }
+        else {
+            byteRate[j] = ToHex(ch[i] / 16);
+            byteRate[j + 1] = ToHex(ch[i] % 16);
+        }
+    }
+    byteRate[8] = '\0';
+
+    int n_size = ToDec(size);
+    int n_byteRate = ToDec(byteRate);
+    double time = (double)n_size / (double)n_byteRate;
+    
+    return time;
+}
\ No newline at end of file
--- 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;