Erick Cardenas / Mbed 2 deprecated 4180_Lab4_v6

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

Fork of 4180_Lab4_v6 by Scott Williams

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "PinDetect.h"
00004 #include "uLCD_4DGL.h"
00005 uLCD_4DGL uLCD(p28, p27, p30);
00006 #include "Robot.h"
00007 #include "Nav_Switch.h"
00008 #include "Speaker.h"
00009 #include "wave_player.h"
00010 #include "SDFileSystem.h"
00011 BusOut mbedleds(LED1,LED2,LED3,LED4);
00012 Robot myRobot;
00013 Nav_Switch myNav( p25, p22, p23, p21, p24);
00014 Mutex stdio_mutex;
00015 AnalogOut DACout(p18); // used to play sound on speaker
00016 wave_player waver(&DACout);
00017 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
00018 //PinDetect pb1(p25);
00019 //Wiring Set-up:
00020 //Speaker: p18
00021 //SD Card: DO=p6,SCK=p7,DI=p5,CS=p8,VCC=Vout,GND=GND
00022 //Thumb-Stick: U=p25,C=p24,L=p23,D=p22,R=p21,+=pVout,-=GND
00023 bool Alive = 1;
00024 bool Drown = 0;
00025 bool Splat = 0;
00026 bool Victory = 0;
00027 double CarWidth = 10;
00028 int i = 11;
00029 int j = 11;
00030 int C1 = 11;
00031 int C2 = 32;
00032 int C3 = 53;
00033 int C4 = 74;
00034 int C5 = 95;
00035 int C6 = 116;
00036 double Position1 = 90;
00037 double Position2 = 90;
00038 double Column1;
00039 double Column2;
00040 int Rate1 = 3;
00041 int FrogX;
00042 int FrogY;
00043     
00044 /*void pb1_hit_callback (void)
00045 {
00046     stdio_mutex.lock();
00047     myRobot.moveForward();
00048     stdio_mutex.unlock();   
00049 }*/
00050   
00051 void DeathCheck_thread(void const *argument)
00052 {
00053     while(Alive) {
00054         FrogX = myRobot.getXPosition();
00055         FrogY = myRobot.getYPosition(); 
00056     
00057         if(FrogY == 32){   //Check win-condition on landing on Row 2
00058             Alive = 0;
00059             stdio_mutex.lock();
00060             uLCD.locate(1,1);
00061             uLCD.printf("VICTORY!");
00062             stdio_mutex.unlock();
00063             }//End 53
00064         if(FrogY == 74){   //Check dangers on water Row 4
00065             if(FrogX == 32 || FrogX == 74 || FrogX == 116){
00066                 Alive = 0;
00067                 Drown = 1;
00068                 
00069                 stdio_mutex.lock();
00070                 myRobot.drawDrown1();
00071                 stdio_mutex.unlock();
00072                 Thread::wait(50);
00073                 stdio_mutex.lock();
00074                 myRobot.drawDrown2();
00075                 stdio_mutex.unlock();
00076                 Thread::wait(50);
00077                 stdio_mutex.lock();
00078                 myRobot.drawDrown3();
00079                 stdio_mutex.unlock();
00080                 Thread::wait(50);
00081                 stdio_mutex.lock();
00082                 myRobot.drawDrown4();
00083                 stdio_mutex.unlock();
00084                 Thread::wait(50);
00085                 stdio_mutex.lock();
00086                 myRobot.drawDrown5();
00087                 stdio_mutex.unlock();
00088                 }
00089         }//End 74
00090     Thread::wait(10);
00091     }//End While    
00092 }//End Thread
00093 
00094 void AutoPilot_thread(void const *argument)
00095 {
00096     while(Alive) {
00097     //Auto-pilot Testing
00098        
00099     //stdio_mutex.lock();
00100     //myRobot.drawEraser();
00101     //myRobot.moveForward();
00102     //myRobot.drawFrog();
00103     //stdio_mutex.unlock();
00104     //Thread::wait(50);
00105     
00106     break;
00107     }
00108 }
00109 
00110 void Traffic1_thread(void const *argument) //Blue Car
00111 {
00112     while(Alive) {
00113         Position1 = Position1 + Rate1;
00114         if(Position1 > 140 ){
00115             Position1 = -10;
00116             }
00117             
00118         if(FrogY == 95){   //Check dangers on Traffic 1 Row 5                
00119             if(((Position1 + CarWidth)>(FrogX - 5)) && ((Position1 - CarWidth)<(FrogX + 5))){                        
00120                 Alive = 0;
00121                 stdio_mutex.lock();
00122                 myRobot.drawDeadFrog();
00123                 stdio_mutex.unlock();
00124             }    
00125         }    
00126         Column1 = (ceil((Position1 - CarWidth)/21))*21;
00127         
00128         /*stdio_mutex.lock();
00129         uLCD.locate(10,1);
00130         uLCD.printf("%3.0f", Column1);
00131         stdio_mutex.unlock();
00132         */
00133         stdio_mutex.lock();
00134         myRobot.drawRoad(Column1-10,95);
00135         myRobot.drawRoad(Column1-31,95);
00136         myRobot.drawCar1(Position1,CarWidth);
00137         stdio_mutex.unlock();
00138         Thread::wait(10);
00139     }
00140 }
00141 
00142 void Traffic2_thread(void const *argument)  //Red Car
00143 {
00144     while(Alive) {
00145         Position2 = Position2 - Rate1;
00146         if(Position2 < 0 ){
00147             Position2 = 140;
00148             }
00149         
00150         if(FrogY == 53){   //Check dangers on Traffic 2 Row 3               
00151             if(((Position2 + CarWidth)>(FrogX - 5)) && ((Position2 - CarWidth)<(FrogX + 5))){                               
00152                 Alive = 0;
00153                 stdio_mutex.lock();
00154                 myRobot.drawDeadFrog();
00155                 stdio_mutex.unlock();
00156             }    
00157         }  
00158         Column2 = (ceil((Position2 - CarWidth)/21))*21;
00159         stdio_mutex.lock();
00160         myRobot.drawRoad(Column2+10,53);
00161         myRobot.drawRoad(Column2+31,53);
00162         myRobot.drawCar2(Position2,CarWidth);
00163         stdio_mutex.unlock();
00164         Thread::wait(35);
00165     }
00166 }
00167 
00168 void SFX_thread(void const *argument)
00169 {
00170     while(1){
00171     if(Alive == 1){
00172         //stdio_mutex.lock();
00173         //uLCD.locate(1,1);
00174         //uLCD.printf("DRY");
00175         //stdio_mutex.unlock();
00176         }
00177     
00178     if(Drown == 1){
00179         stdio_mutex.lock();
00180         uLCD.locate(1,1);
00181         uLCD.printf("SPLASH!");
00182         stdio_mutex.unlock();
00183         
00184         FILE *wave_file;
00185         wave_file=fopen("/sd/Splash.wav","r");
00186         waver.play(wave_file);
00187         fclose(wave_file);
00188         }
00189     if(Splat == 1){
00190         stdio_mutex.lock();
00191         uLCD.locate(1,1);
00192         uLCD.printf("SPLAT!");
00193         stdio_mutex.unlock();
00194         
00195         FILE *wave_file;
00196         wave_file=fopen("/sd/Splat.wav","r");
00197         waver.play(wave_file);
00198         fclose(wave_file);
00199         }
00200          if(Victory == 1){
00201         stdio_mutex.lock();
00202         uLCD.locate(1,1);
00203         uLCD.printf("Victory!");
00204         stdio_mutex.unlock();
00205         
00206         FILE *wave_file;
00207         wave_file=fopen("/sd/Victory.wav","r");
00208         waver.play(wave_file);
00209         fclose(wave_file);
00210         }
00211     Thread::wait(10);
00212     }
00213 }
00214 
00215 void Location_thread(void const *argument)
00216 {
00217     while(1){
00218         //stdio_mutex.lock();
00219         //uLCD.locate(1,1);
00220         //uLCD.printf("%5.0d", myRobot.getXPosition());
00221         //uLCD.locate(8,1);
00222         //uLCD.printf("%5.0d", myRobot.getYPosition());
00223         //stdio_mutex.unlock();
00224     Thread::wait(10);
00225     }
00226 }
00227 
00228 void ThumbStick_thread(void const *argument)
00229 {
00230     while(1) {
00231             // Player Movement checked with navigation switch
00232             if (myNav.left() ) 
00233             {            
00234                 stdio_mutex.lock();
00235                 myRobot.moveLeft();
00236                 stdio_mutex.unlock();   
00237             } 
00238             
00239             if (myNav.right()) 
00240             {             
00241                 stdio_mutex.lock(); 
00242                 myRobot.moveRight();  
00243                 stdio_mutex.unlock();             
00244             }
00245             
00246             if (myNav.up()) 
00247             {                
00248                 stdio_mutex.lock();
00249                 myRobot.moveForward();
00250                 stdio_mutex.unlock();              
00251             }
00252             
00253             if (myNav.down()) 
00254             {
00255                 stdio_mutex.lock();
00256                 myRobot.moveBackward();
00257                 stdio_mutex.unlock(); 
00258             }
00259         Thread::wait(150);
00260     }
00261 }
00262 
00263 int main()
00264 {
00265     //pb1.mode(PullUp);
00266     // Delay for initial pullup to take effect
00267     //Thread::wait(1000*.01);
00268     // Setup Interrupt callback functions for a pb hit
00269     //pb1.attach_deasserted(&pb1_hit_callback);
00270     // Start sampling pb inputs using interrupts
00271     //pb1.setSampleFrequency();
00272     //Initialize Background
00273     uLCD.baudrate(500000);
00274     uLCD.filled_rectangle(0, 0 , 127, 127, RED); 
00275     myRobot.drawOutline();
00276     while(i <= 116){  
00277         while(j <= 116){    
00278         myRobot.drawHelper(i,j);
00279         j = j + 21;
00280         }
00281     j = 11;
00282     i = i + 21;
00283     }
00284     myRobot.drawFrog();
00285     
00286     Thread thread1(AutoPilot_thread);
00287     Thread thread2(Location_thread);
00288     Thread thread3(ThumbStick_thread);
00289     Thread thread4(SFX_thread);
00290     Thread thread5(Traffic1_thread);
00291     Thread thread6(Traffic2_thread);
00292     Thread thread7(DeathCheck_thread);
00293     
00294     while (1) {
00295         
00296         Thread::wait(500);
00297     }
00298 }