@@ -22,6 +22,11 @@ GPSHardware::GPSHardware() {
2222 _driver_type = GPS_DRV_NONE;
2323 _nmea_baud_rate = DEFAULT_MTK_NMEA_BAUD_RATE;
2424 _nmea_update_rate = DEFAULT_MTK_NMEA_UPDATE_RATE;
25+
26+ // Initialize NMEA buffer
27+ _nmea_buff.head = 0 ;
28+ _nmea_buff.tail = 0 ;
29+ _nmea_buff.maxlen = MAX_NMEA_SENTENCES;
2530}
2631
2732/* !
@@ -244,10 +249,12 @@ bool GPSHardware::DetectUbxI2C(uint32_t addr) {
244249 _ubx_gps_ddc = new Adafruit_UBloxDDC (addr, _wire);
245250 if (!_ubx_gps_ddc->begin ())
246251 return false ;
247- _ubx_gps = new Adafruit_UBX (& _ubx_gps_ddc);
252+ _ubx_gps = new Adafruit_UBX (* _ubx_gps_ddc);
248253 if (!_ubx_gps->begin ())
249254 return false ;
250255 _ubx_gps->verbose_debug = 3 ; // TODO: Set this to 1 in production
256+ // Configure Adafruit_GPS instance for parsing NMEA sentences only
257+ _ada_gps = new Adafruit_GPS ();
251258 _driver_type = GPS_DRV_UBLOX;
252259 return true ;
253260}
@@ -542,33 +549,41 @@ void GPSHardware::PollStoreSentences() {
542549 _ubx_gps_ddc->available ();
543550 }
544551 WS_DEBUG_PRINTLN (" [gps] Polling completed!" );
545- WS_DEBUG_PRINT (" Did We get a NMEA sentence? " );
546552 uint8_t buffer[MAX_LEN_NMEA_SENTENCE];
547553 String nmeaBuffer = " " ;
548- size_t bytesToRead;
549- size_t bytesRead;
550554 int bytesAvailable = _ubx_gps_ddc->available ();
555+ WS_DEBUG_PRINT (" [gps] Reading u-blox GPS data, bytes available: " );
556+ WS_DEBUG_PRINTLN (bytesAvailable);
557+ size_t bytesToRead = min (bytesAvailable, 82 );
558+ size_t bytesRead;
551559 if (bytesAvailable > 0 ) {
552- min (bytesAvailable, MAX_NEMA_SENTENCE_LEN);
553- _ubx_gps_ddc->readBytes (buffer, bytesToRead);
560+ bytesRead = _ubx_gps_ddc->readBytes (buffer, bytesToRead);
554561 }
562+ WS_DEBUG_PRINT (" [gps] Read " );
563+ WS_DEBUG_PRINT (bytesAvailable);
564+ WS_DEBUG_PRINTLN (" bytes from u-blox GPS, parsing sentences...\n " );
555565 // Build NMEA sentences and parse when complete
556566 for (size_t i = 0 ; i < bytesRead; i++) {
557567 char c = buffer[i];
558568 nmeaBuffer += c;
559569 // Check for end of NMEA sentence
560570 if (c == ' \n ' ) {
561- WS_DEBUG_PRINT (" [gps] NMEA sentence: " );
571+ WS_DEBUG_PRINT (" [gps] GOT NMEA sentence: " );
562572 WS_DEBUG_PRINTLN (nmeaBuffer.c_str ());
563573 // Push the NMEA sentence to the buffer
574+ WS_DEBUG_PRINTLN (" [gps] Pushing NMEA sentence to buffer..." );
564575 if (NmeaBufPush (nmeaBuffer.c_str ()) == 0 ) {
565576 WS_DEBUG_PRINTLN (" [gps] NMEA sentence pushed to buffer." );
566577 } else {
567578 WS_DEBUG_PRINTLN (
568579 " [gps] ERROR: Unable to push NMEA sentence to buffer!" );
569580 }
581+ // Reset buffer for next sentence
582+ nmeaBuffer = " " ;
570583 }
571584 }
585+ // Done
586+ WS_DEBUG_PRINTLN (" [gps] u-blox GPS polling completed!" );
572587 } else {
573588 WS_DEBUG_PRINTLN (" [gps] ERROR: Unsupported GPS driver type for polling!" );
574589 }
@@ -580,8 +595,19 @@ void GPSHardware::PollStoreSentences() {
580595 * @return 0 on success, -1 if the buffer is full.
581596 */
582597int GPSHardware::NmeaBufPush (const char *new_sentence) {
583- if (!new_sentence)
598+ WS_DEBUG_PRINT (" [gps] NmeaBufPush called with sentence: " );
599+ if (!new_sentence) {
600+ WS_DEBUG_PRINTLN (" NULL" );
584601 return -1 ;
602+ }
603+ WS_DEBUG_PRINTLN (new_sentence);
604+
605+ WS_DEBUG_PRINT (" [gps] Buffer state - head: " );
606+ WS_DEBUG_PRINT (_nmea_buff.head );
607+ WS_DEBUG_PRINT (" , tail: " );
608+ WS_DEBUG_PRINT (_nmea_buff.tail );
609+ WS_DEBUG_PRINT (" , maxlen: " );
610+ WS_DEBUG_PRINTLN (_nmea_buff.maxlen );
585611
586612 int next = _nmea_buff.head + 1 ; // points to head after the current write
587613 if (next >= _nmea_buff.maxlen )
@@ -592,11 +618,14 @@ int GPSHardware::NmeaBufPush(const char *new_sentence) {
592618 _nmea_buff.tail = (_nmea_buff.tail + 1 ) % _nmea_buff.maxlen ;
593619 }
594620
621+ WS_DEBUG_PRINTLN (" [gps] About to call strncpy..." );
595622 // Copy the new sentence into the buffer
596623 strncpy (_nmea_buff.sentences [_nmea_buff.head ], new_sentence,
597624 MAX_LEN_NMEA_SENTENCE - 1 );
625+ WS_DEBUG_PRINTLN (" [gps] strncpy completed, setting null terminator..." );
598626 _nmea_buff.sentences [_nmea_buff.head ][MAX_LEN_NMEA_SENTENCE - 1 ] = ' \0 ' ;
599627 _nmea_buff.head = next;
628+ WS_DEBUG_PRINTLN (" [gps] NmeaBufPush completed successfully" );
600629 return 0 ;
601630}
602631
@@ -630,6 +659,8 @@ int GPSHardware::NmeaBufPop(char *sentence) {
630659bool GPSHardware::ParseNMEASentence (char *sentence) {
631660 if (!sentence)
632661 return false ;
662+ WS_DEBUG_PRINT (" [gps] ParseNMEASentence: " );
663+ WS_DEBUG_PRINTLN (sentence);
633664 return _ada_gps->parse (sentence);
634665}
635666
0 commit comments