replaces CDU_Mbed_26

Dependencies:   4DGL MODSERIAL mbed mbos

Fork of CDU_Mbed_26 by Engravity-CDU

Files at this revision

API Documentation at this revision

Comitter:
LvdK
Date:
Wed Nov 28 14:24:08 2012 +0000
Parent:
2:cdc3ccd10040
Child:
4:c23c570e4454
Commit message:
Stabiele versie met rx ringbuffer

Changed in this revision

USB_receive.cpp Show annotated file Show diff for this revision Revisions of this file
data_structures.cpp Show annotated file Show diff for this revision Revisions of this file
decode_1.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/USB_receive.cpp	Mon Nov 26 16:38:37 2012 +0000
+++ b/USB_receive.cpp	Wed Nov 28 14:24:08 2012 +0000
@@ -4,74 +4,99 @@
 
 #define TRUE  1
 #define FALSE 0
-#define max_rx_buffer 100
+#define max_rx_buffer 500  // : 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;
+extern Serial USB;      // >>>>>>>>>>> alleen t.b.v TEST output !!
 void decode_string();
 
-char rx_buf[max_rx_buffer + 1];  //  !!!// : main serial receiving buffer
-char string_received[max_rx_buffer + 1]; //  !!!// : string starting with $ and ending with CR/LF
-int  string_complete = FALSE; // : no complete string received with $ and CR/LF
+// 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, '0'};   // : initialize pointers and 'new_data' 
 
