hahaha

Dependencies:   mbed

main.cpp

Committer:
siwakon
Date:
2016-12-06
Revision:
2:ce3ee4bc8cf7
Parent:
1:d8ce226c8c2e

File content as of revision 2:ce3ee4bc8cf7:

#include "mbed.h"
#include "bmu.h"

InterruptIn lim(D3);
InterruptIn rotate(D4);
InterruptIn choose(D5);
//Ticker flipper;
Ticker Update;

bmuimu imu;

Serial pc(USBTX,USBRX);
Serial mas(D8,D2);
Serial duino(PA_11, PA_12);

Timer t;
Timer timer;

//*********************************************************************************************************************************
//*********************************************************************************************************************************
//*********************************************************************************************************************************
//                                                          variable
//*********************************************************************************************************************************
//*********************************************************************************************************************************
//*********************************************************************************************************************************
bool anach = false;
bool buttonState[3] = {0,0,0}; // button limit switchm rotate and choose state respectively.
unsigned int county = 10; // county down.
char data[50];
char elem;
int yyaaww;
bool errorie = false; // store error in ambigous imu data. 
int helperRoll = 0, helperPitch=0,helperYaw=0;
unsigned int indx = 0;
unsigned int state = 0;
int dete[50];
int lis[50] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};

// state    0 = initial / start.
//          1 = waiting for limit switch.

unsigned int turn = 0; // turn.

unsigned int x = 0, y = 0, z = 0,lx=0,ly=0,lz=0;
int winStatus = 0;



int A , B , C , player , button; 
int WMbox[6];
int map[5][5][5],rmap[5][5][5]; //store map.
int Check1[5][5][5] , Check2[5][5][5]; //convert to x or o map.
//*********************************************************************************************************************************
//*********************************************************************************************************************************
//*********************************************************************************************************************************
//                                                          function
//*********************************************************************************************************************************
//*********************************************************************************************************************************
//*********************************************************************************************************************************

int Cross(int degree)
        {
            if (degree == 0)
            {
                return 1;
            }else if (degree == 180)
            {
                return -1;
            }else
            {
                return 0;   
            }
        }
    
int Zine(int degree)
        {
            if (degree == 90)
            {
                return 1;
            }else if (degree == -90)
            {
                return -1;
            }else
            {
                return 0;   
            }
        }

void Rise1()
{
    buttonState[0] = 1;
}

void Fall1()
{
    buttonState[0] = 0;   
}
void Rise2()
{
    buttonState[1] = 1;
}

void Fall2()
{
    buttonState[1] = 0;   
}
void Rise3()
{
    buttonState[2] = 1;
}

void Fall3()
{
    buttonState[2] = 0;   
}

void UUpdate()
{
    imu.bmuupdate();   
}

void Sent(char arrayi[],float it,int siz)
{
    for (int i = 0;i<siz;i++)
        {
            mas.putc(char(arrayi[i]));UUpdate();
            wait(it);UUpdate();
        }   
}

void getStr()
{
    UUpdate();
    while (mas.readable())
    {
        
        elem = mas.getc();UUpdate();
        pc.printf("char is %d\n\r",elem);UUpdate();
        if ((elem == '\n'))
        {
            pc.printf("string is %s\n\r",data);UUpdate();   
            for(int i=0;i<indx;i++)
            {
                if (indx > 2)
                {
                    dete[i] = data[i];UUpdate();      
                }
            }
            for(int i=0;i<30;i++)
            {
                data[i] = '\0';
            }
            indx = 0;UUpdate();
            break;
        }else
        {
            data[indx] = elem;UUpdate();
            indx += 1;UUpdate();
        }
            
    }  
}

void LChackmape(){  //
    if (t.read() > 1) {
            
            
        duino.putc(lis[23]);UUpdate();
            
        timer.reset();UUpdate();
    }
    }
 
void LWin(){  //ชนะ
    if ( timer.read() > 1) {
    //pc.printf("Writing!\n\r");
    duino.putc( lis[0]);UUpdate();
    duino.putc( lis[1]);UUpdate();
    duino.putc( lis[2]);UUpdate();
    timer.reset();UUpdate();
 }
 }
 
