Erick Cardenas / Mbed 2 deprecated Frogger1

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 "uLCD_4DGL.h"
00004 uLCD_4DGL uLCD(p28, p27, p30);
00005 #include "Robot.h"
00006 #include "Nav_Switch.h"
00007 #include "Speaker.h"
00008 #include "wave_player.h"
00009 #include "SDFileSystem.h"
00010 
00011 
00012 BusOut mbedleds(LED1,LED2,LED3,LED4);
00013 Robot myRobot;
00014 Nav_Switch myNav( p21, p22, p23, p24, p25);
00015 Mutex stdio_mutex;
00016 AnalogOut DACout(p18); // used to play sound on speaker
00017 wave_player waver(&DACout);
00018 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
00019 
00020 
00021 //Wiring Set-up:
00022 //Speaker: p18
00023 //SD Card: DO=p6,SCK=p7,DI=p5,CS=p8,VCC=Vout,GND=GND
00024 //Thumb-Stick: U=p25,C=p24,L=p23,D=p22,R=p21,+=pVout,-=GND
00025 bool Alive = 1;
00026 bool Drown = 0;
00027 bool Car = 0;
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 int Position1 = 90;
00037 int Position2 = 90;
00038 int Rate1 = 3;
00039     
00040 void AutoPilot_thread(void const *argument)
00041 {
00042     while(Alive) {
00043     //Auto-pilot Testing
00044     Thread::wait(500);
00045     stdio_mutex.lock();
00046     myRobot.drawEraser();
00047     myRobot.moveForward();
00048     myRobot.drawFrog();
00049     stdio_mutex.unlock();
00050     
00051     if(myRobot.getYPosition() == 53){   //Check dangers on Traffic 2 Row 3
00052             }
00053         if(myRobot.getYPosition() == 74){   //Check dangers on water Row 4
00054             if(myRobot.getXPosition() == 53 || myRobot.getXPosition() == 74 || myRobot.getXPosition() == 116){
00055                 Alive = 0;
00056                 Drown = 1;
00057                 
00058                 stdio_mutex.lock();
00059                 myRobot.drawDrown1();
00060                 stdio_mutex.unlock();
00061                 Thread::wait(50);
00062                 stdio_mutex.lock();
00063                 myRobot.drawDrown2();
00064                 stdio_mutex.unlock();
00065                 Thread::wait(50);
00066                 stdio_mutex.lock();
00067                 myRobot.drawDrown3();
00068                 stdio_mutex.unlock();
00069                 Thread::wait(50);
00070                 stdio_mutex.lock();
00071                 myRobot.drawDrown4();
00072                 stdio_mutex.unlock();
00073                 Thread::wait(50);
00074                 stdio_mutex.lock();
00075                 myRobot.drawDrown5();
00076                 stdio_mutex.unlock();
00077                 }
00078             }
00079         if(myRobot.getYPosition() == 95){   //Check dangers on Traffic 1 Row 5
00080             }
00081             
00082     }
00083 }
00084 
00085 void Traffic1_thread(void const *argument) //Blue Car
00086 {
00087     while(1) {
00088         Position1 = Position1 + Rate1;
00089         if(Position1 > 140 ){
00090             Position1 = -10;
00091             }
00092         /*stdio_mutex.lock();
00093         uLCD.locate(3,3);
00094         uLCD.printf("%5.0d", Position1);
00095         stdio_mutex.unlock();
00096         */
00097         
00098         stdio_mutex.lock();
00099         uLCD.filled_rectangle(Position1-10,95-4,Position1+10,95+4,BLUE);
00100         stdio_mutex.unlock();
00101         Thread::wait(30);
00102     }
00103 }
00104 
00105 void Traffic2_thread(void const *argument)  //Red Car
00106 {
00107     while(1) {
00108         Position2 = Position2 - Rate1;
00109         if(Position2 < 0 ){
00110             Position2 = 140;
00111             }
00112         stdio_mutex.lock();
00113         uLCD.filled_rectangle(Position2-10,52-4,Position2+10,52+4,RED);
00114         stdio_mutex.unlock();
00115         Thread::wait(35);
00116     }
00117 }
00118 
00119 void SFX_thread(void const *argument)
00120 {
00121     while(Alive){
00122     stdio_mutex.lock();
00123     uLCD.locate(1,1);
00124     uLCD.printf("DRY");
00125     stdio_mutex.unlock();
00126         //Thread::wait(100);
00127     }
00128     stdio_mutex.lock();
00129     uLCD.locate(1,1);
00130     uLCD.printf("SPLASH!");
00131     stdio_mutex.unlock();
00132         
00133     FILE *wave_file;
00134     wave_file=fopen("/sd/Splash.wav","r");
00135     waver.play(wave_file);
00136     fclose(wave_file);
00137     Thread::wait(100);
00138 }
00139 
00140 void Location_thread(void const *argument)
00141 {
00142     while(1){
00143         //stdio_mutex.lock();
00144         //uLCD.locate(1,1);
00145         //uLCD.printf("%5.0d", myRobot.getXPosition());
00146         //uLCD.locate(8,1);
00147         //uLCD.printf("%5.0d", myRobot.getYPosition());
00148         //stdio_mutex.unlock();
00149     Thread::wait(10);
00150     }
00151 }
00152 void ThumbStick_thread(void const *argument)
00153 {
00154     while(1) {
00155         mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs
00156         if(myNav.fire()) mbedleds = 0x0F; 
00157         
00158         // Player Movement checked with navigation switch
00159             if (myNav.left() ) 
00160             {            
00161                 myRobot.moveLeft();   
00162             } 
00163             
00164             if (myNav.right()) 
00165             {             
00166                 myRobot.moveRight();              
00167             }
00168             
00169             if (myNav.up()) 
00170             {                
00171                 myRobot.moveForward();                
00172             }
00173             
00174             if (myNav.down()) 
00175             {
00176                 myRobot.moveBackward();
00177             }
00178         Thread::wait(50);
00179     }
00180 }
00181 
00182 int main()
00183 {
00184     //Initialize Background
00185     uLCD.filled_rectangle(0, 0 , 127, 127, RED); 
00186     myRobot.drawOutline();
00187     while(i <= 116){  
00188         while(j <= 116){    
00189         myRobot.drawHelper(i,j);
00190         j = j + 21;
00191         }
00192     j = 11;
00193     i = i + 21;
00194     }
00195     myRobot.drawFrog();
00196     
00197     //Thread thread1(AutoPilot_thread);
00198     Thread thread2(Location_thread);
00199     Thread thread3(ThumbStick_thread);
00200     Thread thread4(SFX_thread);
00201     Thread thread5(Traffic1_thread);
00202     Thread thread6(Traffic2_thread);
00203     
00204     while (1) {
00205         
00206         Thread::wait(500);
00207     }
00208 }