ECE_4180_Project / Mbed 2 deprecated Final_Project_ECE_4180

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Jared Walker
00003     Ridwan Sadiq
00004     Emanuel Abdul-Salaam
00005     Juan Padilla
00006 */
00007 #include <time.h>       /* time_t, struct tm, difftime, time, mktime */
00008 #include <ctime>
00009 #include "mbed.h"
00010 #include "uLCD_4DGL.h"
00011 //#include "SDFileSystem.h"
00012 #include "SDFileSystem.h"
00013 #include "wave_player.h"
00014 #include "PinDetect.h"
00015 #include "MMA8452.h"
00016 #include "Speaker.h"
00017 
00018 MMA8452 acc(p28, p27, 40000); //instantiate an acc object
00019 BusOut myled(LED1,LED2,LED3,LED4);
00020 Serial blue(p9,p10);
00021 Serial pc(USBTX, USBRX);
00022 uLCD_4DGL ulcd(p13,p14,p11);
00023 DigitalIn pb2(p24);//check later if thats ok
00024 DigitalIn pb1(p25);//check later if thats ok
00025 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
00026 AnalogOut speaker(p26); 
00027 wave_player waver(&speaker);
00028 
00029 int screenBreak = 0;
00030 int but_pushed1 = 0; // ==1 -> pb1 pushed, == 2 -> pb2 pushed
00031 int but_pushed2 = 0;
00032 bool selectedDiceArray[5];
00033 int faceValCarry[5];
00034 int randoArray[5]; 
00035 int accFlag = 0; // this is gonna be the variable that the accelerometer flips to roll the dice
00036 
00037 
00038 int selectDice()
00039 {
00040     char bnum=0;
00041     char bhit=0;
00042     int diceNum = 0;
00043     
00044     if (blue.getc()=='!') {
00045         if (blue.getc()=='B') { //button data packet
00046             bnum = blue.getc(); //button number
00047             bhit = blue.getc(); //1=hit, 0=release
00048             if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
00049                 if ((bnum>='1')&&(bnum<='5')){ //is a number button 1..4
00050                     diceNum = bnum-48;
00051                     selectedDiceArray[diceNum]= 1;
00052                     if (bhit=='1') {
00053                         myled = bnum - '0';
00054                     } else {
00055                         myled = '0';
00056                     }
00057                 }
00058             }
00059         }
00060     }
00061     return diceNum;
00062 }
00063 
00064 
00065 void draw_start(){
00066     ulcd.text_width(2); 
00067     ulcd.text_height(2);
00068     ulcd.locate(2,2);
00069     ulcd.printf("\n YAHTZEE");
00070     
00071     ulcd.text_width(1); 
00072     ulcd.text_height(1);
00073     ulcd.locate(4,8);
00074     ulcd.printf("\n   Press to Roll");
00075 
00076     // Processing
00077     
00078     
00079 }
00080 
00081 void shaking_sound() {
00082     FILE *wave_file;
00083     //open wav file and play it
00084     wave_file=fopen("/sd/thread_sound.wav","r");
00085     waver.play(wave_file);
00086     fclose(wave_file);
00087     //end of program
00088     wait(.2);
00089 }
00090 
00091 void draw_readyRoll(){
00092     ulcd.text_width(1); 
00093     ulcd.text_height(1);
00094     ulcd.locate(4,1);
00095     ulcd.printf("\n   Ready to Roll");
00096     
00097     ulcd.text_width(2); 
00098     ulcd.text_height(2);
00099     ulcd.locate(2,1);
00100     ulcd.printf("\n  Press\n  Roll\n Button\n to Roll");
00101 
00102     // Processing
00103     
00104     
00105 }
00106 
00107 
00108 void draw_diceDisp(){
00109     ulcd.text_width(1); 
00110     ulcd.text_height(1);
00111     ulcd.locate(1,1);
00112     ulcd.printf("Do you want to select any dice?");
00113 
00114     // Processing
00115     
00116     
00117 }
00118 
00119 int roll(){
00120 
00121     int rando = (rand()%6)+1; 
00122     
00123     return rando;
00124 }
00125 
00126 void draw_diceSelect(){
00127     ulcd.text_width(1); 
00128     ulcd.text_height(1);
00129     ulcd.locate(1,1);
00130     ulcd.printf("Press button 1 to roll again");
00131     ulcd.printf("\n Press button 2 to break");
00132     
00133     // Processing
00134     
00135     
00136 }
00137 
00138 
00139 void draw_scoreScreen(){
00140     // I think we should add "ulcd.cls;" to declutter the screen sometimes, or print a black background
00141     ulcd.text_width(1); 
00142     ulcd.text_height(1);
00143     ulcd.locate(1,1);
00144     ulcd.printf("Please score your round and reset.");
00145     
00146     // Processing
00147     
00148     
00149 }
00150 
00151 // This fuctuion prints a single dice to the screen, but can be used over and over to print every dice
00152     int xarry[] = {22,22,64,64,105};//holds the center point for every dice on the X axis (for ulcd)
00153     int yarry[] = {32,96,32,96,32};//holds the center point for every dice on the Y axis (for ulcd)
00154 void diceLoc(int diceLoc,int diceFaceNum,int color)//diceLoc is the location of the dice (1-5) and diceFaceNum is the number on the dice face (provided by a random number gen
00155 { 
00156         
00157             int x1=xarry[diceLoc] + 20;
00158             int x2=xarry[diceLoc] - 20;
00159             int y1=yarry[diceLoc] + 20;
00160             int y2=yarry[diceLoc] - 20;
00161             if(color == 1){
00162                 ulcd.rectangle(x1,y1,x2,y2,WHITE);
00163             }
00164             else if(color == 2){
00165                 ulcd.rectangle(x1,y1,x2,y2,BLUE);
00166             }
00167         switch(diceFaceNum){
00168             case 1://if dice =1
00169                 ulcd.filled_circle(xarry[diceLoc] , yarry[diceLoc] ,3, WHITE);
00170             break;
00171             
00172             case 2://if dice =2
00173          
00174                 ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10), 3, WHITE);
00175                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
00176 
00177             break;
00178             
00179             case 3://so on
00180                 ulcd.filled_circle((xarry[diceLoc]) , (yarry[diceLoc]) ,3, WHITE);
00181                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
00182                 ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc] - 10) ,3, WHITE);
00183 
00184             break;  
00185                       
00186             case 4://so forth 
00187                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
00188                 ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+10) ,3, WHITE);
00189                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
00190                 ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
00191 
00192             break; 
00193                        
00194             case 5:
00195               
00196                 ulcd.filled_circle((xarry[diceLoc]) , (yarry[diceLoc]) ,3, WHITE);
00197                 ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
00198                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
00199                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
00200                 ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
00201             break;                
00202             case 6:
00203        
00204                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
00205                 ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+10) ,3, WHITE);
00206                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
00207                 ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
00208                 ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]) ,3, WHITE);
00209                 ulcd.filled_circle((xarry[diceLoc]- 10 ) , (yarry[diceLoc]) ,3, WHITE);
00210                 break;  
00211             
00212             default:
00213                 break;
00214             }
00215 }
00216 //end dice printing function 
00217 
00218 
00219 void shakeDetect()
00220 {   
00221         acc.setBitDepth(MMA8452::BIT_DEPTH_12);
00222         acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
00223         acc.setDataRate(MMA8452::RATE_100);// dont worry about this garbage. it just sets some internal values for the acc                  
00224         double x = 0;
00225         double y = 0;
00226         
00227         while(1)
00228         {      
00229             acc.readXGravity(&x); //notice this is passed by reference use pointers
00230             acc.readYGravity(&y);
00231             if(x >= .6 || y >= .6)
00232             {
00233                 accFlag = 1;
00234                 break;
00235             }        
00236         }
00237         
00238 }// end acc stuff
00239 
00240 int main()
00241 {
00242 
00243     ulcd.cls();
00244     ulcd.baudrate(3000000); //jack up baud rate to max for fast display
00245     //int selectedDice = 0;
00246     
00247     while(1)
00248     {
00249         draw_start(); 
00250         //audio needed here
00251         
00252         screenBreak = pb1;
00253         if(screenBreak == 1)
00254         {
00255             break;
00256         }
00257     }
00258     
00259     screenBreak = 0;
00260     
00261     for (int j=0; 3 ;++j)
00262     { //tbis is where the Game happens, breaks after j = 3 for three rounds   
00263         while(1)
00264         {
00265             draw_readyRoll();
00266             screenBreak = pb1;   
00267             shakeDetect();
00268             if (accFlag== 1 && screenBreak == 1)
00269             {
00270                 break;
00271             }
00272             
00273         }
00274         accFlag = 0;
00275         screenBreak = 0;
00276         for(int k = 0; 5 ; ++k)
00277         {
00278             if(selectedDiceArray[k] == false)
00279             {
00280             randoArray[k] = roll();
00281             }
00282         }
00283      
00284         for (int i=0; 5 ;++i)
00285         { //tbis part prints the dice on the ulcd 
00286 
00287             if(selectedDiceArray[i] == false)
00288             {//this wont reprint the values of the duce if the dice has been selected on the bluetooth   
00289                 diceLoc(i,randoArray[i],1);// print function for dice. i is the location of the dice and rando is a random number for the dice face
00290             }
00291             else if (selectedDiceArray[i] == true)
00292             {
00293                 diceLoc(i,randoArray[i],2);
00294                 //faceValCarry[i] = randoArray[i];// I think this works, what I am tring to do is retain the random value I generated earlier
00295             }
00296         }
00297         
00298         draw_diceDisp();
00299         wait(2);
00300         
00301         while(1)
00302         {
00303             draw_diceSelect();
00304             but_pushed1 = pb1;
00305             but_pushed2 = pb2;
00306             if(but_pushed1==1)
00307             {
00308                     selectDice();
00309                     break;
00310             }
00311             
00312             else if(but_pushed2==1)
00313             {    
00314                     break;
00315             }
00316         }  
00317     }
00318         
00319         while (1)
00320         {//score screen
00321             draw_scoreScreen();
00322         }
00323             
00324 
00325 }//end main()
00326 
00327     
00328         //selectedDice = selectDice();
00329         //pc.printf("Selected Dice: '%d  %d'\n", selectedDice, selectedDiceArray[selectedDice]);
00330         
00331         /* pseudocode to match flow block diagram
00332         but_pushed = -1;
00333         while(but_pushed != 2){ // enter while if 1 pressed (yes) -> (initially -1)
00334             but_pushed = -1; 
00335             while (but_pushed != 1){ 
00336                 draw_readyRoll();
00337                 draw_diceDisp();
00338             }
00339             draw_diceSelect();
00340         }
00341         */
00342 //        draw_scoreScreen();
00343