OX V1

Dependencies:   mbed

Committer:
arthicha
Date:
Mon Dec 05 15:55:28 2016 +0000
Revision:
0:6182212860fb
Child:
1:6514e03fe62f
V1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arthicha 0:6182212860fb 1 #include "mbed.h"
arthicha 0:6182212860fb 2 #include "Moop.h"
arthicha 0:6182212860fb 3 #include "bmu.h"
arthicha 0:6182212860fb 4 #include "Latrix.h"
arthicha 0:6182212860fb 5 #include "zmu9250.h"
arthicha 0:6182212860fb 6 #include "MPU9250.h"
arthicha 0:6182212860fb 7
arthicha 0:6182212860fb 8 InterruptIn lim(PinName D3);
arthicha 0:6182212860fb 9 InterruptIn rotate(PinName D4);
arthicha 0:6182212860fb 10 InterruptIn choose(PinName D5);
arthicha 0:6182212860fb 11 Ticker flipper;
arthicha 0:6182212860fb 12 Ticker Update;
arthicha 0:6182212860fb 13
arthicha 0:6182212860fb 14 Moop map;
arthicha 0:6182212860fb 15
arthicha 0:6182212860fb 16 Serial pc(PinName USBTX,PinName USBRX);
arthicha 0:6182212860fb 17 Serial mas(PinName D8,PinName D2);
arthicha 0:6182212860fb 18
arthicha 0:6182212860fb 19 bmuimu imu;
arthicha 0:6182212860fb 20 Latrix ledMatrix;
arthicha 0:6182212860fb 21
arthicha 0:6182212860fb 22 Time t;
arthicha 0:6182212860fb 23
arthicha 0:6182212860fb 24 bool anach = false;
arthicha 0:6182212860fb 25 bool buttonState = {0,0,0}; // button limit switchm rotate and choose state respectively.
arthicha 0:6182212860fb 26 unsigned int count = 10; // count down.
arthicha 0:6182212860fb 27 char data[50];
arthicha 0:6182212860fb 28 char elem;
arthicha 0:6182212860fb 29 bool error = false; // store error in ambigous imu data.
arthicha 0:6182212860fb 30 int helperRoll = 0, helperPitch=0,helperYaw=0;
arthicha 0:6182212860fb 31 unsigned int indx = 0;
arthicha 0:6182212860fb 32 unsigned int state = 0;
arthicha 0:6182212860fb 33
arthicha 0:6182212860fb 34 // state 0 = initial / start.
arthicha 0:6182212860fb 35 // 1 = waiting for limit switch.
arthicha 0:6182212860fb 36
arthicha 0:6182212860fb 37 unsigned int turn = 0; // turn.
arthicha 0:6182212860fb 38
arthicha 0:6182212860fb 39 unsigned int x = 0, y = 0, z = 0,lx=0,ly=0,lz=0;
arthicha 0:6182212860fb 40 int winStatus = 0;
arthicha 0:6182212860fb 41
arthicha 0:6182212860fb 42 void Rise(int but)
arthicha 0:6182212860fb 43 {
arthicha 0:6182212860fb 44 buttonState[but-3] = 1;
arthicha 0:6182212860fb 45 }
arthicha 0:6182212860fb 46
arthicha 0:6182212860fb 47 void Fall(int but)
arthicha 0:6182212860fb 48 {
arthicha 0:6182212860fb 49 buttonState[but-3] = 0;
arthicha 0:6182212860fb 50 }
arthicha 0:6182212860fb 51
arthicha 0:6182212860fb 52 void Update()
arthicha 0:6182212860fb 53 {
arthicha 0:6182212860fb 54 imu.Update();
arthicha 0:6182212860fb 55 }
arthicha 0:6182212860fb 56
arthicha 0:6182212860fb 57 void Sent(char arrayi[],float it,int siz)
arthicha 0:6182212860fb 58 {
arthicha 0:6182212860fb 59 for (int i = 0;i<siz;i++)
arthicha 0:6182212860fb 60 {
arthicha 0:6182212860fb 61 mas.putc(arrayi[i]);
arthicha 0:6182212860fb 62 wait(it);
arthicha 0:6182212860fb 63 }
arthicha 0:6182212860fb 64 }
arthicha 0:6182212860fb 65
arthicha 0:6182212860fb 66 void getStr()
arthicha 0:6182212860fb 67 {
arthicha 0:6182212860fb 68 while (mas.readable())
arthicha 0:6182212860fb 69 {
arthicha 0:6182212860fb 70
arthicha 0:6182212860fb 71 elem = mas.getc();
arthicha 0:6182212860fb 72 pc.printf("char is %d\n\r",elem);
arthicha 0:6182212860fb 73 if ((elem == '\n'))
arthicha 0:6182212860fb 74 {
arthicha 0:6182212860fb 75 pc.printf("string is %s\n\r",data);
arthicha 0:6182212860fb 76 for(int i=0;i<indx;i++)
arthicha 0:6182212860fb 77 {
arthicha 0:6182212860fb 78 if (indx > 2)
arthicha 0:6182212860fb 79 {
arthicha 0:6182212860fb 80 dete[i] = data[i];
arthicha 0:6182212860fb 81 }
arthicha 0:6182212860fb 82 }
arthicha 0:6182212860fb 83 for(int i=0;i<30;i++)
arthicha 0:6182212860fb 84 {
arthicha 0:6182212860fb 85 data[i] = '\0';
arthicha 0:6182212860fb 86 }
arthicha 0:6182212860fb 87 indx = 0;
arthicha 0:6182212860fb 88 break;
arthicha 0:6182212860fb 89 }else
arthicha 0:6182212860fb 90 {
arthicha 0:6182212860fb 91 data[indx] = elem;
arthicha 0:6182212860fb 92 indx += 1;
arthicha 0:6182212860fb 93 }
arthicha 0:6182212860fb 94
arthicha 0:6182212860fb 95 }
arthicha 0:6182212860fb 96 }
arthicha 0:6182212860fb 97
arthicha 0:6182212860fb 98 void ShowMap()
arthicha 0:6182212860fb 99 {
arthicha 0:6182212860fb 100 int mmapx[5][5][5] = {0};
arthicha 0:6182212860fb 101 int mmapy[5][5][5] = {0};
arthicha 0:6182212860fb 102 for(int i=0;i<5;i++)
arthicha 0:6182212860fb 103 {
arthicha 0:6182212860fb 104 for(int j = 0;j<5;j++)
arthicha 0:6182212860fb 105 {
arthicha 0:6182212860fb 106 for (int k =0;k<5;k++)
arthicha 0:6182212860fb 107 {
arthicha 0:6182212860fb 108 mmapx[i][j] = map.check1[i][j][k]*pow(2,k);
arthicha 0:6182212860fb 109 Sent(char(mmapx[i]+64)[j],0.1,1);
arthicha 0:6182212860fb 110 Sent("\n",0.1,1);
arthicha 0:6182212860fb 111 mmapy[i][j] = map.check2[i][j][k]*pow(2,k);
arthicha 0:6182212860fb 112 Sent(char(mmapy[i]+64)[j],0.1,1);
arthicha 0:6182212860fb 113 Sent("\n",0.1,1);
arthicha 0:6182212860fb 114 }
arthicha 0:6182212860fb 115 }
arthicha 0:6182212860fb 116 }
arthicha 0:6182212860fb 117 int page = 0;
arthicha 0:6182212860fb 118 while(1)
arthicha 0:6182212860fb 119 {
arthicha 0:6182212860fb 120 if (page == 5)
arthicha 0:6182212860fb 121 {
arthicha 0:6182212860fb 122 page = 4;
arthicha 0:6182212860fb 123 }else if(page == -1)
arthicha 0:6182212860fb 124 {
arthicha 0:6182212860fb 125 page = 0;
arthicha 0:6182212860fb 126 }
arthicha 0:6182212860fb 127 ledMatrix.Show(mmapx[page],mmapy[page]);
arthicha 0:6182212860fb 128 if(buttonState[2]==1)
arthicha 0:6182212860fb 129 {
arthicha 0:6182212860fb 130 while(buttonState[2]==1)
arthicha 0:6182212860fb 131 {
arthicha 0:6182212860fb 132
arthicha 0:6182212860fb 133 }
arthicha 0:6182212860fb 134 page += 1;
arthicha 0:6182212860fb 135 }
arthicha 0:6182212860fb 136 else if(buttonState[1]==1)
arthicha 0:6182212860fb 137 {
arthicha 0:6182212860fb 138 while(buttonState[1]==1)
arthicha 0:6182212860fb 139 {
arthicha 0:6182212860fb 140
arthicha 0:6182212860fb 141 }
arthicha 0:6182212860fb 142 page -= 1;
arthicha 0:6182212860fb 143 }
arthicha 0:6182212860fb 144 }
arthicha 0:6182212860fb 145 }
arthicha 0:6182212860fb 146
arthicha 0:6182212860fb 147 void Loose()
arthicha 0:6182212860fb 148 {
arthicha 0:6182212860fb 149 ledMatrix.Loose();
arthicha 0:6182212860fb 150 Sent("los\n",0.1,4);
arthicha 0:6182212860fb 151 ShowMap();
arthicha 0:6182212860fb 152 }
arthicha 0:6182212860fb 153
arthicha 0:6182212860fb 154 void Wiin()
arthicha 0:6182212860fb 155 {
arthicha 0:6182212860fb 156 ledMatrix.Wiin();
arthicha 0:6182212860fb 157 Sent("lwin\n",0.1,4);
arthicha 0:6182212860fb 158 ShowMap();
arthicha 0:6182212860fb 159 }
arthicha 0:6182212860fb 160
arthicha 0:6182212860fb 161
arthicha 0:6182212860fb 162
arthicha 0:6182212860fb 163 bool chooseAnother()
arthicha 0:6182212860fb 164 {
arthicha 0:6182212860fb 165 Sent('cho\n',0.1,4);
arthicha 0:6182212860fb 166 while(1)
arthicha 0:6182212860fb 167 {
arthicha 0:6182212860fb 168 if ((dete[0]='c')&&(dete[1]='h')&&(dete[2]='0'))
arthicha 0:6182212860fb 169 {
arthicha 0:6182212860fb 170 return true;
arthicha 0:6182212860fb 171 }else if ((dete[0]='c')&&(dete[1]='h')&&(dete[2]='x'))
arthicha 0:6182212860fb 172 {
arthicha 0:6182212860fb 173 return false;
arthicha 0:6182212860fb 174 }
arthicha 0:6182212860fb 175 }
arthicha 0:6182212860fb 176 }
arthicha 0:6182212860fb 177
arthicha 0:6182212860fb 178 bool anotherRotate()
arthicha 0:6182212860fb 179 {
arthicha 0:6182212860fb 180 Sent('rot\n',0.1,4);
arthicha 0:6182212860fb 181 while(1)
arthicha 0:6182212860fb 182 {
arthicha 0:6182212860fb 183 if ((dete[0]='r')&&(dete[1]='o')&&(dete[2]='t'))
arthicha 0:6182212860fb 184 {
arthicha 0:6182212860fb 185 return true;
arthicha 0:6182212860fb 186 }else if ((dete[0]='r')&&(dete[1]='o')&&(dete[2]='x'))
arthicha 0:6182212860fb 187 {
arthicha 0:6182212860fb 188 return false;
arthicha 0:6182212860fb 189 }
arthicha 0:6182212860fb 190 }
arthicha 0:6182212860fb 191 }
arthicha 0:6182212860fb 192
arthicha 0:6182212860fb 193 int main()// main program of
arthicha 0:6182212860fb 194 {
arthicha 0:6182212860fb 195 lim.rise(&Rise(3));
arthicha 0:6182212860fb 196 lim.fall(&Fall(3));
arthicha 0:6182212860fb 197 rotate.rise(&Rise(4));
arthicha 0:6182212860fb 198 rotate.fall(&Fall(4));
arthicha 0:6182212860fb 199 choose.rise(&Rise(5));
arthicha 0:6182212860fb 200 choose.fall(&Fall(5));
arthicha 0:6182212860fb 201
arthicha 0:6182212860fb 202 ledMatrix.Initial(); // set initial pattern in led matrix.
arthicha 0:6182212860fb 203 wait(5); // wait 5 second for putting the box down to set initial position of imu.
arthicha 0:6182212860fb 204 //imu.Initial(); // set initial for imu.
arthicha 0:6182212860fb 205 Update.attach(&Update,1.0); //timer interrupt for recieving bluetooth.
arthicha 0:6182212860fb 206 while((dete[0]!='i')||(dete[1]!='n')||(dete[2]!='i')||(dete[3]!='t'))
arthicha 0:6182212860fb 207 {
arthicha 0:6182212860fb 208 }
arthicha 0:6182212860fb 209 ledMatrix.Wbox(); // set led to tell the player that the game is ready and this is the woman-box.
arthicha 0:6182212860fb 210 t.start();
arthicha 0:6182212860fb 211 while (1) // void loop.
arthicha 0:6182212860fb 212 {
arthicha 0:6182212860fb 213 state = 1;
arthicha 0:6182212860fb 214
arthicha 0:6182212860fb 215 if (buttonState[0] == 1) // if the boxes are connected.
arthicha 0:6182212860fb 216 {
arthicha 0:6182212860fb 217 x = map.GetCoordinateX();
arthicha 0:6182212860fb 218 y = map.GetCoordinateY();
arthicha 0:6182212860fb 219 z = map.GetCoordinateZ(); // get the position of main box which linking with map or the box not be choosen.
arthicha 0:6182212860fb 220 /*Sent("helr\n",0.1,5); // tell the other box, that we want helper roll.
arthicha 0:6182212860fb 221 while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='r'))
arthicha 0:6182212860fb 222 {
arthicha 0:6182212860fb 223 pc.printf("srt %s\n\r",dete);
arthicha 0:6182212860fb 224 }
arthicha 0:6182212860fb 225 helperRoll = int( ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48) ); //get roll from another box.
arthicha 0:6182212860fb 226 Sent("help\n",0.1,5); // tell the other box, that we want helper roll.
arthicha 0:6182212860fb 227 while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='p'))
arthicha 0:6182212860fb 228 {
arthicha 0:6182212860fb 229 pc.printf("srt %s\n\r",dete);
arthicha 0:6182212860fb 230 }
arthicha 0:6182212860fb 231 helperPitch = int( ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48) ); //get roll from another box.
arthicha 0:6182212860fb 232 Sent("hely\n",0.1,5); // tell the other box, that we want helper roll.
arthicha 0:6182212860fb 233 while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='y'))
arthicha 0:6182212860fb 234 {
arthicha 0:6182212860fb 235 pc.printf("srt %s\n\r",dete);
arthicha 0:6182212860fb 236 }
arthicha 0:6182212860fb 237 helperYaw = int( ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48) ); //get roll from another box.*/
arthicha 0:6182212860fb 238 //Sent("ok\n",0.1,3); //free the otherbox.
arthicha 0:6182212860fb 239
arthicha 0:6182212860fb 240 error = imu.getposition(x,y,z)//,helperRoll,helperPitch,helperYaw); // calculate position from imu.
arthicha 0:6182212860fb 241 if (error)
arthicha 0:6182212860fb 242 {
arthicha 0:6182212860fb 243 ledMatrix.Error()
arthicha 0:6182212860fb 244 while(buttonState[0] == 1)
arthicha 0:6182212860fb 245 {
arthicha 0:6182212860fb 246
arthicha 0:6182212860fb 247 }
arthicha 0:6182212860fb 248 }else
arthicha 0:6182212860fb 249 {
arthicha 0:6182212860fb 250 lx = x;
arthicha 0:6182212860fb 251 ly = y;
arthicha 0:6182212860fb 252 lz = z;
arthicha 0:6182212860fb 253 x = imu.getX();
arthicha 0:6182212860fb 254 y = imu.getY();
arthicha 0:6182212860fb 255 z = imu.getZ(); // get new possition.
arthicha 0:6182212860fb 256
arthicha 0:6182212860fb 257 winStatus = map.Update(x,y,z)//,helperRoll); //update new position in map.
arthicha 0:6182212860fb 258
arthicha 0:6182212860fb 259 map.ChangeOX([x,y,z,lx,ly,lz])
arthicha 0:6182212860fb 260 if (winStatus == 1)
arthicha 0:6182212860fb 261 {
arthicha 0:6182212860fb 262 Wiin(); // win 3 in a row.
arthicha 0:6182212860fb 263 }else if (winStatus == -1)
arthicha 0:6182212860fb 264 {
arthicha 0:6182212860fb 265 Loose();// lose out of map or un valid position.
arthicha 0:6182212860fb 266 }else
arthicha 0:6182212860fb 267 {
arthicha 0:6182212860fb 268 Sent("mapc\n",0.1,4); //map complete.
arthicha 0:6182212860fb 269 ledMatrix.MapComplete(); // show that this position is ok.
arthicha 0:6182212860fb 270 while(1)
arthicha 0:6182212860fb 271 {
arthicha 0:6182212860fb 272 anach = chooseAnother() // wheather thr others box is choosen.
arthicha 0:6182212860fb 273 if ((buttonState[2]==1) && (!anach))
arthicha 0:6182212860fb 274 {
arthicha 0:6182212860fb 275 Sent("ch1\n",0.1,4);
arthicha 0:6182212860fb 276
arthicha 0:6182212860fb 277 map.Choose(1); // choose main box.
arthicha 0:6182212860fb 278 }else if((buttonState[2]==0) && (anach))
arthicha 0:6182212860fb 279 {
arthicha 0:6182212860fb 280 map.Choose(0); // choose another box.
arthicha 0:6182212860fb 281
arthicha 0:6182212860fb 282 }else if(buttonState[2]==0)
arthicha 0:6182212860fb 283 {
arthicha 0:6182212860fb 284 if((buttonState[1]==0)||(anotherRotate())) // click buutton some rotate box.
arthicha 0:6182212860fb 285 {
arthicha 0:6182212860fb 286 state = 3;
arthicha 0:6182212860fb 287 while((buttonState[1]==0)&&(anotherRotate()))
arthicha 0:6182212860fb 288 {
arthicha 0:6182212860fb 289
arthicha 0:6182212860fb 290 }
arthicha 0:6182212860fb 291
arthicha 0:6182212860fb 292 int yyaaww = imu.Rotate(); // check wheater rotating is error.
arthicha 0:6182212860fb 293 if (false)
arthicha 0:6182212860fb 294 {
arthicha 0:6182212860fb 295 ledMatrix.Error();
arthicha 0:6182212860fb 296
arthicha 0:6182212860fb 297 }else
arthicha 0:6182212860fb 298 {
arthicha 0:6182212860fb 299 map.Rotate(0,0,yyaaww)//imu.Rotate_Yaw); // private variable/arttribute in imu class.
arthicha 0:6182212860fb 300 ledMatrix.RotateComplete();
arthicha 0:6182212860fb 301 }
arthicha 0:6182212860fb 302 }
arthicha 0:6182212860fb 303 } //close choose or rotate.
arthicha 0:6182212860fb 304 } //close while true.
arthicha 0:6182212860fb 305 turn += 1;
arthicha 0:6182212860fb 306 Sent("T++\n",0.1,4);
arthicha 0:6182212860fb 307 t.start();
arthicha 0:6182212860fb 308 state = 2;
arthicha 0:6182212860fb 309 ledMatrix.ChooseComplete();
arthicha 0:6182212860fb 310 } // close winState.
arthicha 0:6182212860fb 311 } // close not error.
arthicha 0:6182212860fb 312
arthicha 0:6182212860fb 313 }else // if the boxes aren't connect.
arthicha 0:6182212860fb 314 {
arthicha 0:6182212860fb 315 if (count == 0) // time out.
arthicha 0:6182212860fb 316 {
arthicha 0:6182212860fb 317 t.stop();
arthicha 0:6182212860fb 318 Loose(); // lose state
arthicha 0:6182212860fb 319 }else if(turn != 0)
arthicha 0:6182212860fb 320 {
arthicha 0:6182212860fb 321 t.stop();
arthicha 0:6182212860fb 322 if (t.read() > 1.0);
arthicha 0:6182212860fb 323 {
arthicha 0:6182212860fb 324 t.start();
arthicha 0:6182212860fb 325 count -= 1; // time decreasing.
arthicha 0:6182212860fb 326 }
arthicha 0:6182212860fb 327 } // close check time
arthicha 0:6182212860fb 328 ledMatrix.CountDown(count);.
arthicha 0:6182212860fb 329 Sent("Tur\n",0.1,4)
arthicha 0:6182212860fb 330 } // close boxes aren't connect.
arthicha 0:6182212860fb 331
arthicha 0:6182212860fb 332 } // close loop.
arthicha 0:6182212860fb 333
arthicha 0:6182212860fb 334
arthicha 0:6182212860fb 335
arthicha 0:6182212860fb 336 } // close main.*/