Rishi Bhargava / Mbed 2 deprecated 4180FinalLab

Dependencies:   4DGL-uLCD-SE PinDetect mbed SparkfunAnalogJoystick mbed-rtos

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 "rtos.h"
00003 #include "PinDetect.h"
00004 #include "Speaker.h"
00005 #include "soundBuilder.h"
00006 #include "SparkfunAnalogJoystick.h"
00007 #include "paddle.h"
00008 #include "ball.h"
00009 
00010 // Pushbuttons
00011 SparkfunAnalogJoystick joystick(p16, p15, p14);
00012 //PinDetect select(p13);
00013 //Speaker
00014 Speaker mySpeaker(p25);
00015 Serial pc(USBTX, USBRX);
00016  
00017 // State machine definitions
00018 enum gameStateType {START, WAIT, GAME_SETUP, GAME, WIN, LOSE};
00019 /* State Definitions:
00020  * START -- Creates the start screen
00021  * WAIT -- After the start screen, goes into wait where mbed spins and does nothing
00022  * GAME_SETUP -- Sets up one time things (like boarders, initializes beginning velocity
00023  * GAME -- When the user actually gets to play
00024  * LOSE -- clears the screen, prints you lose, waits, then goes back to start
00025  */
00026  
00027 // Global state machine variable (So that the select can modify it)
00028 gameStateType gameState = START;
00029 bool ready = false;
00030 
00031 Paddle botPaddle(0, 0, 0, 0);
00032 Paddle topPaddle(0, 0, 0, 0);
00033 Ball ball(0, 0, 0);
00034 
00035 // thread that plays game sounds through the speaker
00036 void speaker_thread(void const *argument) {
00037 
00038     Speaker *player = &mySpeaker;
00039 
00040     // Start Song
00041     float sFreq[] = {550,750,550,750};
00042     float sDur[] = {.3,.3,.3,.3};
00043     float sVol[] = {.01,.01,.01,.01};
00044     SoundBuilder startSong(sFreq, sDur, sVol, sizeof(sFreq)/sizeof(*sFreq), player);
00045     
00046     // End Song
00047     float eFreq[] = {300,200,250,225,200,150,150,100};
00048     float eDur[] = {.3,.3,.3,.3,.3,.3,.3,.3};
00049     float eVol[] = {.01,.01,.01,.01,.01,.01,.01,.01};
00050     SoundBuilder endSong(eFreq, eDur, eVol, sizeof(eFreq)/sizeof(*eFreq), player);
00051 
00052     while (true) {
00053         switch (gameState) {
00054         case GAME: // if game is running and user dodges a pipe, play a note
00055             if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), ball.getSize())
00056             || botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), ball.getSize())
00057             || ball.getFutureX() <= 10
00058             || ball.getFutureX() + ball.getSize() >= 190) {
00059                 mySpeaker.PlayNote(440, 0.1, 0.01);
00060             }
00061             
00062             if (ball.getY() < 5){
00063                 mySpeaker.PlayNote(440, 0.1, 0.01);
00064                 mySpeaker.PlayNote(880, 0.1, 0.01);
00065             }
00066             else if (ball.getY() + ball.getSize() > 245){
00067                 mySpeaker.PlayNote(880, 0.1, 0.01);
00068                 mySpeaker.PlayNote(440, 0.1, 0.01);
00069             }
00070             
00071             break;
00072         case START: // play a song at the start of the game
00073             startSong.playSong();
00074             Thread::wait(5000);
00075             break;
00076         case WIN:  // play a song when the player wins the game
00077         case LOSE: // play a song when the player loses the game
00078             endSong.playSong();
00079             Thread::wait(5000);
00080             break;
00081         }
00082     }
00083 }
00084 
00085 // thread that writes to the sd card
00086 void sd_card_thread(void const *argument) {
00087 
00088     while (true) {
00089         switch (gameState) {
00090         case WIN:
00091         case LOSE:
00092             
00093             break;
00094         }
00095     }
00096 }
00097  
00098 int main() 
00099 {   
00100     // This is setting up the joystick select as a pushbutton
00101     //joystick.set_callback(&select_hit_callback);
00102     
00103     pc.format(8, SerialBase::None, 1);
00104     pc.baud(115200);
00105 
00106     uint8_t gameLowX = 10, gameHighX = 190, gameLowY = 5, gameHighY = 245;
00107     uint8_t gameCenterX = (gameHighX-gameLowX)/2, gameCenterY = (gameHighY-gameLowY)/2;
00108     uint8_t ballSize=10;
00109     uint8_t paddleWidth = 5, paddleLength = 35;
00110     float botMove = joystick.yAxis();
00111     uint8_t botScore = 0;
00112     uint8_t topScore = 0;
00113     int i = 0;
00114 
00115     botPaddle = Paddle(gameCenterX-(paddleLength/2), gameHighY, paddleLength, paddleWidth);
00116     botPaddle.setLimits(gameLowX, gameHighX - paddleWidth);
00117     botPaddle.setMaxMove(2);
00118     topPaddle = Paddle(gameCenterX-(paddleLength/2), gameLowY, paddleLength, paddleWidth);
00119     topPaddle.setLimits(gameLowX, gameHighX - paddleWidth);
00120     topPaddle.setMaxMove(2);
00121     ball = Ball(gameCenterX, gameCenterY, ballSize);
00122     
00123     ball.setVxDir(true);
00124     ball.setVyDir(true);
00125     
00126     while (!pc.readable()){
00127     
00128     }
00129     
00130     Thread thread1(speaker_thread);
00131     Thread thread2(sd_card_thread);
00132     
00133     while (1) 
00134     {
00135         switch (gameState)
00136         {
00137         case START:
00138             if (pc.writeable()){
00139                 pc.printf("%c%c%c%c%c", 0, 0, 0, 0, 0);
00140                 ball.setVxDir(true);
00141                 ball.setVyDir(true);
00142                 gameState = WAIT;
00143             }
00144             break;
00145         case GAME_SETUP:
00146             ball.reset(gameCenterX, gameCenterY, 0, sqrt(5.0));
00147             botPaddle.reset(gameCenterX-(paddleLength/2), gameHighY);
00148             topPaddle.reset(gameCenterX-(paddleLength/2), gameLowY);
00149             if (pc.writeable()){
00150                 pc.printf("%c%c%c%c%c", 3, botScore, topScore, 0, 0);
00151                 ready = false;
00152                 srand(i);
00153                 gameState = GAME;
00154                 Thread::wait(2000);
00155             }
00156             break;
00157         case GAME:
00158             if (pc.writeable()) {
00159                 pc.printf("%c%c%c%c%c", 4, botPaddle.getX(), topPaddle.getX(), ball.getX(), ball.getY());
00160                 
00161                 uint8_t size = ball.getSize(); //stored in a temp variable because used a lot
00162                 
00163                 if (ball.getFutureX() <= gameLowX){
00164                     ball.reverseXDirection();
00165                 }
00166                 else if (ball.getFutureX() + size >= gameHighX){
00167                     ball.reverseXDirection();
00168                 }
00169                 
00170                 if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)) {
00171                     ball.reverseYDirection();
00172                     uint8_t fx = ball.getFutureX();
00173                     double newVx = topPaddle.returnAngle(fx, size);
00174                     double newVy = sqrt(5.0 - (newVx * newVx));
00175                     ball.setVx(newVx);
00176                     ball.setVy(newVy);
00177                     ball.setVxDir(topPaddle.returnDir(fx, size));
00178                 }
00179                 else if (botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)) {
00180                     ball.reverseYDirection();
00181                     uint8_t fx = ball.getFutureX();
00182                     double newVx = botPaddle.returnAngle(fx, size);
00183                     double newVy = sqrt(5.0 - (newVx * newVx));
00184                     ball.setVx(newVx);
00185                     ball.setVy(newVy);
00186                     ball.setVx(botPaddle.returnAngle(fx, size));
00187                     ball.setVxDir(botPaddle.returnDir(fx, size));
00188                 }
00189                 
00190                 if (ball.getY() < gameLowY){
00191                     botScore++;
00192                     gameState = GAME_SETUP;
00193                 }
00194                 else if (ball.getY() + size > gameHighY){
00195                     topScore++;
00196                     gameState = GAME_SETUP;
00197                 }
00198                 
00199                 if (botScore >= 5) {
00200                     gameState = WIN;
00201                 }
00202                 else if (topScore >= 5) {
00203                     gameState = LOSE;   
00204                 }
00205                 
00206                 ball.update();
00207                 botMove = -joystick.yAxis();
00208                 if (botMove < -0.1 || botMove > 0.1) {
00209                     botPaddle.move(botMove);
00210                 }
00211                 //GET OTHER PADDLE SPOT AND UPDATE topPaddle
00212             }
00213             break;
00214         case LOSE:
00215             if (pc.writeable()){
00216                 pc.printf("%c%c%c%c%c", 5, 0, 0, 0, 0);
00217                 botScore = 0;
00218                 topScore = 0;
00219                 Thread::wait(5000);
00220                 gameState = START;
00221             }
00222             break;
00223         case WIN:
00224             if (pc.writeable()){
00225                 pc.printf("%c%c%c%c%c", 6, 0, 0, 0, 0);
00226                 botScore = 0;
00227                 topScore = 0;
00228                 Thread::wait(5000);
00229                 gameState = START;
00230             }
00231             break;
00232         case WAIT:
00233             if (pc.writeable()){
00234                 // Used to seed the rand() function so the ball has a random starting direction
00235                 i++;
00236                 if (joystick.button())
00237                     ready = true;
00238                 if (ready){
00239                     pc.printf("%c%c%c%c%c", 2, 0, 0, 0, 0);
00240                     Thread::wait(2000);
00241                     gameState = GAME_SETUP;
00242                 }
00243                 else {
00244                     pc.printf("%c%c%c%c%c", 1, 0, 0, 0, 0);
00245                 }
00246             }
00247             break;
00248         }
00249         Thread::wait(20);
00250     } 
00251 }