voor willem test

Dependencies:   4DGL MODSERIAL mbed mbos

CDU2FS_message_2.cpp

Committer:
LvdK
Date:
2013-01-29
Revision:
6:e0bf3b244d7c

File content as of revision 6:e0bf3b244d7c:

// L. van der Kolk, ELVEDEKA, Holland //
// File:  CDU2FS_message_2.cpp 

#include "mbed.h"
#include "MODSERIAL.h"

extern MODSERIAL USB;
extern int CDU_FS_interface; 

int key_hit_ID = 0;         // : number of key that was hit, 0 = no hit of any key. ( global flag ! )

char key_message[20]   =  "$PCDUKEY,";  // : setup begin of KEY message to FS
char val_message[50]   =  "$PCDUVAL,";  // : setup begin of VAL message to FS
char alive_message[20] =  "$PCDUOKE,";  // : setup begin of ALIVE message to FS

// ---- Key strings for FS-to-CDU key message SPCDUKEY ------------------------------------------------
#define max_keys_CDUpanel 70 // : absolute max. number of keys found on CDU panel
const char *key_value[max_keys_CDUpanel] =  // : array of pointers to keystrings terminated by '*'
           { "???*",     // key 0 not valid
             "LSK1*",    // key 1
             "LSK2*",    // key 2
             "LSK3*",    // key 3
             "LSK4*",    // key 4         
             "LSK5*",    // key 5
             "LSK6*",    // key 6
             "INITRF*",  // key 7
             "RTE*",     // key 8             
             "CLB*",     // key 9                                   
             "CRZ*",     // key 10
             "DES*",     // key 11           
             "MENU*",    // key 12
             "LEGS*",    // key 13            
             "DEPARR*",  // key 14             
             "HOLD*",    // key 15
             "PROG*",    // key 16 
             "EXEC*",    // key 17
             "N1LMT*",   // key 18 
             "FIX*",     // key 19
             "PRPAGE*",  // key 20            
             "NXPAGE*",  // key 21             
             "1*",       // key 22
             "2*",       // key 23             
             "4*",       // key 24
             "5*",       // key 25             
             "6*",       // key 26             
             "7*",       // key 27             
             "8*",       // key 28             
             "9*",       // key 29             
             "0*",       // key 30
             ".*",       // key 31
             "+/-*",     // key 32
             "A*",       // key 33
             "B*",       // key 34             
             "C*",       // key 35             
             "D*",       // key 36
             "E*",       // key 37                          
             "F*",       // key 38
             "G*",       // key 39
             "H*",       // key 40
             "I*",       // key 41                                                                                           
             "J*",       // key 42
             "K*",       // key 43             
             "L*",       // key 44             
             "M*",       // key 45             
             "N*",       // key 46             
             "O*",       // key 47  
             "P*",       // key 48               
             "Q*",       // key 49               
             "R*",       // key 50               
             "S*",       // key 51  
             "T*",       // key 52               
             "U*",       // key 53               
             "V*",       // key 54               
             "W*",       // key 55               
             "X*",       // key 56               
             "Y*",       // key 57  
             "Z*",       // key 58 
             "SPC*",     // key 59
             "DEL*",     // key 60
             "CLR*",     // key 61
             "/*",       // key 62
             "RSK1*",    // key 63
             "RSK2*",    // key 64
             "RSK3*",    // key 65
             "RSK4*",    // key 67                                      
             "RSK5*",    // key 68
             "RSK6*"     // key 69             
            }; 
// ------------------------------------------------------------------------------------------------

