Final code

Dependencies:   C12832 PinDetect USBHost mbed wave_player

Fork of MusicPlayer_ThaoLeMinh by Yan Long

Files at this revision

API Documentation at this revision

Comitter:
thaolmk54
Date:
Thu Dec 15 13:49:06 2016 +0000
Parent:
3:82b68ab94e3e
Commit message:
Final code_Thao

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Dec 13 10:00:51 2016 +0000
+++ b/main.cpp	Thu Dec 15 13:49:06 2016 +0000
@@ -25,37 +25,37 @@
 //Joystick controller
 PinDetect pb1(p13);//joyleft
 PinDetect pb2(p16);//joyright
-PinDetect pb3(p12); //joyup
-PinDetect pb4(p15); //joydown
+PinDetect pb3(p12);//joyup
+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
-int vol = 0; // volume controller
+int pos = 0; // song index
+int vol = 0; // volume level
  
-bool playing = false; //variable for pause/play 
+bool playing = false; //controlling playing state
 bool firstplay = false; //variable for first play
 int total_time = 0;
-vector<string> filenames; //filenames are stored in a vector string
+vector<string> songnames; //array to store name of songs
 
 void read_file_names(char *dir) // function that reads in file names from sd cards
 {
     DIR *dp;
     struct dirent *dirp;
     dp = opendir(dir);
-    //read all directory and file names in current directory into filename vector
+    //read all directory and then put file paths into a vector
     while((dirp = readdir(dp)) != NULL) {
-        filenames.push_back(string(dirp->d_name));
+        songnames.push_back(string(dirp->d_name));
     }
 }
 
-//interrupt handler for pb1 
-void pb1_hit_callback (void)
+//interrupt handler for shifting farward to next song 
+void pb1_next_song (void)
 { 
-    int l = filenames.size();
+    int l = songnames.size();
     if (pos < (l-1)) {
         pos++;
     } else if (pos == (l-1)) {
@@ -66,10 +66,10 @@
     led3 = 0;
     led4 = 0;
 }
-//interrupt handler for pb2
-void pb2_hit_callback (void)
+//interrupt handler for shifting back to privious song 
+void pb2_previous_song (void)
 {
-    int l = filenames.size();
+    int l = songnames.size();
     if (pos > 0) {
         pos--;
     } else if (pos == 0 ) {
@@ -81,8 +81,8 @@
     led4 = 0;
 }
 
-//interrupt handler for pb3
-void pb3_hit_callback (void){
+//interrupt handler for volume up
+void pb3_volume_up (void){
     vol = (vol+1) % 16;
     led1 = 0;
     led2 = 0;
@@ -90,8 +90,8 @@
     led4 = 0;
 }
 
-//interrupt handler for pb4 
-void pb4_hit_callback (void){
+//interrupt handler for volume down 
+void pb4_volume_down (void){
     if (vol > 1) {
         vol = (vol-1) % 16;
     }
@@ -101,8 +101,8 @@
     led4 = 1;
 }
 
-//interrupt handler for 3rd pushbutton
-void pb5_hit_callback (void)
+//interrupt handler for play/pause
+void pb5_play_pause (void)
 {
     //this interrupt handler changes the play to pause mode or vice versa
     if (playing == false && firstplay == false) {
@@ -110,7 +110,7 @@
         firstplay = true;
         r = 1;
     } else if (playing == true) {
-        string songname = filenames[pos];
+        string songname = songnames[pos];
         playing = false;
         firstplay = false;
         g = 1;
@@ -158,13 +158,13 @@
     pb5.mode(PullDown);
 
     wait(.01);
-    // Setup Interrupt callback functions for a pb hit
-    pb1.attach_deasserted(&pb1_hit_callback);
-    pb2.attach_deasserted(&pb2_hit_callback);
-    pb3.attach_deasserted(&pb3_hit_callback);
-    pb4.attach_deasserted(&pb4_hit_callback);
-    pb5.attach_deasserted(&pb5_hit_callback);
-    // Start sampling pb inputs using interrupts
+    // Setup Interrupt callback functions for 5-way joystick
+    pb1.attach_deasserted(&pb1_next_song);
+    pb2.attach_deasserted(&pb2_previous_song);
+    pb3.attach_deasserted(&pb3_volume_up);
+    pb4.attach_deasserted(&pb4_volume_down);
+    pb5.attach_deasserted(&pb5_play_pause);
+    // Start sampling 
     pb1.setSampleFrequency();
     pb2.setSampleFrequency();
     pb3.setSampleFrequency();
@@ -187,7 +187,7 @@
         lcd.printf("Press joystick to play");
         //while pb3 is low, press fire button to start playing a song
         while(playing == true && firstplay == false) { 
-            string songname = filenames[pos];
+            string songname = songnames[pos];
             string a = "/msc/music_wav/";
             string fname = a + songname; //retrieves the file name
             FILE *wave_file;