Serial Communication
Dependencies: mbed
Fork of Nucleo_serial_menu by
main.cpp@5:85178023086a, 2016-11-30 (annotated)
- Committer:
- gunpuen2
- Date:
- Wed Nov 30 06:11:39 2016 +0000
- Revision:
- 5:85178023086a
- Parent:
- 4:fde2463b2846
- Child:
- 6:cac4ed620e57
For my team (FRA221)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soulx | 0:f4e546303b4e | 1 | #include "mbed.h" |
gunpuen2 | 5:85178023086a | 2 | |
gunpuen2 | 5:85178023086a | 3 | // Initail I/O. |
gunpuen2 | 5:85178023086a | 4 | AnalogIn VRx(A2); |
gunpuen2 | 5:85178023086a | 5 | AnalogIn VRy(A3); |
gunpuen2 | 5:85178023086a | 6 | DigitalIn Button(D13); |
soulx | 0:f4e546303b4e | 7 | |
gunpuen2 | 5:85178023086a | 8 | // Initial function. |
gunpuen2 | 5:85178023086a | 9 | char check_joys(int jx,int jy); |
gunpuen2 | 5:85178023086a | 10 | int check_joys_int(int jx,int jy); |
gunpuen2 | 5:85178023086a | 11 | int check_correctPosition(int X,int Y); |
gunpuen2 | 5:85178023086a | 12 | int get_position(int input_position,int ship_type); |
gunpuen2 | 5:85178023086a | 13 | void Select_Position(char M); |
gunpuen2 | 5:85178023086a | 14 | void print_metrix(); |
soulx | 0:f4e546303b4e | 15 | |
soulx | 0:f4e546303b4e | 16 | |
gunpuen2 | 5:85178023086a | 17 | // Initial variable. |
gunpuen2 | 5:85178023086a | 18 | int select_Position = 0; |
gunpuen2 | 5:85178023086a | 19 | int check_ship2 = 0; |
gunpuen2 | 5:85178023086a | 20 | int ship_flor[8][8] = {{0}}; //__________________________________________________This is ship area. |
gunpuen2 | 5:85178023086a | 21 | int ship4[4] = {0}; |
gunpuen2 | 5:85178023086a | 22 | int ship3[3] = {0}; |
gunpuen2 | 5:85178023086a | 23 | int ship2[2][2] = {{0}}; |
gunpuen2 | 5:85178023086a | 24 | int ship1[4] = {0}; |
soulx | 0:f4e546303b4e | 25 | |
gunpuen2 | 5:85178023086a | 26 | int main(){ //-------------------------------------------------------------------Main Function. |
gunpuen2 | 5:85178023086a | 27 | float Vx; |
gunpuen2 | 5:85178023086a | 28 | float Vy; |
gunpuen2 | 5:85178023086a | 29 | int SW,iVxVy,iSW,n; |
gunpuen2 | 5:85178023086a | 30 | int iShipType = 0; |
gunpuen2 | 5:85178023086a | 31 | int select_Ship[8] = {4,3,2,2,1,1,1,1}; |
gunpuen2 | 5:85178023086a | 32 | char m; |
gunpuen2 | 5:85178023086a | 33 | while(1) { |
gunpuen2 | 5:85178023086a | 34 | if(iShipType<8){ |
gunpuen2 | 5:85178023086a | 35 | Vx = VRx.read() * 1024; |
gunpuen2 | 5:85178023086a | 36 | Vy = VRy.read() * 1024; |
gunpuen2 | 5:85178023086a | 37 | SW = Button.read(); |
gunpuen2 | 5:85178023086a | 38 | |
gunpuen2 | 5:85178023086a | 39 | m = check_joys(Vx,Vy); |
gunpuen2 | 5:85178023086a | 40 | n = check_joys_int(Vx,Vy); |
gunpuen2 | 5:85178023086a | 41 | if(n == 1){ //_______________________________________________________ For make sure coordinate is press 1 time. |
gunpuen2 | 5:85178023086a | 42 | iVxVy = 0; |
gunpuen2 | 5:85178023086a | 43 | } |
gunpuen2 | 5:85178023086a | 44 | if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time. |
gunpuen2 | 5:85178023086a | 45 | iSW = 0; |
gunpuen2 | 5:85178023086a | 46 | } |
gunpuen2 | 5:85178023086a | 47 | if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. |
gunpuen2 | 5:85178023086a | 48 | //printf("%c\n",m); |
gunpuen2 | 5:85178023086a | 49 | Select_Position(m); |
gunpuen2 | 5:85178023086a | 50 | printf("select_Position = %d\n",select_Position); |
gunpuen2 | 5:85178023086a | 51 | iVxVy = 1; |
gunpuen2 | 5:85178023086a | 52 | } |
gunpuen2 | 5:85178023086a | 53 | if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human. |
gunpuen2 | 5:85178023086a | 54 | printf("Wait!\n"); |
gunpuen2 | 5:85178023086a | 55 | if(get_position(select_Position,select_Ship[iShipType])==0){ //__Please read explanation in function get_position. |
gunpuen2 | 5:85178023086a | 56 | printf("Pls. input correct position\n"); |
gunpuen2 | 5:85178023086a | 57 | }else{ |
gunpuen2 | 5:85178023086a | 58 | iShipType++; |
gunpuen2 | 5:85178023086a | 59 | } |
gunpuen2 | 5:85178023086a | 60 | iSW = 1; |
gunpuen2 | 5:85178023086a | 61 | print_metrix(); |
gunpuen2 | 5:85178023086a | 62 | } |
gunpuen2 | 5:85178023086a | 63 | }else{ |
gunpuen2 | 5:85178023086a | 64 | break; |
gunpuen2 | 5:85178023086a | 65 | } |
gunpuen2 | 5:85178023086a | 66 | //printf("x: %d ||y: %d ||sw: %d\n",(int)Vx,(int)Vy,SW); |
gunpuen2 | 5:85178023086a | 67 | wait(0.1); |
gunpuen2 | 5:85178023086a | 68 | } |
gunpuen2 | 5:85178023086a | 69 | while(1){ |
gunpuen2 | 5:85178023086a | 70 | printf("Ready to play\n"); |
gunpuen2 | 5:85178023086a | 71 | } |
gunpuen2 | 5:85178023086a | 72 | } |
soulx | 0:f4e546303b4e | 73 | |
gunpuen2 | 5:85178023086a | 74 | char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right. |
gunpuen2 | 5:85178023086a | 75 | if(jx < 24.0){ |
gunpuen2 | 5:85178023086a | 76 | return 'a'; |
gunpuen2 | 5:85178023086a | 77 | } |
gunpuen2 | 5:85178023086a | 78 | else if(jx > 1000.0){ |
gunpuen2 | 5:85178023086a | 79 | return 'd'; |
gunpuen2 | 5:85178023086a | 80 | } |
gunpuen2 | 5:85178023086a | 81 | else if(jy < 24.0){ |
gunpuen2 | 5:85178023086a | 82 | return 'w'; |
gunpuen2 | 5:85178023086a | 83 | } |
gunpuen2 | 5:85178023086a | 84 | else if(jy > 1000.0){ |
gunpuen2 | 5:85178023086a | 85 | return 's'; |
gunpuen2 | 5:85178023086a | 86 | }else{ |
gunpuen2 | 5:85178023086a | 87 | return NULL; |
gunpuen2 | 5:85178023086a | 88 | } |
gunpuen2 | 5:85178023086a | 89 | } |
gunpuen2 | 5:85178023086a | 90 | int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre. |
gunpuen2 | 5:85178023086a | 91 | if(jx>=450.0&&jx<=600.0){ |
gunpuen2 | 5:85178023086a | 92 | if(jy>=450.0&&jy<=600.0){ |
gunpuen2 | 5:85178023086a | 93 | return 1; |
gunpuen2 | 5:85178023086a | 94 | }else{ |
gunpuen2 | 5:85178023086a | 95 | return 0; |
gunpuen2 | 5:85178023086a | 96 | } |
gunpuen2 | 5:85178023086a | 97 | }else{ |
gunpuen2 | 5:85178023086a | 98 | return 0; |
gunpuen2 | 5:85178023086a | 99 | } |
gunpuen2 | 5:85178023086a | 100 | } |
gunpuen2 | 5:85178023086a | 101 | void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick). |
gunpuen2 | 5:85178023086a | 102 | switch(M){ |
gunpuen2 | 5:85178023086a | 103 | case 'a': |
gunpuen2 | 5:85178023086a | 104 | if(select_Position%10!=0){select_Position = select_Position - 1;} |
gunpuen2 | 5:85178023086a | 105 | break; |
gunpuen2 | 5:85178023086a | 106 | case 'd': |
gunpuen2 | 5:85178023086a | 107 | if(select_Position%10!=7){select_Position = select_Position + 1;} |
gunpuen2 | 5:85178023086a | 108 | break; |
gunpuen2 | 5:85178023086a | 109 | case 'w': |
gunpuen2 | 5:85178023086a | 110 | if(select_Position/10!=0){select_Position = select_Position - 10;} |
gunpuen2 | 5:85178023086a | 111 | break; |
gunpuen2 | 5:85178023086a | 112 | case 's': |
gunpuen2 | 5:85178023086a | 113 | if(select_Position/10!=7){select_Position = select_Position + 10;} |
gunpuen2 | 5:85178023086a | 114 | break; |
gunpuen2 | 5:85178023086a | 115 | } |
gunpuen2 | 5:85178023086a | 116 | } |
gunpuen2 | 5:85178023086a | 117 | int get_position(int input_position,int ship_type){ //---------------------------Plot ship in ship area. |
gunpuen2 | 5:85178023086a | 118 | int i,x,y = 0; |
gunpuen2 | 5:85178023086a | 119 | int error = 0; |
gunpuen2 | 5:85178023086a | 120 | |
gunpuen2 | 5:85178023086a | 121 | x = input_position/10; |
gunpuen2 | 5:85178023086a | 122 | y = input_position%10; |
gunpuen2 | 5:85178023086a | 123 | |
gunpuen2 | 5:85178023086a | 124 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 5:85178023086a | 125 | if(check_correctPosition(x,y) == 0){ |
gunpuen2 | 5:85178023086a | 126 | error = 1; |
gunpuen2 | 5:85178023086a | 127 | break; |
gunpuen2 | 5:85178023086a | 128 | }else{ |
gunpuen2 | 5:85178023086a | 129 | if(ship_type == 4){ship4[i] = (x*10)+y;} |
gunpuen2 | 5:85178023086a | 130 | else if(ship_type == 3){ship3[i] = (x*10)+y;} |
gunpuen2 | 5:85178023086a | 131 | else if(ship_type == 2){ship2[check_ship2][i] = (x*10)+y;check_ship2++;} |
gunpuen2 | 5:85178023086a | 132 | else if(ship_type == 1){ship1[i] = (x*10)+y;} |
gunpuen2 | 5:85178023086a | 133 | } |
gunpuen2 | 5:85178023086a | 134 | if(ship_type%2 == 0){ |
gunpuen2 | 5:85178023086a | 135 | x++; |
gunpuen2 | 5:85178023086a | 136 | }else{ |
gunpuen2 | 5:85178023086a | 137 | y++; |
gunpuen2 | 5:85178023086a | 138 | } |
gunpuen2 | 5:85178023086a | 139 | } |
gunpuen2 | 5:85178023086a | 140 | if(error == 1){ |
gunpuen2 | 5:85178023086a | 141 | return 0; |
gunpuen2 | 5:85178023086a | 142 | }else{ |
gunpuen2 | 5:85178023086a | 143 | x = input_position/10; |
gunpuen2 | 5:85178023086a | 144 | y = input_position%10; |
gunpuen2 | 5:85178023086a | 145 | for(i=0;i<ship_type;i++){ |
gunpuen2 | 5:85178023086a | 146 | ship_flor[x][y] = ship_type; |
gunpuen2 | 5:85178023086a | 147 | if(ship_type%2 == 0){ |
gunpuen2 | 5:85178023086a | 148 | x++; |
gunpuen2 | 5:85178023086a | 149 | }else{ |
gunpuen2 | 5:85178023086a | 150 | y++; |
soulx | 0:f4e546303b4e | 151 | } |
soulx | 0:f4e546303b4e | 152 | } |
gunpuen2 | 5:85178023086a | 153 | return 1; |
soulx | 0:f4e546303b4e | 154 | } |
soulx | 0:f4e546303b4e | 155 | } |
gunpuen2 | 5:85178023086a | 156 | int check_correctPosition(int X,int Y){ |
gunpuen2 | 5:85178023086a | 157 | if(X<=7||Y<=7){ |
gunpuen2 | 5:85178023086a | 158 | if(ship_flor[X][Y] == 0){ |
gunpuen2 | 5:85178023086a | 159 | return 1; |
gunpuen2 | 5:85178023086a | 160 | }else{ |
gunpuen2 | 5:85178023086a | 161 | return 0; |
gunpuen2 | 5:85178023086a | 162 | } |
gunpuen2 | 5:85178023086a | 163 | }else{ |
gunpuen2 | 5:85178023086a | 164 | return 0; |
gunpuen2 | 5:85178023086a | 165 | } |
gunpuen2 | 5:85178023086a | 166 | } |
gunpuen2 | 5:85178023086a | 167 | void print_metrix(){ |
gunpuen2 | 5:85178023086a | 168 | for(int i=0;i<8;i++){ |
gunpuen2 | 5:85178023086a | 169 | for(int j=0;j<8;j++){ |
gunpuen2 | 5:85178023086a | 170 | printf("| %d |",ship_flor[i][j]); |
gunpuen2 | 5:85178023086a | 171 | } |
gunpuen2 | 5:85178023086a | 172 | printf("\n"); |
gunpuen2 | 5:85178023086a | 173 | } |
gunpuen2 | 5:85178023086a | 174 | } |