Bluetooth

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

Revision:
3:2499b6bda80f
Parent:
2:655c8a5c2ad8
--- a/main.cpp	Sat Dec 03 17:30:14 2022 +0000
+++ b/main.cpp	Mon Dec 05 04:19:54 2022 +0000
@@ -8,17 +8,31 @@
 #include <ctime>
 #include "mbed.h"
 #include "uLCD_4DGL.h"
+//#include "SDFileSystem.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+#include "PinDetect.h"
+#include "MMA8452.h"
+#include "Speaker.h"
 
 MMA8452 acc(p28, p27, 40000); //instantiate an acc object
 BusOut myled(LED1,LED2,LED3,LED4);
 Serial blue(p9,p10);
 Serial pc(USBTX, USBRX);
 uLCD_4DGL ulcd(p13,p14,p11);
-
+DigitalIn pb2(p24);//check later if thats ok
+DigitalIn pb1(p25);//check later if thats ok
+SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
+AnalogOut speaker(p26); 
+wave_player waver(&speaker);
 
-int but_pushed = -1; // ==1 -> pb1 pushed, == 2 -> pb2 pushed
+int screenBreak = 0;
+int but_pushed1 = 0; // ==1 -> pb1 pushed, == 2 -> pb2 pushed
+int but_pushed2 = 0;
 bool selectedDiceArray[5];
-int accFlag = 1; // this is gonna be the variable that the accelerometer flips to roll the dice
+int faceValCarry[5];
+int randoArray[5]; 
+int accFlag = 0; // this is gonna be the variable that the accelerometer flips to roll the dice
 
 
 int selectDice()
@@ -64,6 +78,15 @@
     
 }
 
+void shaking_sound() {
+    FILE *wave_file;
+    //open wav file and play it
+    wave_file=fopen("/sd/thread_sound.wav","r");
+    waver.play(wave_file);
+    fclose(wave_file);
+    //end of program
+    wait(.2);
+}
 
 void draw_readyRoll(){
     ulcd.text_width(1); 
@@ -93,6 +116,12 @@
     
 }
 
+int roll(){
+
+    int rando = (rand()%6)+1; 
+    
+    return rando;
+}
 
 void draw_diceSelect(){
     ulcd.text_width(1); 
@@ -108,6 +137,7 @@
 
 
 void draw_scoreScreen(){
+    // I think we should add "ulcd.cls;" to declutter the screen sometimes, or print a black background
     ulcd.text_width(1); 
     ulcd.text_height(1);
     ulcd.locate(1,1);
@@ -119,100 +149,184 @@
 }
 
 // This fuctuion prints a single dice to the screen, but can be used over and over to print every dice
-    int xarry[] = {22,22,64,64,105};//holds the center point for every dice on the X axis (for uLCD)
-    int yarry[] = {32,96,32,96,32};//holds the center point for every dice on the Y axis (for uLCD)
-void diceLoc(int diceLoc,int diceFaceNum)//diceLoc is the location of the dice (1-5) and diceFaceNum is the number on the dice face (provided by a random number gen
+    int xarry[] = {22,22,64,64,105};//holds the center point for every dice on the X axis (for ulcd)
+    int yarry[] = {32,96,32,96,32};//holds the center point for every dice on the Y axis (for ulcd)
+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
 { 
         
             int x1=xarry[diceLoc] + 20;
             int x2=xarry[diceLoc] - 20;
             int y1=yarry[diceLoc] + 20;
             int y2=yarry[diceLoc] - 20;
-            uLCD.rectangle(x1,y1,x2,y2,WHITE);
+            if(color == 1){
+                ulcd.rectangle(x1,y1,x2,y2,WHITE);
+            }
+            else if(color == 2){
+                ulcd.rectangle(x1,y1,x2,y2,BLUE);
+            }
         switch(diceFaceNum){
             case 1://if dice =1
-                uLCD.filled_circle(xarry[diceLoc] , yarry[diceLoc] ,3, WHITE);
-                num1++;
+                ulcd.filled_circle(xarry[diceLoc] , yarry[diceLoc] ,3, WHITE);
             break;
             
             case 2://if dice =2
          
-                uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10), 3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
-                num2++;
+                ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10), 3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
 
             break;
             
             case 3://so on
-                uLCD.filled_circle((xarry[diceLoc]) , (yarry[diceLoc]) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc] - 10) ,3, WHITE);
-                num3++;
+                ulcd.filled_circle((xarry[diceLoc]) , (yarry[diceLoc]) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc] - 10) ,3, WHITE);
 
             break;  
                       
             case 4://so forth 
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
-                num4++;
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
 
             break; 
                        
             case 5:
               
