Serial Communication
Dependencies: mbed
Fork of Nucleo_serial_menu by
main.cpp@6:cac4ed620e57, 2016-12-01 (annotated)
- Committer:
- gunpuen2
- Date:
- Thu Dec 01 08:45:38 2016 +0000
- Revision:
- 6:cac4ed620e57
- Parent:
- 5:85178023086a
- Child:
- 7:a651b3c90958
wait foe Joe
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 | 6:cac4ed620e57 | 88 | Vx = VRx.read() * 1024; |
gunpuen2 | 6:cac4ed620e57 | 89 | Vy = VRy.read() * 1024; |
gunpuen2 | 6:cac4ed620e57 | 90 | SW = Button.read(); |
gunpuen2 | 6:cac4ed620e57 | 91 | m = check_joys(Vx,Vy); |
gunpuen2 | 6:cac4ed620e57 | 92 | n = check_joys_int(Vx,Vy); |
gunpuen2 | 6:cac4ed620e57 | 93 | if(n == 1){ |
gunpuen2 | 6:cac4ed620e57 | 94 | iVxVy = 0; |
gunpuen2 | 6:cac4ed620e57 | 95 | } |
gunpuen2 | 6:cac4ed620e57 | 96 | if(SW == 1){ |
gunpuen2 | 6:cac4ed620e57 | 97 | iSW = 0; |
gunpuen2 | 6:cac4ed620e57 | 98 | } |
gunpuen2 | 6:cac4ed620e57 | 99 | if(m!=NULL && iVxVy==0){ |
gunpuen2 | 6:cac4ed620e57 | 100 | Select_Position(m); |
gunpuen2 | 6:cac4ed620e57 | 101 | printf("select_Position = %d\n",select_Position); |
gunpuen2 | 6:cac4ed620e57 | 102 | iVxVy = 1; |
gunpuen2 | 6:cac4ed620e57 | 103 | } |
gunpuen2 | 6:cac4ed620e57 | 104 | if(SW==0 && iSW==0){ |
gunpuen2 | 6:cac4ed620e57 | 105 | printf("send\n"); |
gunpuen2 | 6:cac4ed620e57 | 106 | bt.printf("%d",select_Position); |
gunpuen2 | 6:cac4ed620e57 | 107 | state = 0; |
gunpuen2 | 6:cac4ed620e57 | 108 | iSW = 1; |
gunpuen2 | 6:cac4ed620e57 | 109 | } |
gunpuen2 | 6:cac4ed620e57 | 110 | }else{ |
gunpuen2 | 6:cac4ed620e57 | 111 | while(1){ |
gunpuen2 | 6:cac4ed620e57 | 112 | if(bt.readable()){ |
gunpuen2 | 6:cac4ed620e57 | 113 | // Wait for resive position of enemy that atk you. |
gunpuen2 | 6:cac4ed620e57 | 114 | iWaitATK = 1; |
gunpuen2 | 6:cac4ed620e57 | 115 | } |
gunpuen2 | 6:cac4ed620e57 | 116 | if(iWaitATK==1){ |
gunpuen2 | 6:cac4ed620e57 | 117 | break; |
gunpuen2 | 6:cac4ed620e57 | 118 | } |
gunpuen2 | 6:cac4ed620e57 | 119 | } |
gunpuen2 | 6:cac4ed620e57 | 120 | } |
gunpuen2 | 5:85178023086a | 121 | } |
gunpuen2 | 5:85178023086a | 122 | } |
soulx | 0:f4e546303b4e | 123 | |
gunpuen2 | 6:cac4ed620e57 | 124 | |
gunpuen2 | 6:cac4ed620e57 | 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 | 5:85178023086a | 147 | char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right. |
gunpuen2 | 5:85178023086a | 148 | if(jx < 24.0){ |
gunpuen2 | 5:85178023086a | 149 | return 'a'; |
gunpuen2 | 5:85178023086a | 150 | } |
gunpuen2 | 5:85178023086a | 151 | else if(jx > 1000.0){ |
gunpuen2 | 5:85178023086a | 152 | return 'd'; |
gunpuen2 | 5:85178023086a | 153 | } |
gunpuen2 | 5:85178023086a | 154 | else if(jy < 24.0){ |
gunpuen2 | 5:85178023086a | 155 | return 'w'; |
gunpuen2 | 5:85178023086a | 156 | } |
gunpuen2 | 5:85178023086a | 157 | else if(jy > 1000.0){ |
gunpuen2 | 5:85178023086a | 158 | return 's'; |
gunpuen2 | 5:85178023086a | 159 | }else{ |
gunpuen2 | 5:85178023086a | 160 | return NULL; |
gunpuen2 | 5:85178023086a | 161 | } |
gunpuen2 | 5:85178023086a | 162 | } |
gunpuen2 | 5:85178023086a | 163 | int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre. |
gunpuen2 | 5:85178023086a | 164 | if(jx>=450.0&&jx<=600.0){ |
gunpuen2 | 5:85178023086a | 165 | if(jy>=450.0&&jy<=600.0){ |
gunpuen2 | 5:85178023086a | 166 | return 1; |
gunpuen2 | 5:85178023086a | 167 | }else{ |
gunpuen2 | 5:85178023086a | 168 | return 0; |
gunpuen2 | 5:85178023086a | 169 | } |
gunpuen2 | 5:85178023086a | 170 | }else{ |
gunpuen2 | 5:85178023086a | 171 | return 0; |
gunpuen2 | 5:85178023086a | 172 | } |
gunpuen2 | 5:85178023086a | 173 | } |
gunpuen2 | 5:85178023086a | 174 | void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick). |
gunpuen2 | 5:85178023086a | 175 | switch(M){ |
gunpuen2 | 5:85178023086a | 176 | case 'a': |
gunpuen2 | 5:85178023086a | 177 | if(select_Position%10!=0){select_Position = select_Position - 1;} |
gunpuen2 | 5:85178023086a | 178 | break; |
gunpuen2 | 5:85178023086a | 179 | case 'd': |
gunpuen2 | 5:85178023086a | 180 | if(select_Position%10!=7){select_Position = select_Position + 1;} |
gunpuen2 | 5:85178023086a | 181 | break; |
gunpuen2 | 5:85178023086a | 182 | case 'w': |
gunpuen2 | 5:85178023086a | 183 | if(select_Position/10!=0){select_Position = select_Position - 10;} |
gunpuen2 | 5:85178023086a | 184 | break; |
gunpuen2 | 5:85178023086a | 185 | case 's': |
gunpuen2 | 5:85178023086a | 186 | if(select_Position/10!=7){select_Position = select_Position + 10;} |
gunpuen2 | 5:85178023086a | 187 | break; |
gunpuen2 | 5:85178023086a | 188 | } |
gunpuen2 | 5:85178023086a | 189 | } |
gunpuen2 | 5:85178023086a | 190 | int get_position(int input_position,int ship_type){ //---------------------------Plot ship in ship area. |
gunpuen2 | 5:85178023086a | 191 | int i,x,y = 0; |
gunpuen2 | 5:85178023086a | 192 | int error = 0; |
gunpuen2 | 5:85178023086a | 193 | |
gunpuen2 | 5:85178023086a | 194 | x = input_position/10; |
gunpuen2 | 5:85178023086a | 195 | y = input_position%10; |
gunpuen2 | 5:85178023086a | 196 | |
gunpuen2 | 5:85178023086a | 197 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 5:85178023086a | 198 | if(check_correctPosition(x,y) == 0){ |
gunpuen2 | 5:85178023086a | 199 | error = 1; |
gunpuen2 | 5:85178023086a | 200 | break; |
gunpuen2 | 5:85178023086a | 201 | }else{ |
gunpuen2 | 5:85178023086a | 202 | if(ship_type == 4){ship4[i] = (x*10)+y;} |
gunpuen2 | 5:85178023086a | 203 | else if(ship_type == 3){ship3[i] = (x*10)+y;} |
gunpuen2 | 6:cac4ed620e57 | 204 | else if(ship_type == 2){ship2[check_ship2][i] = (x*10)+y;} |
gunpuen2 | 6:cac4ed620e57 | 205 | else if(ship_type == 1){ship1[check_ship1] = (x*10)+y;check_ship1++;} |
gunpuen2 | 5:85178023086a | 206 | } |
gunpuen2 | 5:85178023086a | 207 | if(ship_type%2 == 0){ |
gunpuen2 | 5:85178023086a | 208 | x++; |
gunpuen2 | 5:85178023086a | 209 | }else{ |
gunpuen2 | 5:85178023086a | 210 | y++; |
gunpuen2 | 5:85178023086a | 211 | } |
gunpuen2 | 5:85178023086a | 212 | } |
gunpuen2 | 6:cac4ed620e57 | 213 | if(ship_type==2){ |
gunpuen2 | 6:cac4ed620e57 | 214 | check_ship2++; |
gunpuen2 | 6:cac4ed620e57 | 215 | } |
gunpuen2 | 5:85178023086a | 216 | if(error == 1){ |
gunpuen2 | 5:85178023086a | 217 | return 0; |
gunpuen2 | 5:85178023086a | 218 | }else{ |
gunpuen2 | 5:85178023086a | 219 | x = input_position/10; |
gunpuen2 | 5:85178023086a | 220 | y = input_position%10; |
gunpuen2 | 5:85178023086a | 221 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 5:85178023086a | 222 | ship_flor[x][y] = ship_type; |
gunpuen2 | 5:85178023086a | 223 | if(ship_type%2 == 0){ |
gunpuen2 | 5:85178023086a | 224 | x++; |
gunpuen2 | 5:85178023086a | 225 | }else{ |
gunpuen2 | 5:85178023086a | 226 | y++; |
soulx | 0:f4e546303b4e | 227 | } |
soulx | 0:f4e546303b4e | 228 | } |
gunpuen2 | 5:85178023086a | 229 | return 1; |
soulx | 0:f4e546303b4e | 230 | } |
soulx | 0:f4e546303b4e | 231 | } |
gunpuen2 | 5:85178023086a | 232 | int check_correctPosition(int X,int Y){ |
gunpuen2 | 5:85178023086a | 233 | if(X<=7||Y<=7){ |
gunpuen2 | 5:85178023086a | 234 | if(ship_flor[X][Y] == 0){ |
gunpuen2 | 5:85178023086a | 235 | return 1; |
gunpuen2 | 5:85178023086a | 236 | }else{ |
gunpuen2 | 5:85178023086a | 237 | return 0; |
gunpuen2 | 5:85178023086a | 238 | } |
gunpuen2 | 5:85178023086a | 239 | }else{ |
gunpuen2 | 5:85178023086a | 240 | return 0; |
gunpuen2 | 5:85178023086a | 241 | } |
gunpuen2 | 5:85178023086a | 242 | } |
gunpuen2 | 5:85178023086a | 243 | void print_metrix(){ |
gunpuen2 | 5:85178023086a | 244 | for(int i=0;i<8;i++){ |
gunpuen2 | 5:85178023086a | 245 | for(int j=0;j<8;j++){ |
gunpuen2 | 5:85178023086a | 246 | printf("| %d |",ship_flor[i][j]); |
gunpuen2 | 5:85178023086a | 247 | } |
gunpuen2 | 5:85178023086a | 248 | printf("\n"); |
gunpuen2 | 5:85178023086a | 249 | } |
gunpuen2 | 6:cac4ed620e57 | 250 | } |
gunpuen2 | 6:cac4ed620e57 | 251 | void check_ready(){ |
gunpuen2 | 6:cac4ed620e57 | 252 | if(bt.readable()){ |
gunpuen2 | 6:cac4ed620e57 | 253 | U_ready = 1; |
gunpuen2 | 6:cac4ed620e57 | 254 | } |
gunpuen2 | 6:cac4ed620e57 | 255 | } |
gunpuen2 | 6:cac4ed620e57 | 256 | /* |
gunpuen2 | 6:cac4ed620e57 | 257 | int check_when_enemyATK(int position){ |
gunpuen2 | 6:cac4ed620e57 | 258 | |
gunpuen2 | 6:cac4ed620e57 | 259 | } |
gunpuen2 | 6:cac4ed620e57 | 260 | */ |