Jared Thompson / Mbed 2 deprecated Mbed_Party_Bus

Dependencies:   4DGL-uLCD-SE Motor PinDetect SDFileSystem mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 #include "SDFileSystem.h"
00004 #include "uLCD_4DGL.h"
00005 #include "wave_player.h"
00006 #include "Motor.h"
00007 #include <vector>
00008 #include <string>
00009 //SDfile
00010 SDFileSystem sd(p5, p6, p7, p8, p9, "sd"); //SD card
00011 //Pushbuttons
00012 PinDetect pbStop(p17);
00013 //LEDS and Bluetooth Serial Pins
00014 BusOut myled(LED1,LED2,LED3,LED4);
00015 Serial blue(p13,p14); //tx, rx
00016 
00017 //Motor
00018 Motor m(p21, p22, p23); // pwm, fwd, rev
00019 Motor m2(p26, p25, p24);
00020 
00021 
00022 //uLCD
00023 uLCD_4DGL uLCD(p28, p27, p29);
00024 //Wave Player
00025 AnalogOut DACout(p18);
00026 wave_player waver(&DACout);
00027 int pbPlays = 1;
00028 //Create Music State
00029 enum musicStateType {START, WAIT, WAIT2, LIST,REVERSE, NEXT, PLAY};
00030 FILE *wave_file;
00031 musicStateType musicState = START;
00032 //Callback Functions
00033 void pbUp_hit_callback (void)
00034 {
00035     switch (musicState)
00036     {
00037     case WAIT:
00038         musicState = LIST;
00039         break;
00040     case WAIT2:   
00041         musicState = REVERSE;
00042         break;
00043     }
00044 }
00045  
00046 void pbDown_hit_callback (void)
00047 {
00048     switch (musicState)
00049     {
00050     case WAIT:
00051         musicState = LIST;
00052         break;
00053     case WAIT2:
00054         musicState = NEXT;
00055         break; 
00056     }
00057 }
00058 
00059 void pbPlay_hit_callback (void)
00060 {
00061     switch (musicState)
00062     {
00063     case WAIT:
00064         musicState = LIST;
00065         break;
00066     case WAIT2:   
00067         musicState = PLAY;
00068         break;
00069     case PLAY:
00070         pbPlays = 0;
00071         break;
00072     }
00073 }
00074 
00075 vector<string> filenames; //filenames are stored in a vector string
00076 void read_file_names(char *dir)
00077 {
00078 DIR *dp;
00079 struct dirent *dirp;
00080 dp = opendir(dir);
00081 //read all directory and file names in current directory into filename vector
00082 while((dirp = readdir(dp)) != NULL) {
00083     filenames.push_back(string(dirp->d_name));
00084     }
00085  }
00086 
00087 int main()
00088 {
00089     //Setup for song stop pushbutton
00090     pbStop.mode(PullUp);
00091     wait(0.1);
00092     pbStop.attach_deasserted(&pbPlay_hit_callback);
00093     pbStop.setSampleFrequency();
00094     //intialize uLCD
00095     uLCD.display_control(PORTRAIT);
00096     uLCD.cls();
00097     uLCD.baudrate(BAUD_3000000);
00098     uLCD.background_color(BLACK);
00099     //initialize variables
00100     char bnum=0;
00101     char bhit=0;
00102     while(sd.SD_inserted() == false) {
00103       uLCD.cls();
00104       uLCD.printf("Insert SD Card!\n");
00105       wait(0.5);
00106     }
00107     int i = 0;
00108     vector<string>::iterator it=filenames.begin();
00109     vector<string>::iterator ittemp=filenames.begin();  
00110     //While loop for music state. state is initialized to Start 
00111     //and waits until a button is pressed to go to the music LIST state.   
00112     while (1) 
00113     {   
00114         switch (musicState)
00115         {
00116         case START:
00117             {
00118             read_file_names("/sd/myMusic");
00119             it = filenames.begin();
00120             ittemp = filenames.begin();
00121             uLCD.cls();
00122             uLCD.locate(0,0);
00123             uLCD.printf("MBED Party Bus!!!\n\n");
00124             uLCD.printf("Press Key to Start");
00125             musicState = WAIT;
00126             break;
00127             }
00128         case LIST:
00129             {
00130             pbPlays = 1;
00131             // read file names into vector of strings
00132             uLCD.cls();
00133             // print filename strings from vector using an iterator
00134             for(it = ittemp;it < filenames.end(); it++) 
00135             {   
00136             uLCD.printf("%s\n\r", (*it).substr(0, (*it).length()-4).c_str());
00137                         }
00138             }
00139             musicState = WAIT2;
00140             break;
00141         //Case for next song in list
00142         case NEXT:
00143             {
00144             if(ittemp == filenames.end())
00145             {ittemp = filenames.begin();
00146             musicState = LIST;}
00147             else
00148             {ittemp = ittemp++;
00149             musicState = LIST;}
00150             break;
00151             }
00152         //Case for previous song in list
00153         case REVERSE:    
00154             {
00155                 if(ittemp == filenames.begin())
00156                 {ittemp = filenames.begin();
00157                 musicState = LIST;
00158                 }
00159                 else
00160                 {ittemp = ittemp--;
00161                 musicState = LIST;}
00162                 break;
00163             }
00164         //Case to play selected song
00165         case PLAY:
00166             {
00167             uLCD.cls();
00168             uLCD.printf("%s\n\r", (*ittemp).substr(0, (*ittemp).length()-4).c_str());
00169             uLCD.printf("Now Playing");
00170             string filename = "/sd/myMusic/";
00171             filename = filename + (*ittemp);
00172             //If statements below deteremine which song is playing and what album artwork to show on uLCD screen
00173             if(filename == "/sd/myMusic/badboys2.wav"){
00174             uLCD.media_init();
00175             uLCD.set_sector_address(0x0000, 0x0000);
00176             uLCD.display_image(0,0);
00177             }
00178             if(filename == "/sd/myMusic/Childish_3005.wav"){
00179             uLCD.media_init();
00180             uLCD.set_sector_address(0x0000, 0x0041);
00181             uLCD.display_image(0,0);
00182             }
00183             if(filename == "/sd/myMusic/Drake.wav"){
00184             uLCD.media_init();
00185             uLCD.set_sector_address(0x0000, 0x0082);
00186             uLCD.display_image(0,0);
00187             }
00188             if(filename == "/sd/myMusic/Chase.wav"){
00189             uLCD.media_init();
00190             uLCD.set_sector_address(0x0000, 0x00C3);
00191             uLCD.display_image(0,0);
00192             }
00193             if(filename == "/sd/myMusic/Kanye.wav"){
00194             uLCD.media_init();
00195             uLCD.set_sector_address(0x0000, 0x0104);
00196             uLCD.display_image(0,0);
00197             }
00198             //Start up motor for robot. Left motor speed is faster because of weight on shadowbot(Can be changed). 
00199             m.speed(-0.4); 
00200             m2.speed(-0.55); 
00201             wave_file=fopen(filename.c_str(), "r");
00202             //Play song
00203             waver.play(wave_file, pbPlays);        
00204             fclose(wave_file);
00205             //Turn off robot motors
00206             m.speed(0);
00207             m2.speed(0);  
00208             //Return to music LIST
00209             musicState = LIST;
00210             break;
00211             }
00212         case WAIT:
00213             i++;
00214             //Commands for intial wait. The bluetooth commands for up, down, and right all change the music state to the LIST at startup
00215             if (blue.getc()=='!') {
00216             if (blue.getc()=='B') { //button data packet
00217                 bnum = blue.getc(); //button number
00218                 bhit = blue.getc(); //1=hit, 0=release
00219                 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
00220                     myled = bnum - '0'; //current button number will appear on LEDs
00221                     switch (bnum) {
00222                         case '5': //button 5 up arrow
00223                             if (bhit=='1') {
00224                                 //add hit code here
00225                             } else {
00226                                  pbUp_hit_callback();
00227                             }
00228                             break;
00229                         case '6': //button 6 down arrow
00230                             if (bhit=='1') {
00231                                 //add hit code here
00232                             } else {
00233                                 pbDown_hit_callback();//add release code here
00234                             }
00235                             break;
00236                         case '8': //button 8 right arrow
00237                             if (bhit=='1') {
00238                                 //add hit code here
00239                             } else {
00240                                 pbPlay_hit_callback();//add release code here
00241                             }
00242                             break;
00243                     }
00244                 }
00245             }} 
00246             break;
00247         case WAIT2:
00248         if (blue.getc()=='!') {
00249             //Second wait function moves up and down in list with bluetooth commands. Right ble button plays the song here
00250             if (blue.getc()=='B') { //button data packet
00251                 bnum = blue.getc(); //button number
00252                 bhit = blue.getc(); //1=hit, 0=release
00253                 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
00254                     myled = bnum - '0'; //current button number will appear on LEDs
00255                     switch (bnum) {
00256                         case '5': //button 5 up arrow
00257                             if (bhit=='1') {
00258                                 //add hit code here
00259                             } else {
00260                                  pbUp_hit_callback();
00261                             }
00262                             break;
00263                         case '6': //button 6 down arrow
00264                             if (bhit=='1') {
00265                                 //add hit code here
00266                             } else {
00267                                 pbDown_hit_callback();//add release code here
00268                             }
00269                             break;
00270                         case '8': //button 8 right arrow
00271                             if (bhit=='1') {
00272                                 //add hit code here
00273                             } else {
00274                                 pbPlay_hit_callback();//add release code here
00275                             }
00276                             break;
00277                     }
00278                 }
00279             }} 
00280             i++;
00281             break;
00282         }
00283     }
00284 }
00285