Serial Communication
Dependencies: mbed
Fork of Nucleo_serial_menu by
Diff: main.cpp
- Revision:
- 6:cac4ed620e57
- Parent:
- 5:85178023086a
- Child:
- 7:a651b3c90958
diff -r 85178023086a -r cac4ed620e57 main.cpp --- a/main.cpp Wed Nov 30 06:11:39 2016 +0000 +++ b/main.cpp Thu Dec 01 08:45:38 2016 +0000 @@ -1,23 +1,34 @@ +/******************************************************************************/ +/* This Nicleo is stat 0! */ +/******************************************************************************/ #include "mbed.h" // Initail I/O. AnalogIn VRx(A2); AnalogIn VRy(A3); DigitalIn Button(D13); +Serial bt(PA_15, PB_7); // Initial function. char check_joys(int jx,int jy); int check_joys_int(int jx,int jy); int check_correctPosition(int X,int Y); int get_position(int input_position,int ship_type); -void Select_Position(char M); +void check_ready(); void print_metrix(); - +void Select_Position(char M); // Initial variable. int select_Position = 0; +int ATKposition = 0; +int iWaitATK = 0; +int U_ready = 0; +int state = 0; +int ship_notdestroy[8] = {4,3,2,2,1,1,1,1}; +int ship_destroyed[8] = {0}; +int ship_flor[8][8] = {{0}}; //__________________________________________________This is ship area. +int check_ship1 = 0; int check_ship2 = 0; -int ship_flor[8][8] = {{0}}; //__________________________________________________This is ship area. int ship4[4] = {0}; int ship3[3] = {0}; int ship2[2][2] = {{0}}; @@ -28,49 +39,111 @@ float Vy; int SW,iVxVy,iSW,n; int iShipType = 0; - int select_Ship[8] = {4,3,2,2,1,1,1,1}; char m; while(1) { if(iShipType<8){ Vx = VRx.read() * 1024; Vy = VRy.read() * 1024; SW = Button.read(); - m = check_joys(Vx,Vy); n = check_joys_int(Vx,Vy); if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time. iVxVy = 0; + check_ready(); } if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time. iSW = 0; + check_ready(); } if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. - //printf("%c\n",m); Select_Position(m); + check_ready(); printf("select_Position = %d\n",select_Position); iVxVy = 1; } if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human. printf("Wait!\n"); - if(get_position(select_Position,select_Ship[iShipType])==0){ //__Please read explanation in function get_position. - printf("Pls. input correct position\n"); + check_ready(); + if(get_position(select_Position,ship_notdestroy[iShipType])==0){ //__Please read explanation in function get_position. + printf("Please input correct position\n"); }else{ iShipType++; } + check_ready(); iSW = 1; print_metrix(); } }else{ + if(U_ready!=1){bt.putc('E');} + + printf("\n"); break; } - //printf("x: %d ||y: %d ||sw: %d\n",(int)Vx,(int)Vy,SW); - wait(0.1); } - while(1){ - printf("Ready to play\n"); + select_Position = 0; + + while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play + if(U_ready == 1){state = 0;}else{state = 1;} + if(state == 1){ + Vx = VRx.read() * 1024; + Vy = VRy.read() * 1024; + SW = Button.read(); + m = check_joys(Vx,Vy); + n = check_joys_int(Vx,Vy); + if(n == 1){ + iVxVy = 0; + } + if(SW == 1){ + iSW = 0; + } + if(m!=NULL && iVxVy==0){ + Select_Position(m); + printf("select_Position = %d\n",select_Position); + iVxVy = 1; + } + if(SW==0 && iSW==0){ + printf("send\n"); + bt.printf("%d",select_Position); + state = 0; + iSW = 1; + } + }else{ + while(1){ + if(bt.readable()){ + // Wait for resive position of enemy that atk you. + iWaitATK = 1; + } + if(iWaitATK==1){ + break; + } + } + } } } + + + + + + + + + + + + + + + + + + + + + + + char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right. if(jx < 24.0){ return 'a'; @@ -128,8 +201,8 @@ }else{ if(ship_type == 4){ship4[i] = (x*10)+y;} else if(ship_type == 3){ship3[i] = (x*10)+y;} - else if(ship_type == 2){ship2[check_ship2][i] = (x*10)+y;check_ship2++;} - else if(ship_type == 1){ship1[i] = (x*10)+y;} + else if(ship_type == 2){ship2[check_ship2][i] = (x*10)+y;} + else if(ship_type == 1){ship1[check_ship1] = (x*10)+y;check_ship1++;} } if(ship_type%2 == 0){ x++; @@ -137,6 +210,9 @@ y++; } } + if(ship_type==2){ + check_ship2++; + } if(error == 1){ return 0; }else{ @@ -171,4 +247,14 @@ } printf("\n"); } -} \ No newline at end of file +} +void check_ready(){ + if(bt.readable()){ + U_ready = 1; + } +} +/* +int check_when_enemyATK(int position){ + +} +*/ \ No newline at end of file