void send_message_to_FS(char *message_string) {
    // Common fnction to send a created message string (VAL, KEY or OKE) to the FS.
    // Parameter is pointer to char string that has to be sent.
    // Interface can be USB port or Ethernet port.
    if ( CDU_FS_interface == 0 ) {  // : messages will be sent by USB port
        // Test if TX buffer is not full:
        // Note: Only writing in the Tx buffer if there is enough space,
        // prevents Tx buffer overflow, but caharcters can be lost when Tx buffersize is defined too small !
        // In this case the Blue-Leds-Of-Death due to a TX overflow interrupt will never occur,
        // and Mbed will keep running !
        USB.puts(message_string); // : write total string in USB TX buffer <<<<<<<<<<<<<<<<<  ????
    }
    //if ( CDU_FS_interface == 1 ) Ethernet.puts(message_string); // : messages will be sent by Ehternet
}

void Send_VAL_message(int var, char *char_pntr) {
     // Function creates a valid VAL message out of var parameter and out of value string
     // which is addressed by parameter char_pntr.
     // After adding a checksum, the total VAL message string will be sent.
     
        //USB.printf("pseudo VAL message:  %d , %s\n",var,char_pntr); // debug only !
        int i;
        char byte_read;
        char exor_byte = 0;
        //  Create VAL message, starting with "$PCDUVAL," message header
        i = 9; // : i points to first place after "$PCDUVAL," header
        // : add 2 digit var number and comma to message:
        sprintf(&val_message[i],"%02d,",var);
        // Add value of var and extra '*' char to VAL message:
        i = 12;
        sprintf(&val_message[i],"%s*",char_pntr);
        // Calculate checksum now : 
        i = 1; // : i points to first place after '$' in message
        do { byte_read = val_message[i];
             if (byte_read == '*') break; // : exclude '*' from calculation
             exor_byte = exor_byte ^ byte_read;
             i++;
        } while ( i < 50 );
        i++;  // : i now points to first digit of checksum after '*'
        // Add exor_byte in 2 hex digits and a CR + LF:
        sprintf(&val_message[i],"%02x\r\n",exor_byte); // : extra NULL char added by sprintf
        send_message_to_FS(val_message); // : send message to defined CDU-FS interface        
}

void Send_ALIVE_message(int seconds){
    int i;
    char byte_read;
    char exor_byte = 0;
     //Create alive message:
        i = 9; // : i points to first place after "$PCDUOKE,"
        // Add seconds in 2 dec digits and a '*' char :
        sprintf(&alive_message[i],"%02d*",seconds); 
        // Calculate checksum now : 
        i = 1; // : i points to first place after '$'
        do { byte_read = alive_message[i];
             if (byte_read == '*') break; // : exclude '*' from exor calculation
             exor_byte = exor_byte ^ byte_read;
             i++;
        } while ( i < 20 );
        i++;  // : i now points to first digit of checksum after '*'
        // Add exor_byte in 2 hex chars (with upper case A-F) and a CR + LF:
        sprintf(&alive_message[i],"%02X\r\n",exor_byte); // : + extra NULL char added by sprintf 
        send_message_to_FS(alive_message); // : send message to defined CDU-FS interface
        
}

void Send_KEY_message(int key_nr) {
        // Function creates a valid KEY message out of key_nr parameter.
        // Based on key_nr, a key char string is looked up and added to the message.
        // After adding a checksum, the total KEY message will be sent.
        int i;
        char byte_read;
        char exor_byte = 0;
        //  Create key message, starting with "$PCDUKEY," message header
        i = 9; // : i points to first position after "$PCDUKEY," message header 
        // Add key string to message string including '*'
        if ( key_nr != 0 && key_nr < max_keys_CDUpanel ) {
             strcpy(&key_message[i],key_value[key_nr]); 
        }
        // Calculate checksum now : 
        i = 1; // : i points to first place after '$' in message
        do { byte_read = key_message[i];
             if (byte_read == '*') break; // : exclude '*' from calculation
             exor_byte = exor_byte ^ byte_read;
             i++;
        } while ( i < 20 );
        i++;  // : i now points to first digit of checksum after '*'
        // Add exor_byte in 2 hex digits and a CR + LF:
        sprintf(&key_message[i],"%02x\r\n",exor_byte); // : extra NULL char added by sprintf
        send_message_to_FS(key_message); // : send message to defined CDU-FS interface   
}