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: 4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player
main.cpp
00001 #include "mbed.h" 00002 #include <vector> 00003 #include "SDFileSystem.h" 00004 #include "uLCD_4DGL.h" 00005 #include <string> 00006 #include "PinDetect.h" 00007 #include "wave_player.h" 00008 #include "math.h" 00009 AnalogOut DACout(p18); 00010 wave_player waver(&DACout); 00011 DigitalOut myled(LED1); 00012 DigitalOut myled2(LED4); 00013 SDFileSystem sd(p5, p6, p7, p8,p14, "sd"); //SD card 00014 uLCD_4DGL lcd(p28, p27, p30); 00015 00016 vector<string> filenames; //filenames are stored in a vector string 00017 vector<string> CopyMatSongs; 00018 string noextend; 00019 int stringsize; 00020 void read_file_names(char *dir) 00021 { 00022 DIR *dp; 00023 struct dirent *dirp; 00024 dp = opendir(dir); 00025 //read all directory and file names in current directory into filename vector 00026 while((dirp = readdir(dp)) != NULL) { 00027 filenames.push_back(string(dirp->d_name)); 00028 } 00029 } 00030 int test; 00031 PinDetect A(p9); 00032 PinDetect B(p10); 00033 PinDetect D(p11); 00034 PinDetect C(p12); 00035 int count=0; 00036 int array17[100]; 00037 int array4[100]; 00038 string DummyMatrix[] = {"a","a","a","a"}; 00039 int countstate = 0; 00040 int volatile songnum = 0 ; 00041 int numberofsongs; 00042 bool player = false; 00043 bool *playpoint = &player; 00044 string y =""; 00045 FILE *wave_file; 00046 00047 int placement = rand()%4; 00048 int randsong = rand()%4; 00049 00050 void D_callback (void) 00051 { player = !player; 00052 if(randsong != placement) 00053 { 00054 myled2 = 1; 00055 wait(1); 00056 myled2 = 0; 00057 DummyMatrix[0] = CopyMatSongs[array17[0+count]]; 00058 DummyMatrix[1] = CopyMatSongs[array17[1+count]]; 00059 DummyMatrix[2] = CopyMatSongs[array17[2+count]]; 00060 DummyMatrix[3] = CopyMatSongs[array17[3+count]]; 00061 placement = rand()%4; 00062 randsong = rand()%4; 00063 countstate=0; 00064 count++; 00065 } 00066 else{ 00067 DummyMatrix[0] = CopyMatSongs[array17[0+count]]; 00068 DummyMatrix[1] = CopyMatSongs[array17[1+count]]; 00069 DummyMatrix[2] = CopyMatSongs[array17[2+count]]; 00070 DummyMatrix[3] = CopyMatSongs[array17[3+count]]; 00071 placement = rand()%4; 00072 randsong = rand()%4; 00073 countstate=0; 00074 count++; 00075 } 00076 00077 00078 } 00079 void A_callback (void) 00080 { 00081 player = !player; 00082 00083 if(randsong == 0 && placement == 0) 00084 { 00085 myled2 = 1; 00086 wait(1); 00087 myled2 = 0; 00088 DummyMatrix[0] = CopyMatSongs[array17[4+count]]; 00089 DummyMatrix[1] = CopyMatSongs[array17[5+count]]; 00090 DummyMatrix[2] = CopyMatSongs[array17[6+count]]; 00091 DummyMatrix[3] = CopyMatSongs[array17[7+count]]; 00092 placement = rand()%4; 00093 randsong = rand()%4; 00094 countstate=0; 00095 count++; 00096 } 00097 else{ 00098 DummyMatrix[0] = CopyMatSongs[array17[4+count]]; 00099 DummyMatrix[1] = CopyMatSongs[array17[5+count]]; 00100 DummyMatrix[2] = CopyMatSongs[array17[6+count]]; 00101 DummyMatrix[3] = CopyMatSongs[array17[7+count]]; 00102 placement = rand()%4; 00103 randsong = rand()%4; 00104 countstate=0; 00105 count++; 00106 } 00107 00108 } 00109 00110 00111 00112 void B_callback(void) 00113 { player = !player; 00114 if(randsong ==1 && placement == 1) 00115 { 00116 myled2 = 1; 00117 wait(1); 00118 myled2 = 0; 00119 DummyMatrix[0] = CopyMatSongs[array17[8+count]]; 00120 DummyMatrix[1] = CopyMatSongs[array17[9+count]]; 00121 DummyMatrix[2] = CopyMatSongs[array17[10+count]]; 00122 DummyMatrix[3] = CopyMatSongs[array17[11+count]]; 00123 placement = rand()%4; 00124 randsong = rand()%4; 00125 countstate=0; 00126 count++; 00127 } 00128 else{ 00129 DummyMatrix[0] = CopyMatSongs[array17[8+count]]; 00130 DummyMatrix[1] = CopyMatSongs[array17[9+count]]; 00131 DummyMatrix[2] = CopyMatSongs[array17[10+count]]; 00132 DummyMatrix[3] = CopyMatSongs[array17[11+count]]; 00133 placement = rand()%4; 00134 randsong = rand()%4; 00135 countstate=0; 00136 count++; 00137 } 00138 } 00139 00140 00141 00142 00143 void C_callback (void) 00144 { 00145 player = !player; 00146 if(randsong == 2 && placement==2) 00147 { 00148 myled2 = 1; 00149 wait(1); 00150 myled2 = 0; 00151 DummyMatrix[0] = CopyMatSongs[array17[12+count]]; 00152 DummyMatrix[1] = CopyMatSongs[array17[13+count]]; 00153 DummyMatrix[2] = CopyMatSongs[array17[14+count]]; 00154 DummyMatrix[3] = CopyMatSongs[array17[15+count]]; 00155 placement = rand()%4; 00156 randsong = rand()%4; 00157 countstate=0; 00158 count++; 00159 } 00160 else{ 00161 DummyMatrix[0] = CopyMatSongs[array17[12+count]]; 00162 DummyMatrix[1] = CopyMatSongs[array17[13+count]]; 00163 DummyMatrix[2] = CopyMatSongs[array17[14+count]]; 00164 DummyMatrix[3] = CopyMatSongs[array17[15+count]]; 00165 placement = rand()%4; 00166 randsong = rand()%4; 00167 countstate=0; 00168 count++; 00169 } 00170 } 00171 00172 int main() 00173 00174 { 00175 srand(2); 00176 00177 00178 for(int i = 0; i < 100; i++) 00179 { 00180 00181 int x = rand()%4; 00182 00183 array4[i] = x; 00184 00185 printf("%d\n\r ", array4[i]); 00186 } 00187 00188 for(int i = 0; i < 100; i++) 00189 { 00190 00191 int x = rand()%17; 00192 00193 array17[i] = x; 00194 00195 printf("%d\n\r ", array17[i]); 00196 } 00197 while(1) 00198 { 00199 00200 /*if (sd.cdcheck()== false) 00201 { 00202 lcd.cls(); 00203 lcd.printf("Please Insert SD Card\n"); 00204 wait(.1); 00205 } 00206 else 00207 { 00208 lcd.cls(); 00209 lcd.printf("thank you"); 00210 lcd.locate(0,1); 00211 lcd.printf("restart system"); 00212 wait(.1); 00213 */ 00214 00215 A.mode(PullUp); 00216 B.mode(PullUp); 00217 D.mode(PullUp); 00218 C.mode(PullUp); 00219 wait(.01); 00220 00221 // Setup Interrupt callback functions for a pb hit 00222 A.attach_deasserted(&A_callback); 00223 B.attach_deasserted(&B_callback); 00224 D.attach_deasserted(&D_callback); 00225 C.attach_deasserted(&C_callback); 00226 00227 // Start sampling pb inputs using interrupts 00228 A.setSampleFrequency(); 00229 B.setSampleFrequency(); 00230 D.setSampleFrequency(); 00231 C.setSampleFrequency(); 00232 00233 00234 // via interrupts that activate the callback counter function 00235 00236 00237 wait(.001); 00238 // read file names into vector of strings 00239 read_file_names("/sd/myMusic"); 00240 numberofsongs= filenames.size(); 00241 printf("%d",numberofsongs); 00242 // print filename strings from vector using an iterator 00243 /*for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) 00244 { 00245 printf("%s\n\r",(*it).c_str()); 00246 00247 }*/ 00248 00249 //this is what pulls in all file names and strips off .wav and puts it into vector 00250 // name of vector is filename[index of i] 00251 00252 00253 00254 //printf("%s CopyMatsongs:", CopyMatSongs); 00255 for(int i=0; i < filenames.size(); i++) 00256 { 00257 noextend = filenames[i]; 00258 filenames[i] = noextend.substr(0, noextend.rfind(".")); 00259 printf("%s\n\r", filenames[i]); 00260 } 00261 CopyMatSongs = filenames; 00262 //printf("%s\n\r", CopyMatSongs); 00263 00264 printf("%s\n\r",DummyMatrix[0]); 00265 00266 for(int aa = 0; aa <4; aa++) 00267 { 00268 DummyMatrix[array4[aa]] = CopyMatSongs[array17[aa]]; 00269 printf("%s \n\r", DummyMatrix[aa]); 00270 } 00271 00272 00273 00274 00275 while(1) 00276 { 00277 if (countstate == 0) 00278 { 00279 00280 00281 lcd.cls(); 00282 if (placement == 0 && randsong==0) 00283 { 00284 test = 0; 00285 lcd.locate(0,0); 00286 lcd.printf("%s",DummyMatrix[randsong].substr(0, noextend.rfind("."))); 00287 lcd.locate(0,1); 00288 lcd.printf("%s",DummyMatrix[randsong+1].substr(0, noextend.rfind("."))); 00289 lcd.locate(0,2); 00290 lcd.printf("%s",DummyMatrix[randsong+2].substr(0, noextend.rfind("."))); 00291 lcd.locate(0,3); 00292 lcd.printf("%s",DummyMatrix[randsong+3].substr(0, noextend.rfind("."))); 00293 } 00294 00295 if(placement == 1 && randsong == 1) 00296 { 00297 test =1; 00298 lcd.locate(0,0); 00299 lcd.printf("%s",DummyMatrix[randsong-1].substr(0, noextend.rfind("."))); 00300 lcd.locate(0,1); 00301 lcd.printf("%s",DummyMatrix[randsong].substr(0, noextend.rfind("."))); 00302 lcd.locate(0,2); 00303 lcd.printf("%s",DummyMatrix[randsong+1].substr(0, noextend.rfind("."))); 00304 lcd.locate(0,3); 00305 lcd.printf("%s",DummyMatrix[randsong+2].substr(0, noextend.rfind("."))); 00306 } 00307 if(placement == 2 &&randsong == 2) 00308 { 00309 test = 2; 00310 lcd.locate(0,0); 00311 lcd.printf("%s",DummyMatrix[randsong-2].substr(0, noextend.rfind("."))); 00312 lcd.locate(0,1); 00313 lcd.printf("%s",DummyMatrix[randsong-1].substr(0, noextend.rfind("."))); 00314 lcd.locate(0,2); 00315 lcd.printf("%s",DummyMatrix[randsong].substr(0, noextend.rfind("."))); 00316 lcd.locate(0,3); 00317 lcd.printf("%s",DummyMatrix[randsong+1].substr(0, noextend.rfind("."))); 00318 } 00319 else 00320 { 00321 { 00322 test=3; 00323 lcd.locate(0,0); 00324 lcd.printf("%s",DummyMatrix[0].substr(0, noextend.rfind("."))); 00325 lcd.locate(0,1); 00326 lcd.printf("%s",DummyMatrix[1].substr(0, noextend.rfind("."))); 00327 lcd.locate(0,2); 00328 lcd.printf("%s",DummyMatrix[2].substr(0, noextend.rfind("."))); 00329 lcd.locate(0,3); 00330 lcd.printf("%s",DummyMatrix[3].substr(0, noextend.rfind("."))); 00331 } 00332 } 00333 player = !player; 00334 countstate++; 00335 } 00336 if ( player == true) 00337 { 00338 string xx = "/sd/myMusic/"; 00339 //songnum is the index of the song being played in the array 00340 if(test == 3) 00341 { y = xx + DummyMatrix[3] + ".wav"; 00342 } 00343 else{ 00344 y = xx + DummyMatrix[randsong]+ ".wav"; 00345 } 00346 y = y.c_str(); 00347 //here is where it is actually playing the song 00348 wave_file=fopen(y.c_str(),"r"); 00349 waver.play(wave_file, playpoint); 00350 fclose(wave_file); 00351 player = false; 00352 } 00353 myled = 1; 00354 wait(.25); 00355 myled=0; 00356 00357 wait(.25); 00358 00359 00360 } 00361 } 00362 } 00363 00364 00365
Generated on Sat Jul 16 2022 20:47:03 by
1.7.2