void LCountDown(int ixxii){  //นับเวลา
    if ( timer.read() > 1) {
        duino.putc(ixxii);UUpdate();
        timer.reset();UUpdate();
                }
    }
         
         
void LCorrect(){  //เช็คถูก
            if ( timer.read() > 1) {
                //pc.printf("Writing!\n\r");UUpdate();
                 duino.putc(lis[18]);UUpdate();
                 timer.reset();UUpdate();
                }
            }
            
void LError(){  // 
            if ( timer.read() > 1) {
                //pc.printf("Writing!\n\r");UUpdate();
                 duino.putc( lis[19]);UUpdate();
                 timer.reset();UUpdate();
                }
            }
void LMapComplete(){  //ลงค่าเสร็จ
        if ( timer.read() > 1) {
                //pc.printf("Writing!\n\r");UUpdate();
                
                
                 duino.putc( lis[20]);UUpdate();
                
                
                 timer.reset();UUpdate();}}
                       
void LWbox(){  //ตัวหลัก
            if ( timer.read() > 1) {
                 duino.putc( lis[21]);UUpdate();
                 timer.reset();UUpdate();
                    }
    }
            
void LRotateComplete(){  //หมุน
            if ( timer.read() > 1) {
                //pc.printf("Writing!\n\r");UUpdate();
                
                
                 duino.putc( lis[22]);UUpdate();
                
                
                 timer.reset();UUpdate();
                        }
            }


            
void LLose(){ //แพ้
            
            if ( t.read() > 1) {
                //pc.printf("Writing!\n\r");
                 duino.putc( lis[3]);UUpdate();
                 duino.putc( lis[4]);UUpdate();
                 duino.putc( lis[5]);UUpdate();
                 duino.putc( lis[6]);UUpdate();
                 t.reset();UUpdate();
                }
            if ( duino.readable())
                {
                //pc.printf("recieving %c",duino.getc());
                }
         
            }    

void Lose()
{
    LLose();UUpdate();
    Sent("los\n",0.1,4);
}

void Win()
{
    LWin();UUpdate();
    Sent("lwin\n",0.1,4);
}



bool chooseAnother()
{
    Sent("cho\n",0.1,4);
    while(1)
    {
        if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='0'))   
        {
            return true;
        }else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='x')) 
        {
            return false;
        }
    }
}

bool anotherRotate()
{
    Sent("rot\n",0.1,4);
    while(1)
    {
        if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='t'))   
        {
            return true;
        }else if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='x')) 
        {
            return false;
        }
    }
}


