Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PinDetect SDFileSystem Speaker TextLCD mbed wave_player
Fork of musicplayer by
main.cpp
00001 #include "mbed.h" 00002 #include "SDFileSystem.h" 00003 #include "wave_player.h" 00004 #include "TextLCD.h" 00005 #include "PinDetect.h" 00006 #include "Speaker.h" 00007 #include <vector> 00008 #include <string> 00009 00010 //Set up LEDs 00011 DigitalOut myled(LED1); 00012 DigitalOut myled2(LED2); 00013 DigitalOut myled3(LED3); 00014 DigitalOut myled4(LED4); 00015 00016 using namespace std; 00017 00018 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card 00019 TextLCD lcd(p9, p10, p11, p12, p13, p14); // rs, e, d4-d7 00020 DigitalIn sdDetect(p17); // Set up a pin for SD Card Detect 00021 00022 PinDetect pb1(p28); // pb forup shift 00023 PinDetect pb2(p29); // pb fordown shift 00024 PinDetect pb3(p30); // pb for pause 00025 PinDetect pb4(p27); // pb for volume or play 00026 00027 00028 AnalogOut DACout(p18); //set up speaker 00029 wave_player waver(&DACout); //set up wave player library 00030 int pos = 0; // index of the song 00031 int vol = 0; // volume controller 00032 00033 bool playing = false; //variable for pause/play since we only have 1 pb for that 00034 vector<string> filenames; //filenames are stored in a vector string 00035 void read_file_names(char *dir) // function that reads in file names from sd cards 00036 { 00037 DIR *dp; 00038 struct dirent *dirp; 00039 dp = opendir(dir); 00040 //read all directory and file names in current directory into filename vector 00041 while((dirp = readdir(dp)) != NULL) { 00042 filenames.push_back(string(dirp->d_name)); 00043 } 00044 } 00045 //interrupt handler for pb1 00046 void pb1_hit_callback (void) 00047 { 00048 // 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 00049 int l = filenames.size(); 00050 if (pos < (l-1)) { 00051 pos++; 00052 } else if (pos == (l-1)) { 00053 pos = 0; 00054 } 00055 string songname = filenames[pos]; 00056 unsigned index = songname.find(".wav"); 00057 songname = songname.substr(0,index); 00058 lcd.cls(); 00059 lcd.printf(songname.c_str()); //it clears screen and then sets the new index song in the lcd display 00060 } 00061 //interrupt handler for pb2 00062 void pb2_hit_callback (void) 00063 { 00064 //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 00065 int l = filenames.size(); 00066 if (pos > 0) { 00067 pos--; 00068 } else if (pos == 0 ) { 00069 pos = l-1; 00070 } 00071 string songname = filenames[pos]; 00072 unsigned index = songname.find(".wav"); 00073 songname = songname.substr(0,index); 00074 lcd.cls(); 00075 lcd.printf(songname.c_str()); 00076 } 00077 //interrupt handler for 3rd pushbutton 00078 void pb3_hit_callback (void) 00079 { 00080 //this interrupt handler changes the play to pause mode or vice versa 00081 //this is done using the boolean playing 00082 if (playing == false) { 00083 playing = true; 00084 } else if (playing == true) { 00085 lcd.cls(); 00086 playing = false; 00087 string songname = filenames[pos]; 00088 unsigned index = songname.find(".wav"); 00089 songname = songname.substr(0,index); 00090 lcd.printf(songname.c_str()); 00091 } 00092 } 00093 //interrupt handler for pb4 00094 void pb4_hit_callback (void){ 00095 // this pb changes the volume by lowering the volume until it reaches 0. then it resets to the max volume 00096 // the volume range has been divided into 16 possible ranges. and hence, it toggles through those 16 values 00097 // this only changes the variable vol, which is then used in the wave player file to actually adjust the volume 00098 vol = (vol+1) % 16; 00099 } 00100 00101 int main() 00102 { 00103 sdDetect.mode(PullUp); 00104 wait(.1); 00105 //wait after pulling up the sd card, 00106 // read file names into vector of strings 00107 pb1.mode(PullUp); 00108 pb2.mode(PullUp); 00109 pb3.mode(PullUp); 00110 pb4.mode(PullUp); 00111 // Delay for initial pullup to take effect 00112 wait(.01); 00113 // Setup Interrupt callback functions for a pb hit 00114 pb1.attach_deasserted(&pb1_hit_callback); 00115 pb2.attach_deasserted(&pb2_hit_callback); 00116 pb3.attach_deasserted(&pb3_hit_callback); 00117 pb4.attach_deasserted(&pb4_hit_callback); 00118 // Start sampling pb inputs using interrupts 00119 pb1.setSampleFrequency(); 00120 pb2.setSampleFrequency(); 00121 pb3.setSampleFrequency(); 00122 pb4.setSampleFrequency(); 00123 lcd.cls(); 00124 //detects whethere there is a SD card or not.. if not then it prints and informs the user 00125 while(sdDetect ==0) { 00126 lcd.locate(0,0); 00127 lcd.printf("Insert SD Card"); 00128 wait(.5); 00129 } 00130 lcd.cls(); 00131 wait(.5); 00132 sd.disk_initialize(); 00133 read_file_names("/sd/Music"); 00134 while(1) { 00135 //while pb3 is low, then we can start playing the song 00136 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 00137 string songname = filenames[pos]; 00138 string a = "/sd/Music/"; 00139 string fname = a + songname; //retrieves the file name 00140 FILE *wave_file; 00141 lcd.locate(0,1); 00142 lcd.printf("Now Playing"); 00143 wave_file = fopen(fname.c_str(),"r"); //opens the music file 00144 waver.play(wave_file); //plays the music file 00145 fclose(wave_file); 00146 } 00147 } 00148 }
Generated on Tue Jul 12 2022 21:11:42 by
1.7.2
