Engravity-CDU / CDU_Mbed_35

Dependencies:   4DGL-UC MODSERIAL mbed mbos

Fork of CDU_Mbed_30 by Engravity-CDU

decode_1.cpp

Committer:
LvdK
Date:
2012-12-08
Revision:
5:99594f4ab659
Parent:
4:c23c570e4454

File content as of revision 5:99594f4ab659:

// L. van der Kolk, ELVEDEKA, Holland //
// File:  decode_1.cpp   version  1.0

#include "mbed.h"
#include "ctype.h"

#define TRUE  1
#define FALSE 0

//DigitalOut led4(LED4);  // TEST Led4

extern char string_received[];   // : complete string starting with $ and ending with CR/LF
extern int nr_of_received_char;  // : number of chars in string_receved[];

extern Serial USB;  // >>>>>>>>>>> alleen t.b.v TEST output !!

void read_datafields(int command_number);

int command_valid;
int checksum_error = FALSE;
char command_string[20];  // <<<<<<<<<< ?
int command_number = 0;
int select_key_nr = 0;

int comma[8];      // : array with pointers to all found commas in received string

// Command headers are defined in an array command[] of command structures :
struct command_struct {
    char *name_string;   // : name of command
    int data_fields;     // : number of following data fields after commandheader seperated by commas
                        } command[20] = {    // laat aantal weg ??? <<<<<<<<<<<<<<<<<<<<  ?
    "123",0,    // : no serious command nr. 0
    "DSB",4,    // : command nr. 1
    "TIT",4,    // : command nr. 2
    "PGE",4,    // : command nr. 3
    "SPD",4,    // : command nr. 4
    "CLS",0,    // : command nr. 5
    "SID",2,    // : command nr. 6
    "LxT",4,    // : command nr. 7
    "LxS",4,    // : command nr. 8
    "RxT",4,    // : command nr. 9
    "RxS",4,    // : command nr. 10
    "PUTS",6,   // : command nr. 11
    "SETV",2,   // : command nr. 12
    "GETV",1    // : command nr. 13
};



void decode_string()
{   // -- This decodes a received string in array string_received[] --

    int  char_cntr,i,c;
    int  equal;
    char key_id, byte_read;
    char checksum[3], exor_byte;
          
        //led4 = 1; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< test
        command_valid = FALSE;
        key_id = '0';
        select_key_nr = 0;
              
        // Get checksum and number of comma seperated datafields : -----------------------------
        exor_byte = 0;
        i = 1;  // : i points to first char after $
        c = 1;  
        //USB.printf("eerste kar is : %c\n",string_received[i] );  // >>>>>>>>>>>>>>>>>>>>> ( TEST !)
        do {
            byte_read = string_received[i];
            if (byte_read == ',' && c < 8) {
                  comma[c] = i;
                  c++;
            }
            if (byte_read == '*') break;
            exor_byte = exor_byte ^ byte_read;
            i++;
        } while ( i < nr_of_received_char );
        i++;    // : i points now to checksum after * char
        //USB.printf("pointer i staat op : %c\n",string_received[i] );  // >>>>>>>>>>>>>>>>>>>>> ( TEST !
        strncpy(checksum,&string_received[i],2);    // : copy 2 char checksum after * 
        checksum[2] = '\0'; // : mark end of text 
        
    if (1==1) {  
         equal = (atoi(checksum));  
         USB.printf("exor_byte, ASCII checksum, atoi checksum dec : %02x, %s, %d\n",exor_byte, checksum, equal );  // >>>>>>>>>>>>>>>>>>>>> ( TEST !)
         //USB.printf("exor_byte, checksum, aantal kar is : %02x, %s, %d\n",exor_byte, checksum, nr_of_received_char );  // >>>>>>>>>>>>>>>>>>>>> ( TEST !)
         USB.printf("aantal comma's is :  %d\n",c-1 );  // >>>>>>>>>>>>>>>>>>>>> ( TEST !)

        // Check on 5 char "$PCDU" header:
        equal = strncmp(string_received,"$PCDU",5);
        if (equal != 0) {
        }
        else {

            // Find first comma and find chars before comma:
            char_cntr = 5; // : exclude first 5 chars because they have already been found
            do {
                if( string_received[char_cntr] == ',') break;
                char_cntr++;
            } while ( char_cntr < 10);

            if (char_cntr == 9) { // : command found with 4 char.
                strncpy(command_string,&string_received[5],4);
                USB.printf("commando is : %4s\n",command_string );  // show command with 4 chars ( TEST !)


                i = 10; // : start on first command with 4 characters
                do {
                    equal = strncmp(&string_received[5],command[i].name_string,4);
                    if( equal == 0) break;
                    i++;
                } while ( i < 15);

                if (equal == 0) {
                     USB.printf("gevonden commando4nummer is : %d\n",i );  // show command4 number  (TEST ! )
                     command_valid = TRUE;
                }
            }
            else if (char_cntr == 8) { // : command found with 3 char.
                strncpy(command_string,&string_received[5],3);
                USB.printf("commando is : %3s\n",command_string );  // show command with 3 chars ( TEST !)

                i = 1; // : start on first command with 3 characters
                do {
                    equal = strncmp(&string_received[5],command[i].name_string,3);
                    if( equal == 0) break;
                    i++;
                } while ( i < 7); // stop on commmand 6

                if (equal == 0) {
                    USB.printf("gevonden commando3nummer is : %d\n",i );  // show command3 number  (TEST ! )
                    command_valid = TRUE;
                }
                else {       
                    // Test on special key commands LxT, LxS, RxT, RxS :
                    if (string_received[5] == 'L') {    // : command is LxT or LxS
                        if (string_received[7] == 'T') {
                            i = 7; // : command 7 = LxT found
                            key_id = string_received[6];
                        }
                        else if (string_received[7] == 'S') { 
                            i = 8; // : command 8 = LxS found
                            key_id = string_received[6];
                        }                        
                    } 
                    else if (string_received[5] == 'R') {  // : command is RxT or RxS
                        if (string_received[7] == 'T') {
                            i = 9; // : command 9 = RxT found
                            key_id = string_received[6];
                        }
                        else if (string_received[7] == 'S') { 
                            i = 10; // : command 10 = RxS found
                            key_id = string_received[6];
                        }                     
                    }
                    if (isdigit(key_id)) {  // : test if DIGIT
                       USB.printf("gevonden key commando is : %d, key %c \n",i, key_id );  // show command3 number and key (TEST ! )
                       select_key_nr = atoi(&key_id); // : convert to integer
                       command_valid = TRUE;
                    }
                     
                } // end of special test <<<<<<<<<<<<

             }// end of char 3 test <<<<<<<<<<

        }// end of header test <<<<<<<<<<
 
        if (command_valid == TRUE) {
            command_number = i;
             USB.printf("found command %d with possible key %d \n",command_number, select_key_nr);
             
             // Command is known now,
             // so now read all data fields:
             // ---------------------------------------------------
             // call function to get the rest of the data fields :
             // ---------------------------------------------------
             read_datafields(command_number);
        } 
        
    } // end of checksum test
            
  
 //led4 = 0;            
              
}