SEU10f / Mbed 2 deprecated P4

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pwm.cpp Source File

pwm.cpp

00001 #include "mbed.h"
00002  
00003 Ticker ticker;
00004 DigitalOut led1(LED1);
00005 DigitalOut led2(LED2);
00006 PwmOut pwmx(p26);
00007 PwmOut pwmy(p25);
00008 CAN can2(p30, p29);
00009 char counter = 0;
00010 int x=0;
00011 int y=0;
00012 int px=0;
00013 int py=0;
00014 int state = 0; // 0 INI 1 JUGANDO 2 ACABADO
00015 
00016 
00017 #define PERIOD 0.000005
00018 #define MAX 1300.0f
00019 void send() {
00020     //printf("Empezando \n");     
00021     ////printf("send()\n");
00022     char s[8];
00023     s[0]=0;
00024     if(can2.write(CANMessage(0, s, 8))) {
00025     //    printf("Message sent: \n");
00026     }else{
00027     //    printf("Message fail: \n");
00028     }
00029  }
00030 int main() {
00031       printf("Esperando Jugador \n\r");  
00032   //  ticker.attach(&send, 1);
00033     CANMessage msg;
00034     can2.frequency(1000000);
00035     pwmx.period(PERIOD);  // 4 second period
00036     pwmy.period(PERIOD); 
00037     while(1) {
00038       //  printf("loop()\n");
00039       
00040         if(state == 0){
00041             if(counter ==100){  led1=!led1; counter =0;}  
00042             counter++;                      
00043         }
00044       
00045         if(can2.read(msg)) {
00046             
00047             if(state == 0){
00048              //  printf("Esperando Jugador");    
00049                 if(msg.id == 5){
00050                         printf("Jugando \n\r");       
00051                        state=1;
00052                        x=0;
00053                        y=0;
00054                        px = ((int) msg.data[3])*256 + ((int)msg.data[2]);
00055                        py = ((int)msg.data[1])*256 + ((int) msg.data[0]);   
00056                        send();   
00057                    //    printf("Estado %d %d \n\r",px,py);      
00058                 }     
00059             }else if (state==1) {
00060                 if(msg.id == 5){
00061                     send(); 
00062                 }
00063                 if( msg.id ==4 || msg.id ==3){ 
00064                     x = ((int) msg.data[3])*256 + ((int)msg.data[2]);
00065                     y = ((int) msg.data[1])*256 + ((int) msg.data[0]);     
00066                     pwmx.write(y/MAX); 
00067                     pwmy.write(1-x/MAX);
00068                 
00069                
00070                     if(abs(px-x) + abs(py-y) <=50){
00071                         state=2;   
00072                         if(msg.id==4){
00073                             printf("I lost  \n\r");   
00074                         }else{
00075                             printf("I won  \n\r");   
00076                         }
00077                     }
00078                 }
00079                 //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);
00080                 led2 = !led2;
00081             }else{
00082               wait(5);   
00083               state=0;
00084               printf("--------------------------- \n\r");
00085                printf("Esperando Jugador \n\r");    
00086             }
00087             
00088           //  printf("ID: %d \n\r",msg.id);    
00089         }else {
00090         
00091         }
00092       //  printf("Estado %d x: %d y: %d px: %d py: %d  \n\r",state,x,y,px,py);       
00093        // wait(0.005);
00094     }
00095 }