Serial Communication
Dependencies: mbed
Fork of Nucleo_serial_menu by
main.cpp@8:d90b2dfb339d, 2016-12-01 (annotated)
- Committer:
- gunpuen2
- Date:
- Thu Dec 01 09:40:13 2016 +0000
- Revision:
- 8:d90b2dfb339d
- Parent:
- 7:a651b3c90958
print u turn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gunpuen2 | 6:cac4ed620e57 | 1 | /******************************************************************************/ |
gunpuen2 | 6:cac4ed620e57 | 2 | /* This Nicleo is stat 0! */ |
gunpuen2 | 6:cac4ed620e57 | 3 | /******************************************************************************/ |
soulx | 0:f4e546303b4e | 4 | #include "mbed.h" |
gunpuen2 | 5:85178023086a | 5 | |
gunpuen2 | 5:85178023086a | 6 | // Initail I/O. |
gunpuen2 | 5:85178023086a | 7 | AnalogIn VRx(A2); |
gunpuen2 | 5:85178023086a | 8 | AnalogIn VRy(A3); |
gunpuen2 | 5:85178023086a | 9 | DigitalIn Button(D13); |
gunpuen2 | 6:cac4ed620e57 | 10 | Serial bt(PA_15, PB_7); |
soulx | 0:f4e546303b4e | 11 | |
gunpuen2 | 5:85178023086a | 12 | // Initial function. |
gunpuen2 | 5:85178023086a | 13 | char check_joys(int jx,int jy); |
gunpuen2 | 5:85178023086a | 14 | int check_joys_int(int jx,int jy); |
gunpuen2 | 5:85178023086a | 15 | int check_correctPosition(int X,int Y); |
gunpuen2 | 5:85178023086a | 16 | int get_position(int input_position,int ship_type); |
gunpuen2 | 6:cac4ed620e57 | 17 | void check_ready(); |
gunpuen2 | 5:85178023086a | 18 | void print_metrix(); |
gunpuen2 | 6:cac4ed620e57 | 19 | void Select_Position(char M); |
soulx | 0:f4e546303b4e | 20 | |
gunpuen2 | 5:85178023086a | 21 | // Initial variable. |
gunpuen2 | 5:85178023086a | 22 | int select_Position = 0; |
gunpuen2 | 6:cac4ed620e57 | 23 | int ATKposition = 0; |
gunpuen2 | 6:cac4ed620e57 | 24 | int iWaitATK = 0; |
gunpuen2 | 6:cac4ed620e57 | 25 | int U_ready = 0; |
gunpuen2 | 6:cac4ed620e57 | 26 | int state = 0; |
gunpuen2 | 6:cac4ed620e57 | 27 | int ship_notdestroy[8] = {4,3,2,2,1,1,1,1}; |
gunpuen2 | 6:cac4ed620e57 | 28 | int ship_destroyed[8] = {0}; |
gunpuen2 | 6:cac4ed620e57 | 29 | int ship_flor[8][8] = {{0}}; //__________________________________________________This is ship area. |
gunpuen2 | 6:cac4ed620e57 | 30 | int check_ship1 = 0; |
gunpuen2 | 5:85178023086a | 31 | int check_ship2 = 0; |
gunpuen2 | 5:85178023086a | 32 | int ship4[4] = {0}; |
gunpuen2 | 5:85178023086a | 33 | int ship3[3] = {0}; |
gunpuen2 | 5:85178023086a | 34 | int ship2[2][2] = {{0}}; |
gunpuen2 | 5:85178023086a | 35 | int ship1[4] = {0}; |
soulx | 0:f4e546303b4e | 36 | |
gunpuen2 | 5:85178023086a | 37 | int main(){ //-------------------------------------------------------------------Main Function. |
gunpuen2 | 5:85178023086a | 38 | float Vx; |
gunpuen2 | 5:85178023086a | 39 | float Vy; |
gunpuen2 | 5:85178023086a | 40 | int SW,iVxVy,iSW,n; |
gunpuen2 | 5:85178023086a | 41 | int iShipType = 0; |
gunpuen2 | 5:85178023086a | 42 | char m; |
gunpuen2 | 5:85178023086a | 43 | while(1) { |
gunpuen2 | 5:85178023086a | 44 | if(iShipType<8){ |
gunpuen2 | 5:85178023086a | 45 | Vx = VRx.read() * 1024; |
gunpuen2 | 5:85178023086a | 46 | Vy = VRy.read() * 1024; |
gunpuen2 | 5:85178023086a | 47 | SW = Button.read(); |
gunpuen2 | 5:85178023086a | 48 | m = check_joys(Vx,Vy); |
gunpuen2 | 5:85178023086a | 49 | n = check_joys_int(Vx,Vy); |
gunpuen2 | 5:85178023086a | 50 | if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time. |
gunpuen2 | 5:85178023086a | 51 | iVxVy = 0; |
gunpuen2 | 6:cac4ed620e57 | 52 | check_ready(); |
gunpuen2 | 5:85178023086a | 53 | } |
gunpuen2 | 5:85178023086a | 54 | if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time. |
gunpuen2 | 5:85178023086a | 55 | iSW = 0; |
gunpuen2 | 6:cac4ed620e57 | 56 | check_ready(); |
gunpuen2 | 5:85178023086a | 57 | } |
gunpuen2 | 5:85178023086a | 58 | if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. |
gunpuen2 | 5:85178023086a | 59 | Select_Position(m); |
gunpuen2 | 6:cac4ed620e57 | 60 | check_ready(); |
gunpuen2 | 5:85178023086a | 61 | printf("select_Position = %d\n",select_Position); |
gunpuen2 | 5:85178023086a | 62 | iVxVy = 1; |
gunpuen2 | 5:85178023086a | 63 | } |
gunpuen2 | 5:85178023086a | 64 | if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human. |
gunpuen2 | 5:85178023086a | 65 | printf("Wait!\n"); |
gunpuen2 | 6:cac4ed620e57 | 66 | check_ready(); |
gunpuen2 | 6:cac4ed620e57 | 67 | if(get_position(select_Position,ship_notdestroy[iShipType])==0){ //__Please read explanation in function get_position. |
gunpuen2 | 6:cac4ed620e57 | 68 | printf("Please input correct position\n"); |
gunpuen2 | 5:85178023086a | 69 | }else{ |
gunpuen2 | 5:85178023086a | 70 | iShipType++; |
gunpuen2 | 5:85178023086a | 71 | } |
gunpuen2 | 6:cac4ed620e57 | 72 | check_ready(); |
gunpuen2 | 5:85178023086a | 73 | iSW = 1; |
gunpuen2 | 5:85178023086a | 74 | print_metrix(); |
gunpuen2 | 5:85178023086a | 75 | } |
gunpuen2 | 5:85178023086a | 76 | }else{ |
gunpuen2 | 6:cac4ed620e57 | 77 | if(U_ready!=1){bt.putc('E');} |
gunpuen2 | 6:cac4ed620e57 | 78 | |
gunpuen2 | 6:cac4ed620e57 | 79 | printf("\n"); |
gunpuen2 | 5:85178023086a | 80 | break; |
gunpuen2 | 5:85178023086a | 81 | } |
gunpuen2 | 5:85178023086a | 82 | } |
gunpuen2 | 6:cac4ed620e57 | 83 | select_Position = 0; |
gunpuen2 | 6:cac4ed620e57 | 84 | |
gunpuen2 | 6:cac4ed620e57 | 85 | while(1) { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Play |
gunpuen2 | 6:cac4ed620e57 | 86 | if(U_ready == 1){state = 0;}else{state = 1;} |
gunpuen2 | 6:cac4ed620e57 | 87 | if(state == 1){ |
gunpuen2 | 8:d90b2dfb339d | 88 | printf("Your turn.\n"); |
gunpuen2 | 6:cac4ed620e57 | 89 | Vx = VRx.read() * 1024; |
gunpuen2 | 6:cac4ed620e57 | 90 | Vy = VRy.read() * 1024; |
gunpuen2 | 6:cac4ed620e57 | 91 | SW = Button.read(); |
gunpuen2 | 6:cac4ed620e57 | 92 | m = check_joys(Vx,Vy); |
gunpuen2 | 6:cac4ed620e57 | 93 | n = check_joys_int(Vx,Vy); |
gunpuen2 | 6:cac4ed620e57 | 94 | if(n == 1){ |
gunpuen2 | 6:cac4ed620e57 | 95 | iVxVy = 0; |
gunpuen2 | 6:cac4ed620e57 | 96 | } |
gunpuen2 | 6:cac4ed620e57 | 97 | if(SW == 1){ |
gunpuen2 | 6:cac4ed620e57 | 98 | iSW = 0; |
gunpuen2 | 6:cac4ed620e57 | 99 | } |
gunpuen2 | 6:cac4ed620e57 | 100 | if(m!=NULL && iVxVy==0){ |
gunpuen2 | 6:cac4ed620e57 | 101 | Select_Position(m); |
gunpuen2 | 6:cac4ed620e57 | 102 | printf("select_Position = %d\n",select_Position); |
gunpuen2 | 6:cac4ed620e57 | 103 | iVxVy = 1; |
gunpuen2 | 6:cac4ed620e57 | 104 | } |
gunpuen2 | 6:cac4ed620e57 | 105 | if(SW==0 && iSW==0){ |
gunpuen2 | 6:cac4ed620e57 | 106 | printf("send\n"); |
gunpuen2 | 6:cac4ed620e57 | 107 | bt.printf("%d",select_Position); |
gunpuen2 | 6:cac4ed620e57 | 108 | state = 0; |
gunpuen2 | 6:cac4ed620e57 | 109 | iSW = 1; |
gunpuen2 | 6:cac4ed620e57 | 110 | } |
gunpuen2 | 6:cac4ed620e57 | 111 | }else{ |
gunpuen2 | 6:cac4ed620e57 | 112 | while(1){ |
gunpuen2 | 7:a651b3c90958 | 113 | printf(""); |
gunpuen2 | 6:cac4ed620e57 | 114 | if(bt.readable()){ |
gunpuen2 | 6:cac4ed620e57 | 115 | // Wait for resive position of enemy that atk you. |
gunpuen2 | 6:cac4ed620e57 | 116 | iWaitATK = 1; |
gunpuen2 | 6:cac4ed620e57 | 117 | } |
gunpuen2 | 6:cac4ed620e57 | 118 | if(iWaitATK==1){ |
gunpuen2 | 6:cac4ed620e57 | 119 | break; |
gunpuen2 | 6:cac4ed620e57 | 120 | } |
gunpuen2 | 6:cac4ed620e57 | 121 | } |
gunpuen2 | 6:cac4ed620e57 | 122 | } |
gunpuen2 | 5:85178023086a | 123 | } |
gunpuen2 | 5:85178023086a | 124 | } |
soulx | 0:f4e546303b4e | 125 | |
gunpuen2 | 6:cac4ed620e57 | 126 | |
gunpuen2 | 6:cac4ed620e57 | 127 | |
gunpuen2 | 6:cac4ed620e57 | 128 | |
gunpuen2 | 6:cac4ed620e57 | 129 | |
gunpuen2 | 6:cac4ed620e57 | 130 | |
gunpuen2 | 6:cac4ed620e57 | 131 | |
gunpuen2 | 6:cac4ed620e57 | 132 | |
gunpuen2 | 6:cac4ed620e57 | 133 | |
gunpuen2 | 6:cac4ed620e57 | 134 | |
gunpuen2 | 6:cac4ed620e57 | 135 | |
gunpuen2 | 6:cac4ed620e57 | 136 | |
gunpuen2 | 6:cac4ed620e57 | 137 | |
gunpuen2 | 6:cac4ed620e57 | 138 | |
gunpuen2 | 6:cac4ed620e57 | 139 | |
gunpuen2 | 6:cac4ed620e57 | 140 | |
gunpuen2 | 6:cac4ed620e57 | 141 | |
gunpuen2 | 6:cac4ed620e57 | 142 | |
gunpuen2 | 6:cac4ed620e57 | 143 | |
gunpuen2 | 6:cac4ed620e57 | 144 | |
gunpuen2 | 6:cac4ed620e57 | 145 | |
gunpuen2 | 6:cac4ed620e57 | 146 | |
gunpuen2 | 6:cac4ed620e57 | 147 | |
gunpuen2 | 6:cac4ed620e57 | 148 | |
gunpuen2 | 5:85178023086a | 149 | char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right. |
gunpuen2 | 5:85178023086a | 150 | if(jx < 24.0){ |
gunpuen2 | 5:85178023086a | 151 | return 'a'; |
gunpuen2 | 5:85178023086a | 152 | } |
gunpuen2 | 5:85178023086a | 153 | else if(jx > 1000.0){ |
gunpuen2 | 5:85178023086a | 154 | return 'd'; |
gunpuen2 | 5:85178023086a | 155 | } |
gunpuen2 | 5:85178023086a | 156 | else if(jy < 24.0){ |
gunpuen2 | 5:85178023086a | 157 | return 'w'; |
gunpuen2 | 5:85178023086a | 158 | } |
gunpuen2 | 5:85178023086a | 159 | else if(jy > 1000.0){ |
gunpuen2 | 5:85178023086a | 160 | return 's'; |
gunpuen2 | 5:85178023086a | 161 | }else{ |
gunpuen2 | 5:85178023086a | 162 | return NULL; |
gunpuen2 | 5:85178023086a | 163 | } |
gunpuen2 | 5:85178023086a | 164 | } |
gunpuen2 | 5:85178023086a | 165 | int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre. |
gunpuen2 | 5:85178023086a | 166 | if(jx>=450.0&&jx<=600.0){ |
gunpuen2 | 5:85178023086a | 167 | if(jy>=450.0&&jy<=600.0){ |
gunpuen2 | 5:85178023086a | 168 | return 1; |
gunpuen2 | 5:85178023086a | 169 | }else{ |
gunpuen2 | 5:85178023086a | 170 | return 0; |
gunpuen2 | 5:85178023086a | 171 | } |
gunpuen2 | 5:85178023086a | 172 | }else{ |
gunpuen2 | 5:85178023086a | 173 | return 0; |
gunpuen2 | 5:85178023086a | 174 | } |
gunpuen2 | 5:85178023086a | 175 | } |
gunpuen2 | 5:85178023086a | 176 | void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick). |
gunpuen2 | 5:85178023086a | 177 | switch(M){ |
gunpuen2 | 5:85178023086a | 178 | case 'a': |
gunpuen2 | 5:85178023086a | 179 | if(select_Position%10!=0){select_Position = select_Position - 1;} |
gunpuen2 | 5:85178023086a | 180 | break; |
gunpuen2 | 5:85178023086a | 181 | case 'd': |
gunpuen2 | 5:85178023086a | 182 | if(select_Position%10!=7){select_Position = select_Position + 1;} |
gunpuen2 | 5:85178023086a | 183 | break; |
gunpuen2 | 5:85178023086a | 184 | case 'w': |
gunpuen2 | 5:85178023086a | 185 | if(select_Position/10!=0){select_Position = select_Position - 10;} |
gunpuen2 | 5:85178023086a | 186 | break; |
gunpuen2 | 5:85178023086a | 187 | case 's': |
gunpuen2 | 5:85178023086a | 188 | if(select_Position/10!=7){select_Position = select_Position + 10;} |
gunpuen2 | 5:85178023086a | 189 | break; |
gunpuen2 | 5:85178023086a | 190 | } |
gunpuen2 | 5:85178023086a | 191 | } |
gunpuen2 | 5:85178023086a | 192 | int get_position(int input_position,int ship_type){ //---------------------------Plot ship in ship area. |
gunpuen2 | 5:85178023086a | 193 | int i,x,y = 0; |
gunpuen2 | 5:85178023086a | 194 | int error = 0; |
gunpuen2 | 5:85178023086a | 195 | |
gunpuen2 | 7:a651b3c90958 | 196 | y = input_position/10; |
gunpuen2 | 7:a651b3c90958 | 197 | x = input_position%10; |
gunpuen2 | 5:85178023086a | 198 | |
gunpuen2 | 5:85178023086a | 199 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 7:a651b3c90958 | 200 | if(check_correctPosition(y,x) == 1){ |
gunpuen2 | 7:a651b3c90958 | 201 | if(ship_type == 4){ship4[i] = (y*10)+x;} |
gunpuen2 | 7:a651b3c90958 | 202 | else if(ship_type == 3){ship3[i] = (y*10)+x;} |
gunpuen2 | 7:a651b3c90958 | 203 | else if(ship_type == 2){ship2[check_ship2][i] = (y*10)+x;} |
gunpuen2 | 7:a651b3c90958 | 204 | else if(ship_type == 1){ship1[check_ship1] = (y*10)+x;check_ship1++;} |
gunpuen2 | 7:a651b3c90958 | 205 | }else{ |
gunpuen2 | 5:85178023086a | 206 | error = 1; |
gunpuen2 | 5:85178023086a | 207 | break; |
gunpuen2 | 5:85178023086a | 208 | } |
gunpuen2 | 5:85178023086a | 209 | if(ship_type%2 == 0){ |
gunpuen2 | 7:a651b3c90958 | 210 | y++; |
gunpuen2 | 5:85178023086a | 211 | }else{ |
gunpuen2 | 7:a651b3c90958 | 212 | x++; |
gunpuen2 | 5:85178023086a | 213 | } |
gunpuen2 | 5:85178023086a | 214 | } |
gunpuen2 | 6:cac4ed620e57 | 215 | if(ship_type==2){ |
gunpuen2 | 6:cac4ed620e57 | 216 | check_ship2++; |
gunpuen2 | 6:cac4ed620e57 | 217 | } |
gunpuen2 | 5:85178023086a | 218 | if(error == 1){ |
gunpuen2 | 5:85178023086a | 219 | return 0; |
gunpuen2 | 5:85178023086a | 220 | }else{ |
gunpuen2 | 7:a651b3c90958 | 221 | y = input_position/10; |
gunpuen2 | 7:a651b3c90958 | 222 | x = input_position%10; |
gunpuen2 | 5:85178023086a | 223 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 7:a651b3c90958 | 224 | ship_flor[y][x] = ship_type; |
gunpuen2 | 5:85178023086a | 225 | if(ship_type%2 == 0){ |
gunpuen2 | 7:a651b3c90958 | 226 | y++; |
gunpuen2 | 7:a651b3c90958 | 227 | }else{ |
gunpuen2 | 5:85178023086a | 228 | x++; |
soulx | 0:f4e546303b4e | 229 | } |
soulx | 0:f4e546303b4e | 230 | } |
gunpuen2 | 5:85178023086a | 231 | return 1; |
soulx | 0:f4e546303b4e | 232 | } |
soulx | 0:f4e546303b4e | 233 | } |
gunpuen2 | 5:85178023086a | 234 | int check_correctPosition(int X,int Y){ |
gunpuen2 | 7:a651b3c90958 | 235 | if(X<=7){ |
gunpuen2 | 7:a651b3c90958 | 236 | if(Y<=7){ |
gunpuen2 | 7:a651b3c90958 | 237 | if(ship_flor[X][Y] == 0){ |
gunpuen2 | 7:a651b3c90958 | 238 | return 1; |
gunpuen2 | 7:a651b3c90958 | 239 | }else{ |
gunpuen2 | 7:a651b3c90958 | 240 | return 0; |
gunpuen2 | 7:a651b3c90958 | 241 | } |
gunpuen2 | 5:85178023086a | 242 | } |
gunpuen2 | 5:85178023086a | 243 | }else{ |
gunpuen2 | 5:85178023086a | 244 | return 0; |
gunpuen2 | 5:85178023086a | 245 | } |
gunpuen2 | 5:85178023086a | 246 | } |
gunpuen2 | 5:85178023086a | 247 | void print_metrix(){ |
gunpuen2 | 5:85178023086a | 248 | for(int i=0;i<8;i++){ |
gunpuen2 | 5:85178023086a | 249 | for(int j=0;j<8;j++){ |
gunpuen2 | 5:85178023086a | 250 | printf("| %d |",ship_flor[i][j]); |
gunpuen2 | 5:85178023086a | 251 | } |
gunpuen2 | 5:85178023086a | 252 | printf("\n"); |
gunpuen2 | 5:85178023086a | 253 | } |
gunpuen2 | 6:cac4ed620e57 | 254 | } |
gunpuen2 | 6:cac4ed620e57 | 255 | void check_ready(){ |
gunpuen2 | 6:cac4ed620e57 | 256 | if(bt.readable()){ |
gunpuen2 | 6:cac4ed620e57 | 257 | U_ready = 1; |
gunpuen2 | 6:cac4ed620e57 | 258 | } |
gunpuen2 | 6:cac4ed620e57 | 259 | } |
gunpuen2 | 6:cac4ed620e57 | 260 | /* |
gunpuen2 | 6:cac4ed620e57 | 261 | int check_when_enemyATK(int position){ |
gunpuen2 | 6:cac4ed620e57 | 262 | |
gunpuen2 | 6:cac4ed620e57 | 263 | } |
gunpuen2 | 6:cac4ed620e57 | 264 | */ |