Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Revision:
5:11782a2008c2
Parent:
4:c70ef089a3fd
Child:
6:6d1683c8b26b
--- a/SPS/SPS.cpp	Thu Mar 02 14:04:56 2017 +0000
+++ b/SPS/SPS.cpp	Thu Mar 09 12:03:55 2017 +0000
@@ -5,65 +5,88 @@
     currentString = "";
     currentErrString = "";
     headerString = "/tag identifier_id group_id date time zzz | s1 source_id interpreter_id data_line_version encoding source_firmware_version interpreter_firmware_version | latitude longitude gpsFixFlag mag_time mag_nt mag_sq checksum";
+    crc_tab16_init = 0;
+    errStatus = true;
            
 };
 
-void SPS::UpdateCurrentString(string tag, string identifier_id, string group_id, string date, string time, string ZZZ,string s1, string source_id, string interpreter_id, string data_line_version, string encoding, string source_firmware_version, string interpreter_firmware_version, string latitude, string longitude, char gpsFixFlag, string mag_time, string mag_nt, string mag_sq){
+void SPS::UpdateCurrentString(string tag, string identifier_id, string group_id, string date, string time, string ZZZ,string s1, string source_id, string interpreter_id, string data_line_version, string source_firmware_version, string interpreter_firmware_version, string latitude, string longitude, bool gpsFixFlag, string mag_time, string mag_nt, string mag_sq, Serial * dbg){
 
     currentString = "";
-    string checkSum = "";
+    currentString.resize(256);
+    char checkSum[5];
+    memset(checkSum,'\0',5);
+    unsigned short chkSum = 0;
     
+    dbg->printf("Commencing sps line generation\r\n");
     //create current string
-    currentString.push_back(LINESTART);
-    currentString.append(tag);
-    currentString.push_back(SPACE);
-    currentString.append(identifier_id);
-    currentString.push_back(SPACE);
-    currentString.append(group_id);
-    currentString.push_back(SPACE);
-    currentString.append(date);
-    currentString.push_back(SPACE);
-    currentString.append(time);
-    currentString.push_back(SPACE); 
-    currentString.append("ZZZ");
-    currentString.push_back(SPACE);
-    currentString.push_back(HEADEREND);
-    currentString.push_back(SPACE);
-    currentString.append(s1);
-    currentString.push_back(SPACE);
-    currentString.append(source_id);
-    currentString.push_back(SPACE);
-    currentString.append(interpreter_id);
-    currentString.push_back(SPACE);
-    currentString.append(data_line_version);
-    currentString.push_back(SPACE);
-    currentString.append(encoding);
-    currentString.push_back(SPACE);
-    currentString.append(source_firmware_version);
-    currentString.push_back(SPACE);
-    currentString.append(interpreter_firmware_version);
-    currentString.push_back(SPACE);
-    currentString.push_back(HEADEREND);
-    currentString.push_back(SPACE);
-    currentString.append(latitude);
-    currentString.push_back(SPACE);
-    currentString.append(longitude);
-    currentString.push_back(SPACE);
-    currentString.push_back(gpsFixFlag);
-    currentString.push_back(SPACE);
-    currentString.append(mag_time);
-    currentString.push_back(SPACE);
-    currentString.append(mag_nt);
-    currentString.push_back(SPACE);
-    currentString.append(mag_sq);
+    addToCurrentString(LINESTART);
+    addToCurrentString(tag);
+    addToCurrentString(SPACE);
+    addToCurrentString(identifier_id);
+    addToCurrentString(SPACE);
+    addToCurrentString(group_id);
+    addToCurrentString(SPACE);
+    addToCurrentString(date);
+    addToCurrentString(SPACE);
+    addToCurrentString(time);
+    addToCurrentString(SPACE); 
+    addToCurrentString("ZZZ");
+    addToCurrentString(SPACE);
+    addToCurrentString(HEADEREND);
+    addToCurrentString(SPACE);
+    addToCurrentString(s1);
+    addToCurrentString(SPACE);
+    addToCurrentString(source_id);
+    addToCurrentString(SPACE);
+    addToCurrentString(interpreter_id);
+    addToCurrentString(SPACE);
+    addToCurrentString(data_line_version);
+    addToCurrentString(SPACE);
+    addToCurrentString(source_firmware_version);
+    addToCurrentString(SPACE);
+    addToCurrentString(interpreter_firmware_version);
+    addToCurrentString(SPACE);
+    addToCurrentString(HEADEREND);
+    addToCurrentString(SPACE);
+    addToCurrentString(latitude);
+    addToCurrentString(SPACE);
+    addToCurrentString(longitude);
+    addToCurrentString(SPACE);
+    
+    if(gpsFixFlag){
+        addToCurrentString('1');    
+    }
+    if(!gpsFixFlag){
+        addToCurrentString('0');
+    }
+    
+    addToCurrentString(SPACE);
+    addToCurrentString(mag_time);
+    addToCurrentString(SPACE);
+    addToCurrentString(mag_nt);
+    addToCurrentString(SPACE);
+    addToCurrentString(mag_sq);
+    
+    dbg->printf("CurrentStringBeforeChecksumCalc: ");
+    dbg->printf(currentString.c_str());
+    dbg->printf("\r\n");
     
     //calculate checksum
-    checkSum = this->checkSumCalc();
+    for(int i = 0; i < strlen(currentString.c_str()); i++){
+        if(currentString[i] != '>' || currentString[i] != '<'){
+            
+            chkSum = update_crc_16(chkSum, currentString[i]);
+            
+        }         
+    }
+    
+    sprintf(checkSum, "%4x", chkSum);
     
     //append rest of string
-    currentString.push_back(SPACE);
-    currentString.append(checkSum);
-    currentString.push_back(LINESTOP);
+    addToCurrentString(SPACE);
+    addToCurrentString(checkSum);
+    addToCurrentString(LINESTOP);
  
 };
 
@@ -109,4 +132,41 @@
 
     return crc;
 };
-//***************************************************************************
\ No newline at end of file
+
+
+//Gets / returns current sps data string
+string SPS::getCurrentString(void){
+    return this->currentString; 
+};
+
+
+//Get error status
+bool SPS::getErrStatus(void){
+    return this->errStatus;    
+}; 
+  
+//Set err status
+void SPS::setErrStatus(bool status){
+    this->errStatus = status;
+};
+
+//get headerstring
+string SPS::getHeaderString(void){
+    return this->headerString;    
+};
+
+//add data to current string
+void SPS::addToCurrentString(string data){
+    int currentStartIndex = strlen(currentString.c_str());
+    char dataStrLen = 0;
+    dataStrLen = strlen(data.c_str());
+    
+    for(int i = 0; i < dataStrLen; i++){
+        currentString[currentStartIndex+i] = data[i];               
+    }                
+}
+
+void SPS::addToCurrentString(char data){
+    int currentStartIndex = strlen(currentString.c_str());
+    currentString[currentStartIndex] = data;          
+};
\ No newline at end of file