OX / Mbed 2 deprecated Ox_ver_function

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main2.h Source File

main2.h

00001 #include "mbed.h"
00002 #include "bmu.h"
00003 
00004 
00005 InterruptIn lim(D3);
00006 InterruptIn rotate(D4);
00007 InterruptIn choose(D5);
00008 Ticker flipper;
00009 Ticker Update;
00010 
00011 bmuimu imu;
00012 
00013 Serial pc(USBTX,USBRX);
00014 Serial mas(D8,D2);
00015 Serial duino(PA_11, PA_12);
00016 
00017 Timer t;
00018 Timer timer;
00019 
00020 //*********************************************************************************************************************************
00021 //*********************************************************************************************************************************
00022 //*********************************************************************************************************************************
00023 //                                                          variable
00024 //*********************************************************************************************************************************
00025 //*********************************************************************************************************************************
00026 //*********************************************************************************************************************************
00027 bool anach = false;
00028 bool buttonState[3] = {0,0,0}; // button limit switchm rotate and choose state respectively.
00029 unsigned int county = 10; // county down.
00030 char data[50];
00031 char elem;
00032 int yyaaww;
00033 bool errorie = false; // store error in ambigous imu data. 
00034 int helperRoll = 0, helperPitch=0,helperYaw=0;
00035 unsigned int indx = 0;
00036 unsigned int state = 0;
00037 int dete[50];
00038 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};
00039 
00040 // state    0 = initial / start.
00041 //          1 = waiting for limit switch.
00042 
00043 unsigned int turn = 0; // turn.
00044 
00045 unsigned int x = 0, y = 0, z = 0,lx=0,ly=0,lz=0;
00046 int winStatus = 0;
00047 
00048 
00049 
00050 int A , B , C , player , button; 
00051 int countDown = 9;
00052 int WMbox[6];
00053 int map[5][5][5],rmap[5][5][5]; //store map.
00054 int Check1[5][5][5] , Check2[5][5][5]; //convert to x or o map.
00055 
00056 void Rise(int but)
00057 {
00058     buttonState[but-3] = 1;
00059 }
00060 
00061 void Fall(int but)
00062 {
00063     buttonState[but-3] = 0;   
00064 }
00065 
00066 void Sent(char arrayi[],float it,int siz)
00067 {
00068     for (int i = 0;i<siz;i++)
00069         {
00070             mas.putc(arrayi[i]);
00071             wait(it);
00072         }   
00073 }
00074 
00075 void LInitial(){  //ลงค่าเสร็จ
00076     if (timer.read() > 1) {
00077         
00078         
00079         duino.putc(lis[20]);
00080         
00081         
00082         timer.reset();
00083                 }
00084     if (duino.readable()){
00085             }         
00086 }
00087 
00088 void getStr()
00089 {
00090     while (mas.readable())
00091     {
00092         
00093         elem = mas.getc();
00094         pc.printf("char is %d\n\r",elem);
00095         if ((elem == '\n'))
00096         {
00097             pc.printf("string is %s\n\r",data);   
00098             for(int i=0;i<indx;i++)
00099             {
00100                 if (indx > 2)
00101                 {
00102                     dete[i] = data[i];      
00103                 }
00104             }
00105             for(int i=0;i<30;i++)
00106             {
00107                 data[i] = '\0';
00108             }
00109             indx = 0;
00110             break;
00111         }else
00112         {
00113             data[indx] = elem;
00114             indx += 1;
00115         }
00116             
00117     }  
00118 }
00119 bool chooseAnother()
00120 {
00121     Sent("cho\n",0.1,4);
00122     while(1)
00123     {
00124         if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='0'))   
00125         {
00126             return true;
00127         }else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='x')) 
00128         {
00129             return false;
00130         }
00131     }
00132 }
00133 
00134 void Rise1()
00135 {
00136     buttonState[0] = 1;
00137 }
00138 
00139 void Fall1()
00140 {
00141     buttonState[0] = 0;   
00142 }
00143 void Rise2()
00144 {
00145     buttonState[1] = 1;
00146 }
00147 
00148 void Fall2()
00149 {
00150     buttonState[1] = 0;   
00151 }
00152 void LLose(){ //แพ้
00153             
00154             if ( t.read() > 1) {
00155                 //pc.printf("Writing!\n\r");
00156                  duino.putc( lis[3]);
00157                  duino.putc( lis[4]);
00158                  duino.putc( lis[5]);
00159                  duino.putc( lis[6]);
00160                  t.reset();
00161                 }
00162             if ( duino.readable())
00163                 {
00164                 //pc.printf("recieving %c",duino.getc());
00165                 }
00166          
00167             }
00168 void Lose()
00169 {
00170     LLose();
00171     Sent("los\n",0.1,4);
00172 }
00173 
00174 void LWin(){  //ชนะ
00175     if ( timer.read() > 1) {
00176     //pc.printf("Writing!\n\r");
00177     duino.putc( lis[0]);
00178     duino.putc( lis[1]);
00179     duino.putc( lis[2]);
00180     timer.reset();
00181  }
00182  }
00183 void Win()
00184 {
00185     LWin();
00186     Sent("lwin\n",0.1,4);
00187 }
00188 void Rise3()
00189 {
00190     buttonState[2] = 1;
00191 }
00192 
00193 void Fall3()
00194 {
00195     buttonState[2] = 0;   
00196 }
00197 bool anotherRotate()
00198 {
00199     Sent("rot\n",0.1,4);
00200     while(1)
00201     {
00202         if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='t'))   
00203         {
00204             return true;
00205         }else if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='x')) 
00206         {
00207             return false;
00208         }
00209     }
00210 }
00211 void LCountDown(int ixxii){  //นับเวลา
00212     if ( timer.read() > 1) {
00213         duino.putc(ixxii);
00214         timer.reset();
00215                 }
00216     }
00217 void LMapComplete(){  //ลงค่าเสร็จ
00218         if ( timer.read() > 1) {
00219                 //pc.printf("Writing!\n\r");
00220                 
00221                 
00222                  duino.putc( lis[20]);
00223                 
00224                 
00225                  timer.reset();}}
00226 
00227 int    GetCoordinateX()
00228         {
00229             if ( WMbox[0] != -1)
00230             {
00231               return  WMbox[0];   
00232             }else
00233             {
00234                 return  WMbox[3];   
00235             }
00236         }
00237         
00238 int   GetCoordinateY()
00239         {
00240             if ( WMbox[1] != -1)
00241             {
00242               return  WMbox[1];   
00243             }else
00244             {
00245                 return  WMbox[4];   
00246             }
00247         }
00248         
00249 int    GetCoordinateZ()
00250         {
00251             if ( WMbox[2] != -1)
00252             {
00253               return  WMbox[2];   
00254             }else
00255             {
00256                 return  WMbox[5];   
00257             }
00258         }
00259         
00260 void   Choose(int box)
00261         {
00262             if (box)
00263             {
00264                  WMbox[0] = -1;
00265                  WMbox[1] = -1;
00266                  WMbox[2] = -1;   
00267             }else
00268             {
00269                  WMbox[3] = -1;
00270                  WMbox[4] = -1;
00271                  WMbox[5] = -1;   
00272             }
00273         }
00274         
00275         
00276 
00277 
00278 
00279 
00280 
00281 int main()
00282 {
00283     lim.rise(&Rise1);
00284     lim.fall(&Fall1);
00285     rotate.rise(&Rise2);
00286     rotate.fall(&Fall2);
00287     choose.rise(&Rise3);
00288     choose.fall(&Fall3);
00289     flipper.attach(&getStr,1.0); //timer interrupt for recieving bluetooth. 
00290     LInitial(); // set initial pattern in led matrix.
00291     wait(5); // wait 5 second for putting the box down to set initial position of imu.
00292     
00293     while(1){
00294         
00295         if ((dete[0]=='l')&&(dete[1]=='o')&&(dete[2]=='s')){
00296             Lose();
00297             
00298             break;
00299             }
00300         else if ((dete[0]=='l')&&(dete[1]=='w')&&(dete[2]=='i')&&(dete[3]=='n')){
00301             Win();
00302             
00303             break;
00304             }  
00305         else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='o')){
00306             chooseAnother();
00307             } 
00308         else if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='t')){
00309             anotherRotate();
00310             } 
00311         else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='r')){
00312             GetCoordinateX();
00313             } 
00314         else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='p')){
00315             GetCoordinateY();
00316             } 
00317         else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='y')){
00318             GetCoordinateZ();
00319             } 
00320         else if ((dete[0]=='o')&&(dete[1]=='k')){
00321             LMapComplete();
00322             } 
00323         else if ((dete[0]=='m')&&(dete[1]=='a')&&(dete[2]=='p')&&(dete[3]=='c')){
00324             LMapComplete();
00325             } 
00326         else if ((dete[0]=='T')&&(dete[1]=='+')&&(dete[2]=='+')){
00327             turn += 1;
00328             } 
00329         else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='1')){
00330             Choose(1);
00331             } 
00332         else if ((dete[0]=='T')&&(dete[1]=='u')&&(dete[2]=='r')){
00333             LCountDown(turn);
00334             
00335             } 
00336         } 
00337 }