Team X / Mbed 2 deprecated final_mbed

Dependencies:   LSM9DS1_Library_cal RPCInterface final mbed

Fork of SDFileSystem_HelloWorld by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001  #include "mbed.h" 
00002  #include "VS1002.h"
00003   
00004  using namespace mbed;
00005    
00006 /*For Change Task*/
00007 
00008  #define MUSIC_PLAYER 's'
00009  #define LED 'l'
00010  #define MOTOR 'm'
00011 volatile char task=MUSIC_PLAYER;
00012 /*Serial COM port*/
00013 Serial pc1(USBTX, USBRX);
00014 Serial taskport(p28,p27); 
00015 
00016 /*For Music Control*/  
00017 VS1002 mp3(p5, p6, p7, p8,"sd",p11, p12 ,p13, p14, p18, p19, p20, p15);  
00018 char *song_name[6]={"Escape","Summer of 69","Monster", "Leave out all the rest","Kings","Interstellar Docking"}; //Array of song names entered manually
00019 int new_song_number=1;  //Variable to store the Song Number
00020 int volume_set=-5;     //Variable to store the Volume
00021 int previous_volume;    //Variable to store the volume when muted
00022 bool pause=false;       //Variable to store the status of Pause button 
00023 bool mute=false;        //Variable to store the status of mute button
00024  #define NEXT 7
00025  #define PREVIOUS 8
00026  #define PP 9
00027  #define V_DOWN 10
00028  #define V_UP 11
00029  #define MUTE_UNMUTE 12
00030 
00031 /*For LED Control*/
00032 PwmOut red(p24);
00033 PwmOut green(p23);
00034 PwmOut blue(p22);
00035 /*For Motor Control*/
00036 PwmOut fanSwitch(p21);
00037 /*For Debugging*/
00038  DigitalOut led1(LED1);
00039  DigitalOut led2(LED2);
00040 
00041 /*For Change Task*/
00042   void changeTask()
00043   {
00044       if(taskport.readable()){
00045           char c=taskport.getc();
00046           pc1.putc(c);
00047           task=c;
00048           }
00049       }
00050 /*For Do Different Function*/
00051   void changeAction(){
00052   if(pc1.readable()){
00053   const char arg0=pc1.getc();
00054   if(task==LED){
00055      switch(arg0)
00056     {
00057         case '1':
00058                     {
00059                         red = 1.0;
00060                         green = 1.0;
00061                         blue = 1.0;
00062                     }
00063                     break;
00064         case '2':
00065                     {
00066                         red = 0.75;
00067                         green = 0.75;
00068                         blue = 0.75;
00069                     }
00070                     break;    
00071         case '3':
00072                     {
00073                         red = 0.5;
00074                         green = 0.5;
00075                         blue = 0.5;
00076                     }
00077                     break; 
00078         case '4':
00079                     {
00080                         red = 0.25;
00081                         green = 0.25;
00082                         blue = 0.25;
00083                     }
00084                     break;  
00085 
00086         default :
00087                     break;                                                
00088     }
00089     led1=!led1;
00090   }else if(task==MOTOR){
00091      switch(arg0)
00092     {
00093         case '1':
00094                     {
00095                         fanSwitch = 1.0; //Full Speed
00096                     }
00097                     break;
00098         case '2':
00099                     {
00100                         fanSwitch = 0.3; // Medium Speed
00101                     }
00102                     break;    
00103         case '3':
00104                     {
00105                         fanSwitch = 0; // Low Speed
00106                     }
00107                     break; 
00108         
00109           
00110         default :
00111                     break;                                                
00112     }
00113     led2=!led2;
00114   }else if(task==MUSIC_PLAYER)
00115   {
00116   int key_code=0;
00117     switch(arg0){
00118     case '1': key_code=V_UP; break;
00119     case '2': key_code=V_DOWN; break;
00120     case '3': key_code=PP; break;
00121     case '4': key_code=NEXT; break;
00122     case '5': key_code=PREVIOUS; break;
00123     case '6': key_code=MUTE_UNMUTE; break;   
00124     default : key_code=0; break; 
00125     }
00126     
00127 
00128  switch(key_code)  // Different cases depending on key press
00129  { 
00130   case NEXT:
00131             pc1.printf("next\r\n");
00132             new_song_number+=1;  // Next song
00133           if(new_song_number==7)
00134            new_song_number=1;
00135           break;
00136   case PREVIOUS: 
00137             pc1.printf("previous\r\n");
00138             new_song_number-=1;  // Previous Song
00139           if(new_song_number==0)
00140            new_song_number=6;
00141           break;
00142   case PP: 
00143             pc1.printf("pp\r\n");
00144             pause=!pause; // Pause/Play button
00145           break;
00146   case V_UP: 
00147             pc1.printf("v_up\r\n");
00148             volume_set+=10; // Volume Up
00149            if(volume_set>=0)
00150             volume_set=0;
00151            break;
00152   case V_DOWN: 
00153             pc1.printf("v_down\r\n");
00154             volume_set-=10;  //Volume Down
00155            if(volume_set<-55)
00156             volume_set=-55;
00157             break;
00158   case MUTE_UNMUTE: 
00159             pc1.printf("mute_unmute\r\n");
00160             mute=!mute;  //Mute/Unmute
00161            if(mute)
00162            {
00163             previous_volume=volume_set; // Attenuation of -55 db is small enough to not hear anything
00164             volume_set=-55;
00165            }
00166            else
00167            {
00168            volume_set=previous_volume;
00169            }
00170            break;
00171   default: ;//pc.cls();
00172            pc1.printf("error");  // exit on error
00173            exit(1);
00174         }
00175  
00176  /* Print to LCD the status of Song */
00177   //pc.cls();
00178   if(pause)
00179     pc1.printf("Paused ");
00180   if(mute)
00181    pc1.printf("Muted");
00182   if(!mute && !pause)
00183    pc1.printf("Playing"); 
00184   pc1.printf("\r\n %d %s",new_song_number,song_name[new_song_number-1]);   
00185   }
00186   }
00187 }
00188  int main () 
00189  { 
00190     led1=1;
00191     led2=1;
00192     pc1.printf("hello\r\n");      
00193     
00194      mp3._RST = 1; 
00195      mp3.cs_high();                                  //chip disabled 
00196      mp3.sci_initialise();                           //initialise MBED 
00197      mp3.sci_write(0x00,(SM_SDINEW+SM_STREAM+SM_DIFF)); 
00198      mp3.sci_write(0x03, 0x9800); 
00199      mp3.sdi_initialise();    
00200       
00201     pc1.attach(&changeAction, Serial::RxIrq);//Serial interrupt for function code
00202     taskport.attach(&changeTask, Serial::RxIrq);// Serial interrupt for Task code
00203          while(1)
00204          {
00205          mp3.play_song(new_song_number);
00206          }       
00207  
00208  } 
00209  
00210