Final Working Version of Songpop
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player
main.cpp
- Committer:
- bkf34
- Date:
- 2015-03-13
- Revision:
- 0:10b73315cdb0
File content as of revision 0:10b73315cdb0:
#include "mbed.h" #include <vector> #include "SDFileSystem.h" #include "uLCD_4DGL.h" #include <string> #include "PinDetect.h" #include "wave_player.h" #include "math.h" AnalogOut DACout(p18); wave_player waver(&DACout); DigitalOut myled(LED1); DigitalOut myled2(LED4); SDFileSystem sd(p5, p6, p7, p8,p14, "sd"); //SD card uLCD_4DGL lcd(p28, p27, p30); vector<string> filenames; //filenames are stored in a vector string vector<string> CopyMatSongs; string noextend; int stringsize; void read_file_names(char *dir) { DIR *dp; struct dirent *dirp; dp = opendir(dir); //read all directory and file names in current directory into filename vector while((dirp = readdir(dp)) != NULL) { filenames.push_back(string(dirp->d_name)); } } int test; PinDetect A(p9); PinDetect B(p10); PinDetect D(p11); PinDetect C(p12); int count=0; int array17[100]; int array4[100]; string DummyMatrix[] = {"a","a","a","a"}; int countstate = 0; int volatile songnum = 0 ; int numberofsongs; bool player = false; bool *playpoint = &player; string y =""; FILE *wave_file; int placement = rand()%4; int randsong = rand()%4; void D_callback (void) { player = !player; if(randsong != placement) { myled2 = 1; wait(1); myled2 = 0; DummyMatrix[0] = CopyMatSongs[array17[0+count]]; DummyMatrix[1] = CopyMatSongs[array17[1+count]]; DummyMatrix[2] = CopyMatSongs[array17[2+count]]; DummyMatrix[3] = CopyMatSongs[array17[3+count]]; placement = rand()%4; randsong = rand()%4; countstate=0; count++; } else{ DummyMatrix[0] = CopyMatSongs[array17[0+count]]; DummyMatrix[1] = CopyMatSongs[array17[1+count]]; DummyMatrix[2] = CopyMatSongs[array17[2+count]]; DummyMatrix[3] = CopyMatSongs[array17[3+count]]; placement = rand()%4; randsong = rand()%4; countstate=0; count++; } } void A_callback (void) { player = !player; if(randsong == 0 && placement == 0) { myled2 = 1; wait(1); myled2 = 0; DummyMatrix[0] = CopyMatSongs[array17[4+count]]; DummyMatrix[1] = CopyMatSongs[array17[5+count]]; DummyMatrix[2] = CopyMatSongs[array17[6+count]]; DummyMatrix[3] = CopyMatSongs[array17[7+count]]; placement = rand()%4; randsong = rand()%4; countstate=0; count++; } else{ DummyMatrix[0] = CopyMatSongs[array17[4+count]]; DummyMatrix[1] = CopyMatSongs[array17[5+count]]; DummyMatrix[2] = CopyMatSongs[array17[6+count]]; DummyMatrix[3] = CopyMatSongs[array17[7+count]]; placement = rand()%4; randsong = rand()%4; countstate=0; count++; } } void B_callback(void) { player = !player; if(randsong ==1 && placement == 1) { myled2 = 1; wait(1); myled2 = 0; DummyMatrix[0] = CopyMatSongs[array17[8+count]]; DummyMatrix[1] = CopyMatSongs[array17[9+count]]; DummyMatrix[2] = CopyMatSongs[array17[10+count]]; DummyMatrix[3] = CopyMatSongs[array17[11+count]]; placement = rand()%4; randsong = rand()%4; countstate=0; count++; } else{ DummyMatrix[0] = CopyMatSongs[array17[8+count]]; DummyMatrix[1] = CopyMatSongs[array17[9+count]]; DummyMatrix[2] = CopyMatSongs[array17[10+count]]; DummyMatrix[3] = CopyMatSongs[array17[11+count]]; placement = rand()%4; randsong = rand()%4; countstate=0; count++; } } void C_callback (void) { player = !player; if(randsong == 2 && placement==2) { myled2 = 1; wait(1); myled2 = 0; DummyMatrix[0] = CopyMatSongs[array17[12+count]]; DummyMatrix[1] = CopyMatSongs[array17[13+count]]; DummyMatrix[2] = CopyMatSongs[array17[14+count]]; DummyMatrix[3] = CopyMatSongs[array17[15+count]]; placement = rand()%4; randsong = rand()%4; countstate=0; count++; } else{ DummyMatrix[0] = CopyMatSongs[array17[12+count]]; DummyMatrix[1] = CopyMatSongs[array17[13+count]]; DummyMatrix[2] = CopyMatSongs[array17[14+count]]; DummyMatrix[3] = CopyMatSongs[array17[15+count]]; placement = rand()%4; randsong = rand()%4; countstate=0; count++; } } int main() { srand(2); for(int i = 0; i < 100; i++) { int x = rand()%4; array4[i] = x; printf("%d\n\r ", array4[i]); } for(int i = 0; i < 100; i++) { int x = rand()%17; array17[i] = x; printf("%d\n\r ", array17[i]); } while(1) { /*if (sd.cdcheck()== false) { lcd.cls(); lcd.printf("Please Insert SD Card\n"); wait(.1); } else { lcd.cls(); lcd.printf("thank you"); lcd.locate(0,1); lcd.printf("restart system"); wait(.1); */ A.mode(PullUp); B.mode(PullUp); D.mode(PullUp); C.mode(PullUp); wait(.01); // Setup Interrupt callback functions for a pb hit A.attach_deasserted(&A_callback); B.attach_deasserted(&B_callback); D.attach_deasserted(&D_callback); C.attach_deasserted(&C_callback); // Start sampling pb inputs using interrupts A.setSampleFrequency(); B.setSampleFrequency(); D.setSampleFrequency(); C.setSampleFrequency(); // via interrupts that activate the callback counter function wait(.001); // read file names into vector of strings read_file_names("/sd/myMusic"); numberofsongs= filenames.size(); printf("%d",numberofsongs); // print filename strings from vector using an iterator /*for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) { printf("%s\n\r",(*it).c_str()); }*/ //this is what pulls in all file names and strips off .wav and puts it into vector // name of vector is filename[index of i] //printf("%s CopyMatsongs:", CopyMatSongs); for(int i=0; i < filenames.size(); i++) { noextend = filenames[i]; filenames[i] = noextend.substr(0, noextend.rfind(".")); printf("%s\n\r", filenames[i]); } CopyMatSongs = filenames; //printf("%s\n\r", CopyMatSongs); printf("%s\n\r",DummyMatrix[0]); for(int aa = 0; aa <4; aa++) { DummyMatrix[array4[aa]] = CopyMatSongs[array17[aa]]; printf("%s \n\r", DummyMatrix[aa]); } while(1) { if (countstate == 0) { lcd.cls(); if (placement == 0 && randsong==0) { test = 0; lcd.locate(0,0); lcd.printf("%s",DummyMatrix[randsong].substr(0, noextend.rfind("."))); lcd.locate(0,1); lcd.printf("%s",DummyMatrix[randsong+1].substr(0, noextend.rfind("."))); lcd.locate(0,2); lcd.printf("%s",DummyMatrix[randsong+2].substr(0, noextend.rfind("."))); lcd.locate(0,3); lcd.printf("%s",DummyMatrix[randsong+3].substr(0, noextend.rfind("."))); } if(placement == 1 && randsong == 1) { test =1; lcd.locate(0,0); lcd.printf("%s",DummyMatrix[randsong-1].substr(0, noextend.rfind("."))); lcd.locate(0,1); lcd.printf("%s",DummyMatrix[randsong].substr(0, noextend.rfind("."))); lcd.locate(0,2); lcd.printf("%s",DummyMatrix[randsong+1].substr(0, noextend.rfind("."))); lcd.locate(0,3); lcd.printf("%s",DummyMatrix[randsong+2].substr(0, noextend.rfind("."))); } if(placement == 2 &&randsong == 2) { test = 2; lcd.locate(0,0); lcd.printf("%s",DummyMatrix[randsong-2].substr(0, noextend.rfind("."))); lcd.locate(0,1); lcd.printf("%s",DummyMatrix[randsong-1].substr(0, noextend.rfind("."))); lcd.locate(0,2); lcd.printf("%s",DummyMatrix[randsong].substr(0, noextend.rfind("."))); lcd.locate(0,3); lcd.printf("%s",DummyMatrix[randsong+1].substr(0, noextend.rfind("."))); } else { { test=3; lcd.locate(0,0); lcd.printf("%s",DummyMatrix[0].substr(0, noextend.rfind("."))); lcd.locate(0,1); lcd.printf("%s",DummyMatrix[1].substr(0, noextend.rfind("."))); lcd.locate(0,2); lcd.printf("%s",DummyMatrix[2].substr(0, noextend.rfind("."))); lcd.locate(0,3); lcd.printf("%s",DummyMatrix[3].substr(0, noextend.rfind("."))); } } player = !player; countstate++; } if ( player == true) { string xx = "/sd/myMusic/"; //songnum is the index of the song being played in the array if(test == 3) { y = xx + DummyMatrix[3] + ".wav"; } else{ y = xx + DummyMatrix[randsong]+ ".wav"; } y = y.c_str(); //here is where it is actually playing the song wave_file=fopen(y.c_str(),"r"); waver.play(wave_file, playpoint); fclose(wave_file); player = false; } myled = 1; wait(.25); myled=0; wait(.25); } } }