Kevin Lin / Mbed 2 deprecated Lab4Farkle

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  
00003 Author: Kevin Lin
00004 Title: Lab 4
00005 Date: October 22 2021
00006 Description: Main function that implements the FarkleGame and its dice.
00007  
00008 **/
00009 #include "die.h"
00010 #include "farklegame.h"
00011 #include "PinDetect.h"
00012  
00013 #include "mbed.h"
00014  
00015 using namespace std;
00016  
00017 Serial pc(USBTX,USBRX);
00018 Speaker mySpeaker(p21);
00019 
00020 
00021  
00022 int main(){
00023     
00024     //clear system
00025     system("clear");
00026     
00027     //declare buttons
00028 
00029     
00030     //declare uLCD screen
00031     uLCD_4DGL uLCD(p9,p10,p11);
00032     uLCD.cls();
00033     uLCD.display_control(PORTRAIT);
00034     PinDetect Button1(p16);
00035     PinDetect Button2(p17);
00036     Button1.mode(PullUp);
00037     Button2.mode(PullUp);
00038     
00039     //declare accelerometer
00040     MMA8452 acc(p28, p27, 40000);
00041     acc.setBitDepth(MMA8452::BIT_DEPTH_12);
00042     acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
00043     acc.setDataRate(MMA8452::RATE_100);
00044     
00045     int numDice = 6;
00046     
00047     FarkleGame game;
00048     
00049     while(1){
00050         game.displayIntro(uLCD, numDice);
00051         game.restart();
00052         
00053         while(!game.enoughGravity(acc)){   
00054             if(!Button2){
00055                 numDice = (numDice+1)%7;
00056                 game.displayIntro(uLCD,numDice);
00057             }        
00058         }
00059         
00060         while(Button1){
00061             if(game.enoughGravity(acc)){
00062                 //roll dice and displar
00063                 game.rollDice(mySpeaker);
00064                 game.displayDice(uLCD, numDice);
00065                 
00066                 //update scores
00067                 game.loadVals(numDice);
00068                 game.changeScore();
00069                 
00070                 //display screens based on scores
00071                 if(game.calculateRoll() == 0){
00072                     game.displayFarkle(uLCD);
00073                     break;
00074                 }
00075                 else{
00076                     game.displayScore(uLCD);
00077                 }
00078             }
00079         }
00080         
00081         //display turn end screen
00082         game.displayTurnScore(uLCD);
00083         //wait for button to be pressed
00084         while(Button1){
00085         }
00086         //reset numDice
00087         numDice = 6;
00088     }
00089  
00090 }