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:
Mon Nov 26 12:45:02 2012 +0000
Child:
1:b3553e2842ee
Commit message:
basis

Changed in this revision

USB_receive.cpp Show annotated file Show diff for this revision Revisions of this file
USB_setup.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USB_receive.cpp	Mon Nov 26 12:45:02 2012 +0000
@@ -0,0 +1,88 @@
+// L. van der Kolk, ELVEDEKA, Holland //
+
+#include "mbed.h"
+
+#define TRUE  1
+#define FALSE 0
+#define max_rx_buffer 100
+
+DigitalOut led2(LED2);  // TEST Led2
+DigitalOut led3(LED3);  // TEST Led3
+
+extern Serial USB;
+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
+
+void char_received()
+{   // -- This functiom will be called on each RX interrupt --
+    // it fills the RX buffer and sets the string_complete flag when a string
+    // starting with $ and ending with CR/LF has been received.
+    char rx_char;
+    static int $_detected = FALSE;      // : no begin of string detected
+    static int rx_buf_pntr = 0;         // : bufferpointer at begin of buffer 
+
+    led2 = !led2;           // : toggle LED2 to show 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++;
+    
+    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 ( rx_char == '\n' && $_detected == TRUE) {
+        string_complete = TRUE;
+        strncpy(string_received,rx_buf,rx_buf_pntr); 
+     
+        // Decode string:
+                       
+        //USB.printf("%s",string_received);  // show string ( TEST !)
+        
+        led3 = 1;       // ( TEST ! )
+        
+        decode_string();
+                  
+        led3 = 0;       // ( TEST ! )
+        
+        // make ready for receiving new command:
+        $_detected = FALSE;
+        string_complete = FALSE;
+        rx_buf_pntr = 0;
+    }        
+}
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+   
+    
+    
+    
+    
+    
+    
+       
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USB_setup.cpp	Mon Nov 26 12:45:02 2012 +0000
@@ -0,0 +1,14 @@
+#include "mbed.h"
+
+Serial USB(USBTX, USBRX); // : declare USB serial communication on Mbed defined USB TX and RX lines
+
+void char_received();
+
+void init_USB()
+{   // -- This function is the setup for the USB communication --
+    USB.baud(38400); // set initial baudrate to 38400
+    
+    //Attach function char_received  to be called when a serial
+    //receive interrupt is received:
+    USB.attach(&char_received);    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/decode_1.cpp	Mon Nov 26 12:45:02 2012 +0000
@@ -0,0 +1,146 @@
+#include "mbed.h"
+
+#define TRUE  1
+#define FALSE 0
+
+//#define command_header "$PCDU"
+
+
+DigitalOut led4(LED4);  // TEST Led4
+
+extern char string_received[]; // : complete string starting with $ and ending with CR/LF
+extern int  string_complete;
+extern Serial USB;  // >>>>>>>>>>> alleen t.b.v TEST output !!
+
+int command_valid;
+int checksum_error;
+char command_string[20];  // <<<<<<<<<< ?
+int command_number;
+
+
+// Commands 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 command 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 function checks the received string --
+
+    // char *first_comma_pntr;
+
+    int char_cntr,i;
+    int equal;
+    int command_possible;
+    int command_number;
+    char key_id;
+
+    if (string_complete == TRUE) {
+       led4 = 1;
+        command_possible = FALSE;
+        command_valid = FALSE;
+        key_id = '0';
+
+        // Check checksum:
+        // ---- komt nog --------------------------
+
+        // Check on 5 char "$PCDU" header:
+        equal = strncmp(string_received,"$PCDU",5);
+        if (equal != 0) {
+            command_possible = FALSE;
+            USB.printf("header is not $PCDU but : %5s\n",string_received );  // print first 5 chars ( TEST !)
+        }
+        else {
+            // find first comma :
+            //first_comma_pntr = strchr(string_received,',');
+            //if (first_comma_pntr != NULL) {
+            //USB.printf("aantal char voor eerste komma : %d\n",first_comma_pntr - string_received );  // show string ( TEST !)
+            //}
+
+            // 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.
+                command_possible = TRUE;
+                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 ! )
+
+            }
+            else if (char_cntr == 8) { // : command found with 3 char.
+                command_possible = TRUE;
+                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 ! )
+                }
+                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 (key_id != '0') USB.printf("gevonden key commando is : %d, key %c \n",i, key_id );  // show command3 number and key (TEST ! )
+                } // end of special test <<<<<<<<<<<<
+
+             }// end of char 3 test <<<<<<<<<<
+
+        }// end of header test <<<<<<<<<<
+        led4 = 0;
+    } // end of string complete test
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Nov 26 12:45:02 2012 +0000
@@ -0,0 +1,17 @@
+#include "mbed.h"
+ 
+DigitalOut led1(LED1); // TEST Led1
+
+
+void init_USB();
+ 
+main()
+{
+
+    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 ???????????
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Nov 26 12:45:02 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ed12d17f06
\ No newline at end of file