New work version with additional functions

Dependencies:   4DGL-UC ConfigFile MODSERIAL mbed mbos

Fork of CDU_Mbed_35 by Engravity-CDU

Files at this revision

API Documentation at this revision

Comitter:
LvdK
Date:
Tue Jan 29 14:54:14 2013 +0000
Parent:
5:99594f4ab659
Child:
7:6576a287e563
Commit message:
Goed werkende mbos versie met 0.5 sec ALIVE message

Changed in this revision

CDU2FS_message_2.cpp Show annotated file Show diff for this revision Revisions of this file
FS2CDU_data_2.cpp Show annotated file Show diff for this revision Revisions of this file
MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
USB_receive.cpp Show diff for this revision Revisions of this file
USB_receive_2.cpp Show annotated file Show diff for this revision Revisions of this file
USB_setup.cpp Show diff for this revision Revisions of this file
alive_timer.cpp Show diff for this revision Revisions of this file
buttons.cpp Show diff for this revision Revisions of this file
buttontest.cpp Show annotated file Show diff for this revision Revisions of this file
data_structures.cpp Show diff for this revision Revisions of this file
decode_1.cpp Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mbos.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CDU2FS_message_2.cpp	Tue Jan 29 14:54:14 2013 +0000
@@ -0,0 +1,188 @@
+// 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   
+}           
+        
+
+
+
+    
+    
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FS2CDU_data_2.cpp	Tue Jan 29 14:54:14 2013 +0000
@@ -0,0 +1,669 @@
+// L. van der Kolk, ELVEDEKA, Holland //
+// File:  FS2CDU_data_2.cpp  
+
+#include "mbed.h"
+#include "MODSERIAL.h"  
+
+// ---- FS-to-CDU data structures filled with received data ---------------------------------------
+#define max_text0    48   //  max text length font 0      
+#define max_text1    24   //  max text length font 1  
+#define max_lines    14   //  max nr of screen lines 
+#define max_font      1   //  possible fonts are 0 to max_font, so now 0 and 1 possible
+#define max_col_nr  255   //  highest possible R,G, or B colour number
+#define fstyle_1     'S'  //  possible font style character 1
+#define fstyle_2     'N'  //  possible font style character 2
+#define max_keys    100   //  max total nr of select keys 0 - 99
+#define max_leftkeys  6   //  max nr of used LEFT select keys ( 0 - 49 )
+#define max_rightkeys 6   //  max nr of used RIGHT select keys ( 50 - 99 )
+
+// Background colour of display:
+struct { int   RED;                 //  : RED value 
+         int   GREEN;               //  : GREEN value 
+         int   BLUE;                //  : BLUE value     
+         int   updated;             //  : update flag, true when background colour was updated
+         } BACKGROUND_COL;
+
+// Maintext lines at selectkeys:
+struct { char  text[max_text0+2];    //  : maintext string, ending with '\0'   
+         int   font_size;            //  : fontsize             
+         char  font_style;           //  : style character                          
+         int   text_RED;             //  : RED value of textline
+         int   text_GREEN;           //  : GREEN value of textline
+         int   text_BLUE;            //  : BLUE value of textline
+         int   updated;              //  : update flag, true when a selectkey maintext was updated
+         } SELKEY_MAINTEXT[max_keys];//  : array of selectkey Maintext structures
+        
+// Subtext lines at selectkeys:
+struct { char  text[max_text0+2];    //  : subtext string, ending with '\0'   
+         int   font_size;            //  : fontsize                                
+         char  font_style;           //  : style character                          
+         int   text_RED;             //  : RED value of textline
+         int   text_GREEN;           //  : GREEN value of textline
+         int   text_BLUE;            //  : BLUE value of textline
+         int   updated;              //  : update flag, true when a selectkey subtext was updated
+         } SELKEY_SUBTEXT[max_keys]; //  : array of selectkey Subtext structures 
+
+// Screen textlines:
+struct { char  text[max_text0+2];    //  : text line string, ending with '\0'   
+         int   font_size;            //  : fontsize of textline           
+         char  font_style;           //  : style character                             
+         int   text_RED;             //  : RED value of textline
+         int   text_GREEN;           //  : GREEN value of textline
+         int   text_BLUE;            //  : BLUE value of textline
+         int   updated;              //  : update flag, true when a textline was updated
+         } TEXTLINE[max_lines+1];    //  : array of textline structures
+            
+// CDU status:            
+struct { int mess_light;            //  : 0 = light OFF, 1 = light ON
+         int exec_indicator;        //  : 0 = indicator OFF, 1 = indicator ON
+         int backlight;             //  : 0 = light OFF, 1 = light ON 
+         int stby_mode;             //  : 0 = operational mode, 1 = standby mode
+         int updated;               //  : update flag, true when one or more status items were updated
+         } CDU_STATUS;           
+            
+// Clear screen flag:
+int DO_CLR_SCREEN = false;          //  : Clear screen flag, true = perform a clear-screen
+
+// CDU settings:
+struct { int Baud;              //  : Baudrate can be 2400, 4800, 9600, 19200, 38400
+         char Firmware_nr[15];  //  : firmware ID string, terminated with '\0' !
+         char CDU_type[15];     //  : CDU type ID string, terminated with '\0' !
+         int  Serial_nr;        //  : integer serial number
+         char IP_address[20];   //  : IP address, 0.0.0.0 to 255.255.255.255 , terminated with '\0' !
+         int  Port_nr;          //  : integer Port number
+         char Server_ip[20];    //  : IP address of server , terminated with '\0' !     
+         int  updated;          //  : update flag, true when one or more items were updated
+         } CDU_SET;
+
+// Common flag to signal that one or more updates were performed:
+int FSdata_received_flag = false;  //  : true when one or more FS-to-CDU data was updated                                 
+// --------------------------------------------------------------------------------------------------
+
+extern MODSERIAL USB;      // >>>>>>>>>>> alleen t.b.v TEST output !!
+extern char string_received[];  
+extern int comma[];
+
+void Send_VAL_message(int var, char *char_pntr); 
+
+void set_initial_CDU_screen()
+{  // Fill all FS-to-CDU datastructures with data for initial power-up conditions:
+   int key_nr, line_nr;
+ 
+    FSdata_received_flag = true;    // <<<<<<<<<<<<<< !
+    
+  // Init background colour of screen :
+   BACKGROUND_COL.RED = 0;
+   BACKGROUND_COL.GREEN = 0;
+   BACKGROUND_COL.BLUE = 0;
+   BACKGROUND_COL.updated = true;
+   
+  // Init Maintext at left selectkeys starting from 0 :
+  for ( key_nr = 0; key_nr < max_leftkeys; key_nr++ )
+  {  SELKEY_MAINTEXT[key_nr].text_RED =   255;
+     SELKEY_MAINTEXT[key_nr].text_GREEN = 255;
+     SELKEY_MAINTEXT[key_nr].text_BLUE =  255;
+     SELKEY_MAINTEXT[key_nr].font_size =    0;
+     SELKEY_MAINTEXT[key_nr].font_style = fstyle_1;
+     strncpy(SELKEY_MAINTEXT[key_nr].text, "0|....key maintext..............................|\0",
+     max_text0 + 2 ); // (max_text0+2) chars copied
+     SELKEY_MAINTEXT[key_nr].updated = true;   
+   }
+  // Init Maintext at right selectkeys starting from 50 :
+  for ( key_nr = 50; key_nr < (50+max_rightkeys); key_nr++ )
+  {  SELKEY_MAINTEXT[key_nr].text_RED =   255;
+     SELKEY_MAINTEXT[key_nr].text_GREEN = 255;
+     SELKEY_MAINTEXT[key_nr].text_BLUE =  255;
+     SELKEY_MAINTEXT[key_nr].font_size =    0;
+     SELKEY_MAINTEXT[key_nr].font_style = fstyle_1; 
+     strncpy(SELKEY_MAINTEXT[key_nr].text, "0|....key maintext..............................|\0",
+     max_text0 + 2 ); // (max_text0+2) chars copied
+     SELKEY_MAINTEXT[key_nr].updated = true;   
+   }
+  // Init Subtext at left selectkeys starting from 0 :
+  for ( key_nr = 0; key_nr < max_leftkeys; key_nr++ )
+  {  SELKEY_SUBTEXT[key_nr].text_RED =   255;
+     SELKEY_SUBTEXT[key_nr].text_GREEN = 255;
+     SELKEY_SUBTEXT[key_nr].text_BLUE =  255;
+     SELKEY_SUBTEXT[key_nr].font_size =    0;
+     SELKEY_SUBTEXT[key_nr].font_style = fstyle_1; 
+     strncpy(SELKEY_SUBTEXT[key_nr].text, "0|....key subtext...............................|\0",
+     max_text0 + 2 ); // (max_text0+2) chars copied
+     SELKEY_SUBTEXT[key_nr].updated = true;   
+   }   
+  // Init Subtext at right selectkeys starting from 50 :
+  for ( key_nr = 50; key_nr < (50+max_rightkeys); key_nr++ )
+  {  SELKEY_SUBTEXT[key_nr].text_RED =   255;
+     SELKEY_SUBTEXT[key_nr].text_GREEN = 255;
+     SELKEY_SUBTEXT[key_nr].text_BLUE =  255;
+     SELKEY_SUBTEXT[key_nr].font_size =    0;
+     SELKEY_SUBTEXT[key_nr].font_style = fstyle_1; 
+     strncpy(SELKEY_SUBTEXT[key_nr].text, "0|....key subtext...............................|\0",
+     max_text0 + 2 ); // (max_text0+2) chars copied 
+     SELKEY_SUBTEXT[key_nr].updated = true;   
+   }     
+  // Init screentext :
+  for ( line_nr = 1; line_nr <= 14; line_nr++ )
+  {  TEXTLINE[line_nr].text_RED =   255;
+     TEXTLINE[line_nr].text_GREEN = 255;
+     TEXTLINE[line_nr].text_BLUE =  255;
+     TEXTLINE[line_nr].font_size =    0;
+     TEXTLINE[line_nr].font_style = fstyle_1; 
+     strncpy(TEXTLINE[line_nr].text, "0|....text line.................................|\0",
+     max_text0 + 2 ); // (max_text0+2) chars copied 
+     TEXTLINE[line_nr].updated = true;   
+   }   
+   // Init CDU status items:
+   CDU_STATUS.mess_light = 1;
+   CDU_STATUS.exec_indicator = 1;
+   CDU_STATUS.backlight = 1;
+   CDU_STATUS.stby_mode = 0;
+   CDU_STATUS.updated = true;
+   
+   // Init Clear screen flag:
+   DO_CLR_SCREEN = false;       
+   
+   // INIT CDU settings:
+   CDU_SET.Baud = 38400; // : will be default baudrate
+   strcpy(CDU_SET.Firmware_nr, "v1.0a\0"); // copy chars incl. '\0'
+   strcpy(CDU_SET.CDU_type, "colour CDU\0"); // copy chars incl. '\0'
+   CDU_SET.Serial_nr = 12345;
+   strcpy(CDU_SET.IP_address, "255.255.255.123\0"); // copy chars incl. '\0'
+   CDU_SET.Port_nr = 4321;
+   strcpy(CDU_SET.Server_ip, "255.255.255.456\0"); // copy chars incl. '\0'
+   CDU_SET.updated = true;
+}
+
+void read_datafields(int command_number) {
+    // Function to read all comma seperated datafields in string_received[]
+    // and fill in FS-to-CDU data structures.
+    // Parameter is found command_number.
+    int colour, size, pos, line, last, cnt, k_id, var, a ;
+    int modified, char_pntr;
+    char val_string[25]; // <<<<<---------------------------------------------------------Local !!!!! 
+    char ch;
+        
+    modified = false;    
+    switch ( command_number )
+    {
+        case 0:  
+        {   // 0 is no valid commandnumber
+             break;
+        }
+        
+        case 1:  // MSG command, message indicator control CDU
+        {              
+             // Get message indicator status:
+             char_pntr = comma[1] + 1; // : set char pointer to char after comma 1
+             if (string_received[char_pntr] == '0' || string_received[char_pntr] == '1')
+             { CDU_STATUS.mess_light = atoi(&string_received[char_pntr]);
+               CDU_STATUS.updated = true;    // : signal update of CDU status
+               FSdata_received_flag = true;  // : set common update flag
+             }
+             break;
+        }
+        
+        case 2: // EXC command, exec indicator control CDU
+        {               
+             // Get exec indicator status:
+             char_pntr = comma[1] + 1; // : set char pointer to char after comma 1
+             if (string_received[char_pntr] == '0' || string_received[char_pntr] == '1')
+             { CDU_STATUS.exec_indicator = atoi(&string_received[char_pntr]);
+               CDU_STATUS.updated = true;   // : signal update of of CDU status
+               FSdata_received_flag = true;  // : set common update flag
+             }
+             break;
+        }
+
+        case 3: // BLT command, backlight control CDU     
+        {                 
+             // Get backlight status:
+             char_pntr = comma[1] + 1; // : set char pointer to char after comma 1
+             if (string_received[char_pntr] == '0' || string_received[char_pntr] == '1')
+             { CDU_STATUS.backlight = atoi(&string_received[char_pntr]);
+               CDU_STATUS.updated = true;   // : signal update of of CDU status
+               FSdata_received_flag = true;  // : set common update flag
+             }
+             break;
+        } 
+        
+        case 4: // SBY command, standby control CDU      
+        {               
+             // Get standby status:
+             char_pntr = comma[1] + 1; // : set char pointer to char after comma 1
+             if (string_received[char_pntr] == '0' || string_received[char_pntr] == '1')
+             { CDU_STATUS.stby_mode = atoi(&string_received[char_pntr]);
+               CDU_STATUS.updated = true;   // : signal update of CDU status
+               FSdata_received_flag = true;  // : set common update flag
+             }
+             break;
+        }        
+        
+        case 5: // CLS command, CDU clear screen control
+        {    
+             DO_CLR_SCREEN = true;        // : tell to clear the screen
+             FSdata_received_flag = true;  // : set common update flag
+             break;
+        }        
+
+
+        case 6: // SBC command, screen background colour control       
+        {                 
+             char_pntr = comma[1] + 1; // : set char pointer to char after comma 1
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                BACKGROUND_COL.RED = colour;
+                modified = true;
+             }
+             char_pntr = comma[2] + 1; // : set char pointer to char after comma 2
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                BACKGROUND_COL.GREEN = colour;
+                modified = true;
+             }
+             char_pntr = comma[3] + 1; // : set char pointer to char after comma 3
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                BACKGROUND_COL.BLUE = colour; 
+                modified = true;
+             }                       
+             if ( modified == true )
+             { BACKGROUND_COL.updated = true;   // : signal update of background colour
+               FSdata_received_flag = true;      // : set common update flag 
+             } 
+                         
+             break;
+        }        
+
+        
+        case 7: // WTX command, write textline to CDU screen   
+        {  
+          // Read linenumber:
+          char_pntr = comma[1] + 1; // : set char pointer to char after comma 1   
+          line = atoi(&string_received[char_pntr]);
+          // Read char position:
+          char_pntr = comma[2] + 1; // : set char pointer to char after comma 2
+          pos = atoi(&string_received[char_pntr]);
+           
+          // Test if char X,Y position is within range:
+          if ( line >= 1 && line <= max_lines && pos >= 1 && pos <= max_text0 )
+          {  // Read font size 0 - 9:
+                char_pntr = comma[3] + 1; // : set char pointer to char after comma 3
+                size = atoi(&string_received[char_pntr]);
+                // Test if size is valid:
+                if ( size >= 0 && size <= max_font ) {
+                    TEXTLINE[line].font_size = atoi(&string_received[char_pntr]);
+                    modified = true;
+                }
+             // Read line font style:
+             char_pntr = comma[4] + 1; // : set char pointer to char after comma 4 
+             ch = string_received[char_pntr];
+             if (ch == fstyle_1 || ch == fstyle_2 ) {
+                 TEXTLINE[line].font_style = ch;                                     
+                 modified = true;
+             }
+             // Read RGB line colour:
+             char_pntr = comma[5] + 1; // : set char pointer to char after comma 5
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                TEXTLINE[line].text_RED = colour;
+                modified = true;
+             }
+             char_pntr = comma[6] + 1; // : set char pointer to char after comma 6
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                TEXTLINE[line].text_GREEN = colour;
+                modified = true;
+             }
+             char_pntr = comma[7] + 1; // : set char pointer to char after comma 7
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                TEXTLINE[line].text_BLUE = colour; 
+                modified = true;
+             }                       
+             // Read textfield :
+             char_pntr = comma[8] + 1; // : set char pointer to first char of textfield
+             last = 0;
+             // read font size to determine last possible position:
+             if ( TEXTLINE[line].font_size == 0 )last = max_text0;
+             else if ( TEXTLINE[line].font_size == 1 )last = max_text1;
+             
+             if (last > 0 && pos <= last) {  // : test for valid area and valid fontsize
+                a = pos;
+                modified = true;
+                while ( a <= last )
+                 {  ch = string_received[char_pntr];
+                    if (ch == '*') break; // : do not include * in text
+                    TEXTLINE[line].text[a] = ch;
+                    a++;
+                    char_pntr++;
+                 }
+             //TEXTLINE[line].text[a] = '\0'; // : mark end of text <<<<<<<<<< ??
+             }            
+          } 
+           
+          if ( modified == true )  {
+            TEXTLINE[line].updated = true;    // : signal update of text line
+            FSdata_received_flag = true;      // : set common update flag 
+          }
+           
+          // Debug:
+          // Print all fields : ----------------------------------------------------------------
+          USB.printf("WTX line        : %d\n",line );                         // show line nr 
+          USB.printf("WTX position    : %d\n",pos );                          // show pos   
+          USB.printf("WTX text is     : %s\n",TEXTLINE[line].text );          // show text  
+          USB.printf("WTX fontsize is : %d\n",TEXTLINE[line].font_size );     // show fontsize 
+          USB.printf("WTX fontstyle is: %c\n",TEXTLINE[line].font_style);     // show fontstyle 
+          USB.printf("WTX R_colour is : %d\n",TEXTLINE[line].text_RED );      // show textcolour 
+          USB.printf("WTX G_colour is : %d\n",TEXTLINE[line].text_GREEN );    // show textcolour 
+          USB.printf("WTX B_colour is : %d\n",TEXTLINE[line].text_BLUE );     // show textcolour 
+          // END of debug printing --------------------------------------------------------------
+                    
+          break;
+        }        
+        
+        case 8: // ETX command, erase (part of) textline of CDU screen        
+        { 
+          // Read linenumber:
+          char_pntr = comma[1] + 1; // : set char pointer to char after comma 1   
+          line = atoi(&string_received[char_pntr]);
+          // Read char position:
+          char_pntr = comma[2] + 1; // : set char pointer to char after comma 2
+          pos = atoi(&string_received[char_pntr]);
+          // Read nr of char to be erased:
+          char_pntr = comma[3] + 1; // : set char pointer to char after comma 3          
+          cnt = atoi(&string_received[char_pntr]);
+          // Test if char X,Y position is within range:
+          if ( line >= 1 && line <= max_lines && pos >= 1 && pos <= max_text0 )
+          {  
+             last = 0;          
+             // read font size to determine last possible position:
+             if ( TEXTLINE[line].font_size == 0 )last = max_text0;
+             else if ( TEXTLINE[line].font_size == 1 )last = max_text1;
+             
+             if (last > 0 && pos <= last && cnt > 0) {  // : test if in valid area and chars > 0
+                a = pos;
+                modified = true;
+                while ( a <= last && cnt > 0)
+                 {  
+                    TEXTLINE[line].text[a] = ' '; //: write space
+                    a++;
+                    cnt--;
+                 }
+             }              
+          } 
+          if ( modified == true )  {
+            TEXTLINE[line].updated = true;    // : signal update of text line
+            FSdata_received_flag = true;      // : set common update flag 
+          }
+           
+          // Debug:
+          // Print : -------------------------------------------------------------------------
+          USB.printf("ETX line : %d\n",line );                                // show line nr 
+          USB.printf("ETX position : %d\n",pos );                             // show pos   
+          USB.printf("ETX modified text : %s\n",TEXTLINE[line].text );        // show text  
+          // END of debug printing ------------------------------------------------------------
+        }        
+        
+        case 9: // KTX command, write textline to select key area  
+        {                    
+           // Read key ID:
+          char_pntr = comma[1] + 1; // : set char pointer to char after comma 1   
+          k_id = atoi(&string_received[char_pntr]);
+          // Read text type:
+          char_pntr = comma[2] + 1; // : set char pointer to char after comma 2 
+          ch = string_received[char_pntr];
+          
+          if (ch == 'M' && k_id < max_keys ) // : text type = MAIN text and keyID valid
+          {   // Read font size 0 - 9:
+              char_pntr = comma[3] + 1; // : set char pointer to char after comma 3
+              size = atoi(&string_received[char_pntr]);
+              // Test if size is valid:
+              if ( size >= 0 && size <= max_font ) {
+                  SELKEY_MAINTEXT[k_id].font_size = atoi(&string_received[char_pntr]);
+                  modified = true;
+              }
+             // Read line font style:
+             char_pntr = comma[4] + 1; // : set char pointer to char after comma 4 
+             ch = string_received[char_pntr];
+             if (ch == fstyle_1 || ch == fstyle_2 ) {
+                 SELKEY_MAINTEXT[k_id].font_style = ch;                                     
+                 modified = true;
+             }
+             // Read RGB line colour:
+             char_pntr = comma[5] + 1; // : set char pointer to char after comma 5
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                SELKEY_MAINTEXT[k_id].text_RED = colour;
+                modified = true;
+             }
+             char_pntr = comma[6] + 1; // : set char pointer to char after comma 5
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                SELKEY_MAINTEXT[k_id].text_GREEN = colour;
+                modified = true;
+             }
+             char_pntr = comma[7] + 1; // : set char pointer to char after comma 7
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                SELKEY_MAINTEXT[k_id].text_BLUE = colour; 
+                modified = true;
+             }                       
+             // Read textfield :
+             char_pntr = comma[8] + 1; // : set char pointer to first char of textfield
+             last = 0;
+             // read font size to determine last possible position:
+             if ( SELKEY_MAINTEXT[k_id].font_size == 0 )last = max_text0;
+             else if ( SELKEY_MAINTEXT[k_id].font_size == 1 )last = max_text1;
+             
+             if (last > 0) {  // : test on valid fontsize
+                a = 1;
+                modified = true;
+                while ( a <= last )
+                 {  ch = string_received[char_pntr];
+                    if (ch == '*') break; // : do not include * in text
+                    SELKEY_MAINTEXT[k_id].text[a] = ch;
+                    a++;
+                    char_pntr++;
+                 }
+             //SELKEY_MAINTEXT[k_id].text[a] = '\0'; // : mark end of text <<< ???
+             }            
+ 
+             if ( modified == true )  {
+              SELKEY_MAINTEXT[k_id].updated = true;  // : signal update 
+              FSdata_received_flag = true;      // : set common update flag 
+             }
+ 
+          // Debug:
+          // Print all fields : --------------------------------------------------------------------
+          USB.printf("KTX key nr is   : %d\n",k_id );                             // show key nr 
+          USB.printf("KTX MAINTEXT is : %s\n",SELKEY_MAINTEXT[k_id].text );       // show text  
+          USB.printf("KTX fontsize is : %d\n",SELKEY_MAINTEXT[k_id].font_size );  // show fontsize 
+          USB.printf("KTX fontstyle is: %c\n",SELKEY_MAINTEXT[k_id].font_style);  // show fontstyle 
+          USB.printf("KTX R_colour is : %d\n",SELKEY_MAINTEXT[k_id].text_RED );   // show textcolour 
+          USB.printf("KTX G_colour is : %d\n",SELKEY_MAINTEXT[k_id].text_GREEN ); // show textcolour 
+          USB.printf("KTX B_colour is : %d\n",SELKEY_MAINTEXT[k_id].text_BLUE );  // show textcolour 
+          // END of debug printing -----------------------------------------------------------------
+          
+          }
+          
+          else if ( ch == 'S' && k_id < max_keys ) // : text type = SUB text and keyID valid
+          {   // Read font size 0 - 9:
+              char_pntr = comma[3] + 1; // : set char pointer to char after comma 3
+              size = atoi(&string_received[char_pntr]);
+              // Test if size is valid:
+              if ( size >= 0 && size <= max_font ) {
+                  SELKEY_SUBTEXT[k_id].font_size = atoi(&string_received[char_pntr]);
+                  modified = true;
+              }
+             // Read line font style:
+             char_pntr = comma[4] + 1; // : set char pointer to char after comma 4 
+             ch = string_received[char_pntr];
+             if (ch == fstyle_1 || ch == fstyle_2 ) {
+                 SELKEY_SUBTEXT[k_id].font_style = ch;                                     
+                 modified = true;
+             }
+             // Read RGB line colour:
+             char_pntr = comma[5] + 1; // : set char pointer to char after comma 5
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                 SELKEY_SUBTEXT[k_id].text_RED = colour;
+                 modified = true;
+             }
+             char_pntr = comma[6] + 1; // : set char pointer to char after comma 6
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                 SELKEY_SUBTEXT[k_id].text_GREEN = colour;
+                 modified = true;
+             }
+             char_pntr = comma[7] + 1; // : set char pointer to char after comma 7
+             colour = atoi(&string_received[char_pntr]);
+             if ( colour >= 0 && colour <= max_col_nr ) {
+                 SELKEY_SUBTEXT[k_id].text_BLUE = colour; 
+                 modified = true;
+             }                       
+             // Read textfield :
+             char_pntr = comma[8] + 1; // : set char pointer to first char of textfield
+             last = 0;
+             // read font size to determine last possible position:
+             if ( SELKEY_SUBTEXT[k_id].font_size == 0 )last = max_text0;
+             else if ( SELKEY_SUBTEXT[k_id].font_size == 1 )last = max_text1;
+             
+             if (last > 0) {  // : test on valid fontsize
+                a = 1;
+                modified = true;
+                while ( a <= last )
+                 {  ch = string_received[char_pntr];
+                    if (ch == '*') break; // : do not include * in text
+                    SELKEY_SUBTEXT[k_id].text[a] = ch;
+                    a++;
+                    char_pntr++;
+                 }
+             //SELKEY_SUBTEXT[k_id].text[a] = '\0'; // : mark end of text <<<<<<< ???
+             }            
+ 
+             if ( modified == true )  {
+              SELKEY_SUBTEXT[k_id].updated = true;  // : signal update 
+              FSdata_received_flag = true;      // : set common update flag 
+             }
+             
+          // Debug:
+          // Print all fields : --------------------------------------------------------------------
+          USB.printf("KTX key nr is   : %d\n",k_id );                            // show key nr 
+          USB.printf("KTX SUBTEXT is : %s\n",SELKEY_SUBTEXT[k_id].text );        // show text  
+          USB.printf("KTX fontsize is : %d\n",SELKEY_SUBTEXT[k_id].font_size );  // show fontsize 
+          USB.printf("KTX fontstyle is: %c\n",SELKEY_SUBTEXT[k_id].font_style);  // show fontstyle 
+          USB.printf("KTX R_colour is : %d\n",SELKEY_SUBTEXT[k_id].text_RED );   // show textcolour 
+          USB.printf("KTX G_colour is : %d\n",SELKEY_SUBTEXT[k_id].text_GREEN ); // show textcolour 
+          USB.printf("KTX B_colour is : %d\n",SELKEY_SUBTEXT[k_id].text_BLUE );  // show textcolour 
+          // END of debug printing -----------------------------------------------------------------
+          
+          }
+     
+          break;
+        }        
+        
+        case 10: // STV command, set a status value:
+        { // Read var_number :
+          char_pntr = comma[1] + 1; // : set char pointer to char after comma 1   
+          var = atoi(&string_received[char_pntr]);
+          if ( var == 0 ) // : test if valid var to write (var 0 = baudrate permitted only now)
+          { // Set baudrate :
+            char_pntr = comma[2] + 1; // : set char pointer to char after comma 2
+            a = atoi(&string_received[char_pntr]);
+            // Test for valid baudrates:
+            if (a==2400 || a==4800 || a==9600 || a== 19200 || a==38400 )
+            { CDU_SET.Baud = a;
+                CDU_SET.updated = true;  // : signal status update 
+                // Send a responding VAL message:
+                sprintf(val_string,"%d",CDU_SET.Baud);                
+                Send_VAL_message(var,val_string);
+                FSdata_received_flag = true;      // : set common update flag
+            }
+            else { // no valid baudrate in command
+                   // Send a responding VAL NACK message :
+                   strcpy(val_string,"NACK\0"); // copy chars incl. '\0'
+                   Send_VAL_message(var,val_string);
+            }
+          }
+          else { // var is not valid var for writing
+                   // Send a responding VAL NACK message :
+                   strcpy(val_string,"NACK\0"); // copy chars incl. '\0'
+                   Send_VAL_message(var,val_string);
+          }
+                  
+          break;
+        }               
+        
+        case 11: // GTV command, get a status value:     
+        { // Read var_number :
+          char_pntr = comma[1] + 1; // : set char pointer to char after comma 1   
+          var = atoi(&string_received[char_pntr]);
+          if ( var >= 0 && var <= 6) // : test if valid var to read
+          { // Read value :
+            switch ( var ) {
+                case 0: { // baudrate asked
+                          sprintf(val_string,"%d",CDU_SET.Baud);
+                          Send_VAL_message(var,val_string);
+                break;
+                }
+                case 1: { // firmware ID asked
+                          strcpy(val_string,CDU_SET.Firmware_nr); // copy chars incl. '\0'
+                          Send_VAL_message(var,val_string);                
+                break;
+                }
+                case 2: { // CDU type ID asked
+                          strcpy(val_string,CDU_SET.CDU_type); // copy chars incl. '\0'
+                          Send_VAL_message(var,val_string);
+                break;                
+                } 
+                case 3: { // serial number asked
+                          sprintf(val_string,"%d",CDU_SET.Serial_nr);
+                          Send_VAL_message(var,val_string);                          
+                break;                
+                }
+                case 4: { // IP address of CDU asked
+                          strcpy(val_string,CDU_SET.IP_address); // copy chars incl. '\0''
+                          Send_VAL_message(var,val_string);               
+                break;
+                }
+                case 5: { // Port number asked
+                          sprintf(val_string,"%d",CDU_SET.Port_nr);                
+                          Send_VAL_message(var,val_string);
+                break;                
+                }
+                case 6: { // IP address of server asked
+                          strcpy(val_string,CDU_SET.Server_ip); // copy chars incl. '\0'
+                          Send_VAL_message(var,val_string);
+                break;                
+                }            
+
+                default : { // invalid var , not possible 
+                break;
+                }   
+                           
+            }
+          }
+          else { // var is not valid 
+                   // Send a responding VAL NACK message :
+                   strcpy(val_string,"NACK\0"); // copy chars incl. '\0'
+                   Send_VAL_message(var,val_string); 
+          }
+          break;
+        }        
+               
+        case 12: // dbg, special debug command        
+        {                    
+
+          break;
+        }               
+        
+        default:
+        {   
+          // unknown commandnumber !
+          break;
+        }
+
+
+    } 
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Tue Jan 29 14:54:14 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/MODSERIAL/#3ba4341d74d6
--- a/USB_receive.cpp	Sat Dec 08 20:37:05 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,131 +0,0 @@
-// L. van der Kolk, ELVEDEKA, Holland //
-// File:  USB_receive.cpp   version  1.0
-
-#include "mbed.h"
-
-#define TRUE  1
-#define FALSE 0
-
-#define max_rx_buffer 1024  // : length of main receiving cyclic buffer
-#define max_string_length 80  //  : max length of string starting with $ and ending with CR/LF
-#define min_string_length 10 //  : min length of string starting with $ and ending with CR/LF
-
-DigitalOut led2(LED2);  // TEST Led2
-DigitalOut led3(LED3);  // TEST Led3
-
-extern Serial USB;      // >>>>>>>>>>> alleen t.b.v TEST output !!
-void decode_string();
-
-// Define main receiving cyclic buffer:
-struct cyclic_buf { 
-                    int end_pntr;               // : pointer from where to write in buffer
-                    int start_pntr;             // : pointer from where to read from buffer
-                    int new_data;               // : new_data in buffer counter
-                    char data[max_rx_buffer];   // : main serial receiving cyclic array
-                  } rx_buf = { 0, 0, 0 };       // : initialize pointers and 'new_data' 
-
-
-char string_received[max_string_length + 2]; //  : contains a string starting with $ and ending with CR/LF
-int  nr_of_received_char = 0;
-int  string_complete = FALSE; // : no complete string with $ and CR/LF
-
-void char_received() {
-    // -- This functiom will be called on EACH rx interrupt --
-    // It fills the cyclic RX buffer with data.
-    char rx_char;
-    rx_char = USB.getc();   // : reading also clears receive interrupt <<<<<<<<<<<<< ???????????
-    rx_buf.data[rx_buf.end_pntr] = rx_char;  // : buffer write
-    rx_buf.end_pntr = (rx_buf.end_pntr + 1) % max_rx_buffer;    // : increment write pointer including wrap around
-    rx_buf.new_data++;      // : incremnent new data counter
-        USB.putc(rx_char);      // : echo character back to PC   <<<<<<<<<<<<<<<<<<<<<<<<<<<TEST !  
-    if (rx_buf.new_data >= max_rx_buffer) {
-           // ERROR: receiving buffer overflow, FS data lost !
-           // What to do ?   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-           USB.puts(" RxBUF_OVERFLOW ");      // : echo overflow error back to PC   <<<<<<<<<<<<<<<<<TEST !            
-    }           
-}
-
-
-void collect_decode_FSdata()  {
-     // Function reads data from cyclic RX buffer
-     // and collects strings starting with $ and ending with CR/LF.
-     // Strings shorter than min_string_length or longer than
-     // max_string_length are ignored.
-    static int $_detected = FALSE;      // : no begin of string detected, ONLY first call static init 
-    static int string_pntr = 0;         // : pointer at begin of string, ONLY first call static init 
-    char rx_char;
-    //led2 = 1    ;           // : set LED2 ON to show start of RX interrupt <<<<<<<<<<  TEST ! 
-    if ( rx_buf.new_data > 0 ) {    // : something new to read ?
-         rx_char = rx_buf.data[rx_buf.start_pntr]; // : buffer read
-         rx_buf.start_pntr = (rx_buf.start_pntr + 1) % max_rx_buffer; // : increment read pointer including wrap around
-         rx_buf.new_data--; // : decremnent new data counter
-         
-         //USB.putc(rx_char);      // : echo character back to PC  <<<<<<<<<<<<<<<<<<<<<<<<<<<<< TEST !!
-         
-        // Check for string starting with $ :
-        if ( rx_char == '$' && $_detected == FALSE ){
-            $_detected = TRUE;  // : begin of string detected 
-            string_pntr = 0;    // : set pointer to begin of string_received[] buffer
-        }
-        
-        string_received[string_pntr] = rx_char;
-        string_pntr++;
-    
-        if (string_pntr >= max_string_length) {
-            // command string looks too long,
-            // start all over again:
-            string_pntr = 0;  // : set pointer to begin of string_received[] buffer again
-            $_detected = FALSE;
-        }
-    
-        if ( rx_char == '\r' && $_detected == TRUE ) { 
-            if ( string_pntr > min_string_length ) { // : check minimum string length 
-                // String is now valid when:
-                // it starts with '$' AND
-                // it ends on new-line  AND
-                // it has at least min_string_length characters.
-                string_received[string_pntr] = '\0'; // : mark end of string
-                           
-                USB.printf("string_received : %s\n",string_received );  // show string >>>>>>>>>>>>>>>>( TEST !)
-           
-                nr_of_received_char = string_pntr;
-                decode_string();    // : call decoder to check complete string received
-                
-                // Make ready for receiving new command:
-                $_detected = FALSE;
-                string_pntr = 0;  // : set pointer to begin of string_received[] buffer again
-                nr_of_received_char = 0;
-            }
-            else {
-                $_detected = FALSE;   // : <<<<<<<<<<<<   toegevoegd !!!!
-                string_pntr = 0; // : set pointer to begin of string_received[] buffer again
-                nr_of_received_char = 0;
-            }
-         }
-    } 
-    
-    // led2 = 0;           // : set LED2 OFF to show end of RX interrupt ( TEST ! )       
-}
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-   
-    
-    
-    
-    
-    
-    
-       
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USB_receive_2.cpp	Tue Jan 29 14:54:14 2013 +0000
@@ -0,0 +1,155 @@
+// L. van der Kolk, ELVEDEKA, Holland //
+// File:  USB_receive_2.cpp Mbos version
+
+#include "mbed.h"
+#include "MODSERIAL.h" // : Note: import lib of Erk Olieman V1.25 to prevent compiler error!
+
+// Instantiate USB communication defined by Mbed USB TX and RX lines
+// with TX and RX ringbuffers :
+MODSERIAL USB(USBTX, USBRX, 256, 512);   
+   
+void decode_string(int nummer_of_chars);
+void read_datafields(int command_number);
+
+#define max_string_length 80 // : max length of received string starting with $ and ending with CR/LF
+#define min_string_length 10 // : min length of received string starting with $ and ending with CR/LF
+char string_received[max_string_length + 2]; // : holds received string starting with $ and ending with CR/LF
+
+int  receive_flag = false;   // :  global flag, signals a receive 'event' 
+
+#define max_commas 10  // : max. nr of possible field separating commas in a valid message string to CDU
+int comma[max_commas];      // : array with positions of all found commas in string_receved[]
+#define message_header "$PCDU"  // : common message header in all messages
+
+#define max_nr_of_commands 16   // : max nr of possible FS-to-CDU commands
+// Define array of pointers to possible FS-to-CDU commands with 3 characters:
+const char *command[max_nr_of_commands] = {
+   "123",    // : no valid CDU command nr. 0 , used for debugging only
+   "MSG",    // : command nr. 1
+   "EXC",    // : command nr. 2
+   "BLT",    // : command nr. 3
+   "SBY",    // : command nr. 4
+   "CLS",    // : command nr. 5
+   "SBC",    // : command nr. 6
+   "WTX",    // : command nr. 7
+   "ETX",    // : command nr. 8
+   "KTX",    // : command nr. 9
+   "STV",    // : command nr. 10
+   "GTV",    // : command nr. 11
+};
+
+void collect_FSdata()  {
+     // Function reads data from the RX buffer
+     // and collects strings starting with $ and ending with CR/LF.
+     // Strings shorter than min_string_length or longer than
+     // max_string_length will be ignored, others will be decoded further.
+    static int $_detected = false;      // : no valid begin of string detected (init only on first call)
+    static int string_pntr = 0;         // : pointer at begin of string (init only on first call)
+    static int nr_of_received_char = 0; // : counter of received characters (init only on first call)
+    char rx_char;
+             
+    while ( !USB.rxBufferEmpty() )
+    {    rx_char = USB.getc(); // : get a char from Rx buffer
+        //USB.putc(rx_char);    // : unprotected (!) immediate echo of char in case of debugging  <<<
+        // Check for string starting with $ char:
+        if ( rx_char == '$' && $_detected == false ){
+            $_detected = true;  // : begin of string is detected 
+            string_pntr = 0;    // : set pointer to begin of string_received[] buffer
+        }
+        string_received[string_pntr] = rx_char;
+        string_pntr++;
+        
+        if (string_pntr >= max_string_length) {
+            // command string looks too long, so start all over again:
+            string_pntr = 0;  // : set pointer back to begin of string_received[] again
+            nr_of_received_char = 0;   // : reset number of received chars
+            $_detected = false;
+        }
+    
+        if ( rx_char == '\n' && $_detected == true ) { 
+            if ( string_pntr > min_string_length ) { // : check minimum string length 
+                // Received string can be interesting now because
+                // it starts with '$' AND it ends on New-Line  AND
+                // it has a minimum length AND it is not too long:
+                string_received[string_pntr] = '\0'; // : mark end of string
+                //  USB.printf("string_received : %s",string_received );  // show string for debugging <<
+                nr_of_received_char = string_pntr;
+                //Call decoder to analyse this string:
+                decode_string(nr_of_received_char);
+                  
+                // Get ready for receiving new commands:
+                $_detected = false;
+                string_pntr = 0;  // : set pointer back to begin of string_received[] again
+                nr_of_received_char = 0;   // : reset number of received chars
+            }
+            else {
+                $_detected = false;  
+                string_pntr = 0; // : set pointer back to begin of string_received[] again
+                nr_of_received_char = 0;   // : reset number of received chars
+            }
+         }
+    } 
+}
+    
+void decode_string(int nummer_of_chars)
+{   // -- This function decodes a received $.....CR/LF string written in string_received[] --
+    // First it checks for a valid checksum and reads the positions of commas in the string.
+    // When checksum is OK, it will continue to look for valid 3 char FS-to-CDU commands.
+    // When a valid command is found, data fields will be analyzed further using the found positions
+    // of commas in the string.
+    int  i,c, equal;
+    char byte_read, exor_byte;
+    char command_string[6], received_checksum[4], calc_checksum[4];
+      
+    // Get checksum and position of commas in string_received[] :
+    exor_byte = 0;
+    i = 1;  // : i points to first char after $
+    c = 1;  // : position of first comma
+      do {
+          byte_read = string_received[i];
+          if (byte_read == ',' && c < max_commas) {
+              comma[c] = i;
+              c++;
+          }
+          if (byte_read == '*') break;
+          exor_byte = exor_byte ^ byte_read;
+          i++;
+      } while ( i < nummer_of_chars );
+    //USB.printf("commas found : %d\n",c-1 );  // : show commas for debugging <<<<<<
+    i++;   // : i points to first checksum char after * char
+    strncpy(received_checksum,&string_received[i],2);   // : copy 2 char checksum after * 
+    // Get calculated checksum by transforming exor_byte in 2 hex chars (with upper case A-F) :
+    sprintf(calc_checksum,"%02X",exor_byte); // : + extra NULL char added by sprintf 
+    equal = strncmp(received_checksum,calc_checksum,2);
+    if (equal != 0) { 
+       //USB.printf("checksum is NOT OK ! \n" );  // : show message for debugging <<<
+    }
+    else { // checksum is OK, go on:
+       // Check for 5 char "$PCDU" header:
+       equal = strncmp(string_received,message_header,strlen(message_header));
+        if (equal != 0) {
+            // USB.printf("no $PCDU header in message !\n" );  // : show message for debugging <<<
+        }
+        else {
+             // Read 3 char command after message_header:
+             strncpy(command_string,&string_received[strlen(message_header)],3);
+             //USB.printf("command found : %3s\n",command_string );  // : show command for debugging <<<
+             // Compare found string with known 3 char command list:
+             i = 0; 
+               do {
+                  equal = strncmp(&command_string[0],command[i],3);
+                  if( equal == 0) break;
+                  i++;
+               } while ( i < max_nr_of_commands);
+              //USB.printf("command number is : %d\n",i );  // : show command nr for debugging <<<
+             if (equal == 0) {
+                 // Command is known now, so now read all data fields:
+                 read_datafields(i);
+             }
+        } 
+    } 
+    
+   receive_flag = false;
+ }
+ 
+ 
\ No newline at end of file
--- a/USB_setup.cpp	Sat Dec 08 20:37:05 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-// 
-#include "mbed.h"
-
-Serial USB(USBTX, USBRX); // : declare USB communication defined by Mbed USB TX and RX lines
-
-void char_received();
-
-void init_USB()
-{   // -- This function is the setup for the USB communication --
-    // Function char_received() is attached to Rx interrupt.
-
-    USB.baud(38400); // set initial baudrate to 38400
-    
-    USB.attach(&char_received);    // : attach function
-}
\ No newline at end of file
--- a/alive_timer.cpp	Sat Dec 08 20:37:05 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-// L. van der Kolk, ELVEDEKA, Holland //
-// File:  alive_timer.cpp   version  1.0
-
-#include "mbed.h"
-
-#define TRUE  1
-#define FALSE 0
-
-extern Serial USB;
-
-Ticker alive_tick;  // : define timer
-
-int alive_flag = FALSE; // : TRUE when 5 sec have been passed
-int seconds = 0;
-char alive_message[20] = "$PCDUALIVE,";  // : setup begin of alive message string
-
-void alive(){
-    static int previous_time = 0;  
-    seconds++;
-    // Check for 5 sec interval:
-    if (seconds - previous_time == 5) {
-      previous_time = seconds;
-      alive_flag = TRUE;
-    }
-    seconds = seconds % 60; 
-    previous_time = previous_time % 60;
-}
-
-void init_alivetimer(){
-    alive_tick.attach(&alive, 1); // : call alive() function each second
-}
-
-void check_alive() {
-    int i;
-    char byte_read;
-    char exor_byte = 0;
-    if ( alive_flag == TRUE) {
-    //  Create alive message:
-        i = 11; // : i points to first place after $PCDUALIVE, string
-        // 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 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(&alive_message[i],"%02x\r\n",exor_byte); // : ! extra NULL char added by sprintf 
-        USB.printf("%s",alive_message);
-        alive_flag = FALSE;
-     }
-}
\ No newline at end of file
--- a/buttons.cpp	Sat Dec 08 20:37:05 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
-// L. van der Kolk, ELVEDEKA, Holland //
-// File:  buttons.cpp   version  1.0
-
-#include "mbed.h"
-
-#define TRUE  1
-#define FALSE 0
-
-extern Serial USB;
-
-int button_flag = 0;
-
-char key_message[20] = "$PCDUKEY,";  // : setup begin of key message string to FS
-
-BusInOut button(p24, p23, p22, p21); // : Mbed buttons as 4 bit bus
-
-void init_buttons_Mbed() {
-    // set button bus as input :
-    button.input();
-    // enable pull up resistors on inputs:
-    button.mode(PullUp);
-}
-
-void scan_buttons_Mbed() {
-static int pushed = FALSE;
-  switch (button) {
-    case 0xF : {
-    // nothing pushed, reset pushed flag:
-        pushed = FALSE;
-        break;
-    }
-    
-    case 0x7 : {
-    // button 1 pushed:
-        if ( pushed == FALSE ) button_flag = 1;
-        pushed = TRUE;
-        break;
-    }
-    
-    case 0xB : {
-    // button 2 pushed:
-        if ( pushed == FALSE ) button_flag = 2;
-        pushed = TRUE;
-        break; 
-    }   
-    
-    case 0xD : {
-    // button 3 pushed:
-        if ( pushed == FALSE ) button_flag = 3;
-        pushed = TRUE;
-        break;
-    }
-    
-    case 0xE : {
-    // button 4 pushed:
-        if ( pushed == FALSE ) button_flag = 4;
-        pushed = TRUE;
-        break;
-    }
-    
-    default :  break;
-        
-  }
-}
-
-
-void collect_and_send_FSdata() {
-    int i;
-    char byte_read;
-    char exor_byte = 0;
-    if ( button_flag != 0 ) {
-    //  Create key message:
-        i = 9; // : i points to first place after $PCDUKEY, 
-        // Add key identifier 'SKEY' and a '*' char to key message string:
-        sprintf(&key_message[i],"%s","SKEY");
-        // Add 2 digit keynumber and a '*' char :
-        i = 13; // : i points to first place after $PCDUKEY,SKEY 
-        sprintf(&key_message[i],"%02d*",button_flag);
-        // Calculate checksum now : 
-        i = 1; // : i points to first place after '$'
-        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 
-        USB.printf("%s",key_message);
-        button_flag = 0;
-     }
-}
-
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buttontest.cpp	Tue Jan 29 14:54:14 2013 +0000
@@ -0,0 +1,73 @@
+// L. van der Kolk, ELVEDEKA, Holland //
+// File:  buttontest.cpp 
+// Functions to test KEY message with 4 Mbed board test buttons 
+
+#include "mbed.h"
+#include "MODSERIAL.h"
+
+#include "mbos.h"
+//#include "mbos_definitions.h ------!!!!!    
+#define SEND_KEYMESSAGE_TASK_ID  3      //
+#define KEY_EVENT    4                  //
+//----------------------------------------
+
+DigitalOut led4(LED4); // : LED 4 on Mbed board for test     
+BusInOut button(p24, p23, p22, p21); // : Mbed buttons as 4 bit bus 
+
+extern mbos CDU_OS; 
+extern int key_hit_ID;
+
+int button_info = 0;     
+
+void init_buttons_Mbed() {
+    // set button bus as input :
+    button.input();
+    // enable pull up resistors on inputs:
+    button.mode(PullUp);
+}
+
+void scan_buttons_Mbed() {
+  static int pushed = false;
+  
+  switch (button) {
+    case 0xF : {
+    // nothing pushed, reset pushed flag:
+        pushed = false;
+        break;
+    }
+    case 0x7 : {
+    // button 1 pushed:
+        if ( pushed == false ) button_info = 33;
+        pushed = true;
+        break;
+    }
+    case 0xB : {
+    // button 2 pushed:
+        if ( pushed == false ) button_info = 34;
+        pushed = true;
+        break; 
+    }   
+    case 0xD : {
+    // button 3 pushed:
+        if ( pushed == false ) button_info = 12;
+        pushed = true;
+        break;
+    }
+    case 0xE : {
+    // button 4 pushed:
+        if ( pushed == false ) button_info = 21;
+        pushed = true;
+        break;
+    }
+    default :  {
+         break;
+    }
+  }
+    if (button_info != 0) {
+        key_hit_ID = button_info;  // : set global key_ID
+        button_info = 0;
+        CDU_OS.SetEvent(KEY_EVENT,SEND_KEYMESSAGE_TASK_ID); // : set KEY event ( = key pressed )
+        led4 = !led4; // : toggle LED 4 to show button pressed
+        
+    }  
+}
--- a/data_structures.cpp	Sat Dec 08 20:37:05 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,465 +0,0 @@
-// L. van der Kolk, ELVEDEKA, Holland //
-// File:  data_structures.cpp   version  1.0
-
-#include "mbed.h"
-#include "ctype.h"
-
-#define TRUE  1
-#define FALSE 0
-
-#define max_text_length 40  // : max length of text field
-#define max_style 6         // : max length of style text
-
-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 int comma[];              // : array with pointers to all found commas in received string
-
-extern Serial USB;      // >>>>>>>>>>> alleen t.b.v TEST output !!
-
-// ---------- Data structures of received FS data ------------------------------------------------------
-
-struct {    char  text[max_text_length];//  : maintext with max_text_length chars, ending with '/0'   
-            int   font_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'         
-            int   text_colour;          //  : colour number 0 - 9
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag,  TRUE/FALSE
-        }  KEY_LEFT_MAINTEXT[10];       //  : keys can be 0 - 9
-        
-struct {    char  text[max_text_length];//  : subtext with max_text_length chars, ending with '/0'   
-            int   font_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'         
-            int   text_colour;          //  : colour number 0 - 9
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag,  TRUE/FALSE
-        }  KEY_LEFT_SUBTEXT[10];        //  : keys can be 0 - 9  
-
-struct {    char  text[max_text_length];//  : maintext with max_text_length chars, ending with '/0'    
-            int   font_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'         
-            int   text_colour;          //  : colour number 0 - 9
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag,  TRUE/FALSE
-        }  KEY_RIGHT_MAINTEXT[10];      //  : keys can be 0 - 9
-
-struct {    char  text[max_text_length];//  : subtext with max_text_length chars, ending with '/0' 
-            int   font_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'         
-            int   text_colour;          //  : colour number 0 - 9
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag,  TRUE/FALSE
-        }  KEY_RIGHT_SUBTEXT[10];       //  : keys can be 0 - 9
-        
-struct {    char  text[max_text_length];//  : text with max_text_length chars, ending with '/0'    
-            int   font_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'      
-            int   text_colour;          //  : colour number 0 - 9
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag,  TRUE/FALSE
-        }  DBS_MESSAGE;      
-        
-struct {    char  text[max_text_length];//  : Title text with max_text_length chars, ending with '/0'   
-            int   font_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'         
-            int   text_colour;          //  : colour number 0 - 9
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag,  TRUE/FALSE
-        }  TIT_MESSAGE;  
-        
-struct {    char  text[10];             //  : text max. 10 chars, ending with '/0'    
-            int   font_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'         
-            int   text_colour;          //  : colour number 0 - 9
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag,  TRUE/FALSE
-        }  PGE_MESSAGE; 
-        
-struct {    char  text[max_text_length];//  : text with max_text_length chars, ending with '/0'    
-            int   font_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'         
-            int   text_colour;          //  : colour number 0 - 9
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag, TRUE/FALSE
-        }  SPD_MESSAGE;                      
-        
-struct {    char  text[max_text_length];//  : text with max_text_length chars, ending with '/0'
-            int   x_pos;                //  :  
-            int   y_pos;                //  :
-            int   text_size;            //  : fontsize can be 0 - 9            
-            char  font_style[max_style];//  : can be S,B,I,U,N or combination, ending with '/0'         
-            int   text_colour;          //  : colour number 0 - 9 
-            int   updated;              //  : update flag, TRUE/FALSE
-            int   locked;               //  : locked flag, TRUE/FALSE
-        }  PUT_STRING_MESSAGE; 
-        
-struct {    char  text[10];         //  : text max. 10 chars, ending with '/0' 
-            int   status_id;        //  : can be 0 or 1           
-            int   updated;          //  : update flag, TRUE/FALSE
-            int   locked;           //  : locked flag, TRUE/FALSE
-        }  SID_MESSAGE;
-        
-struct {    char  text[10];         //  : text max. 10  chars, ending with '/0' 
-            int   updated;          //  : update flag, TRUE/FALSE
-            int   locked;           //  : locked flag, TRUE/FALSE
-        }  SETVALUE_MESSAGE[10];        
-        
-int  GETVALUE_MESSAGE[10];          // : update flag, TRUE/FALSE 
-
-int DO_CLR_SCREEN = FALSE;          // : flag, TRUE/FALSE, to tell a clr screen message has been received
-
-int FSdata_updated_flag = FALSE;    // : common flag, TRUE/FALSE, to tell one or more datastructures
-                                    //  have been updated
-
-// ------------------------------------------------------------------------------------------
-
-
-
-void (read_datafields(int command_number)) {
-
-    int char_pntr, a;
-    char textchar;
-    
-    switch ( command_number )
-    {
-        case 0:  
-        {   // 0 is no valid commndnumber
-        
-          break;
-        }
-        
-        case 1:  // DSB message
-        {    // ---------------- locking ---------------------------------------------------
-             while (DBS_MESSAGE.locked == TRUE) {
-                // --- DO NOTHING, WAIT FOR UNLOCKING !! ---
-                // TAKE CARE : Program can HANG if not unlocked !!
-             }
-               DBS_MESSAGE.locked = TRUE; // : lock datastructure to prevent reading from it
-             // ----------------------------------------------------------------------------             
-             
-             // Read fontsize 0 - 9:
-                char_pntr = comma[1] + 2; // : set char pointer to digit after 'F'
-                if (isdigit(string_received[char_pntr])) { // : test on digit
-                   DBS_MESSAGE.font_size = atoi(&string_received[char_pntr]);
-                }
-             // Read fontstyle :
-                a = 0;   
-                char_pntr = comma[2] + 1; // : set char pointer to fontstyle
-                while ( a < max_style-1 && char_pntr < comma[3] ) {
-                 textchar = string_received[char_pntr];
-                  if (textchar=='S' || textchar=='B' || textchar=='I' || textchar== 'U' || textchar== 'N' )
-                    { DBS_MESSAGE.font_style[a] = textchar; }
-                  else DBS_MESSAGE.font_style[a] = '?'; // : unknown style char
-                 char_pntr++;
-                 a++;
-                }
-                DBS_MESSAGE.font_style[a] = '\0'; // : mark end of text 
-             // Read text colour :
-                char_pntr = comma[3] + 1; // : set char pointer to textcolour
-                // test on digit ??? <<<<<<<<<<<<
-                if (string_received[char_pntr] != ',' ) { // : colour field not empty
-                 DBS_MESSAGE.text_colour = atoi(&string_received[char_pntr]);
-                }
-                else DBS_MESSAGE.text_colour = 0;   // : empty field means colour 0
-             // Read textfield :
-                a = 0;
-                char_pntr = comma[4] + 1; // : set char pointer to textfield
-                while ( a < max_text_length-1 )
-                {    textchar = string_received[char_pntr];
-                     if (textchar == '*') break; // : do not include * in text
-                        DBS_MESSAGE.text[a] = textchar;
-                        a++;
-                        char_pntr++;
-                }
-                 DBS_MESSAGE.text[a] = '\0'; // : mark end of text  
-                
-                // Print all for TESTING : ------------------------------------- <<<<<<<<<<<<<<<<<
-                USB.printf("text is : %s\n",DBS_MESSAGE.text );                 // show text  
-                USB.printf("fontsize is : %d\n",DBS_MESSAGE.font_size );        // show fontsize 
-                USB.printf("fontstyle is : %s\n",DBS_MESSAGE.font_style );      // show fontstyle )
-                USB.printf("textcolour is : %d\n\n",DBS_MESSAGE.text_colour );    // show textcolour 
-                // END of TEST printing ---------------------------------------- <<<<<<<<<<<<<<<<<
-                                
-                DBS_MESSAGE.updated = TRUE;  // : tell update of datastructure has been done
-                FSdata_updated_flag = TRUE;  // : set common update flag
-                DBS_MESSAGE.locked = FALSE;  // : FREE datastructure for reading from it
-                                 
-          break;
-        }
-        
-        case 2: // Title message
-        {    // ---------------- locking ---------------------------------------------------
-             while (TIT_MESSAGE.locked == TRUE) {
-                // --- DO NOTHING, WAIT FOR UNLOCKING !! ---
-                // TAKE CARE : Program can HANG if not unlocked !!
-             }
-                TIT_MESSAGE.locked = TRUE; // : lock datastructure to prevent reading from it
-             // ----------------------------------------------------------------------------             
-             
-             // Read fontsize 0 - 9:
-                char_pntr = comma[1] + 2; // : set char pointer to digit after 'F'
-                if (isdigit(string_received[char_pntr])) { // : test on digit
-                   TIT_MESSAGE.font_size = atoi(&string_received[char_pntr]);
-                }
-             // Read fontstyle :
-                a = 0;   
-                char_pntr = comma[2] + 1; // : set char pointer to fontstyle
-                while ( a < max_style-1 && char_pntr < comma[3] ) {
-                 textchar = string_received[char_pntr];
-                  if (textchar=='S' || textchar=='B' || textchar=='I' || textchar== 'U' || textchar== 'N' )
-                    { TIT_MESSAGE.font_style[a] = textchar; }
-                  else TIT_MESSAGE.font_style[a] = '?'; // : unknown style char
-                 char_pntr++;
-                 a++;
-                }
-                TIT_MESSAGE.font_style[a] = '\0'; // : mark end of text 
-             // Read text colour :
-                char_pntr = comma[3] + 1; // : set char pointer to textcolour
-                // test on digit ??? <<<<<<<<<<<<
-                if (string_received[char_pntr] != ',' ) { // : colour field not empty
-                 TIT_MESSAGE.text_colour = atoi(&string_received[char_pntr]);
-                }
-                else TIT_MESSAGE.text_colour = 0;   // : empty field means colour 0
-             // Read textfield :
-                a = 0;
-                char_pntr = comma[4] + 1; // : set char pointer to textfield
-                while ( a < max_text_length-1 )
-                {    textchar = string_received[char_pntr];
-                     if (textchar == '*') break; // : do not include * in text
-                        TIT_MESSAGE.text[a] = textchar;
-                        a++;
-                        char_pntr++;
-                }
-                 TIT_MESSAGE.text[a] = '\0'; // : mark end of text  
-    
-                // Print all for TESTING : ------------------------------------- <<<<<<<<<<<<<<<<<
-                USB.printf("text is : %s\n",TIT_MESSAGE.text );                 // show text  
-                USB.printf("fontsize is : %d\n",TIT_MESSAGE.font_size );        // show fontsize 
-                USB.printf("fontstyle is : %s\n",TIT_MESSAGE.font_style );      // show fontstyle )
-                USB.printf("textcolour is : %d\n\n",TIT_MESSAGE.text_colour );  // show textcolour 
-                // END of TEST printing ---------------------------------------- <<<<<<<<<<<<<<<<<
-                                
-                TIT_MESSAGE.updated = TRUE;  // : tell update of datastructure has been done
-                FSdata_updated_flag = TRUE;  // : set common update flag
-                TIT_MESSAGE.locked = FALSE; // : FREE datastructure for reading from it
-                                 
-          break;
-        }
-
-        case 3: // PGE message        
-        {    // ---------------- locking ---------------------------------------------------
-             while (PGE_MESSAGE.locked == TRUE) {
-                // --- DO NOTHING, WAIT FOR UNLOCKING !! ---
-                // TAKE CARE : Program can HANG if not unlocked !!
-             }
-                PGE_MESSAGE.locked = TRUE; // : lock datastructure to prevent reading from it
-             // ----------------------------------------------------------------------------             
-             
-             // Read fontsize 0 - 9:
-                char_pntr = comma[1] + 2; // : set char pointer to digit after 'F'
-                if (isdigit(string_received[char_pntr])) { // : test on digit
-                   PGE_MESSAGE.font_size = atoi(&string_received[char_pntr]);
-                }
-             // Read fontstyle :
-                a = 0;   
-                char_pntr = comma[2] + 1; // : set char pointer to fontstyle
-                while ( a < max_style-1 && char_pntr < comma[3] ) {
-                 textchar = string_received[char_pntr];
-                  if (textchar=='S' || textchar=='B' || textchar=='I' || textchar== 'U' || textchar== 'N' )
-                    { PGE_MESSAGE.font_style[a] = textchar; }
-                  else PGE_MESSAGE.font_style[a] = '?'; // : unknown style char
-                 char_pntr++;
-                 a++;
-                }
-                PGE_MESSAGE.font_style[a] = '\0'; // : mark end of text 
-             // Read text colour :
-                char_pntr = comma[3] + 1; // : set char pointer to textcolour
-                // test on digit ??? <<<<<<<<<<<<
-                if (string_received[char_pntr] != ',' ) { // : colour field not empty
-                 PGE_MESSAGE.text_colour = atoi(&string_received[char_pntr]);
-                }
-                else PGE_MESSAGE.text_colour = 0;   // : empty field means colour 0
-             // Read textfield :
-                a = 0;
-                char_pntr = comma[4] + 1; // : set char pointer to textfield
-                while ( a < max_text_length-1 )
-                {    textchar = string_received[char_pntr];
-                     if (textchar == '*') break; // : do not include * in text
-                        PGE_MESSAGE.text[a] = textchar;
-                        a++;
-                        char_pntr++;
-                }
-                 PGE_MESSAGE.text[a] = '\0'; // : mark end of text
-                
-                // Print all for TESTING : ------------------------------------- <<<<<<<<<<<<<<<<<
-                USB.printf("text is : %s\n",PGE_MESSAGE.text );                 // show text  
-                USB.printf("fontsize is : %d\n",PGE_MESSAGE.font_size );        // show fontsize 
-                USB.printf("fontstyle is : %s\n",PGE_MESSAGE.font_style );      // show fontstyle )
-                USB.printf("textcolour is : %d\n",PGE_MESSAGE.text_colour );    // show textcolour 
-                // END of TEST printing ---------------------------------------- <<<<<<<<<<<<<<<<<
-                                
-                PGE_MESSAGE.updated = TRUE;  // : tell update of datastructure has been done
-                FSdata_updated_flag = TRUE;  // : set common update flag
-                PGE_MESSAGE.locked = FALSE; // : FREE datastructure for reading from it
-                                 
-          break;
-        }        
-        
-        case 4: // SPD message        
-        {    // ---------------- locking ---------------------------------------------------
-             while (SPD_MESSAGE.locked == TRUE) {
-                // --- DO NOTHING, WAIT FOR UNLOCKING !! ---
-                // TAKE CARE : Program can HANG if not unlocked !!
-             }
-                SPD_MESSAGE.locked = TRUE; // : lock datastructure to prevent reading from it
-             // ----------------------------------------------------------------------------             
-             
-              // Read fontsize 0 - 9:
-                char_pntr = comma[1] + 2; // : set char pointer to digit after 'F'
-                if (isdigit(string_received[char_pntr])) { // : test on digit
-                   SPD_MESSAGE.font_size = atoi(&string_received[char_pntr]);
-                }
-             // Read fontstyle :
-                a = 0;   
-                char_pntr = comma[2] + 1; // : set char pointer to fontstyle
-                while ( a < max_style-1 && char_pntr < comma[3] ) {
-                 textchar = string_received[char_pntr];
-                  if (textchar=='S' || textchar=='B' || textchar=='I' || textchar== 'U' || textchar== 'N' )
-                    { SPD_MESSAGE.font_style[a] = textchar; }
-                  else SPD_MESSAGE.font_style[a] = '?'; // : unknown style char
-                 char_pntr++;
-                 a++;
-                }
-                SPD_MESSAGE.font_style[a] = '\0'; // : mark end of text 
-             // Read text colour :
-                char_pntr = comma[3] + 1; // : set char pointer to textcolour
-                // test on digit ??? <<<<<<<<<<<<
-                if (string_received[char_pntr] != ',' ) { // : colour field not empty
-                 SPD_MESSAGE.text_colour = atoi(&string_received[char_pntr]);
-                }
-                else SPD_MESSAGE.text_colour = 0;   // : empty field means colour 0
-             // Read textfield :
-                a = 0;
-                char_pntr = comma[4] + 1; // : set char pointer to textfield
-                while ( a < max_text_length-1 )
-                {    textchar = string_received[char_pntr];
-                     if (textchar == '*') break; // : do not include * in text
-                        SPD_MESSAGE.text[a] = textchar;
-                        a++;
-                        char_pntr++;
-                }
-                 SPD_MESSAGE.text[a] = '\0'; // : mark end of text
-                
-                // Print all for TESTING : ------------------------------------- <<<<<<<<<<<<<<<<<
-                USB.printf("text is : %s\n",SPD_MESSAGE.text );                 // show text  
-                USB.printf("fontsize is : %d\n",SPD_MESSAGE.font_size );        // show fontsize 
-                USB.printf("fontstyle is : %s\n",SPD_MESSAGE.font_style );      // show fontstyle )
-                USB.printf("textcolour is : %d\n",SPD_MESSAGE.text_colour );    // show textcolour 
-                // END of TEST printing ---------------------------------------- <<<<<<<<<<<<<<<<<
-                                
-                SPD_MESSAGE.updated = TRUE;  // : tell update of datastructure has been done
-                FSdata_updated_flag = TRUE;  // : set common update flag
-                SPD_MESSAGE.locked = FALSE; // : FREE datastructure for reading from it
-                                 
-          break;
-        }        
-        
-        case 5: // CLS message        
-        {       DO_CLR_SCREEN = TRUE;
-                FSdata_updated_flag = TRUE;  // : set common update flag
-          break;
-        }        
-        
-        case 6: // SID message        
-        {    // ---------------- locking ---------------------------------------------------
-             while (SID_MESSAGE.locked == TRUE) {
-                // --- DO NOTHING, WAIT FOR UNLOCKING !! ---
-                // TAKE CARE : Program can HANG if not unlocked !!
-             }
-                SID_MESSAGE.locked = TRUE; // : lock datastructure to prevent reading from it
-             // ----------------------------------------------------------------------------             
-             
-             // Read fontsize 0 - 9:
-               char_pntr = comma[1] + 2; // : set char pointer to digit after 'F'
-                //SID_MESSAGE.font_size = atoi(&string_received[char_pntr]);
-             // Read fontstyle :   
-                char_pntr = comma[2] + 1; // : set char pointer to fontstyle
-                //SID_MESSAGE.font_style = string_received[char_pntr];
-             // Read text colour :
-                char_pntr = comma[3] + 1; // : set char pointer to textcolour
-                if (string_received[char_pntr] != ',' ) { // : colour field not empty
-                //SID_MESSAGE.text_colour = atoi(&string_received[char_pntr]);
-                }
-                //else SID_MESSAGE.text_colour = 0;   // : empty field means colour 0
-             // Read textfield :
-              
-                
-                // Print all for TESTING : ------------------------------------- <<<<<<<<<<<<<<<<<
-                //USB.printf("text is : %s\n",SID_MESSAGE.text );                 // show text  
-                //USB.printf("fontsize is : %d\n",SID_MESSAGE.font_size );        // show fontsize 
-                //USB.printf("fontstyle is : %s\n",SID_MESSAGE.font_style );      // show fontstyle )
-                //USB.printf("textcolour is : %d\n",SID_MESSAGE.text_colour );    // show textcolour 
-                // END of TEST printing ---------------------------------------- <<<<<<<<<<<<<<<<<
-                                
-                SID_MESSAGE.updated = TRUE;  // : tell update of datastructure has been done
-                FSdata_updated_flag = TRUE;  // : set common update flag
-                SID_MESSAGE.locked = FALSE; // : FREE datastructure for reading from it
-                                 
-          break;
-        }        
-        
-        case 7: // LxT message        
-        {                    
-          
-          
-          FSdata_updated_flag = TRUE;  // : set common update flag
-          break;
-        }        
-        
-        case 8: // LxS message        
-        { 
-                           
-          FSdata_updated_flag = TRUE;  // : set common update flag
-          break;
-        }        
-        
-        case 9: // RxT message        
-        {                    
-          
-          FSdata_updated_flag = TRUE;  // : set common update flag
-          break;
-        }        
-        
-        case 10: // RxS message        
-        {                    
-          FSdata_updated_flag = TRUE;  // : set common update flag
-          break;
-        }               
-        
-        case 11: // PUTS message        
-        {                    
-          FSdata_updated_flag = TRUE;  // : set common update flag
-          break;
-        }        
-        
-        case 12: // SETV message        
-        {                    
-          FSdata_updated_flag = TRUE;  // : set common update flag
-          break;
-        }         
-        
-        case 13: // GETV message        
-        {                    
-          FSdata_updated_flag = TRUE;  // : set common update flag
-          break;
-        }               
-        
-        default:
-        
-        {   
-            // illegal commandnumber !
-          break;
-        }
-
-
-    } 
-}
--- a/decode_1.cpp	Sat Dec 08 20:37:05 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,185 +0,0 @@
-// 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;            
-              
-}
\ No newline at end of file
--- a/main.cpp	Sat Dec 08 20:37:05 2012 +0000
+++ b/main.cpp	Tue Jan 29 14:54:14 2013 +0000
@@ -1,58 +1,178 @@
 // L. van der Kolk, ELVEDEKA, Holland //