-                uLCD.filled_circle((xarry[diceLoc]) , (yarry[diceLoc]) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
-                num5++;
+                ulcd.filled_circle((xarry[diceLoc]) , (yarry[diceLoc]) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
             break;                
             case 6:
        
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]) ,3, WHITE);
-                uLCD.filled_circle((xarry[diceLoc]- 10 ) , (yarry[diceLoc]) ,3, WHITE);
-                num6++;
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+ 10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]) ,3, WHITE);
+                ulcd.filled_circle((xarry[diceLoc]- 10 ) , (yarry[diceLoc]) ,3, WHITE);
                 break;  
             
             default:
                 break;
             }
+}
 //end dice printing function 
 
 
-    int shakeDetect(int accFlag){   
+void shakeDetect()
+{   
         acc.setBitDepth(MMA8452::BIT_DEPTH_12);
         acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
-        acc.setDataRate(MMA8452::RATE_100);// dont worry about this garbage. it just sets some internal values for the acc
-                           
+        acc.setDataRate(MMA8452::RATE_100);// dont worry about this garbage. it just sets some internal values for the acc                  
         double x = 0;
-        while(1){      
+        double y = 0;
+        
+        while(1)
+        {      
             acc.readXGravity(&x); //notice this is passed by reference use pointers
-       
-            if(x >= .6){
-                accFlag = 0;
+            acc.readYGravity(&y);
+            if(x >= .6 || y >= .6)
+            {
+                accFlag = 1;
                 break;
-                }
-            }
-    return accFlag;// this is to pass back the value of the acc flag so I dont do an extra run.
-}
-// end acc stuff
+            }        
+        }
+        
+}// end acc stuff
 
 int main()
 {
+
     ulcd.cls();
     ulcd.baudrate(3000000); //jack up baud rate to max for fast display
-    int selectedDice = 0;
-    while(1) {
-        draw_start();
-        selectedDice = selectDice();
-        pc.printf("Selected Dice: '%d  %d'\n", selectedDice, selectedDiceArray[selectedDice]);
+    //int selectedDice = 0;
+    
+    while(1)
+    {
+        draw_start(); 
+        //audio needed here
+        
+        screenBreak = pb1;
+        if(screenBreak == 1)
+        {
+            break;
+        }
+    }
+    
+    screenBreak = 0;
+    
+    for (int j=0; 3 ;++j)
+    { //tbis is where the Game happens, breaks after j = 3 for three rounds   
+        while(1)
+        {
+            draw_readyRoll();
+            screenBreak = pb1;   
+            shakeDetect();
+            if (accFlag== 1 && screenBreak == 1)
+            {
+                break;
+            }
+            
+        }
+        accFlag = 0;
+        screenBreak = 0;
+        for(int k = 0; 5 ; ++k)
+        {
+            if(selectedDiceArray[k] == false)
+            {
+            randoArray[k] = roll();
+            }
+        }
+     
+        for (int i=0; 5 ;++i)
+        { //tbis part prints the dice on the ulcd 
+
+            if(selectedDiceArray[i] == false)
+            {//this wont reprint the values of the duce if the dice has been selected on the bluetooth   
+                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
+            }
+            else if (selectedDiceArray[i] == true)
+            {
+                diceLoc(i,randoArray[i],2);
+                //faceValCarry[i] = randoArray[i];// I think this works, what I am tring to do is retain the random value I generated earlier
+            }
+        }
+        
+        draw_diceDisp();
+        wait(2);
+        
+        while(1)
+        {
+            draw_diceSelect();
+            but_pushed1 = pb1;
+            but_pushed2 = pb2;
+            if(but_pushed1==1)
+            {
+                    selectDice();
+                    break;
+            }
+            
+            else if(but_pushed2==1)
+            {    
+                    break;
+            }
+        }  
+    }
+        
+        while (1)
+        {//score screen
+            draw_scoreScreen();
+        }
+            
+
+}//end main()
+
+    
+        //selectedDice = selectDice();
+        //pc.printf("Selected Dice: '%d  %d'\n", selectedDice, selectedDiceArray[selectedDice]);
         
         /* pseudocode to match flow block diagram
         but_pushed = -1;
@@ -226,5 +340,4 @@
         }
         */
 //        draw_scoreScreen();
-    }
-}
+