Yan commited

Dependencies:   C12832 PinDetect USBHost mbed wave_player

Fork of MusicPlayer_ThaoLeMinh by Thao Le Minh

Revision:
1:45d8f6557ff8
Parent:
0:4b3056d3c684
Child:
2:087f60c3c445
--- a/main.cpp	Thu Mar 13 00:23:46 2014 +0000
+++ b/main.cpp	Mon Mar 17 18:48:13 2014 +0000
@@ -6,7 +6,8 @@
 #include "Speaker.h"
 #include <vector>
 #include <string>
- 
+
+//Set up LEDs
 DigitalOut myled(LED1);
 DigitalOut myled2(LED2);
 DigitalOut myled3(LED3);
@@ -16,29 +17,22 @@
 
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
 TextLCD lcd(p9, p10, p11, p12, p13, p14); // rs, e, d4-d7
-DigitalIn sdDetect(p17);
+DigitalIn sdDetect(p17); // Set up a pin for SD Card Detect
 
-PinDetect pb1(p28); // up shift
-PinDetect pb2(p29); // down shift
-PinDetect pb3(p30); // pause
-PinDetect pb4(p27); // volume or play
+PinDetect pb1(p28); // pb forup shift
+PinDetect pb2(p29); // pb fordown shift
+PinDetect pb3(p30); // pb for pause
+PinDetect pb4(p27); // pb for volume or play
 
 
-AnalogOut DACout(p18);
-wave_player waver(&DACout);
-int pos = 0;
-int xx = -1;
-int vol = 0;
-//extern "C" {
-//int SD_inserted();
-//}
-//extern "C" virtual disk_initialize();
-/*int disk_initialize();
-}*/
-bool playing = false;
-bool old_playing = false;
+AnalogOut DACout(p18); //set up speaker
+wave_player waver(&DACout); //set up wave player library
+int pos = 0; // index of the song
+int vol = 0; // volume controller
+ 
+bool playing = false; //variable for pause/play since we only have 1 pb for that
 vector<string> filenames; //filenames are stored in a vector string
-void read_file_names(char *dir)
+void read_file_names(char *dir) // function that reads in file names from sd cards
 {
     DIR *dp;
     struct dirent *dirp;
@@ -48,9 +42,10 @@
         filenames.push_back(string(dirp->d_name));
     }
 }
-
+//interrupt handler for pb1 
 void pb1_hit_callback (void)
 {
+    // it checks for the total number of songs in the sd card..then increments the index until it reaches the last one, then resets to 0
     int l = filenames.size();
     if (pos < (l-1)) {
         pos++;
@@ -61,10 +56,12 @@
     unsigned index = songname.find(".wav");
     songname = songname.substr(0,index);
     lcd.cls();
-    lcd.printf(songname.c_str());
+    lcd.printf(songname.c_str()); //it clears screen and then sets the new index song in the lcd display
 }
+//interrupt handler for pb2
 void pb2_hit_callback (void)
 {
+    //does opposite of pb1..moves the index down..from 2nd song to 1st song via changing the index number and then calling in the vector
     int l = filenames.size();
     if (pos > 0) {
         pos--;
@@ -77,23 +74,27 @@
     lcd.cls();
     lcd.printf(songname.c_str());
 }
-
+//interrupt handler for 3rd pushbutton
 void pb3_hit_callback (void)
 {
+    //this interrupt handler changes the play to pause mode or vice versa
+    //this is done using the boolean playing
     if (playing == false) {
-        playing = true;
-     //   old_playing = false;
+        playing = true; 
     } else if (playing == true) {
         lcd.cls();
-        playing = false;
-       // old_playing = true;
+        playing = false; 
         string songname = filenames[pos];
         unsigned index = songname.find(".wav");
         songname = songname.substr(0,index);
         lcd.printf(songname.c_str());
     }
 }
+//interrupt handler for pb4
 void pb4_hit_callback (void){
+    // this pb changes the volume by lowering the volume until it reaches 0. then it resets to the max volume
+    // the volume range has been divided into 16 possible ranges. and hence, it toggles through those 16 values
+    // this only changes the variable vol, which is then used in the wave player file to actually adjust the volume
     vol = (vol+1) % 16;
 }
 
@@ -120,30 +121,27 @@
     pb3.setSampleFrequency();
     pb4.setSampleFrequency();
     lcd.cls();
+    //detects whethere there is a SD card or not.. if not then it prints and informs the user
     while(sdDetect ==0) {
         lcd.locate(0,0);
         lcd.printf("Insert SD Card");
         wait(.5);
-    }   
-    //sd.disk_initialize();
-    lcd.cls();
-    //SD_inserted();
-    //lcd.locate(0,0);
-    //lcd.printf("Insert SD Card");
+    }    
+    lcd.cls(); 
     wait(.5);
     sd.disk_initialize();
     read_file_names("/sd/Music");
     while(1) {
         //while pb3 is low, then we can start playing the song
-        while(playing == true) {
+        while(playing == true) { //we have 2 while loops..one while loop makes sure the music player is always on, the other one is for the song
             string songname = filenames[pos];
             string a = "/sd/Music/";
-            string fname = a + songname;
-            FILE *wave_file;
+            string fname = a + songname; //retrieves the file name
+            FILE *wave_file; 
             lcd.locate(0,1);
             lcd.printf("Now Playing");
-            wave_file = fopen(fname.c_str(),"r");
-            waver.play(wave_file);
+            wave_file = fopen(fname.c_str(),"r"); //opens the music file
+            waver.play(wave_file); //plays the music file
             fclose(wave_file);
         }
     }