Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Revision:
6:6d1683c8b26b
Parent:
5:11782a2008c2
Child:
7:872984a67d5b
--- a/main.cpp	Thu Mar 09 12:03:55 2017 +0000
+++ b/main.cpp	Thu Mar 09 15:26:22 2017 +0000
@@ -6,20 +6,21 @@
 bool GPS_Data_Valid = false;
 bool GGA_Fix_Present = false;
 bool firstLineWritten = false;
+bool fileNameUpdated = false;
 
-//global system variables,at the moment, only the currently always true "run variable"
+//global system variables
 bool run = true;
-int writecount = 0;
-int dataRWComplete = 0;
 
 //global tmpGpsRxString
 char tmpGpsRxString[128];
 
-
 //Write to file prototype
-bool writeToUsb(string line, FILE * f, Serial * dbg);
+bool writeToUsb(string line, FILE * f);
 
 int main(void){
+    //initializing watchdog
+    Watchdog wd;
+    wd.init(10.0);   
     
     string currentFilename, nextFilename; 
     
@@ -29,43 +30,58 @@
     string GPS_String_Buff;
     GPS_String_Buff.resize(128);
     memset(tmpGpsRxString,'\0',128);
-        
-    //GPS communication init
-    gps.baud(9600);
-    gps.attach(&rxCallback, MODSERIAL::RxIrq);
     
     //debug comm setup
     dbg.baud(115200);
+    dbg.printf("Init...\r\n");
     
     //setting up USB device
     USBHostMSD msd("usb");
-    msd.connect();
     
     while(!msd.connect()){
         dbg.printf("Trying to connect to usb flash disk\r\n");
-        
-        wait_ms(500);       
+        wait_ms(500);      
     }
     
     //Opening a file on usb disk
     FILE * fp = fopen(currentFilename.c_str(), "a");
     wait_ms(100);
     
-    //Start comms with the GPS, Enabling GPRMC and  GPGGA
-    gps.printf("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n");
-
-    //initializing watchdog
-    Watchdog wd;
-    wd.init(1.0);
-    
     //initializing SPS generation 
     SPS spsGen;
-            
+    
+    //GPS communication init
+    gps.baud(9600);
+    gps.attach(&rxCallback, MODSERIAL::RxIrq);
+    
+    //Start comms with the GPS, Enabling GPRMC and  GPGGA
+    gps.printf("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n");   
+    
+    dbg.printf("Init.... Done!\r\n");        
     //infinite loop running after initialization
     while(run) {
         
+        //if the gps rx buffer is full, flush the buffer
+        if(gps.rxBufferFull()){
+            gps.rxBufferFlush();    
+        }
+        
         if(GPS_Data_Rdy){
             
+            //update filename when date is available with gga fix
+            if(!fileNameUpdated && GGA_Fix_Present){
+                spsGen.generateSpsFilename(gpsNMEA.currentDATEFromGPRMC);
+                nextFilename.assign(spsGen.getSpsFileName());
+                fclose(fp);
+                currentFilename.assign(nextFilename);
+                nextFilename = "";
+                
+                fopen(currentFilename.c_str(), "a");
+                
+                fileNameUpdated = true;                            
+                                                    
+            }
+            
             gps.scanf("%s", &tmpGpsRxString);
             
             //copy c_string to string
@@ -107,24 +123,19 @@
             GPS_Data_Valid = false;
             GPS_Data_Rdy = false;
             
-            //update current data string for sps file
-            dbg.printf("Updating current sps string\r\n");
-            dbg.printf("gpsNMEA.currentUTCFromGPRMC :");
-            dbg.printf(gpsNMEA.currentUTCFromGPRMC.c_str());
-            dbg.printf("\r\n");
-            
+        
             if(GGA_Fix_Present){
                 
                 spsGen.UpdateCurrentString(TAG, IDENTIFIERID, GROUP, gpsNMEA.currentDATEFromGPRMC, gpsNMEA.currentUTCFromGPRMC, TIMEZONE, ENCODING, SOURCEIDENTIFICATION, INTERPRETERID, DATALINEVERSION, FWSRCVERSION, FWIVERSION, gpsNMEA.currentLatitude, gpsNMEA.currentLongitude, GGA_Fix_Present, "magtime", "mag_nt", "mag_sq", &dbg);
                                 
                 //write data strings to sps file
                 if(firstLineWritten){ 
-                    writeToUsb(spsGen.getCurrentString(), fp, &dbg);   
+                    writeToUsb(spsGen.getCurrentString(), fp);   
                 }
         
                 if(!firstLineWritten){ 
-                    writeToUsb(spsGen.getHeaderString(), fp, &dbg);
-                    writeToUsb(spsGen.getCurrentString(), fp, &dbg);
+                    writeToUsb(spsGen.getHeaderString(), fp);
+                    writeToUsb(spsGen.getCurrentString(), fp);
                     firstLineWritten = true;    
                 }                
             }    
@@ -136,7 +147,8 @@
             //reestablish usb connection
             while(!msd.connect()){
                 dbg.printf("Trying to reconnect to usb flash disk\r\n");
-                wait_ms(500);
+                wait_ms(200);
+                msd.connect();
             }
                 
             //Reopening a file on usb disk
@@ -161,20 +173,21 @@
 // the RX buffer. 
 void rxCallback(MODSERIAL_IRQ_INFO *q) {
     MODSERIAL *serial = q->serial;
-    if ( serial->rxGetLastChar() == '\n') {
+    if (serial->rxGetLastChar() == '\n') {
         GPS_Data_Rdy = true;
     }
 }
 
 //Write to file prototype
-bool writeToUsb(string line, FILE * f, Serial * dbg){
+bool writeToUsb(string line, FILE * f){
 
     if (f != NULL) {
-        dbg->printf("Writing to usb flash disk\r\n");
-        writecount += 1;
+        
         fprintf(f, line.c_str());
         fprintf(f, "\r\n");
-    }             
+        
+    }
+                 
     if(f == NULL){
         return false;
     }