SEU10f / Mbed 2 deprecated P4

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Blasko
Date:
Tue Oct 27 18:58:10 2015 +0000
Parent:
1:0bb74fa30daa
Commit message:
sdf

Changed in this revision

pwm.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 0bb74fa30daa -r 4e86f37f1c1c pwm.cpp
--- a/pwm.cpp	Tue Oct 20 17:10:53 2015 +0000
+++ b/pwm.cpp	Tue Oct 27 18:58:10 2015 +0000
@@ -9,29 +9,87 @@
 char counter = 0;
 int x=0;
 int y=0;
+int px=0;
+int py=0;
+int state = 0; // 0 INI 1 JUGANDO 2 ACABADO
+
 
 #define PERIOD 0.000005
 #define MAX 1300.0f
 void send() {
-
+    //printf("Empezando \n");     
+    ////printf("send()\n");
+    char s[8];
+    s[0]=0;
+    if(can2.write(CANMessage(0, s, 8))) {
+    //    printf("Message sent: \n");
+    }else{
+    //    printf("Message fail: \n");
+    }
  }
 int main() {
-    printf("main d()\n");
-    ticker.attach(&send, 1);
+      printf("Esperando Jugador \n\r");  
+  //  ticker.attach(&send, 1);
     CANMessage msg;
     can2.frequency(1000000);
     pwmx.period(PERIOD);  // 4 second period
     pwmy.period(PERIOD); 
     while(1) {
       //  printf("loop()\n");
+      
+        if(state == 0){
+            if(counter ==100){  led1=!led1; counter =0;}  
+            counter++;                      
+        }
+      
         if(can2.read(msg)) {
-            x = ((int) msg.data[5])*255 + ((int)msg.data[4]);
-            y = ((int)msg.data[1])*255 + ((int) msg.data[0]);
-            pwmx.write(x/MAX); 
-            pwmy.write(1-y/MAX); 
-            printf("Message received: x %d %d %d %d %d %d %d %d %d %d\r\n",msg.data[0],msg.data[1],msg.data[2],msg.data[3],msg.data[4],msg.data[5],msg.data[6],msg.data[7], x , y);
-            led2 = !led2;
-        } 
-        wait(0.05);
+            
+            if(state == 0){
+             //  printf("Esperando Jugador");    
+                if(msg.id == 5){
+                        printf("Jugando \n\r");       
+                       state=1;
+                       x=0;
+                       y=0;
+                       px = ((int) msg.data[3])*256 + ((int)msg.data[2]);
+                       py = ((int)msg.data[1])*256 + ((int) msg.data[0]);   
+                       send();   
+                   //    printf("Estado %d %d \n\r",px,py);      
+                }     
+            }else if (state==1) {
+                if(msg.id == 5){
+                    send(); 
+                }
+                if( msg.id ==4 || msg.id ==3){ 
+                    x = ((int) msg.data[3])*256 + ((int)msg.data[2]);
+                    y = ((int) msg.data[1])*256 + ((int) msg.data[0]);     
+                    pwmx.write(y/MAX); 
+                    pwmy.write(1-x/MAX);
+                
+               
+                    if(abs(px-x) + abs(py-y) <=50){
+                        state=2;   
+                        if(msg.id==4){
+                            printf("I lost  \n\r");   
+                        }else{
+                            printf("I won  \n\r");   
+                        }
+                    }
+                }
+                //printf("Message received: x %d %d %d %d %d %d %d %d %d %d\r\n",msg.data[0],msg.data[1],msg.data[2],msg.data[3],msg.data[4],msg.data[5],msg.data[6],msg.data[7], x , y);
+                led2 = !led2;
+            }else{
+              wait(5);   
+              state=0;
+              printf("--------------------------- \n\r");
+               printf("Esperando Jugador \n\r");    
+            }
+            
+          //  printf("ID: %d \n\r",msg.id);    
+        }else {
+        
+        }
+      //  printf("Estado %d x: %d y: %d px: %d py: %d  \n\r",state,x,y,px,py);       
+       // wait(0.005);
     }
 }
\ No newline at end of file