mp3 player hw gatech
Dependencies: PinDetect SDFileSystem TextLCD mbed wave_player
main.cpp
00001 00002 #include "mbed.h" 00003 #include "SDFileSystem.h" 00004 #include "wave_player.h" 00005 #include "TextLCD.h" 00006 #include "PinDetect.h" 00007 #include <string> 00008 00009 #include <vector> 00010 00011 SDFileSystem sd(p5, p6, p7, p8,p9, "sd"); //SD card 00012 TextLCD myLCD(p22, p23, p24, p25, p26, p27); //TextLCD 00013 AnalogOut DACout(p18); 00014 //AnalogOut DACout(p20); 00015 00016 void read_file_names(char *); 00017 void volumePressed(); 00018 wave_player waver(&DACout); 00019 vector<string> filenames; //filenames are stored in a vector string 00020 PinDetect pb1(p28); // Up button - scrolls up the list 00021 PinDetect pb2(p29); // Down button - scrolls down the list 00022 PinDetect pb3(p30); // Play/Pause- selects and plays or pauses the filename 00023 PinDetect pb4(p15); 00024 int volatile Scrollup=0; // heat to temp 00025 int volatile Scrolldown=0;// cool to temp 00026 int volatile total=0; // heat or cool mpde 00027 bool select=false; 00028 bool *selectptr=&select; 00029 00030 00031 void pb1_hit_callback (void) 00032 { 00033 Scrollup++; 00034 00035 00036 if(Scrollup >= total) 00037 Scrollup=0; 00038 00039 } 00040 // Callback routine is interrupt activated by a debounced pb2 hit 00041 void pb2_hit_callback (void) 00042 { 00043 00044 Scrollup--; 00045 if(Scrollup < 0) 00046 Scrollup = total-1; 00047 } 00048 00049 // Callback routine is interrupt activated by a debounced pb3 hit 00050 00051 void pb3_hit_callback (void) 00052 { 00053 select=!select; 00054 } 00055 // Callback routine is interrupt activated by a debounced pb3 hit 00056 void pb4_hit_callback (void) 00057 { 00058 00059 volumePressed(); 00060 00061 00062 00063 } 00064 int main() 00065 { 00066 // Use internal pullups for the three pushbuttons 00067 pb1.mode(PullUp); 00068 pb2.mode(PullUp); 00069 pb3.mode(PullUp); 00070 pb4.mode(PullUp); 00071 // Delay for initial pullup to take effect 00072 wait(.01); 00073 //Setup Interrupt callback functions for a pb hit 00074 pb1.attach_deasserted(&pb1_hit_callback); 00075 pb2.attach_deasserted(&pb2_hit_callback); 00076 pb3.attach_deasserted(&pb3_hit_callback); 00077 pb4.attach_deasserted(&pb4_hit_callback); 00078 // Start sampling pb inputs using interrupts 00079 pb1.setSampleFrequency(); 00080 pb2.setSampleFrequency(); 00081 pb3.setSampleFrequency(); 00082 pb4.setSampleFrequency(); 00083 00084 00085 // myLCD.printf("\n\n\nHello, wave world!\n"); 00086 // wave_file = fopen("/sd/myMusic", "r"); 00087 bool SDcard=0; 00088 while(!SDcard) 00089 { SDcard=sd.sd_inserted(); 00090 if( SDcard==false){ 00091 myLCD.locate(0,0); 00092 myLCD.printf("Insert SDCARD"); 00093 } 00094 else 00095 myLCD.cls(); 00096 } 00097 FILE *wave_file; 00098 read_file_names("/sd/myMusic"); 00099 string directory; 00100 directory="/sd/myMusic"; 00101 string song; 00102 00103 00104 while(1) { 00105 //int pos=size((*it).c_str())-4; 00106 int index=0; 00107 index=Scrollup; 00108 int len=filenames[index].size(); 00109 00110 myLCD.cls(); 00111 myLCD.locate(0,0); 00112 myLCD.printf("%s",filenames[index].substr(0,len-4)); 00113 00114 if(select==true) { 00115 song = directory + "/" + filenames[index]; 00116 wave_file = fopen(song.c_str(), "r"); 00117 myLCD.locate(0,1); 00118 myLCD.printf("%s","Now Playing"); 00119 waver.play(wave_file,selectptr); 00120 select=false; 00121 fclose(wave_file); 00122 00123 } 00124 else 00125 //myLCD.cls(); 00126 //myLCD.printf("%d",Scrollup); 00127 //myLCD.printf("%s","now playing"); 00128 //waver.play(wave_file); 00129 wait(0.3); 00130 } 00131 00132 00133 00134 00135 00136 } 00137 00138 00139 void read_file_names(char *dir) 00140 00141 { 00142 00143 DIR *dp; 00144 00145 struct dirent *dirp; 00146 00147 dp = opendir(dir); 00148 00149 //read all directory and file names in current directory into filename vector 00150 while((dirp = readdir(dp)) != NULL) { 00151 filenames.push_back(string(dirp->d_name)); 00152 total++; 00153 } 00154 } 00155 void volumePressed(){ 00156 00157 00158 00159 int *volumeMod=waver.getVolumeMod(); 00160 00161 *volumeMod = *volumeMod + 1; 00162 00163 if (*volumeMod >= 16) 00164 00165 *volumeMod = 0; 00166 } 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176
Generated on Tue Jul 12 2022 23:28:48 by 1.7.2