Serial Communication

Dependencies:   mbed

Fork of Nucleo_serial_menu by FRA221_2017

Revision:
5:85178023086a
Parent:
4:fde2463b2846
Child:
6:cac4ed620e57
diff -r fde2463b2846 -r 85178023086a main.cpp
--- a/main.cpp	Tue Oct 18 08:26:25 2016 +0000
+++ b/main.cpp	Wed Nov 30 06:11:39 2016 +0000
@@ -1,136 +1,174 @@
 #include "mbed.h"
-
-//------------------------------------
-// Hyperterminal configuration
-// 9600 bauds, 8-bit data, no parity
-//------------------------------------
-
-Serial pc(D8, D2);
-
-DigitalOut myled(LED1);
-
-int main()
-{
-
-    uint8_t state_menu=0;
-    uint8_t state_show=0;
-    uint8_t state_exit =0;
-    uint8_t data;
-
-    myled = 0;
-    pc.printf("Hello World !\n\n");
-    while(1) {
-        if(state_show == 0) {
-            pc.printf("************\n");
-            pc.printf("   Menu   \n");
-            pc.printf("************\n");
-            pc.printf("1.LED Test\n");
-            pc.printf("2.Print Test\n");
-            state_show =1;
-        }
-        if(pc.readable()) {
-            data = pc.getc();
-            pc.printf("\n");
-            state_show =0;
-            state_exit =0;
-
-            switch(data) {
-                case '1':
-                    do {
-                            if(state_menu == 0) {
-                                pc.printf("LED Test\n");
-                                pc.printf("a.LED ON\n");
-                                pc.printf("s.LED OFF\n");
-                                pc.printf("x.Exit\n");
-                                state_menu = 1;
-                            }
-                            
-                        if(pc.readable()) {
-                            data = pc.getc();
-                            pc.printf("\n");
-                            state_menu=0;
-
-                            switch(data) {
-
-                                case 'a':
-                                    myled = 1;                                    
-                                    break;
+ 
+// Initail I/O.
+AnalogIn VRx(A2);
+AnalogIn VRy(A3);
+DigitalIn Button(D13);
 
-                                case 's':
-                                    myled =0;
-                                   
-                                    break;
-
-                                case 'x':
-                                    state_exit =1;
-                                   
-                                    break;
-
-                                default:
-                                    pc.printf("plz select a or s\n");
-                                    pc.printf("\n\n");
-                                    break;
-                            }
-                        }
-                    } while(state_exit ==0);
-                    
-                    pc.printf("\n");
-                    break;
-
-                case '2':
-
-                    do {
-                        if(state_menu == 0) {
-                            pc.printf("Print Test\n");
-                            pc.printf("a.Print -Hello-\n");
-                            pc.printf("s.Print -Bye Bye-\n");
-                            pc.printf("x.Exit\n");
-                            state_menu = 1;
-                        }
-                        if(pc.readable()) {
-                            data = pc.getc();
-                            pc.printf("\n");
-                            state_menu=0;
-
-                            switch(data) {
-
-                                case 'a':
-                                    pc.printf("--> Hello <-- \n\n");
-                                    
-                                    break;
-
-                                case 's':
-                                    pc.printf("--> Bye Bye <-- \n\n");
-                                    
-                                    break;
-
-                                case 'x':
-                                    state_exit =1;
-                                  
-                                    
-                                    break;
-
-                                default:
-                                    pc.printf("plz select a or s\n");
-                                    pc.printf("\n");
-                                    break;
-
-                            }
-                        }
-                    } while(state_exit ==0);
-                    pc.printf("\n");
-                    break;
+// 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 print_metrix();
 
 
-                case 0x00:
+// Initial variable.
+int select_Position = 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}};
+int ship1[4] = {0};
 
-                    break;
+int main(){ //-------------------------------------------------------------------Main Function.
+    float Vx;
+    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;
+            }
+            if(SW == 1){ //______________________________________________________ For make sure switch is press 1 time.
+                iSW = 0;
+            }
+            if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch.
+                //printf("%c\n",m);
+                Select_Position(m);
+                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");
+                }else{
+                    iShipType++;
+                }
+                iSW = 1;
+                print_metrix();
+            }
+        }else{
+            break;
+        }
+        //printf("x: %d ||y: %d ||sw: %d\n",(int)Vx,(int)Vy,SW);
+        wait(0.1);
+    }
+    while(1){
+        printf("Ready to play\n");
+    }
+}
 
-                default:
-                    pc.printf("plz select 1 or 2 only\n");
-                    pc.printf("\n");
-                    break;
+char check_joys(int jx,int jy){ //-----------------------------------------------Check joystick that it up,down,left or right.
+    if(jx < 24.0){
+        return 'a';
+    }
+    else if(jx > 1000.0){
+        return 'd';
+    }
+    else if(jy < 24.0){
+        return 'w';
+    }
+    else if(jy > 1000.0){
+        return 's';
+    }else{
+        return NULL;
+    }
+}
+int check_joys_int(int jx,int jy){ //--------------------------------------------Check joystick that it is centre.
+    if(jx>=450.0&&jx<=600.0){
+        if(jy>=450.0&&jy<=600.0){
+            return 1;
+        }else{
+            return 0;
+        }
+    }else{
+        return 0;
+    }
+}
+void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick).
+    switch(M){
+        case 'a':
+            if(select_Position%10!=0){select_Position = select_Position - 1;}
+            break;
+        case 'd':
+            if(select_Position%10!=7){select_Position = select_Position + 1;}
+            break;
+        case 'w':
+            if(select_Position/10!=0){select_Position = select_Position - 10;}
+            break;
+        case 's':
+            if(select_Position/10!=7){select_Position = select_Position + 10;}
+            break;
+    }
+}
+int get_position(int input_position,int ship_type){ //---------------------------Plot ship in ship area.
+    int i,x,y = 0;
+    int error = 0;
+
+    x = input_position/10;
+    y = input_position%10;
+
+    for(i=0;i<ship_type;i++){
+        if(check_correctPosition(x,y) == 0){
+            error = 1;
+            break;
+        }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;}
+        }
+        if(ship_type%2 == 0){
+            x++;
+        }else{
+            y++;
+        }
+    }
+    if(error == 1){
+        return 0;
+    }else{
+        x = input_position/10;
+        y = input_position%10;
+        for(i=0;i<ship_type;i++){
+            ship_flor[x][y] = ship_type;
+            if(ship_type%2 == 0){
+                x++;
+            }else{
+                y++;
             }
         }
+        return 1;
     }
 }
+int check_correctPosition(int X,int Y){
+    if(X<=7||Y<=7){
+        if(ship_flor[X][Y] == 0){
+            return 1;
+        }else{
+            return 0;
+        }
+    }else{
+        return 0;
+    }
+}
+void print_metrix(){
+    for(int i=0;i<8;i++){
+        for(int j=0;j<8;j++){
+            printf("| %d |",ship_flor[i][j]);
+        }
+        printf("\n");
+    }
+}
\ No newline at end of file