-void char_received()
-{   // -- This functiom will be called on each RX interrupt --
-    // It fills the RX buffer and sets the string_complete flag TRUE when a string
-    // starting with $ and ending with CR/LF has been received with at least 10 characters.
-    char rx_char;
-    static int $_detected = FALSE;      // : no begin of string detected, ONLY first call init !
-    static int rx_buf_pntr = 0;         // : bufferpointer at begin of buffer, ONLY first call init !
+
+char string_received[max_string_length + 1]; //  : contains a string starting with $ and ending with CR/LF
+int  string_complete = FALSE; // : no complete string with $ and CR/LF
 
-    led2 = 1    ;           // : set LED2 ON to show start of RX interrupt ( TEST ! )
-    rx_char = USB.getc();   // : reading also clears receive interrupt
-    USB.putc(rx_char);      // : echo character back to PC ( TEST ! ) 
-    
-    
-    
-    
-    
-    if ( rx_char == '$' && $_detected == FALSE ){
-        $_detected = TRUE;  // : begin of string detected ( $ )
-        rx_buf_pntr = 0;    // : set pointer to begin of buffer again
-    }
-    rx_buf[rx_buf_pntr] = rx_char;
-    rx_buf_pntr++;
+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.
+     // Also collects strings from read data, starting with $ and ending with CR/LF
+     // Strings shorter than min_string_length are not collected.
+    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 (rx_buf_pntr == max_rx_buffer) {
-        // receive buffer has overflow, no valid command possible.
-        // start all over again:
-        rx_buf_pntr = 0; // : RX buffer overflow protection
-        $_detected = FALSE;
-        string_complete = FALSE;
-    }
+        if (string_pntr == max_string_length) {
+            // command string is too long,
+            // start all over again:
+            string_pntr = 0;  // : set pointer to begin of string_received[] buffer again
+            $_detected = FALSE;
+        }
     
-    if ( rx_char == '\n' && $_detected == TRUE ) { 
-        // String is valid when:
-        // it starts with '$'
-        // it ends on CR
-        // it has at least 10 characters 
-        
-        if ( rx_buf_pntr > 10 ) { // : check minimum length
-        strncpy(string_received,rx_buf,rx_buf_pntr);
-        string_complete = TRUE;     // : set flag !
-        // Make ready for receiving new command:
-            $_detected = FALSE;
-            rx_buf_pntr = 0;
-             
-        //led3 = 1;       // ( TEST ! )
-        
-        // decode_string();  decode_string() in MAIN now!
-        
-        //led3 = 0;       // ( TEST ! )
-
-        }
-        else  rx_buf_pntr = 0; 
-        
+        if ( rx_char == '\n' && $_detected == TRUE ) { 
+      
+            if ( string_pntr > min_string_length ) { // : check minimum string length 
+                // String is valid when:
+                // it starts with '$' AND
+                // it ends on new-line  AND
+                // it has at least min_string_length  
+                  
+           
+                USB.printf("string_received : %s\n",string_received );  // show string >>>>>>>>>>>>>>>>( TEST !)
+           
+                string_complete = TRUE;     // : set flag for decoder !  <<<<<<<<<<<<<<<<<   NODIG ????????????
+                decode_string();    // : call decoder to proces complete string received
+                
+                // Make ready for receiving new command:
+                $_detected = FALSE;
+                string_pntr = 0;  // : set pointer to begin of string_received[] buffer again
+            }
+            else  string_pntr = 0; // : set pointer to begin of string_received[] buffer again
+         }
     } 
     
-    led2 = 0;           // : set LED2 OFF to show end of RX interrupt ( TEST ! )       
+    // led2 = 0;           // : set LED2 OFF to show end of RX interrupt ( TEST ! )       
 }
     
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data_structures.cpp	Wed Nov 28 14:24:08 2012 +0000
@@ -0,0 +1,132 @@
+#include "mbed.h"
+//#include "ctype.h"
+
+#define TRUE  1
+#define FALSE 0
+
+
+
+// ---------- Data structures of received FS data---------------------------------------------------------
+
+struct {    char  text[40];         //  : maintext max. 40 ? chars    
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag,  TRUE/FALSE
+        }  KEY_LEFT_MAINTEXT[10];   //  : keys can be 0 - 9
+        
+struct {    char  text[40];         //  : subtext max. 40 ? chars    
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag,  TRUE/FALSE
+        }  KEY_LEFT_SUBTEXT[10];    //  : keys can be 0 - 9  
+
+struct {    char  text[40];         //  : maintext max. 40 ? chars    
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag,  TRUE/FALSE
+        }  KEY_RIGHT_MAINTEXT[10];  //  : keys can be 0 - 9
+
+struct {    char  text[40];         //  : subtext max. 40 ? chars    
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag,  TRUE/FALSE
+        }  KEY_RIGHT_SUBTEXT[10];   //  : keys can be 0 - 9
+        
+struct {    char  text[40];         //  : text max. 40 ? chars    
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag,  TRUE/FALSE
+        }  DBS_MESSAGE;      
+        
+struct {    char  text[40];         //  : text max. 40 ? chars    
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag,  TRUE/FALSE
+        }  TITLE_MESSAGE;  
+        
+struct {    char  text[4];          //  : text max. 4 ? chars    
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag,  TRUE/FALSE
+        }  PAGE_NR_MESSAGE; 
+        
+struct {    char  text[40];         //  : text max. 40 ? chars    
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag, TRUE/FALSE
+        }  SCRATCH_MESSAGE;                      
+        
+struct {    char  text[40];         //  : text max. 40 ? chars 
+            int   x_pos;            //  :  
+            int   y_pos;            //  :
+            int   text_font;        //  : font can be 1 – 8            
+            char  font_style;       //  : can be S,B,I,U,N         
+            int   text_colour;      //  : colour number 
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag, TRUE/FALSE
+        }  PUT_STRING_MESSAGE; 
+        
+struct {    char  text[8];          //  : text max. 8 ? chars 
+            int   status_id;        //  : can be 0 or 1           
+            int   updated;          //  : update flag, TRUE/FALSE
+            int   locked;           //  : locked flag, TRUE/FALSE
+        }  STATUS_MESSAGE;
+        
+struct {    char  text[8];          //  : text max. 8 ? chars 
+            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;                  // : update flag, TRUE/FALSE
+// -----------------------------------------------------------------------------
+
+
+void (read_datafields(int command_number)) {
+
+    switch ( command_number )
+    {
+        case 0:  
+        {
+        
+          break;
+        }
+        
+        case 1:  
+        { // DSB command:
+        
+          break;
+        }
+        
+        case 2:
+        {
+        
+          break;
+        }
+        
+        default:
+        {   
+        
+          break;
+        }
+
+
+    } // end switch
+}
--- a/decode_1.cpp	Mon Nov 26 16:38:37 2012 +0000
+++ b/decode_1.cpp	Wed Nov 28 14:24:08 2012 +0000
@@ -45,7 +45,7 @@
 
 void decode_string()
 {
-    // -- This function checks the received string --
+    // -- This function checks a received string --
 
     // char *first_comma_pntr;
 
@@ -151,16 +151,18 @@
             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 string complete test
 
             
- string_complete = FALSE;  // : reset flag again, because string has been analyzed
+ string_complete = FALSE;  // : reset flag again, because total string has been analyzed
  led4 = 0;            
               
 }
\ No newline at end of file
--- a/main.cpp	Mon Nov 26 16:38:37 2012 +0000
+++ b/main.cpp	Wed Nov 28 14:24:08 2012 +0000
@@ -4,7 +4,8 @@
 
 
 void init_USB();
-void decode_string();
+//void decode_string();
+void collect_decode_FSdata();
  
 main()
 {
@@ -12,8 +13,9 @@
     init_USB();  // setup USB communication
         
     while (1) { // this is my endless main loop :
-        led1 = !led1; // : toggle LED1 to show this loop
-        wait(0.5);    // : can be interrupted ???????????
-        decode_string();    // : call decoder to proces any valid command string received
+        led1 = !led1; // : toggle LED1 to show running program
+        wait(0.1);    // : can be interrupted ???????????
+        // decode_string();    // : call decoder to proces any valid command string received
+        collect_decode_FSdata();
     }
 }
\ No newline at end of file