Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Revision:
0:b3313c5ffca3
Child:
1:f347c4ef25fa
Child:
2:39c4a85dc2a4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Feb 17 09:33:57 2017 +0000
@@ -0,0 +1,144 @@
+#include "main.h"
+
+//Global GPS variables
+bool GPS_Data_Rdy = false;
+bool GPS_Data_Valid = false;
+bool GGA_Fix_Present = false;
+
+//global system variables,at the moment, only the currently always true "run variable"
+bool run = true;
+int writecount = 0;
+int dataRWComplete = 0;
+
+//global tmpGpsRxString
+char tmpGpsRxString[128];
+
+WatchDog wdt;
+
+int main(void){
+    
+    //Initializing string buffer for GPS data
+    string GPS_String_Buff;
+    GPS_String_Buff.resize(128);
+    memset(tmpGpsRxString,'\0',128);
+    
+    //initializing watchdog timer to 10 second idle limit
+    wdt.kick(10);
+        
+    //GPS communication init
+    gps.baud(9600);
+    gps.attach(&rxCallback, MODSERIAL::RxIrq);
+    
+    //debug comm setup, to be removed when working correctly :)
+    dbg.baud(115200);
+    
+    //setting up USB device
+    USBHostMSD msd("usb");
+    
+    while(!msd.connect()){
+        dbg.printf("Trying to connect to usb flash disk\r\n");
+        wait_ms(500);       
+    }
+    
+    //Opening a file on usb disk
+    FILE * fp = fopen("/usb/test1.txt", "a");
+    
+    //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");
+        
+    //infinite loop
+    while(run) {
+        
+        if(GPS_Data_Rdy){
+            
+            gps.scanf("%s", &tmpGpsRxString);
+            
+            //copy c_string to string
+            GPS_String_Buff.assign(tmpGpsRxString);
+            
+            //clear tmpRxBuffer
+            memset(tmpGpsRxString,'\0',128);
+            
+            //printing GPS string buffer
+            dbg.printf(GPS_String_Buff.c_str());
+            dbg.printf("\r\n");
+            //dbg.printf("Validating GPS data\r\n");
+            //Validate CRC
+            GPS_Data_Valid = gpsNMEA.ValidateData(GPS_String_Buff, &dbg);
+         
+            //store valid string, either gga or rmc
+            if(GPS_Data_Valid){
+                 gpsNMEA.StoreString(GPS_String_Buff, &dbg);   
+            }
+            
+            GGA_Fix_Present = gpsNMEA.GGAFixVerification(&dbg);
+            
+            //Clearing RX buffer.
+            //dbg.printf("Clearing Rx buffer, and flags... ");  
+            GPS_String_Buff = "";
+            GPS_String_Buff.resize(128);
+            
+            //clearing flags
+            GPS_Data_Valid = false;
+            GPS_Data_Rdy = false;
+            //dbg.printf("Done!\r\n");            
+                            
+            //write current data received data to file if gga fix is present.
+            if (fp != NULL && GGA_Fix_Present) {
+                dbg.printf("Writing to usb flash disk\r\n");
+                writecount += 1;
+                fprintf(fp, gpsNMEA.currentGGAString.c_str());
+                fprintf(fp, "\r\n");
+                fprintf(fp, gpsNMEA.currentGPRMCString.c_str());
+                fprintf(fp, "\r\n");
+            }
+            
+            if(fp == NULL){
+                //Reopening a file on usb disk
+                FILE * fp = fopen("/usb/test1.txt", "a");        
+            }
+            
+            //Close file and reopen
+            if(writecount > 10){
+                //dbg.printf("Closing file and reopening.\r\n");
+                fclose(fp);
+                writecount = 0;
+                FILE * fp = fopen("/usb/test1.txt", "a");    
+            }
+            
+       }
+        
+        
+        
+        //If connection to USB flash disk is lost, reconnect to the flash disk
+        if(!msd.connected()){
+            //reestablish usb connection
+            while(!msd.connect()){
+                dbg.printf("Trying to reconnect to usb flash disk\r\n");
+                wait_ms(500);
+            }
+                
+            //Reopening a file on usb disk
+            FILE * fp = fopen("/usb/test1.txt", "a");
+                     
+        }
+        
+        wdt.kick();
+       
+    }         
+     
+    return 0;    
+}
+
+//Thanks to MODSERIAL!
+// Called everytime a new character goes into
+// the RX buffer. Test that character for \n
+// Note, rxGetLastChar() gets the last char that
+// we received but it does NOT remove it from
+// the RX buffer. 
+void rxCallback(MODSERIAL_IRQ_INFO *q) {
+    MODSERIAL *serial = q->serial;
+    if ( serial->rxGetLastChar() == '\n') {
+        GPS_Data_Rdy = true;
+    }
+}
\ No newline at end of file