music player, led, motor
Dependencies: LSM9DS1_Library_cal RPCInterface final mbed
Fork of final_mbed by
Revision 2:55495227fcca, committed 2017-05-01
- Comitter:
- mikebenq
- Date:
- Mon May 01 11:05:37 2017 +0000
- Parent:
- 1:f54bee9d59de
- Commit message:
- Final_mbed
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Apr 28 19:18:46 2017 +0000 +++ b/main.cpp Mon May 01 11:05:37 2017 +0000 @@ -1,165 +1,132 @@ -/* Author: Pramod Nataraja & Vishnu Venkat */ - #include "mbed.h" - #include "VS1002.h" - //#include "TextLCD.h" - #include "mpr121.h" - #include "LSM9DS1.h" - #include "SerialRPCInterface.h" + #include "VS1002.h" + using namespace mbed; + +/*For Change Task*/ + + #define MUSIC_PLAYER 's' + #define LED 'l' + #define MOTOR 'm' +volatile char task=MUSIC_PLAYER; +/*Serial COM port*/ +Serial pc1(USBTX, USBRX); +Serial taskport(p28,p27); + +/*For Music Control*/ +VS1002 mp3(p5, p6, p7, p8,"sd",p11, p12 ,p13, p14, p18, p19, p20, p15); +char *song_name[6]={"Escape","Summer of 69","Monster", "Leave out all the rest","Kings","Interstellar Docking"}; //Array of song names entered manually +int new_song_number=1; //Variable to store the Song Number +int volume_set=-5; //Variable to store the Volume +int previous_volume; //Variable to store the volume when muted +bool pause=false; //Variable to store the status of Pause button +bool mute=false; //Variable to store the status of mute button #define NEXT 7 #define PREVIOUS 8 #define PP 9 #define V_DOWN 10 #define V_UP 11 #define MUTE_UNMUTE 12 -//Ticker flipper; -//LSM9DS1 IMU(p28, p27, 0xD6, 0x3C); -//SerialRPCInterface RPC(p28, p27); -//void foo(Arguments* input, Reply* output); -//RPCFunction bluetooth_control(&foo, "bluetooth_control"); -/*float tempx=0; -float tempy=0; -float tempz=0; -float delx=0; -float dely=0; -float delz=0; -float del=0;*/ - -VS1002 mp3(p5, p6, p7, p8,"sd",p11, p12 ,p13, p14, p23, p22, p21, p15); //Setup Audio decoder. Name is VS1002 even though VS1053 is used. -//TextLCD lcd1(p30, p29, p17, p18, p19, p20); //setup lcd -//InterruptIn interrupt(p26); // Create the interrupt receiver object on pin 26 -//I2C i2c(p9, p10); // Setup the i2c bus on pins 9 and 10 -//Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); //Setup Mpr121 -Serial pc1(USBTX, USBRX); + +/*For LED Control*/ +PwmOut red(p24); +PwmOut green(p23); +PwmOut blue(p22); +/*For Motor Control*/ +PwmOut fanSwitch(p21); +/*For Debugging*/ + DigitalOut led1(LED1); + DigitalOut led2(LED2); -/* Global Variables to store Status*/ -int new_song_number=1; //Variable to store the Song Number -int volume_set=0; //Variable to store the Volume -int previous_volume; //Variable to store the volume when muted -bool pause=false; //Variable to store the status of Pause button -bool mute=false; //Variable to store the status of mute button +/*For Change Task*/ + void changeTask() + { + if(taskport.readable()){ + char c=taskport.getc(); + pc1.putc(c); + task=c; + } + } +/*For Do Different Function*/ + void changeAction(){ + if(pc1.readable()){ + const char arg0=pc1.getc(); + if(task==LED){ + switch(arg0) + { + case '1': + { + red = 1.0; + green = 1.0; + blue = 1.0; + } + break; + case '2': + { + red = 0.75; + green = 0.75; + blue = 0.75; + } + break; + case '3': + { + red = 0.5; + green = 0.5; + blue = 0.5; + } + break; + case '4': + { + red = 0.25; + green = 0.25; + blue = 0.25; + } + break; -int check=0; //Capacitative touch generates interrupt on both press and release. This variable tracks this and updates only on press. -//char *song_name[6]={"18 till I Die","Summer of 69","Boulevard", "Serenity","Crawling","In the end"}; //Array of song names entered manually -char *song_name[6]={"Escape","Summer of 69","Monster", "Leave out all the rest","Kings","Interstellar Docking"}; //Array of song names entered manually - - -/*void fallInterrupt() -{ + default : + break; + } + led1=!led1; + }else if(task==MOTOR){ + switch(arg0) + { + case '1': + { + fanSwitch = 1.0; //Full Speed + } + break; + case '2': + { + fanSwitch = 0.3; // Medium Speed + } + break; + case '3': + { + fanSwitch = 0; // Low Speed + } + break; + + + default : + break; + } + led2=!led2; + }else if(task==MUSIC_PLAYER) + { int key_code=0; - int i=0; - int value; - value=mpr121.read(0x00); - value +=mpr121.read(0x01)<<8; - - i=0; - if(check) - check=0; - else - check=1; - if(check) - { - for (i=0; i<12; i++) { - if (((value>>i)&0x01)==1) key_code=i+1; //Convert value into decimal - } + switch(arg0){ + case '1': key_code=V_UP; break; + case '2': key_code=V_DOWN; break; + case '3': key_code=PP; break; + case '4': key_code=NEXT; break; + case '5': key_code=PREVIOUS; break; + case '6': key_code=MUTE_UNMUTE; break; + default : key_code=0; break; + } + switch(key_code) // Different cases depending on key press - { - case 0:break; // Invalid entry . Valid 1-12 - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: new_song_number=key_code; // Play the selected song - break; - case NEXT:new_song_number+=1; // Next song - if(new_song_number==7) - new_song_number=1; - break; - case PREVIOUS: new_song_number-=1; // Previous Song - if(new_song_number==0) - new_song_number=6; - break; - case PP: pause=!pause; // Pause/Play button - break; - case V_UP: volume_set+=3; // Volume Up - if(volume_set>=0) - volume_set=0; - break; - case V_DOWN: volume_set-=3; //Volume Down - if(volume_set<-55) - volume_set=-55; - break; - case MUTE_UNMUTE: mute=!mute; //Mute/Unmute - if(mute) - { - previous_volume=volume_set; // Attenuation of -55 db is small enough to not hear anything - volume_set=-55; - } - else - { - volume_set=previous_volume; - } - break; - default: ;//pc.cls(); - pc1.printf("error"); // exit on error - exit(1); - } - } - - if(pause) - pc1.printf("Paused "); - if(mute) - pc1.printf("Muted"); - if(!mute && !pause) - pc1.printf("Playing"); - pc1.printf("\r\n %d %s",new_song_number,song_name[new_song_number-1]); - - }*/ -/*void IMUInterrupt() -{ - int key_code=0; - - - while(!IMU.accelAvailable()); - tempx=IMU.calcAccel(IMU.ax); - tempy=IMU.calcAccel(IMU.ay); - tempz=IMU.calcAccel(IMU.az); - IMU.readAccel(); - delx=tempx-IMU.calcAccel(IMU.ax); - dely=tempy-IMU.calcAccel(IMU.ay); - delz=tempz-IMU.calcAccel(IMU.az); - //del=delx*delx+dely*dely+delz*delz; - if(delx>0.15) - key_code=NEXT; - if(delx<-0.15) - key_code=PREVIOUS; - if(dely>0.15) - key_code=V_UP; - if(dely<-0.15) - key_code=V_DOWN; - if(delz>0.15) - key_code=PP; - if(delz<-0.15) - key_code=MUTE_UNMUTE; - //pc1.printf("delx:%f dely:%f delz:%f\r\n",delx,dely,delz); - //pc1.printf("key_code:%d\r\n",key_code); - - - - - switch(key_code) // Different cases depending on key press - { - case 0:break; // Invalid entry . Valid 1-12 - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: new_song_number=key_code; // Play the selected song - break; + { case NEXT: pc1.printf("next\r\n"); new_song_number+=1; // Next song @@ -178,106 +145,13 @@ break; case V_UP: pc1.printf("v_up\r\n"); - volume_set+=3; // Volume Up + volume_set+=10; // Volume Up if(volume_set>=0) volume_set=0; break; case V_DOWN: pc1.printf("v_down\r\n"); - volume_set-=3; //Volume Down - if(volume_set<-55) - volume_set=-55; - break; - case MUTE_UNMUTE: - pc1.printf("mute_unmute\r\n"); - mute=!mute; //Mute/Unmute - if(mute) - { - previous_volume=volume_set; // Attenuation of -55 db is small enough to not hear anything - volume_set=-55; - } - else - { - volume_set=previous_volume; - } - break; - default: ;//pc.cls(); - pc1.printf("error"); // exit on error - exit(1); - } - - - if(pause) - pc1.printf("Paused "); - if(mute) - pc1.printf("Muted"); - if(!mute && !pause) - pc1.printf("Playing"); - pc1.printf("\r\n %d %s",new_song_number,song_name[new_song_number-1]); - - }*/ - //void foo(Arguments * input, Reply * output){ - void foo(){ - //const char *arg0=input->getArg<const char*>(); - pc1.printf("hi\n"); - if(pc1.readable()){ - const char arg0=pc1.getc(); - //pc1.printf(arg0); - int key_code=0; - //switch(*arg0){ - switch(arg0){ - case '1': key_code=V_UP; break; - case '2': key_code=V_DOWN; break; - case '3': key_code=PP; break; - case '4': key_code=MUTE_UNMUTE; break; - case 'N': key_code=NEXT; break; - case 'B': key_code=PREVIOUS; break; - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': key_code=arg0-'0'; // Play the selected song - break; - default : key_code=0; break; - } - - - switch(key_code) // Different cases depending on key press - { - case 0:break; // Invalid entry . Valid 1-12 - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: new_song_number=key_code; // Play the selected song - break; - case NEXT: - pc1.printf("next\r\n"); - new_song_number+=1; // Next song - if(new_song_number==7) - new_song_number=1; - break; - case PREVIOUS: - pc1.printf("previous\r\n"); - new_song_number-=1; // Previous Song - if(new_song_number==0) - new_song_number=6; - break; - case PP: - pc1.printf("pp\r\n"); - pause=!pause; // Pause/Play button - break; - case V_UP: - pc1.printf("v_up\r\n"); - volume_set+=3; // Volume Up - if(volume_set>=0) - volume_set=0; - break; - case V_DOWN: - pc1.printf("v_down\r\n"); - volume_set-=3; //Volume Down + volume_set-=10; //Volume Down if(volume_set<-55) volume_set=-55; break; @@ -309,19 +183,13 @@ pc1.printf("Playing"); pc1.printf("\r\n %d %s",new_song_number,song_name[new_song_number-1]); } + } } int main () { - pc1.printf("hello\r\n"); - /*IMU.begin(); - if (!IMU.begin()) { - pc1.printf("Failed to communicate with LSM9DS1.\r\n"); - } - IMU.calibrate(1);*/ - /*============================================================ - * MP3 Initialising - *==========================================================*/ - + led1=1; + led2=1; + pc1.printf("hello\r\n"); mp3._RST = 1; mp3.cs_high(); //chip disabled @@ -329,15 +197,9 @@ mp3.sci_write(0x00,(SM_SDINEW+SM_STREAM+SM_DIFF)); mp3.sci_write(0x03, 0x9800); mp3.sdi_initialise(); - - /* Touch Pad setup */ - //interrupt.fall(&fallInterrupt); - //interrupt.mode(PullUp); - //interrupt.fall(&IMUInterrupt); - //interrupt.mode(PullUp); - // flipper.attach(&IMUInterrupt, 1); - pc1.attach(&foo, Serial::RxIrq); + pc1.attach(&changeAction, Serial::RxIrq);//Serial interrupt for function code + taskport.attach(&changeTask, Serial::RxIrq);// Serial interrupt for Task code while(1) { mp3.play_song(new_song_number);