@@ -22,6 +22,11 @@ GPSHardware::GPSHardware() {
22
22
_driver_type = GPS_DRV_NONE;
23
23
_nmea_baud_rate = DEFAULT_MTK_NMEA_BAUD_RATE;
24
24
_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;
25
30
}
26
31
27
32
/* !
@@ -244,10 +249,12 @@ bool GPSHardware::DetectUbxI2C(uint32_t addr) {
244
249
_ubx_gps_ddc = new Adafruit_UBloxDDC (addr, _wire);
245
250
if (!_ubx_gps_ddc->begin ())
246
251
return false ;
247
- _ubx_gps = new Adafruit_UBX (& _ubx_gps_ddc);
252
+ _ubx_gps = new Adafruit_UBX (* _ubx_gps_ddc);
248
253
if (!_ubx_gps->begin ())
249
254
return false ;
250
255
_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 ();
251
258
_driver_type = GPS_DRV_UBLOX;
252
259
return true ;
253
260
}
@@ -542,33 +549,41 @@ void GPSHardware::PollStoreSentences() {
542
549
_ubx_gps_ddc->available ();
543
550
}
544
551
WS_DEBUG_PRINTLN (" [gps] Polling completed!" );
545
- WS_DEBUG_PRINT (" Did We get a NMEA sentence? " );
546
552
uint8_t buffer[MAX_LEN_NMEA_SENTENCE];
547
553
String nmeaBuffer = " " ;
548
- size_t bytesToRead;
549
- size_t bytesRead;
550
554
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;
551
559
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);
554
561
}
562
+ WS_DEBUG_PRINT (" [gps] Read " );
563
+ WS_DEBUG_PRINT (bytesAvailable);
564
+ WS_DEBUG_PRINTLN (" bytes from u-blox GPS, parsing sentences...\n " );
555
565
// Build NMEA sentences and parse when complete
556
566
for (size_t i = 0 ; i < bytesRead; i++) {
557
567
char c = buffer[i];
558
568
nmeaBuffer += c;
559
569
// Check for end of NMEA sentence
560
570
if (c == ' \n ' ) {
561
- WS_DEBUG_PRINT (" [gps] NMEA sentence: " );
571
+ WS_DEBUG_PRINT (" [gps] GOT NMEA sentence: " );
562
572
WS_DEBUG_PRINTLN (nmeaBuffer.c_str ());
563
573
// Push the NMEA sentence to the buffer
574
+ WS_DEBUG_PRINTLN (" [gps] Pushing NMEA sentence to buffer..." );
564
575
if (NmeaBufPush (nmeaBuffer.c_str ()) == 0 ) {
565
576
WS_DEBUG_PRINTLN (" [gps] NMEA sentence pushed to buffer." );
566
577
} else {
567
578
WS_DEBUG_PRINTLN (
568
579
" [gps] ERROR: Unable to push NMEA sentence to buffer!" );
569
580
}
581
+ // Reset buffer for next sentence
582
+ nmeaBuffer = " " ;
570
583
}
571
584
}
585
+ // Done
586
+ WS_DEBUG_PRINTLN (" [gps] u-blox GPS polling completed!" );
572
587
} else {
573
588
WS_DEBUG_PRINTLN (" [gps] ERROR: Unsupported GPS driver type for polling!" );
574
589
}
@@ -580,8 +595,19 @@ void GPSHardware::PollStoreSentences() {
580
595
* @return 0 on success, -1 if the buffer is full.
581
596
*/
582
597
int 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" );
584
601
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 );
585
611
586
612
int next = _nmea_buff.head + 1 ; // points to head after the current write
587
613
if (next >= _nmea_buff.maxlen )
@@ -592,11 +618,14 @@ int GPSHardware::NmeaBufPush(const char *new_sentence) {
592
618
_nmea_buff.tail = (_nmea_buff.tail + 1 ) % _nmea_buff.maxlen ;
593
619
}
594
620
621
+ WS_DEBUG_PRINTLN (" [gps] About to call strncpy..." );
595
622
// Copy the new sentence into the buffer
596
623
strncpy (_nmea_buff.sentences [_nmea_buff.head ], new_sentence,
597
624
MAX_LEN_NMEA_SENTENCE - 1 );
625
+ WS_DEBUG_PRINTLN (" [gps] strncpy completed, setting null terminator..." );
598
626
_nmea_buff.sentences [_nmea_buff.head ][MAX_LEN_NMEA_SENTENCE - 1 ] = ' \0 ' ;
599
627
_nmea_buff.head = next;
628
+ WS_DEBUG_PRINTLN (" [gps] NmeaBufPush completed successfully" );
600
629
return 0 ;
601
630
}
602
631
@@ -630,6 +659,8 @@ int GPSHardware::NmeaBufPop(char *sentence) {
630
659
bool GPSHardware::ParseNMEASentence (char *sentence) {
631
660
if (!sentence)
632
661
return false ;
662
+ WS_DEBUG_PRINT (" [gps] ParseNMEASentence: " );
663
+ WS_DEBUG_PRINTLN (sentence);
633
664
return _ada_gps->parse (sentence);
634
665
}
635
666
0 commit comments