Scott Williams / Mbed 2 deprecated 4180_Lab4_v6

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