Davon Cleary / Mbed 2 deprecated Lab2

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

Fork of ECE2036Lab2StarterCode by Joseph Lind

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 #include "uLCD_4DGL.h"
00004 #include "wave_player.h"
00005 #include "SDFileSystem.h"
00006 #include "ball.h"
00007 #include "paddle.h"
00008 
00009 Serial pc(USBTX, USBRX); // tx, rx
00010 SDFileSystem sd(p5, p6, p7, p8, "sd");
00011 AnalogOut DACout(p18);
00012 wave_player waver(&DACout);
00013 
00014 //Seven Segment Display
00015 DigitalOut a(p11);
00016 DigitalOut b(p12);
00017 DigitalOut c(p13);
00018 DigitalOut d(p14);
00019 DigitalOut e(p15);
00020 DigitalOut f(p16);
00021 DigitalOut g(p17);
00022 
00023 Ticker sevSegDisplay;
00024 
00025 // Pushbuttons
00026 PinDetect pbUp(p23);//p15
00027 PinDetect pbDown(p24);//p16
00028 // uLCD
00029 uLCD_4DGL uLCD(p28, p27, p29);
00030 //Ball
00031 Ball myBall;
00032 //Paddle
00033 Paddle myPaddle;
00034  
00035 // State machine definitions
00036 enum gameStateType {START, WAIT, GAME_SETUP, GAME, LOSE};
00037 /* State Definitions:
00038  * START -- Creates the start screen
00039  * WAIT -- After the start screen, goes into wait where mbed spins and does nothing
00040  * GAME_SETUP -- Sets up one time things (like boarders, initializes beginning velocity
00041  * GAME -- When the user actually gets to play
00042  * LOSE -- clears the screen, prints you lose, waits, then goes back to start
00043  */
00044  
00045 // Global state machine variable (So that the pushbuttons can modify it)
00046 gameStateType gameState = START;
00047 
00048 void dScore()
00049 {   
00050     switch(myPaddle.getScore()){
00051         case 0://Zero digit
00052             a=0;b=0;c=0;d=0;e=0;f =0;g=1;
00053             break;        
00054         case 1://One digit
00055             b=0;c =0;
00056             a=1;d=1;e=1;f=1;g = 1;
00057             break;
00058         case 2://Two digit
00059             a=0;b=0;g=0;e=0;d =0;
00060             f=1;c = 1;
00061             break;
00062         case 3://Three digit
00063             e=1;f =1;
00064             a=0;b=0;c=0;d=0;g = 0;
00065             break;
00066         case 4://Four digit
00067             f=0;g=0;b=0;c =0;
00068             a=1;d=1;e = 1;
00069             break;
00070         case 5://Five digit
00071             b=1;e =1;
00072             a=0;c=0;d=0;f=0;g = 0;
00073             break;
00074         case 6://Six digit
00075             b =1;
00076             a=0;c=0;d=0;e=0;f=0;g = 0;
00077             break;
00078         case 7://Seven digit
00079             a=0;b=0;c =0;
00080             d=1;e=1;f=1;g = 1;
00081             break;
00082         case 8://Eight digit
00083             a=0;b=0;c=0;d=0;e=0;f=0;g = 0;
00084             break;
00085         case 9://Nine digit
00086             e=0;d =0;
00087             a=1;b=1;c=1;f=1;g = 1;
00088             break;
00089             }
00090 }
00091 
00092 // Pushbutton callbacks
00093 void pbUp_hit_callback (void)
00094 {
00095     switch (gameState)
00096     {
00097     case WAIT:
00098         gameState = GAME_SETUP;
00099         break;
00100     case GAME:  
00101         myPaddle.movePaddleUp();
00102         break;
00103     }
00104 }
00105  
00106 void pbDown_hit_callback (void)
00107 {
00108     switch (gameState)
00109     {
00110     case WAIT:
00111         gameState = GAME_SETUP;
00112         break;
00113     case GAME:
00114         myPaddle.movePaddleDown();
00115         break;
00116     }
00117 }
00118  
00119 int main() 
00120 {   
00121     // This is setting up the pushbuttons
00122     pbUp.mode(PullUp);
00123     pbDown.mode(PullUp);
00124     wait(0.1);
00125     pbUp.attach_deasserted(&pbUp_hit_callback);
00126     pbDown.attach_deasserted(&pbDown_hit_callback);
00127     pbUp.setSampleFrequency();
00128     pbDown.setSampleFrequency();
00129  
00130     uLCD.display_control(PORTRAIT_R);
00131     uLCD.cls();
00132     uLCD.baudrate(BAUD_3000000);
00133     uLCD.background_color(BLACK);
00134  
00135     int i = 0;
00136     sevSegDisplay.attach(&dScore, 0.001);
00137     
00138     while (1) 
00139     {   
00140         switch (gameState)
00141         {
00142         case START:
00143             gameState = WAIT;
00144             //Bouncing Ball Demo
00145             float fx=50.0,fy=21.0,vx=1.3,vy=0.8;
00146             int x=50,y=21,radius=4;
00147             uLCD.background_color(BLACK);
00148             uLCD.cls();
00149             //draw walls
00150             uLCD.line(0, 0, 127, 0, WHITE);
00151             uLCD.line(127, 0, 127, 127, WHITE);
00152             uLCD.line(127, 127, 0, 127, WHITE);
00153             uLCD.line(0, 127, 0, 0, WHITE);
00154             while(gameState == WAIT) {//for (int i=0; i<1500; i++) {
00155                 //draw ball
00156                 uLCD.filled_circle(x, y, radius, RED);
00157                 //bounce off edge walls and slow down a bit?
00158                 if ((x<=radius+1) || (x>=126-radius)) vx = -vx;
00159                 if ((y<=radius+1) || (y>=126-radius)) vy = -vy;
00160                 //erase old ball location
00161                 uLCD.filled_circle(x, y, radius, BLACK);
00162                 //move ball
00163                 fx=fx+vx;
00164                 fy=fy+vy;
00165                 x=(int)fx;
00166                 y=(int)fy;
00167             }
00168             break;
00169         case GAME_SETUP:
00170             uLCD.cls();
00171             uLCD.line(0, 0, 127, 0, 0xCFB53B);
00172             uLCD.line(127, 0, 127, 127, 0xCFB53B);
00173             uLCD.line(127, 127, 0, 127, 0xCFB53B);
00174             uLCD.line(0, 127, 0, 0, 0xCFB53B);
00175             myBall.startPong( i, &uLCD );
00176             myPaddle.initDraw( &uLCD );
00177             gameState = GAME;
00178             break;
00179         case GAME:
00180             myBall.testConditions(&myPaddle, &uLCD);
00181             if (myBall.getLose())
00182                 gameState = LOSE;
00183             myBall.update( &uLCD );
00184             myPaddle.redraw( &uLCD );
00185             break;
00186         case LOSE:
00187             uLCD.cls();
00188             //uLCD.printf("YOU LOSE D:");
00189             myPaddle.resetScore();
00190             myBall.setLose(0);
00191             myBall.resetBall();
00192             wait(5.0);
00193             gameState = START;
00194             break;
00195         case WAIT:
00196             // Used to seed the rand() function so we don't get the same starting position every time.
00197             i++; 
00198             break;
00199         }
00200     }
00201 }