int    GetCoordinateX()
        {
            if ( WMbox[0] != -1)
            {
              return  WMbox[0];   
            }else
            {
                return  WMbox[3];   
            }
        }
        
        int   GetCoordinateY()
        {
            if ( WMbox[1] != -1)
            {
              return  WMbox[1];   
            }else
            {
                return  WMbox[4];   
            }
        }
        
        int    GetCoordinateZ()
        {
            if ( WMbox[2] != -1)
            {
              return  WMbox[2];   
            }else
            {
                return  WMbox[5];   
            }
        }
        
        void   ChangeOX(int xx, int yy,int zz,int lx,int ly,int lz)
        {
            if (xx != -1)
            {
                 WMbox[0] = xx;UUpdate();
                 WMbox[1] = yy;UUpdate();
                 WMbox[2] = zz;UUpdate();
                 WMbox[3] = lx;UUpdate();
                 WMbox[4] = ly;UUpdate();
                 WMbox[5] = lz;UUpdate();   
            }else
            {
                 WMbox[0] = lx;UUpdate();
                 WMbox[1] = ly;UUpdate();
                 WMbox[2] = lz;UUpdate();
                 WMbox[3] = xx;UUpdate();
                 WMbox[4] = yy;UUpdate();
                 WMbox[5] = zz;UUpdate();
            }
        }
        
        void   Choose(int box)
        {
            if (box)
            {
                 WMbox[0] = -1;UUpdate();
                 WMbox[1] = -1;UUpdate();
                 WMbox[2] = -1;UUpdate();   
            }else
            {
                 WMbox[3] = -1;UUpdate();
                 WMbox[4] = -1;UUpdate();
                 WMbox[5] = -1;UUpdate();   
            }
        }
        
        void MapRotate(int role,int pitch,int yaw)
        {
            if ((pitch == 90) || (pitch == -90)) //{{1,0,0},{0,cosy,-siny},{0,siny,cosy}}{{cosx,0,+sinx},{0,1,0},{-sinx,0,cosx}}
            {
                for (int i=0;i<5;i++)
                {
                    for (int j=0;j<5;j++)
                    {
                        for(int k=0;k<5;k++)
                        {
                             A =  ((i-2)* Cross(pitch))+((k-2)* Zine(pitch));UUpdate();
                             B =  ((j-2)* Cross(yaw))+((i-2)* Zine(pitch)* Zine(yaw))-((k-2)* Zine(yaw)* Cross(pitch));UUpdate();
                             C =  ((j-2)* Zine(yaw))-((i-2)* Cross(yaw)* Zine(pitch))+((k-2)* Cross(yaw)* Cross(pitch));UUpdate();
                             rmap[i+2][j+2][k+2]= map[i][j][k];UUpdate();
                        }   
                    }   
                }
            }else if ((role == 90) || (role == -90)) // {{cosy,0,+siny},{0,1,0},{-siny,0,cosy}}{{1,0,0},{0,cosx,-sinx},{0,sinx,cosx}}
            {
                for (int i=0;i<5;i++)
                {
                    for (int j=0;j<5;j++)
                    {
                        for(int k=0;k<5;k++)
                        {
                             A =  ((i-2)* Cross(yaw))   + ((j-2)* Zine(role)* Zine(yaw)) +((k-2)* Zine(yaw)* Cross(role));UUpdate();
                             B =  ((j-2)* Cross(role))-((k-2)* Zine(role));UUpdate();
                             C =  -((i-2)* Zine(yaw))+((j-2)* Cross(yaw)* Zine(role))+((k-2)* Cross(yaw)* Cross(role));UUpdate();
                             rmap[i+2][j+2][k+2]= map[i][j][k];UUpdate();
                        }   
                    }   
                } 
            }else
            {
                for (int i=0;i<5;i++)
                {
                    for (int j=0;j<5;j++)
                    {
                        for(int k=0;k<5;k++)
                        {
                             A =  ((i-2)* Cross(yaw))-((j-2)* Zine(yaw));UUpdate();
                             B =  ((i-2)* Zine(yaw))+((j-2)* Cross(yaw));UUpdate();
                             C =  (k-2);UUpdate();
                             rmap[i+2][j+2][k+2]= map[i][j][k];UUpdate();
                        }   
                    }   
                } 
            }
            for (int i=0;i<5;i++)
                {
                    for (int j=0;j<5;j++)
                    {
                        for(int k=0;k<5;k++)
                        {
                             map[i][j][k]= rmap[i][j][k];UUpdate();
                        }   
                    }   
                }
        }
        
        void UpdateMap(int i,int j,int k,int turner){
        if(1){
            
            switch(turner%2){                             // เลือกตำแหน่งคนลง
                
                case '0':
                i-- , j-- , k--;
                    if( map[i][j][k] == 0){
                         map[i][j][k] = 5;UUpdate();               // 5 = 'x'
                         turn++;UUpdate();
                        }
                    else{
                         Lose();UUpdate();                  // ลงไมไ่ด้
                        }
                case '1':
                    i-- , j-- , k--;
                    if( map[i][j][k] == 0){
                         map[i][j][k] = 6;UUpdate();               // 6 = 'o'
                         turn++;UUpdate();
                        }
                    else{
                         Lose();UUpdate();                  // ลงไมไ่ด้
                        }
                }
            if(i == 5 or j == 5 or k == 5){             // ลงเกิน map
                 Lose();UUpdate();
                }
            
            for(i=0 ; i<5 ; i++){          //แนว k
                for(j=0 ; j<5 ; j++){
                    for(k=0 ; k<3 ; k++){
                        if( map[i][j][k] and  map[i][j][k+1] and  map[i][j][k+2] == 5){
                             Win();UUpdate();
                                }
                        else if( map[i][j][k] and  map[i][j][k+1] and  map[i][j][k+2] == 6){
                             Win();UUpdate();
                                }
                            }
                        }
                    }
            
            for(i=0 ; i<5 ; i++){           // แนว j
                for(k=0 ; k<5 ; k++){
                    for(j=0 ; j<3 ; j++){   
                        if( map[i][j][k] and  map[i][j+1][k] and  map[i][j+2][k] == 5){
                             Win();UUpdate();
                                }
                        else if( map[i][j][k] and  map[i][j+1][k] and  map[i][j+2][k] == 6){
                             Win();UUpdate();
                                }
                            }
                        }
                    }  
            
            for(j=0 ; j<5 ; j++){           // แนว i
                for(k=0 ; k<5 ; k++){
                    for(i=0 ; i<3 ; i++){
                        if( map[i][j][k] and  map[i+1][j][k] and  map[i+2][j][k] == 5){
                             Win();UUpdate();
                                }
                        else if( map[i][j][k] and  map[i+1][j][k] and  map[i+2][j][k] == 6){
                             Win();UUpdate();
                                }
                            }
                        }
                    } 
                    
            for(i=0 ; i<5 ; i++){       // แนวทแยง i เป็นฐานขึ้น
                for(j=0 ; j<3 ; j++){
                    for(k=0 ; k<3 ; k++){
                        if( map[i][j][k] and  map[i][j+1][k+1] and  map[i][j+2][k+2] == 5){
                              Win();UUpdate();
                            }
                        else if( map[i][j][k] and  map[i][j+1][k+1] and  map[i][j+2][k+2] == 6){
                              Win();UUpdate();
                            }
                        }
                    }
                }
                
            for(j=0 ; j<5 ; j++){       // แนวทแยง j เป็นฐานขึ้น
                for(i=0 ; i<3 ; i++){
                    for(k=0 ; k<3 ; k++){
                        if( map[i][j][k] and  map[i+1][j][k+1] and  map[i+2][j][k+2] == 5){
                              Win();UUpdate();
                            }
                        else if( map[i][j][k] and  map[i+1][j][k+1] and  map[i+2][j][k+2] == 6){
                              Win();UUpdate();
                            }
                        }
                    }
                }
                
            for(k=0 ; k<5 ; k++){       // แนวทแยง k เป็นฐานขึ้น
                for(i=0 ; i<3 ; i++){
                    for(j=0 ; j<3 ; j++){
                        if( map[i][j][k] and  map[i+1][j+1][k] and  map[i+2][j+2][k] == 5){
                             Win();UUpdate();
                            }
                        else if( map[i][j][k] and  map[i+1][j+1][k] and  map[i+2][j+2][k] == 6){
                              Win();UUpdate();
                            }
                        }
                    }
                }
                
            for(i=0 ; i<5 ; i++){       // แนวทแยง i เป็นฐานลง
                for(j=0 ; j<3 ; j++){
                    for(k=4 ; k>1 ; k--){
                        if( map[i][j][k] and  map[i][j+1][k-1] and  map[i][j+2][k-2] == 5){
                              Win();UUpdate();
                            }
                        else if( map[i][j][k] and  map[i][j+1][k-1] and  map[i][j+2][k-2] == 6){
                              Win();UUpdate();
                            }
                        }
                    }
                }
                
            for(j=0 ; j<5 ; j++){       // แนวทแยง j เป็นฐานลง
                for(i=0 ; i<3 ; i++){
                    for(k=4 ; k>1 ; k--){
                        if( map[i][j][k] and  map[i+1][j][k-1] and  map[i+2][j][k-2] == 5){
                              Win();UUpdate();
                            }
                        else if( map[i][j][k] and  map[i+1][j][k-1] and  map[i+2][j][k-2] == 6){
                              Win();UUpdate();
                            }
                        }
                    }
                }
                
             for(k=0 ; k<5 ; k++){      // แนวทแยง k เป็นฐานลง
                for(i=0 ; i<3 ; i++){
                    for(j=4 ; j>1 ; j--){
                        if( map[i][j][k] and  map[i+1][j-1][k] and  map[i+2][j-2][k] == 5){
                              Win();UUpdate();
                            }
                        else if( map[i][j][k] and  map[i+1][j-1][k] and  map[i+2][j-2][k] == 6){
                              Win();UUpdate();
                            }
                        }
                    }
                }
            for(i=0 ; i<3 ; i++){       // แนวทแยงแบบตัด1
                for(j=0 ; j<3 ; j++){
                   for(k=0 ; k<3 ; k++){
                       if( map[i][j][k] and  map[i+1][j+1][k+1] and  map[i+2][j+2][k+2] == 5){
                             Win();UUpdate();
                           }   
                        else if( map[i][j][k] and  map[i+1][j+1][k+1] and  map[i+2][j+2][k+2] == 6){
                             Win();UUpdate();
                            }
                        }
                    }
                }
            for(i=4 ; i>1 ; i--){       // แนวทแยงแบบตัด2
                for(j=0 ; j<3 ; j++){
                   for(k=0 ; k<3 ; k++){
                       if( map[i][j][k] and  map[i-1][j+1][k+1] and  map[i-2][j+2][k+2] == 5){
                             Win();UUpdate();
                           }   
                        else if( map[i][j][k] and  map[i-1][j+1][k+1] and  map[i-2][j+2][k+2] == 6){
                             Win();UUpdate();
                            }
                        }
                    }
                }
            for(i=4 ; i>1 ; i--){       // แนวทแยงแบบตัด 3
                for(j=4 ; j>1 ; j--){
                   for(k=0 ; k<3 ; k++){
                       if( map[i][j][k] and  map[i-1][j-1][k+1] and  map[i-2][j-2][k+2] == 5){
                             Win();UUpdate();
                           }   
                        else if( map[i][j][k] and  map[i-1][j-1][k+1] and  map[i-2][j-2][k+2] == 6){
                             Win();UUpdate();
                            }
                        }
                    }
                }
            for(i=0 ; i<3 ; i++){       // แนวทแยงแบบตัด4
                for(j=4 ; j>1 ; j--){
                   for(k=0 ; k<3 ; k++){
                       if( map[i][j][k] and  map[i+1][j-1][k+1] and  map[i+2][j-2][k+2] == 5){
                             Win();UUpdate();
                           }   
                        else if( map[i][j][k] and  map[i+1][j-1][k+1] and  map[i+2][j-2][k+2] == 6){
                             Win();UUpdate();
                            }
                        }
                    }
                }
                   
                    
                }
                
            }
     
        
        
        void   Check_map(){                   //แสดง map
            for( A=0 ;  A<5 ;  A++){
                for( B=0 ;  B<5 ;  B++){ 
                    for( C=0 ;  C<5 ;  C++){
                        if( map[ A][ B][ C] == 5){
                             Check1[ A][ B][ C] = 1;UUpdate();
                            }
                        else if( map[ A][ B][ C] == 6){
                             Check2[ A][ B][ C] = 1;UUpdate();
                            }
                        else{
                             Check1[ A][ B][ C] = 0;UUpdate();
                             Check2[ A][ B][ C] = 0;UUpdate();
                            }
                        } 
                    }
                }
            }
            
            


