Bluetooth

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

main.cpp

Committer:
jwalker366
Date:
21 months ago
Revision:
2:655c8a5c2ad8
Parent:
1:f264a7ecd285
Child:
3:2499b6bda80f

File content as of revision 2:655c8a5c2ad8:

/*
    Jared Walker
    Ridwan Sadiq
    Emanuel Abdul-Salaam
    Juan Padilla
*/
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */
#include <ctime>
#include "mbed.h"
#include "uLCD_4DGL.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);


int but_pushed = -1; // ==1 -> pb1 pushed, == 2 -> pb2 pushed
bool selectedDiceArray[5];
int accFlag = 1; // this is gonna be the variable that the accelerometer flips to roll the dice


int selectDice()
{
    char bnum=0;
    char bhit=0;
    int diceNum = 0;
    
    if (blue.getc()=='!') {
        if (blue.getc()=='B') { //button data packet
            bnum = blue.getc(); //button number
            bhit = blue.getc(); //1=hit, 0=release
            if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
                if ((bnum>='1')&&(bnum<='5')){ //is a number button 1..4
                    diceNum = bnum-48;
                    selectedDiceArray[diceNum]= 1;
                    if (bhit=='1') {
                        myled = bnum - '0';
                    } else {
                        myled = '0';
                    }
                }
            }
        }
    }
    return diceNum;
}


void draw_start(){
    ulcd.text_width(2); 
    ulcd.text_height(2);
    ulcd.locate(2,2);
    ulcd.printf("\n YAHTZEE");
    
    ulcd.text_width(1); 
    ulcd.text_height(1);
    ulcd.locate(4,8);
    ulcd.printf("\n   Press to Roll");

    // Processing
    
    
}


void draw_readyRoll(){
    ulcd.text_width(1); 
    ulcd.text_height(1);
    ulcd.locate(4,1);
    ulcd.printf("\n   Ready to Roll");
    
    ulcd.text_width(2); 
    ulcd.text_height(2);
    ulcd.locate(2,1);
    ulcd.printf("\n  Press\n  Roll\n Button\n to Roll");

    // Processing
    
    
}


void draw_diceDisp(){
    ulcd.text_width(1); 
    ulcd.text_height(1);
    ulcd.locate(1,1);
    ulcd.printf("Do you want to select any dice?");

    // Processing
    
    
}


void draw_diceSelect(){
    ulcd.text_width(1); 
    ulcd.text_height(1);
    ulcd.locate(1,1);
    ulcd.printf("Press button 1 to roll again");
    ulcd.printf("\n Press button 2 to break");
    
    // Processing
    
    
}


void draw_scoreScreen(){
    ulcd.text_width(1); 
    ulcd.text_height(1);
    ulcd.locate(1,1);
    ulcd.printf("Please score your round and reset.");
    
    // Processing
    
    
}

// 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 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);
        switch(diceFaceNum){
            case 1://if dice =1
                uLCD.filled_circle(xarry[diceLoc] , yarry[diceLoc] ,3, WHITE);
                num1++;
            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++;

            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++;

            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++;

            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++;
            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++;
                break;  
            
            default:
                break;
            }
//end dice printing function 


    int shakeDetect(int accFlag){   
        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
                           
        double x = 0;
        while(1){      
            acc.readXGravity(&x); //notice this is passed by reference use pointers
       
            if(x >= .6){
                accFlag = 0;
                break;
                }
            }
    return accFlag;// this is to pass back the value of the acc flag so I dont do an extra run.
}
// 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]);
        
        /* pseudocode to match flow block diagram
        but_pushed = -1;
        while(but_pushed != 2){ // enter while if 1 pressed (yes) -> (initially -1)
            but_pushed = -1; 
            while (but_pushed != 1){ 
                draw_readyRoll();
                draw_diceDisp();
            }
            draw_diceSelect();
        }
        */
//        draw_scoreScreen();
    }
}