hahaha

Dependencies:   mbed

Revision:
1:d8ce226c8c2e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main2.h	Tue Dec 06 06:11:54 2016 +0000
@@ -0,0 +1,337 @@
+#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 countDown = 9;
+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.
+
+void Rise(int but)
+{
+    buttonState[but-3] = 1;
+}
+
+void Fall(int but)
+{
+    buttonState[but-3] = 0;   
+}
+
+void Sent(char arrayi[],float it,int siz)
+{
+    for (int i = 0;i<siz;i++)
+        {
+            mas.putc(arrayi[i]);
+            wait(it);
+        }   
+}
+
+void LInitial(){  //ลงค่าเสร็จ
+    if (timer.read() > 1) {
+        
+        
+        duino.putc(lis[20]);
+        
+        
+        timer.reset();
+                }
+    if (duino.readable()){
+            }         
+}
+
+void getStr()
+{
+    while (mas.readable())
+    {
+        
+        elem = mas.getc();
+        pc.printf("char is %d\n\r",elem);
+        if ((elem == '\n'))
+        {
+            pc.printf("string is %s\n\r",data);   
+            for(int i=0;i<indx;i++)
+            {
+                if (indx > 2)
+                {
+                    dete[i] = data[i];      
+                }
+            }
+            for(int i=0;i<30;i++)
+            {
+                data[i] = '\0';
+            }
+            indx = 0;
+            break;
+        }else
+        {
+            data[indx] = elem;
+            indx += 1;
+        }
+            
+    }  
+}
+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;
+        }
+    }
+}
+
+void Rise1()
+{
+    buttonState[0] = 1;
+}
+
+void Fall1()
+{
+    buttonState[0] = 0;   
+}
+void Rise2()
+{
+    buttonState[1] = 1;
+}
+
+void Fall2()
+{
+    buttonState[1] = 0;   
+}
+void LLose(){ //แพ้
+            
+            if ( t.read() > 1) {
+                //pc.printf("Writing!\n\r");
+                 duino.putc( lis[3]);
+                 duino.putc( lis[4]);
+                 duino.putc( lis[5]);
+                 duino.putc( lis[6]);
+                 t.reset();
+                }
+            if ( duino.readable())
+                {
+                //pc.printf("recieving %c",duino.getc());
+                }
+         
+            }
+void Lose()
+{
+    LLose();
+    Sent("los\n",0.1,4);
+}
+
+void LWin(){  //ชนะ
+    if ( timer.read() > 1) {
+    //pc.printf("Writing!\n\r");
+    duino.putc( lis[0]);
+    duino.putc( lis[1]);
+    duino.putc( lis[2]);
+    timer.reset();
+ }
+ }
+void Win()
+{
+    LWin();
+    Sent("lwin\n",0.1,4);
+}
+void Rise3()
+{
+    buttonState[2] = 1;
+}
+
+void Fall3()
+{
+    buttonState[2] = 0;   
+}
+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;
+        }
+    }
+}
+void LCountDown(int ixxii){  //นับเวลา
+    if ( timer.read() > 1) {
+        duino.putc(ixxii);
+        timer.reset();
+                }
+    }
+void LMapComplete(){  //ลงค่าเสร็จ
+        if ( timer.read() > 1) {
+                //pc.printf("Writing!\n\r");
+                
+                
+                 duino.putc( lis[20]);
+                
+                
+                 timer.reset();}}
+
+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   Choose(int box)
+        {
+            if (box)
+            {
+                 WMbox[0] = -1;
+                 WMbox[1] = -1;
+                 WMbox[2] = -1;   
+            }else
+            {
+                 WMbox[3] = -1;
+                 WMbox[4] = -1;
+                 WMbox[5] = -1;   
+            }
+        }
+        
+        
+
+
+
+
+
+int main()
+{
+    lim.rise(&Rise1);
+    lim.fall(&Fall1);
+    rotate.rise(&Rise2);
+    rotate.fall(&Fall2);
+    choose.rise(&Rise3);
+    choose.fall(&Fall3);
+    flipper.attach(&getStr,1.0); //timer interrupt for recieving bluetooth. 
+    LInitial(); // set initial pattern in led matrix.
+    wait(5); // wait 5 second for putting the box down to set initial position of imu.
+    
+    while(1){
+        
+        if ((dete[0]=='l')&&(dete[1]=='o')&&(dete[2]=='s')){
+            Lose();
+            
+            break;
+            }
+        else if ((dete[0]=='l')&&(dete[1]=='w')&&(dete[2]=='i')&&(dete[3]=='n')){
+            Win();
+            
+            break;
+            }  
+        else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='o')){
+            chooseAnother();
+            } 
+        else if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='t')){
+            anotherRotate();
+            } 
+        else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='r')){
+            GetCoordinateX();
+            } 
+        else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='p')){
+            GetCoordinateY();
+            } 
+        else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='y')){
+            GetCoordinateZ();
+            } 
+        else if ((dete[0]=='o')&&(dete[1]=='k')){
+            LMapComplete();
+            } 
+        else if ((dete[0]=='m')&&(dete[1]=='a')&&(dete[2]=='p')&&(dete[3]=='c')){
+            LMapComplete();
+            } 
+        else if ((dete[0]=='T')&&(dete[1]=='+')&&(dete[2]=='+')){
+            turn += 1;
+            } 
+        else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='1')){
+            Choose(1);
+            } 
+        else if ((dete[0]=='T')&&(dete[1]=='u')&&(dete[2]=='r')){
+            LCountDown(turn);
+            
+            } 
+        } 
+}
\ No newline at end of file