-// File:  main.cpp   version  1.0
+// File:  main.cpp  Mbos O.S testversion  
 
 #include "mbed.h"
-#include "TextLCD.h"
-TextLCD lcd(p8, p9, p10, p11, p12, p13); // rs, e, d4-d7
+#include "MODSERIAL.h"
+#include "mbos.h"
+
+//--------------------------------------------------------------------------------
+#include "TextLCD.h"                    //  < LCD connected for testing only << !
+TextLCD lcd(p8, p9, p10, p11, p12, p13); // rs, e, d4-d7 << for testing only << !
+//--------------------------------------------------------------------------------
+
+DigitalOut led1(LED1); // : LED 1 on Mbed board toggles when CDU is alive
  
-DigitalOut led1(LED1); // Led1, only for testing
-
-extern Serial USB;      // >>>>>>>>>>> alleen t.b.v TEST output !!
+extern MODSERIAL USB; 
+extern int key_hit_ID;
+int CDU_FS_interface = 0;   // : defines interface to FS: 0 = USB, 1 = Ethernet
 
 void init_USB();
-void init_alivetimer();
-void check_alive();
+void set_initial_CDU_screen();
+void Send_KEY_message(int button); 
+void Send_ALIVE_message(int seconds);
+void collect_FSdata();
+
+//----------------------------------------------------------------
+void init_buttons_Mbed(); // << for testing only << !
+void scan_buttons_Mbed(); // << for testing only << !
+//----------------------------------------------------------------
+
+// ALIVE_TASK properties:
+#define ALIVE_TASK_ID           1
+#define ALIVE_TASK_PRIO         50
+#define ALIVE_TASK_STACK_SZ     100    // : must be 100 to prevent stack overflow error!
+
+// ALIVE_TIMER properties:
+#define ALIVE_TIMER_ID          1
+#define ALIVE_TIMER_PERIOD      500  // = 5 sec
+
+// RECEIVE_DECODE_TASK properties:
+#define RECEIVE_DECODE_TASK_ID       2
+#define RECEIVE_DECODE_TASK_PRIO     80
+#define RECEIVE_DECODE_TASK_STACK_SZ 256  // : must be 256 to prevent stack overflow error!
+
+// SEND_KEYMESSAGE_TASK properties:
+#define SEND_KEYMESSAGE_TASK_ID      3
+#define SEND_KEYMESSAGE_TASK_PRIO    60
+#define SEND_KEYMESSAGE_TASK_STACK_SZ 100   // : must be 100 to prevent stack overflow error!
+
+//------------------------------ LvdK button scan --------------
+// SCAN_KEYS_TASK properties:  
+#define SCAN_KEYS_TASK_ID      4
+#define SCAN_KEYS_TASK_PRIO    85
+#define SCAN_KEYS_TASK_STACK_SZ 100
+
+// SCAN_KEYS_TIMER properties: ( LvdK button scan )
+#define SCAN_KEYS_TIMER_ID          2
+#define SCAN_KEYS_TIMER_PERIOD      50  // = 50msec 
+//----------------------------------------------------------------  
 
