Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Revision:
24:099df3fa2b13
Parent:
23:2e914b705b99
Child:
25:57f9e3273979
diff -r 2e914b705b99 -r 099df3fa2b13 SPS/SPS.cpp
--- a/SPS/SPS.cpp	Tue May 16 09:33:12 2017 +0000
+++ b/SPS/SPS.cpp	Wed May 31 07:02:02 2017 +0000
@@ -10,7 +10,7 @@
     currentString = "";
     currentErrString = "";
     lastString = "";
-    headerString = "/tag identifier_id group_id date time zzz | source_id interpreter_id data_line_version encoding source_firmware_version interpreter_firmware_version | barcode latitude longitude gpsFixFlag batteryvoltage mag_time mag_nt mag_sq checksum";
+    headerString = "";
     crc_tab16_init = 0;
     errStatus = true;
            
@@ -84,18 +84,26 @@
     addToCurrentString(SPACE);
     addToCurrentString(barcode);
     addToCurrentString(SPACE);
-    addToCurrentString(latitude);
+    if(gpsFixFlag){
+        addToCurrentString(latitude);    
+    }
+    if(!gpsFixFlag){
+        addToCurrentString("NaN");    
+    }  
     addToCurrentString(SPACE);
-    addToCurrentString(longitude);
-    addToCurrentString(SPACE);
-    
+    if(gpsFixFlag){
+        addToCurrentString(longitude);    
+    }
+    if(!gpsFixFlag){
+        addToCurrentString("NaN");    
+    } 
+    addToCurrentString(SPACE); 
     if(gpsFixFlag){
         addToCurrentString('1');    
     }
     if(!gpsFixFlag){
         addToCurrentString('0');
     }
-    
     addToCurrentString(SPACE);
     addToCurrentString(batteryvoltage);
     addToCurrentString(SPACE);
@@ -125,7 +133,81 @@
     addToCurrentString(LINESTOP); 
 };
 
+//Updates current header string
+void SPS::UpdateHeaderString(string barcode, string identifier_id, string group_id, string ZZZ, string encoding,string source_id, string interpreter_id, string data_line_version, string source_firmware_version, string interpreter_firmware_version){
 
+    this->headerString = "";
+    this->headerString.resize(256);
+
+    int strLength = 0;
+    char checkSum[5];
+    memset(checkSum,'\0',5);
+    unsigned short chkSum = 0;
+    
+    addToCurrentHeaderString(LINESTART);
+    addToCurrentHeaderString("RMRK");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(identifier_id);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(group_id);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("0000/00/00");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("00:00:00.000");
+    addToCurrentHeaderString(SPACE); 
+    addToCurrentHeaderString("ZZZ");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(HEADEREND);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(source_id);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(interpreter_id);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("01");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(encoding);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(source_firmware_version);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(interpreter_firmware_version);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(HEADEREND);
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("Barcode");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("Latitude");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("Longitude");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("gpsFixFlag");   
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("batteryVoltage");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("mag_time");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("mag_value");
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString("mag_quality");
+    
+    
+    strLength = strlen(this->headerString.c_str());
+    
+    //calculate checksum
+    for(int i = 0; i < strLength; i++){
+        if(this->headerString[i] != '>'){
+            
+            chkSum = update_crc_16(chkSum, this->headerString[i]);
+            
+        }         
+    }
+    
+    sprintf(checkSum, "%04X", chkSum);
+      
+    //append rest of string
+    addToCurrentHeaderString(SPACE);
+    addToCurrentHeaderString(checkSum);
+    addToCurrentHeaderString(LINESTOP);             
+}
 
 //! UpdateCurrentErrString, sps ERR string creation method, taking data and info as parameters.
 /*! 
@@ -159,6 +241,7 @@
     char checkSum[5];
     memset(checkSum,'\0',5);
     unsigned short chkSum = 0;
+    int strLength = 0;
     
     addToCurrentErrString(LINESTART);
     addToCurrentErrString(tag);
@@ -171,22 +254,21 @@
     addToCurrentErrString(SPACE);
     addToCurrentErrString(time);
     addToCurrentErrString(SPACE); 
-    addToCurrentErrString("ZZZ");
-    addToCurrentString(SPACE);
-    addToCurrentString(HEADEREND);
-    addToCurrentString(SPACE);
-    addToCurrentString(source_id);
-    addToCurrentString(SPACE);
-    addToCurrentString(interpreter_id);
-    addToCurrentString(SPACE);
-    addToCurrentString(data_line_version);
-    addToCurrentString(SPACE);
-    addToCurrentString(encoding);
-    addToCurrentString(SPACE);
-    addToCurrentString(source_firmware_version);
-    addToCurrentString(SPACE);
-    addToCurrentString(interpreter_firmware_version);
-    addToCurrentString(SPACE);
+    addToCurrentErrString("NOC");
+    addToCurrentErrString(SPACE);
+    addToCurrentErrString(HEADEREND);
+    addToCurrentErrString(source_id);
+    addToCurrentErrString(SPACE);
+    addToCurrentErrString(interpreter_id);
+    addToCurrentErrString(SPACE);
+    addToCurrentErrString("01");
+    addToCurrentErrString(SPACE);
+    addToCurrentErrString(encoding);
+    addToCurrentErrString(SPACE);
+    addToCurrentErrString(source_firmware_version);
+    addToCurrentErrString(SPACE);
+    addToCurrentErrString(interpreter_firmware_version);
+    addToCurrentErrString(SPACE);
     addToCurrentErrString(HEADEREND);
     addToCurrentErrString(SPACE);
     addToCurrentErrString("BMAG");
@@ -195,9 +277,11 @@
     addToCurrentErrString(SPACE);
     addToCurrentErrString("C03");
     
+    strLength = strlen(this->currentErrString.c_str());
+    
     //calculate checksum
-    for(int i = 0; i < strlen(this->currentString.c_str()); i++){
-        if(this->currentString[i] != '>' || this->currentString[i] != '<'){
+    for(int i = 0; i < strLength; i++){
+        if(this->currentErrString[i] != '>'){
             
             chkSum = update_crc_16(chkSum, this->currentErrString[i]);
             
@@ -332,6 +416,32 @@
     this->currentString[currentStartIndex] = data;          
 };
 
+
+void SPS::addToCurrentHeaderString(string data){
+    int currentStartIndex = strlen(this->headerString.c_str());
+    char dataStrLen = 0;
+    int index = 0;
+    
+    dataStrLen = strlen(data.c_str());
+    
+    for(int i = 0; i < dataStrLen; i++){
+        
+        if((this->headerString[currentStartIndex+i] != '\r') || (this->headerString[currentStartIndex+i] != '\n')){
+            
+            this->headerString[currentStartIndex+i] = data[index];
+            index += 1;
+                  
+        }
+                     
+    }                
+};
+
+
+void SPS::addToCurrentHeaderString(char data){
+    int currentStartIndex = strlen(this->headerString.c_str());
+    this->headerString[currentStartIndex] = data;          
+};
+
 //! addToCurrentErrString, appends string to the sps err_string currently being manipulated.
 /*! 
     \param data: data string