serial test menu
Dependencies: mbed
Fork of Nucleo_serial_menu by
main.cpp
00001 /******************************************************************************/ 00002 /* This Nicleo is stat 0! */ 00003 /******************************************************************************/ 00004 #include "mbed.h" 00005 00006 // Initail I/O. 00007 AnalogIn VRx(A2); 00008 AnalogIn VRy(A3); 00009 DigitalIn Button(D13); 00010 Serial bt(PA_15, PB_7); 00011 00012 // Initial function. 00013 char check_joys(int jx,int jy); 00014 int check_joys_int(int jx,int jy); 00015 int check_correctPosition(int X,int Y); 00016 int get_position(int input_position,int ship_type); 00017 void check_ready(); 00018 void print_metrix(); 00019 void Select_Position(char M); 00020 00021 // Initial variable. 00022 int select_Position = 0; 00023 int ATKposition = 0; 00024 int iWaitATK = 0; 00025 int U_ready = 0; 00026 int state = 0; 00027 int ship_notdestroy[8] = {4,3,2,2,1,1,1,1}; 00028 int ship_destroyed[8] = {0}; 00029 int ship_flor[8][8] = {{0}}; //__________________________________________________This is ship area. 00030 int check_ship1 = 0; 00031 int check_ship2 = 0; 00032 int ship4[4] = {0}; 00033 int ship3[3] = {0}; 00034 int ship2[2][2] = {{0}}; 00035 int ship1[4] = {0}; 00036 00037 int main(){ //-------------------------------------------------------------------Main Function. 00038 float Vx; 00039 float Vy; 00040 int SW,iVxVy,iSW,n; 00041 int iShipType = 0; 00042 char m; 00043 while(1) { 00044 if(iShipType<8){ 00045 Vx = VRx.read() * 1024; 00046 Vy = VRy.read() * 1024; 00047 SW = Button.read(); 00048 m = check_joys(Vx,Vy); 00049 n = check_joys_int(Vx,Vy); 00050 if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time. 00051 iVxVy = 0; 00052 check_ready(); 00053 } 00054 if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time. 00055 iSW = 0; 00056 check_ready(); 00057 } 00058 if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. 00059 Select_Position(m); 00060 check_ready(); 00061 printf("select_Position = %d\n",select_Position); 00062 iVxVy = 1; 00063 } 00064 if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human. 00065 printf("Wait!\n"); 00066 check_ready(); 00067 if(get_position(select_Position,ship_notdestroy[iShipType])==0){ //__Please read explanation in function get_position. 00068 printf("Please input correct position\n"); 00069 }else{ 00070 iShipType++; 00071 } 00072 check_ready(); 00073 iSW = 1; 00074 print_metrix(); 00075 } 00076 }else{ 00077 if(U_ready!=1){bt.putc('E');} 00078 00079 printf("\n"); 00080 break; 00081 } 00082 } 00083 select_Position = 0; 00084 00085 while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play 00086 if(U_ready == 1){state = 0;}else{state = 1;} 00087 if(state == 1){ 00088 printf("Your turn.\n"); 00089 Vx = VRx.read() * 1024; 00090 Vy = VRy.read() * 1024; 00091 SW = Button.read(); 00092 m = check_joys(Vx,Vy); 00093 n = check_joys_int(Vx,Vy); 00094 if(n == 1){ 00095 iVxVy = 0; 00096 } 00097 if(SW == 1){ 00098 iSW = 0; 00099 } 00100 if(m!=NULL && iVxVy==0){ 00101 Select_Position(m); 00102 printf("select_Position = %d\n",select_Position); 00103 iVxVy = 1; 00104 } 00105 if(SW==0 && iSW==0){ 00106 printf("send\n"); 00107 bt.printf("%d",select_Position); 00108 state = 0; 00109 iSW = 1; 00110 } 00111 }else{ 00112 while(1){ 00113 printf(""); 00114 if(bt.readable()){ 00115 // Wait for resive position of enemy that atk you. 00116 iWaitATK = 1; 00117 } 00118 if(iWaitATK==1){ 00119 break; 00120 } 00121 } 00122 } 00123 } 00124 } 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right. 00150 if(jx < 24.0){ 00151 return 'a'; 00152 } 00153 else if(jx > 1000.0){ 00154 return 'd'; 00155 } 00156 else if(jy < 24.0){ 00157 return 'w'; 00158 } 00159 else if(jy > 1000.0){ 00160 return 's'; 00161 }else{ 00162 return NULL; 00163 } 00164 } 00165 int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre. 00166 if(jx>=450.0&&jx<=600.0){ 00167 if(jy>=450.0&&jy<=600.0){ 00168 return 1; 00169 }else{ 00170 return 0; 00171 } 00172 }else{ 00173 return 0; 00174 } 00175 } 00176 void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick). 00177 switch(M){ 00178 case 'a': 00179 if(select_Position%10!=0){select_Position = select_Position - 1;} 00180 break; 00181 case 'd': 00182 if(select_Position%10!=7){select_Position = select_Position + 1;} 00183 break; 00184 case 'w': 00185 if(select_Position/10!=0){select_Position = select_Position - 10;} 00186 break; 00187 case 's': 00188 if(select_Position/10!=7){select_Position = select_Position + 10;} 00189 break; 00190 } 00191 } 00192 int get_position(int input_position,int ship_type){ //---------------------------Plot ship in ship area. 00193 int i,x,y = 0; 00194 int error = 0; 00195 00196 y = input_position/10; 00197 x = input_position%10; 00198 00199 for(i=0;i<ship_type;i++){ 00200 if(check_correctPosition(y,x) == 1){ 00201 if(ship_type == 4){ship4[i] = (y*10)+x;} 00202 else if(ship_type == 3){ship3[i] = (y*10)+x;} 00203 else if(ship_type == 2){ship2[check_ship2][i] = (y*10)+x;} 00204 else if(ship_type == 1){ship1[check_ship1] = (y*10)+x;check_ship1++;} 00205 }else{ 00206 error = 1; 00207 break; 00208 } 00209 if(ship_type%2 == 0){ 00210 y++; 00211 }else{ 00212 x++; 00213 } 00214 } 00215 if(ship_type==2){ 00216 check_ship2++; 00217 } 00218 if(error == 1){ 00219 return 0; 00220 }else{ 00221 y = input_position/10; 00222 x = input_position%10; 00223 for(i=0;i<ship_type;i++){ 00224 ship_flor[y][x] = ship_type; 00225 if(ship_type%2 == 0){ 00226 y++; 00227 }else{ 00228 x++; 00229 } 00230 } 00231 return 1; 00232 } 00233 } 00234 int check_correctPosition(int X,int Y){ 00235 if(X<=7){ 00236 if(Y<=7){ 00237 if(ship_flor[X][Y] == 0){ 00238 return 1; 00239 }else{ 00240 return 0; 00241 } 00242 } 00243 }else{ 00244 return 0; 00245 } 00246 } 00247 void print_metrix(){ 00248 for(int i=0;i<8;i++){ 00249 for(int j=0;j<8;j++){ 00250 printf("| %d |",ship_flor[i][j]); 00251 } 00252 printf("\n"); 00253 } 00254 } 00255 void check_ready(){ 00256 if(bt.readable()){ 00257 U_ready = 1; 00258 } 00259 } 00260 /* 00261 int check_when_enemyATK(int position){ 00262 00263 } 00264 */
Generated on Sat Jul 23 2022 16:43:01 by
1.7.2
