Skip to content

Commit 5ccb7c7

Browse files
committed
GPS - Refactor GPSEvent and processing, datetime construction
1 parent 9a9c57b commit 5ccb7c7

File tree

5 files changed

+113
-5
lines changed

5 files changed

+113
-5
lines changed

src/components/gps/controller.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,19 @@ void GPSController::update() {
165165
_gps_model->CreateGPSEvent();
166166
char nmea_sentence[MAX_LEN_NMEA_SENTENCE];
167167
bool has_gps_event = false;
168+
// TODO: You were here
169+
// Thoughts:
170+
// 1. Split out the parsing into the hardware HAL API
171+
// 2. Return the sentence back to the controller func, here
172+
// 3. Controller will then the sentence to the model for parsing/inclusion
173+
// into a GPSEvent
174+
// 4. controller sends it
168175
while (drv->NmeaBufPop(nmea_sentence) != -1) {
169176
// Parse the NMEA sentence
170177
WS_DEBUG_PRINT("[gps] Parsing NMEA sentence: ");
171178
WS_DEBUG_PRINTLN(nmea_sentence);
172-
if (!ada_gps->parse(nmea_sentence)) {
173-
continue; // Skip parsing this sentence if parsing failed
179+
if (!drv->ParseNMEASentence(nmea_sentence)) {
180+
continue; // Skip this sentence if parsing failed
174181
}
175182
has_gps_event = true;
176183
// Build a GPSEvent message from the sentence

src/components/gps/hardware.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,87 @@ int GPSHardware::NmeaBufPop(char *sentence) {
610610
strcpy(sentence, _nmea_buff.sentences[_nmea_buff.tail]);
611611
_nmea_buff.tail = next; // Advance tail
612612
return 0;
613+
}
614+
615+
/*!
616+
* @brief Parses a NMEA sentence and returns true if it was successfully parsed.
617+
* @param sentence Pointer to the NMEA sentence to be parsed.
618+
* @returns True if the sentence was parsed successfully, False otherwise.
619+
*/
620+
bool GPSHardware::ParseNMEASentence(char *sentence) {
621+
if (!sentence)
622+
return false;
623+
if (_driver_type == GPS_DRV_MTK) {
624+
// Parse the NMEA sentence using Adafruit_GPS
625+
return _ada_gps->parse(sentence);
626+
} else if (_driver_type == GPS_DRV_UBLOX) {
627+
// Parse the NMEA sentence using SFE_UBLOX_GNSS
628+
// TODO!
629+
// return _sfe_gps->parseNMEA(sentence);
630+
}
631+
632+
return false;
633+
}
634+
635+
uint8_t GPSHardware::GetHour() {
636+
if (_driver_type == GPS_DRV_MTK) {
637+
return _ada_gps->hour;
638+
} else if (_driver_type == GPS_DRV_UBLOX) {
639+
// TODO: Implement for UBLOX
640+
}
641+
return 0;
642+
}
643+
644+
uint8_t GPSHardware::GetMinute() {
645+
if (_driver_type == GPS_DRV_MTK) {
646+
return _ada_gps->minute;
647+
} else if (_driver_type == GPS_DRV_UBLOX) {
648+
// TODO: Implement for UBLOX
649+
}
650+
return 0;
651+
}
652+
653+
uint8_t GPSHardware::GetSeconds() {
654+
if (_driver_type == GPS_DRV_MTK) {
655+
return _ada_gps->seconds;
656+
} else if (_driver_type == GPS_DRV_UBLOX) {
657+
// TODO: Implement for UBLOX
658+
}
659+
return 0;
660+
}
661+
662+
uint16_t GPSHardware::GetMilliseconds() {
663+
if (_driver_type == GPS_DRV_MTK) {
664+
return _ada_gps->milliseconds;
665+
} else if (_driver_type == GPS_DRV_UBLOX) {
666+
// TODO: Implement for UBLOX
667+
}
668+
return 0;
669+
}
670+
671+
uint8_t GPSHardware::GetDay() {
672+
if (_driver_type == GPS_DRV_MTK) {
673+
return _ada_gps->day;
674+
} else if (_driver_type == GPS_DRV_UBLOX) {
675+
// TODO: Implement for UBLOX
676+
}
677+
return 0;
678+
}
679+
680+
uint8_t GPSHardware::GetMonth() {
681+
if (_driver_type == GPS_DRV_MTK) {
682+
return _ada_gps->month;
683+
} else if (_driver_type == GPS_DRV_UBLOX) {
684+
// TODO: Implement for UBLOX
685+
}
686+
return 0;
687+
}
688+
689+
uint8_t GPSHardware::GetYear() {
690+
if (_driver_type == GPS_DRV_MTK) {
691+
return _ada_gps->year;
692+
} else if (_driver_type == GPS_DRV_UBLOX) {
693+
// TODO: Implement for UBLOX
694+
}
695+
return 0;
613696
}

src/components/gps/hardware.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,20 @@ class GPSHardware {
8686
SFE_UBLOX_GNSS *GetUbxGps();
8787
GpsDriverType GetDriverType();
8888
GpsInterfaceType GetIfaceType();
89+
int NmeaBufPop(char *sentence);
8990
// HAL Abstraction for GPS driver commands
9091
// Used to abstract the Adafruit_GPS and SFE_UBLOX_GNSS libraries
9192
// and intelligently handle the differences between them
9293
void ReadDiscardBuffer();
9394
void PollStoreSentences();
95+
bool ParseNMEASentence(char *sentence);
96+
uint8_t GetHour();
97+
uint8_t GetMinute();
98+
uint8_t GetSeconds();
99+
uint16_t GetMilliseconds();
100+
uint8_t GetDay();
101+
uint8_t GetMonth();
102+
uint8_t GetYear();
94103

95104
private:
96105
bool QueryModuleType();
@@ -117,9 +126,8 @@ class GPSHardware {
117126
char _micro_nmea_buf[100]; ///< Optional Buffer for MicroNMEA parsing
118127
// NMEA sentence ring buffer
119128
int NmeaBufPush(
120-
const char *new_sentence); ///< Push a sentence to the NMEA buffer
121-
int NmeaBufPop(char *sentence); ///< Pop a sentence from the NMEA buffer
122-
nmea_buffer_t _nmea_buff; ///< NMEA buffer for storing sentences
129+
const char *new_sentence); ///< Push a sentence to the NMEA buffer
130+
nmea_buffer_t _nmea_buff; ///< NMEA buffer for storing sentences
123131
};
124132
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
125133
#endif // WS_GPS_HARDWARE_H

src/components/gps/model.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,13 @@ bool GPSModel::AddGpsEventGGA(wippersnapper_gps_GPSDateTime datetime,
193193
_msg_gps_event.gga_responses[0] = gga_response;
194194
_msg_gps_event.gga_responses_count = 1;
195195
return true;
196+
}
197+
198+
bool GPSModel::ProcessNMEASentence(char *sentence, GPSHardware *drv) {
199+
// Build datetime
200+
wippersnapper_gps_GPSDateTime datetime = CreateGpsDatetime(
201+
drv->GetHour(), drv->GetMinute(), drv->GetSeconds(),
202+
drv->GetMilliseconds(), drv->GetDay(), drv->GetMonth(), drv->GetYear());
203+
204+
return true;
196205
}

src/components/gps/model.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class GPSModel {
3232
void CreateGPSEvent();
3333
bool EncodeGPSEvent();
3434
wippersnapper_gps_GPSEvent *GetGPSEvent();
35+
bool ProcessNMEASentence(char *sentence, GPSHardware *drv);
3536
wippersnapper_gps_GPSDateTime CreateGpsDatetime(uint8_t hour, uint8_t minute,
3637
uint8_t seconds,
3738
uint8_t milliseconds,

0 commit comments

Comments
 (0)