v1.0

Dependencies:   CAN_CONSOLE_v3 mbed

Fork of CAN_CONSOLE_v3 by RaC2018

main.cpp

Committer:
protongamer
Date:
2018-03-02
Revision:
2:1e06fd49ea7b
Parent:
1:4d70e593345f
Child:
3:86e21a36eecb

File content as of revision 2:1e06fd49ea7b:

#include "mbed.h"
 // LR 10/02/18
 //
 // bus CAN generateur de trames
 //
 // Emission d'une trame parmi 4 selection à l'aide du pot1
 // eviter les positions intermédaires du pot1 !
 //  !! emission sur CAN2 p30Rx p29Tx
 // Attention si pas d'ACK faire reset après chaque changement
 // J'ai constaté des comportements bizarre dans cette situation
 // Des infos sur la liaison série
 // La trame est envoyée aussi sur LCD
 // 
 // testé à l'analyseur logique = OK !!
 //
 //
 
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
CAN can1(p9, p10,1000000);// on definit pin et debit
CAN can2(p30, p29, 1000000);
Serial pc(USBTX, USBRX);
//AnalogIn   pot_1(p19); // potard pour selection



void send(int id, char octet_emis[], char RouD, char longueur )
{
    int emis_ok = 0 ;
    
    if (RouD == 'D')
    { emis_ok = can2.write(CANMessage(id, octet_emis, longueur, CANData, CANStandard )) ;
      pc.printf("id: %x, %c L = %d,  \nData : %x:%x:%x:%x ...   \n", id,RouD,longueur,octet_emis[0],octet_emis[1], octet_emis[2], octet_emis[3] );
    }// c'ets la valeur retournée par la fonction write
   else
    { emis_ok = can2.write(CANMessage(id, octet_emis, longueur,  CANRemote, CANStandard ));
        pc.printf("id: %x, %c L = %d,  \nData : %x:%x:%x:%x ...   \n", id,RouD,longueur,octet_emis[0],octet_emis[1], octet_emis[2], octet_emis[3] );
         }
    //lcd.locate(0,10);
    //lcd.printf("id: %x, %c L = %d,  \nData : %x:%x:%x:%x ...   \n", id,RouD,longueur,octet_emis[0],octet_emis[1], octet_emis[2], octet_emis[3] );
    if(emis_ok) {
        // ici octet emis n'a pas de sens car trame remote !
     pc.printf("j'emets bien !!! \n");
    } 
    led1 = !led1;
}



 
int main() {
 //   int choix = 0 ;
    int i = 0 ;
    
char octet_send[9]  = "DDDDDDDD"; // valeurs par defaut D=44
int identif = 0x123 ; // par défaut
char RemoteData = 'D' ;  
char length = 2 ;
char c;
uint32_t instruction = 0;
uint8_t state_val = 0;
uint8_t state = 0;
int data_rc;
int data_report[100];
bool dejafait = 0;
CANMessage msg;

    pc.printf("CAN CONSOLE \r \n");
   
   
   
    while(1) {
        data_rc = 0;
   pc.printf(" Identifier : ");
        while(state == 0){
   c = pc.getc();
   switch(c){
           
           
                
           case '0':
           pc.printf("0");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0000;
                }
           
           if(state_val == 0){
               instruction = 0b0000;
               state_val++;
               }
            
           break;
           
           case '1':
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0001;
                }
                
           pc.printf("1");
           if(state_val == 0){
               instruction = 0b0001;
               state_val++;
               }
            
           break;
           
           case '2':
           
            if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0010;
                }
                
           pc.printf("2");
           if(state_val == 0){
               instruction = 0b0010;
               state_val++;
               }
           
           break;
           
           case '3':
           pc.printf("3");
          
          if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0011;
                }
          
           if(state_val == 0){
               instruction = 0b0011;
               state_val++;
               }
            
           break;
           
           case '4':
           
            if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0100;
                }           
           
           pc.printf("4");
           if(state_val == 0){
               instruction = 0b0100;
               state_val++;
               }

           break;
           
           case '5':
           pc.printf("5");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0101;
                }
           
           if(state_val == 0){
               instruction = 0b0101;
               state_val++;
               }
            
           break;
           
           case '6':
           pc.printf("6");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0110;
                }
           
           if(state_val == 0){
               instruction = 0b0110;
               state_val++;
               }
            
           break;
           
           case '7':
           pc.printf("7");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0111;
                }
           
           if(state_val == 0){
               instruction = 0b0111;
               state_val++;
               }
            
           break;
           
           case '8':
           pc.printf("8");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1000;
                }
           
           
           if(state_val == 0){
               instruction = 0b1000;
               state_val++;
               }
            
           break;
           
           case '9':
           pc.printf("9");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1001;
                }
           
           if(state_val == 0){
               instruction = 0b1001;
               state_val++;
               }
            
           break;
           
           case 'a':
           pc.printf("a");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1010;
                }
           
           if(state_val == 0){
               instruction = 0b1010;
               state_val++;
               }
            
           break;
           
           case 'b':
           pc.printf("b");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1011;
                }
           
           if(state_val == 0){
               instruction = 0b1011;
               state_val++;
               }
            
           break;
           
           case 'c':
           pc.printf("c");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1100;
                }
           
           if(state_val == 0){
               instruction = 0b1100;
               state_val++;
               }
            
           break;
           
           case 'd':
           pc.printf("d");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1101;
                }
           
           if(state_val == 0){
               instruction = 0b1101;
               state_val++;
               }
            
           break;
           
           case 'e':
           pc.printf("e");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1110;
                }
           
           if(state_val == 0){
               instruction = 0b1110;
               state_val++;
               }
            
           break;
           
           case 'f':
           pc.printf("f");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1111;
                }
           
           if(state_val == 0){
               instruction = 0b1111;
               state_val++;
               }
            
           break;
           
           case 'A':
           pc.printf("a");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1010;
                }
           
           if(state_val == 0){
               instruction = 0b1010;
               state_val++;
               }
            
           break;
           
           case 'B':
           pc.printf("b");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1011;
                }
           
           if(state_val == 0){
               instruction = 0b1011;
               state_val++;
               }
            
           break;
           
           case 'C':
           pc.printf("c");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1100;
                }
           
           if(state_val == 0){
               instruction = 0b1100;
               state_val++;
               }
            
           break;
           
           case 'D':
           pc.printf("d");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1101;
                }
           
           if(state_val == 0){
               instruction = 0b1101;
               state_val++;
               }
            
           break;
           
           case 'E':
           pc.printf("e");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1110;
                }
           
           if(state_val == 0){
               instruction = 0b1110;
               state_val++;
               }
            
           break;
           
           case 'F':
           pc.printf("f");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1111;
                }
           
           if(state_val == 0){
               instruction = 0b1111;
               state_val++;
               }
            
           break;
           
           case '\r':
           pc.printf("\r\nOk\r\n");
           identif = instruction;
           state++;
            
           break;
           
           }
           }
           
   pc.printf("\r\n Number of octets : ");
   
   c = pc.getc();
   instruction = 0;
   state_val = 0;
   
    switch(c){
         case '0':
           pc.printf("0");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0000;
                }
           
           if(state_val == 0){
               instruction = 0b0000;
               state_val++;
               }
            
           break;
           
           case '1':
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0001;
                }
                
           pc.printf("1");
           if(state_val == 0){
               instruction = 0b0001;
               state_val++;
               }
            
           break;
           
           case '2':
           
            if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0010;
                }
                
           pc.printf("2");
           if(state_val == 0){
               instruction = 0b0010;
               state_val++;
               }
           
           break;
           
           case '3':
           pc.printf("3");
          
          if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0011;
                }
          
           if(state_val == 0){
               instruction = 0b0011;
               state_val++;
               }
            
           break;
           
           case '4':
           
            if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0100;
                }           
           
           pc.printf("4");
           if(state_val == 0){
               instruction = 0b0100;
               state_val++;
               }

           break;
           
           case '5':
           pc.printf("5");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0101;
                }
           
           if(state_val == 0){
               instruction = 0b0101;
               state_val++;
               }
            
           break;
           
           case '6':
           pc.printf("6");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0110;
                }
           
           if(state_val == 0){
               instruction = 0b0110;
               state_val++;
               }
            
           break;
           
           case '7':
           pc.printf("7");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0111;
                }
           
           if(state_val == 0){
               instruction = 0b0111;
               state_val++;
               }
            
           break;
           
           case '8':
           pc.printf("8");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1000;
                }
           
           
           if(state_val == 0){
               instruction = 0b1000;
               state_val++;
               }
            
           break;
           
           case '9':
           pc.printf("9");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1001;
                }
           
           if(state_val == 0){
               instruction = 0b1001;
               state_val++;
               }
            
           break;
           
           case 'a':
           pc.printf("a");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1010;
                }
           
           if(state_val == 0){
               instruction = 0b1010;
               state_val++;
               }
            
           break;
           
           case 'b':
           pc.printf("b");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1011;
                }
           
           if(state_val == 0){
               instruction = 0b1011;
               state_val++;
               }
            
           break;
           
           case 'c':
           pc.printf("c");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1100;
                }
           
           if(state_val == 0){
               instruction = 0b1100;
               state_val++;
               }
            
           break;
           
           case 'd':
           pc.printf("d");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1101;
                }
           
           if(state_val == 0){
               instruction = 0b1101;
               state_val++;
               }
            
           break;
           
           case 'e':
           pc.printf("e");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1110;
                }
           
           if(state_val == 0){
               instruction = 0b1110;
               state_val++;
               }
            
           break;
           
           case 'f':
           pc.printf("f");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1111;
                }
           
           if(state_val == 0){
               instruction = 0b1111;
               state_val++;
               }
            
           break;
           
           case 'A':
           pc.printf("a");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1010;
                }
           
           if(state_val == 0){
               instruction = 0b1010;
               state_val++;
               }
            
           break;
           
           case 'B':
           pc.printf("b");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1011;
                }
           
           if(state_val == 0){
               instruction = 0b1011;
               state_val++;
               }
            
           break;
           
           case 'C':
           pc.printf("c");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1100;
                }
           
           if(state_val == 0){
               instruction = 0b1100;
               state_val++;
               }
            
           break;
           
           case 'D':
           pc.printf("d");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1101;
                }
           
           if(state_val == 0){
               instruction = 0b1101;
               state_val++;
               }
            
           break;
           
           case 'E':
           pc.printf("e");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1110;
                }
           
           if(state_val == 0){
               instruction = 0b1110;
               state_val++;
               }
            
           break;
           
           case 'F':
           pc.printf("f");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1111;
                }
           
           if(state_val == 0){
               instruction = 0b1111;
               state_val++;
               }
            
           break;
           
           }
    
    length = instruction;
    pc.printf("\r\n");
    if(length > 0){
    pc.printf("Send octets in hexa\r\n");
    i = 0;
    pc.printf("\r\n Octet[%d] : ", i);
    while(i <= length - 1){
    
        
        c = pc.getc();
        switch(c){
         case '0':
           pc.printf("0");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0000;
                }
           
           if(state_val == 0){
               instruction = 0b0000;
               state_val++;
               }
            
           break;
           
           case '1':
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0001;
                }
                
           pc.printf("1");
           if(state_val == 0){
               instruction = 0b0001;
               state_val++;
               }
            
           break;
           
           case '2':
           
            if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0010;
                }
                
           pc.printf("2");
           if(state_val == 0){
               instruction = 0b0010;
               state_val++;
               }
           
           break;
           
           case '3':
           pc.printf("3");
          
          if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0011;
                }
          
           if(state_val == 0){
               instruction = 0b0011;
               state_val++;
               }
            
           break;
           
           case '4':
           
            if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0100;
                }           
           
           pc.printf("4");
           if(state_val == 0){
               instruction = 0b0100;
               state_val++;
               }

           break;
           
           case '5':
           pc.printf("5");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0101;
                }
           
           if(state_val == 0){
               instruction = 0b0101;
               state_val++;
               }
            
           break;
           
           case '6':
           pc.printf("6");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0110;
                }
           
           if(state_val == 0){
               instruction = 0b0110;
               state_val++;
               }
            
           break;
           
           case '7':
           pc.printf("7");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b0111;
                }
           
           if(state_val == 0){
               instruction = 0b0111;
               state_val++;
               }
            
           break;
           
           case '8':
           pc.printf("8");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1000;
                }
           
           
           if(state_val == 0){
               instruction = 0b1000;
               state_val++;
               }
            
           break;
           
           case '9':
           pc.printf("9");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1001;
                }
           
           if(state_val == 0){
               instruction = 0b1001;
               state_val++;
               }
            
           break;
           
           case 'a':
           pc.printf("a");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1010;
                }
           
           if(state_val == 0){
               instruction = 0b1010;
               state_val++;
               }
            
           break;
           
           case 'b':
           pc.printf("b");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1011;
                }
           
           if(state_val == 0){
               instruction = 0b1011;
               state_val++;
               }
            
           break;
           
           case 'c':
           pc.printf("c");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1100;
                }
           
           if(state_val == 0){
               instruction = 0b1100;
               state_val++;
               }
            
           break;
           
           case 'd':
           pc.printf("d");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1101;
                }
           
           if(state_val == 0){
               instruction = 0b1101;
               state_val++;
               }
            
           break;
           
           case 'e':
           pc.printf("e");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1110;
                }
           
           if(state_val == 0){
               instruction = 0b1110;
               state_val++;
               }
            
           break;
           
           case 'f':
           pc.printf("f");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1111;
                }
           
           if(state_val == 0){
               instruction = 0b1111;
               state_val++;
               }
            
           break;
           
           case 'A':
           pc.printf("a");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1010;
                }
           
           if(state_val == 0){
               instruction = 0b1010;
               state_val++;
               }
            
           break;
           
           case 'B':
           pc.printf("b");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1011;
                }
           
           if(state_val == 0){
               instruction = 0b1011;
               state_val++;
               }
            
           break;
           
           case 'C':
           pc.printf("c");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1100;
                }
           
           if(state_val == 0){
               instruction = 0b1100;
               state_val++;
               }
            
           break;
           
           case 'D':
           pc.printf("d");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1101;
                }
           
           if(state_val == 0){
               instruction = 0b1101;
               state_val++;
               }
            
           break;
           
           case 'E':
           pc.printf("e");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1110;
                }
           
           if(state_val == 0){
               instruction = 0b1110;
               state_val++;
               }
            
           break;
           
           case 'F':
           pc.printf("f");
           
           if(state_val > 0){
                instruction = instruction << 4;
                instruction |= 0b1111;
                }
           
           if(state_val == 0){
               instruction = 0b1111;
               state_val++;
               }
            
           break;
           
           case '\r':
           octet_send[i] = instruction;
           instruction = 0;
           state_val = 0;       
           i++;    
           if(i <= length - 1){
           pc.printf("\r\n Octet[%d] : ", i);}
           break;
           
           }
           
              
        }//end of while
        }
        pc.printf("Set RTR (1 or 0): ");
        c = pc.getc();
        if(c == '0'){
            RemoteData = 'D';
            }
            if(c == '1'){
            RemoteData = 'R';
            }
            pc.printf("%c \r\n",c);
            wait(2);
    
    
    for(i = 0; i < 25; i++){
        if(dejafait == 0){
        send(identif, octet_send, RemoteData, length );
    data_rc = 0;
    dejafait = 1;
    }
    
        pc.printf("Awaiting Data... \r\n");
        if(can2.read(msg)) {
            pc.printf("Message received\r\n");
            pc.printf("Number of octet: %d \r\n", msg.len);
            
        } 
        }//end of for
        
        
        for(i = 0; i < msg.len; i++){
        pc.printf("Data captured %d : 0x%x \r\n", i, msg.data[i]);
        
        }
        data_rc = 0;
        dejafait = 0;
        i = 0;
        state = 0;
        pc.printf("\r\n");
        //end of for 
            
    }// fin while(1)
         
} // fin main
         
        /*
        
        printf("loop()\n");
        if(can2.read(msg)) {
            printf("Message received: %d\n", msg.data[0]);
            led2 = !led2;
        } 
        wait(0.2);
    }
} */