-void init_buttons_Mbed();
-void scan_buttons_Mbed(); 
+// Mbos EVENTS:
+#define ALIVE_EVENT     1      
+#define RX_EVENT        2    // : Event flag caused by serial RX interrupt
+#define KEY_EVENT       4
+#define SCAN_KEYS_TIMER_EVENT 8    // : LvdK button scan timer event
+
+// Mbos resources:
+#define USB_TX_RESOURCE      1     // : for locking USB TX buffer
+#define USB_TX_PRIO         95     // : USB TX resource priority ( = highest! )
+
+
+mbos CDU_OS( 8, 6, 5); //instantiate mbos O.S (tasks, timers, resources)
 
-void collect_decode_FSdata();
-void collect_and_send_FSdata();
-//void Braat_test_update_flag_van_LvdK_en_doet_wat_met_de_nieuwe_data();
+ // Task function prototypes:
+ void ALIVE_TASK(void);
+ void RECEIVE_DECODE_TASK(void);
+ void SCAN_KEYS_TASK(void);
+ void SEND_KEYMESSAGE_TASK(void);
+ 
+ void char_received(MODSERIAL_IRQ_INFO *q) {
+    // -- This functiom will be called on EACH rx interrupt --
+    // Note: this is Rx interrupt handling, NOT a mbos task.
+    // It only set an mbos Event.
+    CDU_OS.SetEvent(RX_EVENT,RECEIVE_DECODE_TASK_ID); // : set event flag for Read Serial Task
+}
+
+//------------ USB error handling ------------------------------------------------------------
+void rx_buf_overflow(MODSERIAL_IRQ_INFO *q) {
+    // This functiom will be called when RX buffer overflows
+    // Blue-Leds-of-Death will be showed, USB is dead so message itself will not be seen !
+    error("RX OVERFLOW !"); 
+}
+
+void tx_buf_overflow(MODSERIAL_IRQ_INFO *q) {
+    // This functiom will be called when TX buffer overflows
+    // Blue-Leds-of-Death will be showed, USB is dead so message itself will not be seen !
+    error("TX OVERFLOW !");  
+}
+//---------------------------------------------------------------------------------------------
+
+void init_USB()
+{   // -- This function controls the setup for the CDU-FS USB communication --
+    // Function char_received() is attached to Rx interrupt.
+    USB.baud(38400); // allways set default baudrate to 38400
+    USB.attach(&char_received, MODSERIAL::RxIrq);    // : attach function char_received to RX inerrupt
+    USB.attach(&rx_buf_overflow, MODSERIAL::RxOvIrq); //: attach function to call when RX buffer overflows
+    USB.attach(&tx_buf_overflow, MODSERIAL::TxOvIrq); //: attach function to call when TX buffer overflows
+}
  
 main()
 {
-// -- This is main.cpp created by LvdK to test all FS communication --
-
-    init_USB();  // : setup USB communication and Rx interrupt
-    init_alivetimer();    // : setup alive message timer
-    init_buttons_Mbed();   // : <<<<<<<<<<<<<<<<<<<<< test only
-    
-    lcd.cls(); // : clear LCD display
-    lcd.printf(" -- Mbed CDU --\n");
-    USB.printf(" --- Mbed CDU ---\n");
-    
-        
-    while (1==1) { // this is my endless main loop
+  init_USB();  // : setup USB communication 
+  set_initial_CDU_screen();
+  
+  //------------------ for testing only ! ----------------------------------
+  init_buttons_Mbed();   
+  lcd.cls(); // : clear LCD display
+  lcd.printf("CDU USB Mbos\n");
+  USB.printf("\n--- CDU USB Mbos ---\n");
+  //------------------------------------------------------------------------
     
-        led1 = !led1; // : toggle LED1 to show running program
-        //wait(0.02);    
-        
-        // Check on new received characters from FS in filled Rx ringbuffer
-        // and decode to data if relevant:
-        collect_decode_FSdata();
-        
-        // Proces new received FS data:
-        // Braat_test_update_flag_van_LvdK_en_doet_wat_met_de_nieuwe_data();
-                
-        // Send relevant data to FS:
-        // LvdK test updates van data Braat en leest de data uit afgesproken datagebied
-        // en stuurt dit naar de FS:
-        scan_buttons_Mbed();
-        
-        collect_and_send_FSdata();
-        
-        check_alive(); // : if 5 sec passed, send alive message
-        
+  // Configure all tasks, timers and resources:
+  CDU_OS.CreateTask(ALIVE_TASK_ID, ALIVE_TASK_PRIO, ALIVE_TASK_STACK_SZ, ALIVE_TASK);
+  CDU_OS.CreateTask(RECEIVE_DECODE_TASK_ID, RECEIVE_DECODE_TASK_PRIO, RECEIVE_DECODE_TASK_STACK_SZ, RECEIVE_DECODE_TASK);
+  CDU_OS.CreateTask(SEND_KEYMESSAGE_TASK_ID, SEND_KEYMESSAGE_TASK_PRIO, SEND_KEYMESSAGE_TASK_STACK_SZ, SEND_KEYMESSAGE_TASK);  
+  CDU_OS.CreateTask(SCAN_KEYS_TASK_ID, SCAN_KEYS_TASK_PRIO, SCAN_KEYS_TASK_STACK_SZ, SCAN_KEYS_TASK );
+  CDU_OS.CreateTimer(ALIVE_TIMER_ID, ALIVE_TASK_ID, ALIVE_EVENT);  
+  CDU_OS.CreateTimer(SCAN_KEYS_TIMER_ID, SCAN_KEYS_TASK_ID, SCAN_KEYS_TIMER_EVENT);    
+  CDU_OS.CreateResource(USB_TX_RESOURCE, USB_TX_PRIO);
+  //CDU_OS.CreateTask(TASK4_ID, TASK4_PRIO, TASK4_STACK_SZ, task4);
+     
+  // Start mbos O.S with final statement in main() :
+  CDU_OS.Start(); // : will never return!    
+}        
+
+void RECEIVE_DECODE_TASK(void)
+{    // Task waits for RX_EVENT, then 
+     // collects and decodes data from FS
+    while(1){  
+        CDU_OS.WaitEvent(RX_EVENT); // : wait for RX event
+        collect_FSdata();
+    } 
+}
+
+void SEND_KEYMESSAGE_TASK(void) 
+{
+    while(1){  
+        CDU_OS.WaitEvent(KEY_EVENT); // : wait for KEY event ( = CDU key pressed )
+        Send_KEY_message(key_hit_ID);
+        key_hit_ID = 0;  // : reset Id flag
     }
-}
\ No newline at end of file
+}
+
+void ALIVE_TASK(void)
+{   // Mbos task started by mbos timer to send ALIVE message to FS every 5 sec.
+    CDU_OS.SetTimer( ALIVE_TIMER_ID, ALIVE_TIMER_PERIOD, ALIVE_TIMER_PERIOD );
+    static int seconds = 0;
+    while(1){  // : loop forever because it is a mbos taskfunction
+        CDU_OS.WaitEvent(ALIVE_EVENT ); // : wait for Mbos timer event (5 sec)
+        seconds += 5 ; // : increase seconds by 5 
+        if (seconds >= 60) seconds = 0 ;
+        Send_ALIVE_message(seconds);
+        led1 = !led1;
+    }
+}     
+
+// ------------- LvdK button scan, test only -------------------------------------------------
+void SCAN_KEYS_TASK(void) // : Test if button pressed
+{     // Will be task written by W.B
+      // Is now LvdK test Task, scans some buttons attached to Mbed board.
+      // Task scans buttons every 50msec.
+    CDU_OS.SetTimer( SCAN_KEYS_TIMER_ID, SCAN_KEYS_TIMER_PERIOD, SCAN_KEYS_TIMER_PERIOD );
+  
+    while(1){  // : loop forever because it is a mbos taskfunction
+        CDU_OS.WaitEvent(SCAN_KEYS_TIMER_EVENT );   // : scan buttons every 50msec
+        scan_buttons_Mbed(); 
+    } 
+}
+// -------------------------------------------------------------------------------------------
\ No newline at end of file
--- a/mbed.bld	Sat Dec 08 20:37:05 2012 +0000
+++ b/mbed.bld	Tue Jan 29 14:54:14 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/e2ed12d17f06
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/63cdd78b2dc1
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbos.lib	Tue Jan 29 14:54:14 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AndrewL/code/mbos/#cf660b28b2a4