canuck lehead / lib_gps

Dependents:   Senet NAMote

Fork of lib_gps by wayne roberts

Revision:
6:9dfe135334c9
Parent:
4:b8c049fa7db2
Child:
7:44cb21f05dcc
diff -r 932f35199156 -r 9dfe135334c9 gps.cpp
--- a/gps.cpp	Mon Mar 07 20:08:57 2016 +0000
+++ b/gps.cpp	Mon Mar 07 15:20:16 2016 -0500
@@ -21,6 +21,7 @@
 GPS::GPS(PinName uart_tx, PinName uart_rx, PinName en) : m_uart(uart_tx, uart_rx), m_en_pin(en)
 {
     have_fix = false;
+    uart_rx_count = 0;
  }
     
 GPS::~GPS()
@@ -45,19 +46,29 @@
             if (len < RX_BUF_SIZE) {
                 rx_bufs[rx_bufs_in_idx][len] = ch;
                 rx_buf_lens[rx_bufs_in_idx]++;
-            } /*else
-                printf("S");*/
-        } /*else if (len != 0) {
-            printf("$ at %d\r\n", len);
-        }*/
+            } else
+                printf("S");
+        } else if (len != 0) {
+            if(verbose)
+                printf("$ at %d\r\n", len);
+        }
         
         if (ch == '\n') {
             rx_bufs[rx_bufs_in_idx][len] = 0;  // null terminate
+
+#if 0
+            if(verbose)
+            {
+                printf("on_uart_rx: buf=%d, len=%d\r\n", rx_bufs_in_idx, len);
+            }
+#endif
+
             if (++rx_bufs_in_idx == NUM_RX_BUFS)
                 rx_bufs_in_idx = 0;
             /* just let it overflow -- if (rx_bufs_in_idx == rx_bufs_out_idx)
                 printf("gps overflow\r\n");*/
             rx_buf_lens[rx_bufs_in_idx] = 0;
+            uart_rx_count++;
         }
     } while (m_uart.readable());
 }
@@ -164,14 +175,16 @@
     // could we calculate a verification checksum ?
     if( checksumIndex < 0 )
     {
-        //printf("gps:checksumIndex:%d\r\n", checksumIndex);
+        if (verbose)
+            printf("gps:checksumIndex:%d\r\n", checksumIndex);
         return false;
     }
 
     // check if there are enough char in the serial buffer to read checksum
     if( checksumIndex >= ( rx_buf_lens[idx] - 2 ) )
     {
-        //printf("gps:checksumIndex:%d\r\n", checksumIndex);
+        if (verbose)
+            printf("gps:checksumIndex:%d\r\n", checksumIndex);
         return false;
     }
 
@@ -197,6 +210,7 @@
     double valueTmp3;
     double valueTmp4;
 
+    
     // Convert the latitude from ASCII to uint8_t values
     for( i = 0 ; i < 10 ; i++ )
     {
@@ -327,6 +341,9 @@
                 return FAIL;
             }
         }
+        if(fieldSize == 0)
+            return FAIL;
+
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaLatitude[j] = NmeaString[i];
@@ -340,6 +357,9 @@
                 return FAIL;
             }
         }
+
+        if(fieldSize == 0)
+            return FAIL;
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaLatitudePole[j] = NmeaString[i];
@@ -353,6 +373,9 @@
                 return FAIL;
             }
         }
+        if(fieldSize == 0)
+            return FAIL;
+
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaLongitude[j] = NmeaString[i];
@@ -366,6 +389,9 @@
                 return FAIL;
             }
         }
+
+        if(fieldSize == 0)
+            return FAIL;
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaLongitudePole[j] = NmeaString[i];
@@ -465,6 +491,7 @@
         //FormatGpsData( );
         ConvertPositionFromStringToNumerical( );
         ConvertPositionIntoBinary( );
+        // printf("GPGAA: %s\r\n",NmeaString);
         return SUCCESS;
     }
     else if ( strncmp( ( const char* )NmeaGpsData.NmeaDataType, ( const char* )NmeaDataTypeGPRMC, 5 ) == 0 )
@@ -491,10 +518,16 @@
                 return FAIL;
             }
         }
+        if(fieldSize == 0)
+            return FAIL;
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaDataStatus[j] = NmeaString[i];
         }
+
+        if(NmeaGpsData.NmeaDataStatus[0] != 'A')
+            return FAIL;
+        //
         // NmeaLatitude
         fieldSize = 0;
         while( NmeaString[i + fieldSize++] != ',' )
@@ -504,6 +537,10 @@
                 return FAIL;
             }
         }
+
+        if(fieldSize == 0)
+            return FAIL;
+
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaLatitude[j] = NmeaString[i];
@@ -517,6 +554,8 @@
                 return FAIL;
             }
         }
+        if(fieldSize == 0)
+            return FAIL;
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaLatitudePole[j] = NmeaString[i];
@@ -530,6 +569,8 @@
                 return FAIL;
             }
         }
+        if(fieldSize == 0)
+            return FAIL;
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaLongitude[j] = NmeaString[i];
@@ -543,6 +584,8 @@
                 return FAIL;
             }
         }
+        if(fieldSize == 0)
+            return FAIL;
         for( j = 0; j < fieldSize; j++, i++ )
         {
             NmeaGpsData.NmeaLongitudePole[j] = NmeaString[i];
@@ -590,6 +633,8 @@
         //FormatGpsData( );
         ConvertPositionFromStringToNumerical( );
         ConvertPositionIntoBinary( );
+        // printf("GPMRC: %s\r\n", NmeaString);
+        // printf("ParseGPSData: string=%s, lat=%f, long=%f\r\n",NmeaString, Latitude, Longitude);
         return SUCCESS;
     }
     else
@@ -601,9 +646,6 @@
 void GPS::service()
 {
     while (rx_bufs_in_idx != rx_bufs_out_idx) {
-        //printf("%02d:%s\r\n", rx_buf_lens[rx_bufs_out_idx], rx_bufs[rx_bufs_out_idx]);
-        if (verbose)
-            printf("gps:%s\r\n", rx_bufs[rx_bufs_out_idx]);
         ParseGPSData(rx_bufs_out_idx);
         if (++rx_bufs_out_idx == NUM_RX_BUFS)
             rx_bufs_out_idx = 0;