int main() // main program of 
{
    /*lim.rise(&Rise1);
    lim.fall(&Fall1);
    rotate.rise(&Rise2);
    rotate.fall(&Fall2);
    choose.rise(&Rise3);
    choose.fall(&Fall3);*/
    /*for(int i=0;i<200;i++)
    {
        UUpdate();
    }*/
    //LInitial(); // set initial pattern in led matrix.
    wait(5); // wait 5 second for putting the box down to set initial position of imu.
    //imu.Initial(); // set initial for imu.
    //Update.attach(&UUpdate,0.1); //timer interrupt for recieving bluetooth. 
    //while((dete[0]!='i')||(dete[1]!='n')||(dete[2]!='i')||(dete[3]!='t'))
    //{
    //}
    LWbox(); // set led to tell the player that the game is ready and this is the woman-box.
    t.start();
    while (1) // void loop.
    {
        state = 1;UUpdate();
        UUpdate();
        if (buttonState[0] == 1) // if the boxes are connected.
        {
            x = GetCoordinateX();UUpdate();
            y = GetCoordinateY();UUpdate(); 
            z = GetCoordinateZ();UUpdate(); // get the position of main box which linking with map or the box not be choosen.
            /*Sent("helr\n",0.1,5); // tell the other box, that we want helper roll.
            while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='r'))
            {
                pc.printf("srt %s\n\r",dete);     
            }
            helperRoll = int(  ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48)  ); //get roll from another box.
            Sent("help\n",0.1,5); // tell the other box, that we want helper roll.
            while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='p'))
            {
                pc.printf("srt %s\n\r",dete);     
            }
            helperPitch = int(  ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48)  ); //get roll from another box.
            Sent("hely\n",0.1,5); // tell the other box, that we want helper roll.
            while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='y'))
            {
                pc.printf("srt %s\n\r",dete);     
            }
            helperYaw = int(  ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48)  ); //get roll from another box.*/
            //Sent("ok\n",0.1,3); //free the otherbox.
            
            errorie = imu.Getposition(x,y,z);//,helperRoll,helperPitch,helperYaw);UUpdate(); // calculate position from imu.
            if (errorie)
            {
                LError();UUpdate();
                while(buttonState[0] == 1)
                {
                   UUpdate();    
                }
            }else
            {
                lx = x;UUpdate();
                ly = y;UUpdate();
                lz = z;UUpdate();
                x = imu.X_pos;UUpdate();
                y = imu.Y_pos;UUpdate();
                z = imu.Z_pos;UUpdate(); // get new possition.
                
                UpdateMap(x,y,z,turn);UUpdate();//,helperRoll); //update new position in map.
                
                ChangeOX(x,y,z,lx,ly,lz);UUpdate();
                
                    Sent("mapc\n",0.1,4); //map complete.
                    LMapComplete();UUpdate(); // show that this position is ok.
                    while(1)
                    {
                        UUpdate();
                        anach = chooseAnother() ;UUpdate();// wheather thr others box is choosen. 
                        if ((buttonState[2]==1) && (!anach)) 
                        {
                            Sent("ch1\n",0.1,4);
                            
                            Choose(1);UUpdate(); // choose main box.
                        }else if((buttonState[2]==0) && (anach))
                        {
                            Choose(0);UUpdate(); // choose another box.
                            
                        }else if(buttonState[2]==0) 
                        {
                            if((buttonState[1]==0)||(anotherRotate())) // click buutton some rotate box.
                            {
                                state = 3;UUpdate();
                                while((buttonState[1]==0)&&(anotherRotate()))
                                {
                                    UUpdate();
                                }
                                
                                yyaaww = imu.Rotate();UUpdate(); // check wheater rotating is error. 
                                MapRotate(0,0,yyaaww);UUpdate();//imu.Rotate_Yaw); // private variable/arttribute in imu class.
                                LRotateComplete();UUpdate();  
                            }  
                        } //close choose or rotate.
                     } //close while true.
                     turn ++;UUpdate(); 
                     Sent("T++\n",0.1,4);
                     t.start();UUpdate();
                     state = 2;UUpdate();
                     //LChooseComplete();
            } // close not error.
            
        }else // if the boxes aren't connect.
        {
            if (county == 0) // time out.
            {
                t.stop();UUpdate();
                Lose();UUpdate(); // lose state
            }else if(turn != 0)
            {
                t.stop();UUpdate();
                if (t.read() > 1.0)
                {
                    t.start();UUpdate();
                    county -= 1;UUpdate(); // time decreasing.
                }
            } // close check time
            LCountDown(county);UUpdate();
            Sent("Tur\n",0.1,4);
        } // close boxes aren't connect.
        
    } // close loop.
    
    
    
} // close main.*/