@@ -189,96 +189,119 @@ void GPSController::update() {
189
189
drv->SetPrvKat(millis());
190
190
} */
191
191
192
+ // TODO: Did the polling periods get set? Let's print and check them
193
+ WS_DEBUG_PRINT (" [gps] Poll period: " );
194
+ WS_DEBUG_PRINT (drv->GetPollPeriod ());
195
+ WS_DEBUG_PRINT (" , Previous poll period: " );
196
+ WS_DEBUG_PRINTLN (drv->GetPollPeriodPrv ());
197
+
192
198
// Did read period elapse?
193
199
ulong cur_time = millis ();
194
200
if (cur_time - drv->GetPollPeriodPrv () < drv->GetPollPeriod ())
195
201
continue ; // Not yet elapsed, skip this driver
196
202
197
203
// Discard the GPS buffer before we attempt to do a fresh read
198
- size_t bytes_avail = ada_gps->available ();
199
- if (bytes_avail > 0 ) {
200
- for (size_t i = 0 ; i < bytes_avail; i++) {
201
- ada_gps->read ();
202
- }
203
- }
204
+ WS_DEBUG_PRINTLN (" [gps] Discarding GPS buffer..." );
205
+ WS_DEBUG_PRINT (" iface type: " );
206
+ WS_DEBUG_PRINTLN (drv->GetIfaceType ());
204
207
205
- // Unset the RX flag
206
- if (ada_gps->newNMEAreceived ()) {
207
- ada_gps->lastNMEA ();
208
- }
208
+ if (drv->GetIfaceType () == GPS_IFACE_UART_HW) {
209
+ // TODO: Refactor this into a function within hardware.cpp
210
+ size_t bytes_avail = ada_gps->available ();
211
+ if (bytes_avail > 0 ) {
212
+ for (size_t i = 0 ; i < bytes_avail; i++) {
213
+ WS_DEBUG_PRINT (" [gps] Reading byte: " );
214
+ WS_DEBUG_PRINT (i);
215
+ ada_gps->read ();
216
+ WS_DEBUG_PRINTLN (" ...OK!" );
217
+ }
218
+ } else if (drv->GetIfaceType () == GPS_IFACE_I2C) {
219
+ // For I2C, request and discard any stale data from the device
220
+ WS_DEBUG_PRINT (" [gps] Discarding stale I2C data..." );
221
+ drv->I2cReadDiscard ();
222
+ }
209
223
210
- // Let's attempt to get a sentence from the GPS module
211
- // Read from the GPS module for update_rate milliseconds
212
- ulong update_rate = 1000 / drv->GetNmeaUpdateRate ();
213
- ulong start_time = millis ();
214
-
215
- WS_DEBUG_PRINT (" [gps] Reading GPS data for " );
216
- WS_DEBUG_PRINT (update_rate);
217
- WS_DEBUG_PRINTLN (" ms..." );
218
- while (millis () - start_time < update_rate) {
219
- char c = ada_gps->read ();
220
- // Check if we have a new NMEA sentence
224
+ // Unset the RX flag
225
+ WS_DEBUG_PRINTLN (" [gps] Unsetting RX flag..." );
221
226
if (ada_gps->newNMEAreceived ()) {
222
- // If we have a new sentence, push it to the buffer
223
- char *last_nmea = ada_gps->lastNMEA ();
224
- NmeaBufPush (ada_gps->lastNMEA ());
227
+ ada_gps->lastNMEA ();
225
228
}
226
- }
227
229
228
- // Parse each NMEA sentence in the buffer
229
- char nmea_sentence[MAX_LEN_NMEA_SENTENCE];
230
- bool has_gps_event = false ;
231
- while (NmeaBufPop (nmea_sentence) != -1 ) {
232
- // Parse the NMEA sentence
233
- WS_DEBUG_PRINT (" [gps] Parsing NMEA sentence: " );
234
- WS_DEBUG_PRINTLN (nmea_sentence);
235
- if (!ada_gps->parse (nmea_sentence)) {
236
- continue ; // Skip parsing this sentence if parsing failed
237
- } else {
238
- _gps_model->CreateGPSEvent ();
239
- has_gps_event = true ;
230
+ // Let's attempt to get a sentence from the GPS module
231
+ // Read from the GPS module for update_rate milliseconds
232
+ ulong update_rate = 1000 / drv->GetNmeaUpdateRate ();
233
+ ulong start_time = millis ();
234
+
235
+ WS_DEBUG_PRINT (" [gps] Reading GPS data for " );
236
+ WS_DEBUG_PRINT (update_rate);
237
+ WS_DEBUG_PRINTLN (" ms..." );
238
+ while (millis () - start_time < update_rate) {
239
+ char c = ada_gps->read ();
240
+ // Check if we have a new NMEA sentence
241
+ if (ada_gps->newNMEAreceived ()) {
242
+ // If we have a new sentence, push it to the buffer
243
+ char *last_nmea = ada_gps->lastNMEA ();
244
+ NmeaBufPush (ada_gps->lastNMEA ());
245
+ }
240
246
}
241
247
242
- // Build the GPSEvent message from the sentence
243
- wippersnapper_gps_GPSDateTime datetime = _gps_model->CreateGpsDatetime (
244
- ada_gps->hour , ada_gps->minute , ada_gps->seconds ,
245
- ada_gps->milliseconds , ada_gps->day , ada_gps->month , ada_gps->year );
246
- if (strncmp (nmea_sentence, " $GPRMC" , 6 ) == 0 ) {
247
- _gps_model->AddGpsEventRMC (
248
- datetime, ada_gps->fix , ada_gps->latitude , &ada_gps->lat ,
249
- ada_gps->longitude , &ada_gps->lon , ada_gps->speed , ada_gps->angle );
250
- } else if (strncmp (nmea_sentence, " $GPGGA" , 6 ) == 0 ) {
251
- _gps_model->AddGpsEventGGA (
252
- datetime, ada_gps->fix , ada_gps->latitude , &ada_gps->lat ,
253
- ada_gps->longitude , &ada_gps->lon , ada_gps->satellites ,
254
- ada_gps->HDOP , ada_gps->altitude , ada_gps->geoidheight );
255
- } else {
256
- WS_DEBUG_PRINTLN (
257
- " [gps] WARNING - Parsed sentence is not type RMC or GGA!" );
248
+ // Parse each NMEA sentence in the buffer
249
+ char nmea_sentence[MAX_LEN_NMEA_SENTENCE];
250
+ bool has_gps_event = false ;
251
+ while (NmeaBufPop (nmea_sentence) != -1 ) {
252
+ // Parse the NMEA sentence
253
+ WS_DEBUG_PRINT (" [gps] Parsing NMEA sentence: " );
254
+ WS_DEBUG_PRINTLN (nmea_sentence);
255
+ if (!ada_gps->parse (nmea_sentence)) {
256
+ continue ; // Skip parsing this sentence if parsing failed
257
+ } else {
258
+ _gps_model->CreateGPSEvent ();
259
+ has_gps_event = true ;
260
+ }
261
+
262
+ // Build the GPSEvent message from the sentence
263
+ wippersnapper_gps_GPSDateTime datetime = _gps_model->CreateGpsDatetime (
264
+ ada_gps->hour , ada_gps->minute , ada_gps->seconds ,
265
+ ada_gps->milliseconds , ada_gps->day , ada_gps->month , ada_gps->year );
266
+ if (strncmp (nmea_sentence, " $GPRMC" , 6 ) == 0 ) {
267
+ _gps_model->AddGpsEventRMC (datetime, ada_gps->fix , ada_gps->latitude ,
268
+ &ada_gps->lat , ada_gps->longitude ,
269
+ &ada_gps->lon , ada_gps->speed ,
270
+ ada_gps->angle );
271
+ } else if (strncmp (nmea_sentence, " $GPGGA" , 6 ) == 0 ) {
272
+ _gps_model->AddGpsEventGGA (
273
+ datetime, ada_gps->fix , ada_gps->latitude , &ada_gps->lat ,
274
+ ada_gps->longitude , &ada_gps->lon , ada_gps->satellites ,
275
+ ada_gps->HDOP , ada_gps->altitude , ada_gps->geoidheight );
276
+ } else {
277
+ WS_DEBUG_PRINTLN (
278
+ " [gps] WARNING - Parsed sentence is not type RMC or GGA!" );
279
+ }
258
280
}
259
- }
260
281
261
- // We did not create a GPSEvent because the NMEA sentences were not
262
- // GGA/RMC or parsed correctly
263
- if (!has_gps_event) {
264
- WS_DEBUG_PRINTLN (" [gps] No GPSEvent created from NMEA sentences!" );
265
- continue ;
266
- }
282
+ // We did not create a GPSEvent because the NMEA sentences were not
283
+ // GGA/RMC or parsed correctly
284
+ if (!has_gps_event) {
285
+ WS_DEBUG_PRINTLN (" [gps] No GPSEvent created from NMEA sentences!" );
286
+ continue ;
287
+ }
267
288
268
- // Encode and publish to IO
269
- WS_DEBUG_PRINT (" [gps] Encoding and publishing GPSEvent to IO..." );
270
- bool did_encode = _gps_model->EncodeGPSEvent ();
271
- if (!did_encode) {
272
- WS_DEBUG_PRINTLN (" [gps] ERROR: Failed to encode GPSEvent!" );
273
- } else {
274
- // Publish the GPSEvent to IO
275
- if (!WsV2.PublishSignal (wippersnapper_signal_DeviceToBroker_gps_event_tag,
276
- _gps_model->GetGPSEvent ())) {
277
- WS_DEBUG_PRINTLN (" [gps] ERROR: Failed to publish GPSEvent!" );
289
+ // Encode and publish to IO
290
+ WS_DEBUG_PRINT (" [gps] Encoding and publishing GPSEvent to IO..." );
291
+ bool did_encode = _gps_model->EncodeGPSEvent ();
292
+ if (!did_encode) {
293
+ WS_DEBUG_PRINTLN (" [gps] ERROR: Failed to encode GPSEvent!" );
278
294
} else {
279
- WS_DEBUG_PRINTLN (" [gps] GPSEvent published successfully!" );
295
+ // Publish the GPSEvent to IO
296
+ if (!WsV2.PublishSignal (
297
+ wippersnapper_signal_DeviceToBroker_gps_event_tag,
298
+ _gps_model->GetGPSEvent ())) {
299
+ WS_DEBUG_PRINTLN (" [gps] ERROR: Failed to publish GPSEvent!" );
300
+ } else {
301
+ WS_DEBUG_PRINTLN (" [gps] GPSEvent published successfully!" );
302
+ }
280
303
}
304
+ drv->SetPollPeriodPrv (cur_time);
281
305
}
282
- drv->SetPollPeriodPrv (cur_time);
283
306
}
284
307
}
0 commit comments