Skip to content

Commit 5f6f26f

Browse files
committed
GPS - Add Parsing to SD Card
1 parent 351c156 commit 5f6f26f

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

src/components/gps/controller.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ bool GPSController::AddGPS(TwoWire *wire, uint32_t i2c_addr,
7272
* @param gps_config Pointer to the GPS configuration message.
7373
* @return True if the GPS was added successfully, false otherwise.
7474
*/
75-
bool GPSController::AddGPS(HardwareSerial *serial, uint32_t baudrate, wippersnapper_gps_GPSConfig *gps_config) {
75+
bool GPSController::AddGPS(HardwareSerial *serial, uint32_t baudrate,
76+
wippersnapper_gps_GPSConfig *gps_config) {
7677
GPSHardware *gps_hw = new GPSHardware();
7778

7879
if (!gps_hw->SetInterface(serial, baudrate)) {
@@ -156,17 +157,29 @@ void GPSController::update() {
156157
// Encode and publish to IO
157158
WS_DEBUG_PRINT("[gps] Encoding and publishing GPSEvent to IO...");
158159
bool did_encode = _gps_model->EncodeGPSEvent();
159-
if (!did_encode) {
160-
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to encode GPSEvent!");
161-
} else {
162-
// Publish the GPSEvent to IO
163-
if (!WsV2.PublishSignal(
164-
wippersnapper_signal_DeviceToBroker_gps_event_tag,
165-
_gps_model->GetGPSEvent())) {
166-
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to publish GPSEvent!");
160+
if (did_encode) {
161+
162+
if (!WsV2._sdCardV2->isModeOffline()) {
163+
// Publish the GPSEvent to IO
164+
if (!WsV2.PublishSignal(
165+
wippersnapper_signal_DeviceToBroker_gps_event_tag,
166+
_gps_model->GetGPSEvent())) {
167+
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to publish GPSEvent!");
168+
} else {
169+
WS_DEBUG_PRINTLN("...ok!");
170+
}
167171
} else {
168-
WS_DEBUG_PRINTLN("...ok!");
172+
// Log the GPSEvent to SD card
173+
WS_DEBUG_PRINT("[gps] Logging GPSEvent to SD card...");
174+
/* if (!WsV2._sdCardV2->LogGPSEventToSD(_gps_model->GetGPSEvent())) {
175+
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to log GPSEvent to SD!");
176+
statusLEDSolid(WS_LED_STATUS_FS_WRITE);
177+
} else {
178+
WS_DEBUG_PRINTLN("OK!");
179+
} */
169180
}
181+
} else {
182+
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to encode GPSEvent!");
170183
}
171184
drv->SetPollPeriodPrv(cur_time);
172185
}

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
*/
1515
#include "ws_sdcard.h"
1616

17-
/**************************************************************************/
1817
/*!
1918
@brief Initializes the SD card.
2019
@param pin_cs
2120
The chip select pin for the SD card.
2221
@returns True if the SD card was successfully initialized, False
2322
otherwise.
2423
*/
25-
/**************************************************************************/
2624
bool ws_sdcard::InitSdCard(uint8_t pin_cs) {
2725
WsV2.pin_sd_cs = pin_cs;
2826
#ifdef SD_USE_SPI_1
@@ -1310,14 +1308,12 @@ bool ws_sdcard::LogDS18xSensorEventToSD(
13101308
return true;
13111309
}
13121310

1313-
/**************************************************************************/
13141311
/*!
13151312
@brief Logs an I2C sensor event to the SD card.
13161313
@param msg_device_event
13171314
The I2cDeviceEvent message to log.
13181315
@returns True if the event was successfully logged, False otherwise.
13191316
*/
1320-
/**************************************************************************/
13211317
bool ws_sdcard::LogI2cDeviceEvent(
13221318
wippersnapper_i2c_I2cDeviceEvent *msg_device_event) {
13231319
JsonDocument doc;
@@ -1347,14 +1343,70 @@ bool ws_sdcard::LogI2cDeviceEvent(
13471343
return true;
13481344
}
13491345

1346+
/*!
1347+
@brief Logs a GPS event to the SD card.
1348+
@param msg_gps_event
1349+
The GPSEvent message to log.
1350+
@returns True if the event was successfully logged, False otherwise.
1351+
*/
1352+
bool ws_sdcard::LogGPSEventToSD(wippersnapper_gps_GPSEvent *msg_gps_event) {
1353+
JsonDocument doc;
1354+
1355+
// Log RMC responses
1356+
for (pb_size_t rmc_resp = 0; rmc_resp < msg_gps_event->rmc_responses_count; rmc_resp++) {
1357+
WS_DEBUG_PRINTLN("[SD] Logging RMC response...");
1358+
// Log GPS DateTime
1359+
if (msg_gps_event->rmc_responses[rmc_resp].has_datetime) {
1360+
wippersnapper_gps_GPSDateTime gps_dt = msg_gps_event->rmc_responses[rmc_resp].datetime;
1361+
DateTime gps_datetime(gps_dt.year, gps_dt.month, gps_dt.day,
1362+
gps_dt.hour, gps_dt.minute, gps_dt.seconds);
1363+
doc["timestamp"] = gps_datetime.unixtime();
1364+
}
1365+
// Log GPS data
1366+
doc["fix_status"] = msg_gps_event->rmc_responses[rmc_resp].fix_status;
1367+
doc["latitude"] = msg_gps_event->rmc_responses[rmc_resp].lat;
1368+
doc["lat_dir"] = msg_gps_event->rmc_responses[rmc_resp].lat_dir;
1369+
doc["longitude"] = msg_gps_event->rmc_responses[rmc_resp].lon;
1370+
doc["lon_dir"] = msg_gps_event->rmc_responses[rmc_resp].lon_dir;
1371+
doc["speed"] = msg_gps_event->rmc_responses[rmc_resp].speed;
1372+
doc["angle"] = msg_gps_event->rmc_responses[rmc_resp].angle;
1373+
if (!LogJSONDoc(doc))
1374+
return false;
1375+
}
1376+
1377+
// Log GGA responses
1378+
for (pb_size_t gga_resp = 0; gga_resp < msg_gps_event->gga_responses_count; gga_resp++) {
1379+
WS_DEBUG_PRINTLN("[SD] Logging GGA response...");
1380+
// Log GPS DateTime
1381+
if (msg_gps_event->gga_responses[gga_resp].has_datetime) {
1382+
wippersnapper_gps_GPSDateTime gps_dt = msg_gps_event->gga_responses[gga_resp].datetime;
1383+
DateTime gps_datetime(gps_dt.year, gps_dt.month, gps_dt.day,
1384+
gps_dt.hour, gps_dt.minute, gps_dt.seconds);
1385+
doc["timestamp"] = gps_datetime.unixtime();
1386+
}
1387+
// Log GPS data
1388+
doc["latitude"] = msg_gps_event->gga_responses[gga_resp].lat;
1389+
doc["lat_dir"] = msg_gps_event->gga_responses[gga_resp].lat_dir;
1390+
doc["longitude"] = msg_gps_event->gga_responses[gga_resp].lon;
1391+
doc["lon_dir"] = msg_gps_event->gga_responses[gga_resp].lon_dir;
1392+
doc["fix_quality"] = msg_gps_event->gga_responses[gga_resp].fix_quality;
1393+
doc["num_satellites"] = msg_gps_event->gga_responses[gga_resp].num_satellites;
1394+
doc["hdop"] = msg_gps_event->gga_responses[gga_resp].hdop;
1395+
doc["altitude"] = msg_gps_event->gga_responses[gga_resp].altitude;
1396+
doc["geoid_height"] = msg_gps_event->gga_responses[gga_resp].geoid_height;
1397+
if (!LogJSONDoc(doc))
1398+
return false;
1399+
}
1400+
1401+
return true;
1402+
}
1403+
13501404
#ifdef OFFLINE_MODE_DEBUG
1351-
/**************************************************************************/
13521405
/*!
13531406
@brief Waits for a valid JSON string to be received via the hardware's
13541407
serial input or from a hardcoded test JSON string.
13551408
@returns True if a valid JSON string was received, False otherwise.
13561409
*/
1357-
/**************************************************************************/
13581410
void ws_sdcard::waitForSerialConfig() {
13591411
json_test_data = "{"
13601412
"\"exportVersion\": \"1.0.0\","

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ws_sdcard {
7070
wippersnapper_sensor_SensorType read_type);
7171
bool LogDS18xSensorEventToSD(wippersnapper_ds18x20_Ds18x20Event *event_msg);
7272
bool LogI2cDeviceEvent(wippersnapper_i2c_I2cDeviceEvent *msg_device_event);
73-
73+
bool LogGPSEventToSD(wippersnapper_gps_GPSEvent *msg_gps_event);
7474
private:
7575
bool ParseExportedFromDevice(JsonDocument &doc);
7676
void ConfigureSDCard();

src/provisioning/tinyusb/Wippersnapper_FS.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#include "Adafruit_SPIFlash.h"
1919
#include "Adafruit_TinyUSB.h"
2020
#include "SdFat_Adafruit_Fork.h"
21+
#include "fatfs/ff.h" // NOTE: This should be #included before fatfs/diskio.h!!!
2122
// using f_mkfs() for formatting
2223
#include "fatfs/diskio.h"
23-
#include "fatfs/ff.h" // NOTE: This should be #included before fatfs/diskio.h!!!
2424

2525
#include "Wippersnapper_V2.h"
2626

0 commit comments

Comments
 (0)