![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
hahaha
Dependencies: mbed
Diff: main.cpp
- Revision:
- 0:a291977ec0b1
- Child:
- 1:d8ce226c8c2e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Dec 05 18:31:43 2016 +0000 @@ -0,0 +1,791 @@ +#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.update(); +} + +void Sent(char arrayi[],float it,int siz) +{ + for (int i = 0;i<siz;i++) + { + mas.putc(char(arrayi[i])); + wait(it); + } +} + +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; + } + + } +} + +void LChackmape(){ // + if (t.read() > 1) { + + + duino.putc(lis[23]); + + timer.reset(); + } + } + +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 LCountDown(int ixxii){ //นับเวลา + if ( timer.read() > 1) { + duino.putc(ixxii); + timer.reset(); + } + } + + +void LCorrect(){ //เช็คถูก + if ( timer.read() > 1) { + //pc.printf("Writing!\n\r"); + duino.putc(lis[18]); + timer.reset(); + } + } + +void LError(){ // + if ( timer.read() > 1) { + //pc.printf("Writing!\n\r"); + duino.putc( lis[19]); + timer.reset(); + } + } +void LMapComplete(){ //ลงค่าเสร็จ + if ( timer.read() > 1) { + //pc.printf("Writing!\n\r"); + + + duino.putc( lis[20]); + + + timer.reset();}} + +void LWbox(){ //ตัวหลัก + if ( timer.read() > 1) { + duino.putc( lis[21]); + timer.reset(); + } + } + +void LRotateComplete(){ //หมุน + if ( timer.read() > 1) { + //pc.printf("Writing!\n\r"); + + + duino.putc( lis[22]); + + + timer.reset(); + } + } + + + +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 Win() +{ + LWin(); + 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; + WMbox[1] = yy; + WMbox[2] = zz; + WMbox[3] = lx; + WMbox[4] = ly; + WMbox[5] = lz; + }else + { + WMbox[0] = lx; + WMbox[1] = ly; + WMbox[2] = lz; + WMbox[3] = xx; + WMbox[4] = yy; + WMbox[5] = zz; + } + } + + 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; + } + } + + 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)); + B = ((j-2)* Cross(yaw))+((i-2)* Zine(pitch)* Zine(yaw))-((k-2)* Zine(yaw)* Cross(pitch)); + C = ((j-2)* Zine(yaw))-((i-2)* Cross(yaw)* Zine(pitch))+((k-2)* Cross(yaw)* Cross(pitch)); + rmap[i+2][j+2][k+2]= map[i][j][k]; + } + } + } + }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)); + B = ((j-2)* Cross(role))-((k-2)* Zine(role)); + C = -((i-2)* Zine(yaw))+((j-2)* Cross(yaw)* Zine(role))+((k-2)* Cross(yaw)* Cross(role)); + rmap[i+2][j+2][k+2]= map[i][j][k]; + } + } + } + }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)); + B = ((i-2)* Zine(yaw))+((j-2)* Cross(yaw)); + C = (k-2); + rmap[i+2][j+2][k+2]= map[i][j][k]; + } + } + } + } + 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]; + } + } + } + } + + 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; // 5 = 'x' + turn++; + } + else{ + Lose(); // ลงไมไ่ด้ + } + case '1': + i-- , j-- , k--; + if( map[i][j][k] == 0){ + map[i][j][k] = 6; // 6 = 'o' + turn++; + } + else{ + Lose(); // ลงไมไ่ด้ + } + } + if(i == 5 or j == 5 or k == 5){ // ลงเกิน map + Lose(); + } + + 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(); + } + else if( map[i][j][k] and map[i][j][k+1] and map[i][j][k+2] == 6){ + Win(); + } + } + } + } + + 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(); + } + else if( map[i][j][k] and map[i][j+1][k] and map[i][j+2][k] == 6){ + Win(); + } + } + } + } + + 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(); + } + else if( map[i][j][k] and map[i+1][j][k] and map[i+2][j][k] == 6){ + Win(); + } + } + } + } + + 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(); + } + else if( map[i][j][k] and map[i][j+1][k+1] and map[i][j+2][k+2] == 6){ + Win(); + } + } + } + } + + 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(); + } + else if( map[i][j][k] and map[i+1][j][k+1] and map[i+2][j][k+2] == 6){ + Win(); + } + } + } + } + + 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(); + } + else if( map[i][j][k] and map[i+1][j+1][k] and map[i+2][j+2][k] == 6){ + Win(); + } + } + } + } + + 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(); + } + else if( map[i][j][k] and map[i][j+1][k-1] and map[i][j+2][k-2] == 6){ + Win(); + } + } + } + } + + 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(); + } + else if( map[i][j][k] and map[i+1][j][k-1] and map[i+2][j][k-2] == 6){ + Win(); + } + } + } + } + + 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(); + } + else if( map[i][j][k] and map[i+1][j-1][k] and map[i+2][j-2][k] == 6){ + Win(); + } + } + } + } + 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(); + } + else if( map[i][j][k] and map[i+1][j+1][k+1] and map[i+2][j+2][k+2] == 6){ + Win(); + } + } + } + } + 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(); + } + else if( map[i][j][k] and map[i-1][j+1][k+1] and map[i-2][j+2][k+2] == 6){ + Win(); + } + } + } + } + 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(); + } + else if( map[i][j][k] and map[i-1][j-1][k+1] and map[i-2][j-2][k+2] == 6){ + Win(); + } + } + } + } + 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(); + } + else if( map[i][j][k] and map[i+1][j-1][k+1] and map[i+2][j-2][k+2] == 6){ + Win(); + } + } + } + } + + + } + + } + + + + 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; + } + else if( map[ A][ B][ C] == 6){ + Check2[ A][ B][ C] = 1; + } + else{ + Check1[ A][ B][ C] = 0; + Check2[ A][ B][ C] = 0; + } + } + } + } + } + + +void LInitial(){ //ลงค่าเสร็จ + if (timer.read() > 1) { + + + duino.putc(lis[20]); + + + timer.reset(); + } + if (duino.readable()){ + } +} + + +int main() // main program of +{ + lim.rise(&Rise1); + lim.fall(&Fall1); + rotate.rise(&Rise2); + rotate.fall(&Fall2); + choose.rise(&Rise3); + choose.fall(&Fall3); + + 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,1.0); //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; + + if (buttonState[0] == 1) // if the boxes are connected. + { + x = GetCoordinateX(); + y = GetCoordinateY(); + z = GetCoordinateZ(); // 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); // calculate position from imu. + if (errorie) + { + LError(); + while(buttonState[0] == 1) + { + + } + }else + { + lx = x; + ly = y; + lz = z; + x = imu.X_pos; + y = imu.Y_pos; + z = imu.Z_pos; // get new possition. + + UpdateMap(x,y,z,turn);//,helperRoll); //update new position in map. + + ChangeOX(x,y,z,lx,ly,lz); + + Sent("mapc\n",0.1,4); //map complete. + LMapComplete(); // show that this position is ok. + while(1) + { + anach = chooseAnother() ;// wheather thr others box is choosen. + if ((buttonState[2]==1) && (!anach)) + { + Sent("ch1\n",0.1,4); + + Choose(1); // choose main box. + }else if((buttonState[2]==0) && (anach)) + { + Choose(0); // choose another box. + + }else if(buttonState[2]==0) + { + if((buttonState[1]==0)||(anotherRotate())) // click buutton some rotate box. + { + state = 3; + while((buttonState[1]==0)&&(anotherRotate())) + { + + } + + yyaaww = imu.Rotate(); // check wheater rotating is error. + MapRotate(0,0,yyaaww);//imu.Rotate_Yaw); // private variable/arttribute in imu class. + LRotateComplete(); + } + } //close choose or rotate. + } //close while true. + turn ++; + Sent("T++\n",0.1,4); + t.start(); + state = 2; + //LChooseComplete(); + } // close not error. + + }else // if the boxes aren't connect. + { + if (county == 0) // time out. + { + t.stop(); + Lose(); // lose state + }else if(turn != 0) + { + t.stop(); + if (t.read() > 1.0); + { + t.start(); + county -= 1; // time decreasing. + } + } // close check time + LCountDown(county); + Sent("Tur\n",0.1,4); + } // close boxes aren't connect. + + } // close loop. + + + +} // close main.*/ \ No newline at end of file