//choix= 5*pot_1.read();
   // lcd.locate(0,0);
   // lcd. printf("Trame %d \n", choix);
   /* switch (choix)
    { // debut sw
    case (0): // Trame qui passe le noeud 15 en Operationnal state
    {   identif =  0x00 ;
        octet_send[0] = 0x01 ;
        octet_send[1] = 0x15 ;
        RemoteData = 'D';
        length = 2 ;
        break ;
    }     // fin case 0
     case (1): // Trame qui passe le noeud 20 en Operationnal state
    {   identif =  0x00 ;
         octet_send[0] = 0x01 ;
        octet_send[1] = 0x20 ;
        RemoteData = 'D';
       length = 2 ;
        break ;
    }     // fin case 1
     case (2): // trame qui ping le noeud 15
    {   identif =  0x715 ;
        for(i=0;i<9;i++)
        { octet_send[i]='D';} // 44 en hexa par defaut
        RemoteData = 'R';
        length = 0 ;
        break ;
    }     // fin case 2
    case (3): // trame qui ping le noeud 20
    {   identif =  0x720 ;
         for(i=0;i<9;i++)
        { octet_send[i]='D';} // 44 en hexa par defaut
        RemoteData = 'R';
        length = 0 ;
        break ;
    }     // fin case 3
    
   default : // 4 ou 5
    {   identif =  0x123 ;
         for(i=0;i<9;i++)
        { octet_send[i]='D';} // 44 en hexa par defaut
        RemoteData = 'D';
        length = 8 ;
        break ;
    }     // fin default
    
    } // fin switch
    */