Skip to content

Commit c6d6482

Browse files
committed
SD - Add UART Logging, offline/online switch, small refactor of func names
1 parent 5f6f26f commit c6d6482

File tree

8 files changed

+138
-101
lines changed

8 files changed

+138
-101
lines changed

src/components/analogIO/controller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ bool AnalogIOController::EncodePublishPinEvent(
193193
*/
194194
bool AnalogIOController::EncodePublishPinValue(uint8_t pin, uint16_t value) {
195195
if (WsV2._sdCardV2->isModeOffline()) {
196-
return WsV2._sdCardV2->LogGPIOSensorEventToSD(
196+
return WsV2._sdCardV2->LogEventGpio(
197197
pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW);
198198
} else {
199199
return EncodePublishPinEvent(
@@ -212,7 +212,7 @@ bool AnalogIOController::EncodePublishPinValue(uint8_t pin, uint16_t value) {
212212
*/
213213
bool AnalogIOController::EncodePublishPinVoltage(uint8_t pin, float value) {
214214
if (WsV2._sdCardV2->isModeOffline()) {
215-
return WsV2._sdCardV2->LogGPIOSensorEventToSD(
215+
return WsV2._sdCardV2->LogEventGpio(
216216
pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE);
217217
} else {
218218
return EncodePublishPinEvent(

src/components/digitalIO/controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ bool DigitalIOController::EncodePublishPinEvent(uint8_t pin_name,
305305
WS_DEBUG_PRINTLN("[digitalio] Published DigitalIOEvent to broker!")
306306
} else {
307307
// let's log the event to the SD card
308-
if (!WsV2._sdCardV2->LogGPIOSensorEventToSD(
308+
if (!WsV2._sdCardV2->LogEventGpio(
309309
pin_name, pin_value,
310310
wippersnapper_sensor_SensorType_SENSOR_TYPE_BOOLEAN))
311311
return false;

src/components/ds18x20/controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void DS18X20Controller::update() {
269269
}
270270
WS_DEBUG_PRINTLN("Published!");
271271
} else {
272-
if (!WsV2._sdCardV2->LogDS18xSensorEventToSD(
272+
if (!WsV2._sdCardV2->LogEventDs18x(
273273
_DS18X20_model->GetDS18x20EventMsg())) {
274274
WS_DEBUG_PRINTLN(
275275
"ERROR | DS18x20: Failed to log DS18x20 event to SD card");

src/components/gps/controller.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ void GPSController::update() {
120120
} */
121121

122122
// Did read period elapse?
123+
// TODO: Why is it constantly polling the GPS hardware from the config.json?
124+
// Is this a bug with setting the poll period? Print it out
123125
ulong cur_time = millis();
126+
WS_DEBUG_PRINT("drv->GetPollPeriodPrv(): ");
127+
WS_DEBUG_PRINT(drv->GetPollPeriodPrv());
128+
WS_DEBUG_PRINT(" drv->GetPollPeriod(): ");
129+
WS_DEBUG_PRINTLN(drv->GetPollPeriod());
130+
WS_DEBUG_PRINT("cur_time: ");
131+
WS_DEBUG_PRINTLN(cur_time);
124132
if (cur_time - drv->GetPollPeriodPrv() < drv->GetPollPeriod())
125133
continue; // Not yet elapsed, skip this driver
126134

@@ -154,13 +162,12 @@ void GPSController::update() {
154162
if (!has_gps_event)
155163
continue;
156164

157-
// Encode and publish to IO
158-
WS_DEBUG_PRINT("[gps] Encoding and publishing GPSEvent to IO...");
165+
// Encode and process the GPSEvent
159166
bool did_encode = _gps_model->EncodeGPSEvent();
160167
if (did_encode) {
161-
162168
if (!WsV2._sdCardV2->isModeOffline()) {
163169
// Publish the GPSEvent to IO
170+
WS_DEBUG_PRINT("[gps] Encoding and publishing GPSEvent to IO...");
164171
if (!WsV2.PublishSignal(
165172
wippersnapper_signal_DeviceToBroker_gps_event_tag,
166173
_gps_model->GetGPSEvent())) {
@@ -171,12 +178,12 @@ void GPSController::update() {
171178
} else {
172179
// Log the GPSEvent to SD card
173180
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-
} */
181+
/* if
182+
(!WsV2._sdCardV2->LogEventGps(_gps_model->GetGPSEvent())) {
183+
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to log GPSEvent to
184+
SD!"); statusLEDSolid(WS_LED_STATUS_FS_WRITE); } else {
185+
WS_DEBUG_PRINTLN("OK!");
186+
} */
180187
}
181188
} else {
182189
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to encode GPSEvent!");

src/components/i2c/controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ void I2cController::update() {
14031403

14041404
// Handle the DeviceEvent message
14051405
if (WsV2._sdCardV2->isModeOffline()) {
1406-
if (!WsV2._sdCardV2->LogI2cDeviceEvent(_i2c_model->GetI2cDeviceEvent())) {
1406+
if (!WsV2._sdCardV2->LogEventI2c(_i2c_model->GetI2cDeviceEvent())) {
14071407
WS_DEBUG_PRINTLN(
14081408
"[i2c] ERROR: Unable to log the I2cDeviceEvent to SD!");
14091409
statusLEDSolid(WS_LED_STATUS_FS_WRITE);

src/components/uart/controller.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ void UARTController::update() {
246246
return; // bail-out
247247

248248
for (drvUartBase *drv : _uart_drivers) {
249-
250249
size_t num_sensors = drv->GetNumSensors();
251250
if (num_sensors == 0) {
252251
WS_DEBUG_PRINTLN("[uart] No sensors available for driver: " +
@@ -275,30 +274,29 @@ void UARTController::update() {
275274
}
276275

277276
// Encode the UART input event message
278-
if (_uart_model->EncodeUartInputEvent()) {
279-
// Handle the UartInputEvent message
280-
if (WsV2._sdCardV2->isModeOffline()) {
281-
// TODO: This is UNIMPLEMENTED!
282-
// In offline mode, log to SD card
283-
/* if
284-
(!WsV2._sdCardV2->LogUartInputEvent(_uart_model->GetUartInputEventMsg()))
285-
{ WS_DEBUG_PRINTLN("[uart] ERROR: Unable to log the UartInputEvent to
286-
SD!"); statusLEDSolid(WS_LED_STATUS_FS_WRITE);
287-
} */
288-
} else {
289-
// In online mode, publish to Adafruit IO
290-
if (!WsV2.PublishSignal(
291-
wippersnapper_signal_DeviceToBroker_uart_input_event_tag,
292-
_uart_model->GetUartInputEventMsg())) {
293-
WS_DEBUG_PRINTLN(
294-
"[uart] ERROR: Unable to publish UartInputEvent to IO!");
295-
}
277+
if (!_uart_model->EncodeUartInputEvent()) {
278+
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to encode UartInputEvent!");
279+
continue; // skip this driver if encoding failed
280+
}
281+
// Process the UartInputEvent message
282+
if (!WsV2._sdCardV2->isModeOffline()) {
283+
WS_DEBUG_PRINT("[uart] Publishing UartInputEvent to IO...");
284+
if (!WsV2.PublishSignal(
285+
wippersnapper_signal_DeviceToBroker_uart_input_event_tag,
286+
_uart_model->GetUartInputEventMsg())) {
287+
WS_DEBUG_PRINTLN(
288+
"[uart] ERROR: Unable to publish UartInputEvent to IO!");
296289
}
290+
WS_DEBUG_PRINTLN("OK!");
297291
} else {
298-
WS_DEBUG_PRINTLN(
299-
"[uart] ERROR: Failed to encode UartInputEvent message!");
292+
WS_DEBUG_PRINT("[uart] Logging UartInputEvent to SD card...");
293+
if (!WsV2._sdCardV2->LogEventUart(_uart_model->GetUartInputEventMsg())) {
294+
WS_DEBUG_PRINTLN(
295+
"[uart] ERROR: Unable to log UartInputEvent to SD card!");
296+
}
297+
WS_DEBUG_PRINTLN("OK!");
300298
}
301-
// Update the driver's previous period timestamp
299+
// Update the timestamp
302300
cur_time = millis();
303301
drv->SetSensorPeriodPrv(cur_time);
304302
}

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ bool ws_sdcard::ParseI2cDeviceAddReplace(
628628
WS_DEBUG_PRINTLN(is_gps ? "true" : "false");
629629
if (is_gps) {
630630
msg_i2c_add.is_gps = true;
631-
msg_i2c_add.has_gps_config = false; // TODO: Set to true!
631+
msg_i2c_add.gps_config.period = component["period"] | 15.0;
632+
msg_i2c_add.has_gps_config = true;
632633
return true; // early-out, we don't need to set sensor types for GPS
633634
}
634635

@@ -903,6 +904,7 @@ bool ws_sdcard::ParseFileConfig() {
903904
*/
904905
/**************************************************************************/
905906
bool ws_sdcard::ParseComponents(JsonArray &components) {
907+
delay(9000);
906908
if (components.isNull()) {
907909
WS_DEBUG_PRINTLN("[SD] Error: File missing required components[] array");
908910
return false;
@@ -1229,8 +1231,8 @@ bool ws_sdcard::LogJSONDoc(JsonDocument &doc) {
12291231
@returns True if the event was successfully logged, False otherwise.
12301232
*/
12311233
/**************************************************************************/
1232-
bool ws_sdcard::LogGPIOSensorEventToSD(
1233-
uint8_t pin, float value, wippersnapper_sensor_SensorType read_type) {
1234+
bool ws_sdcard::LogEventGpio(uint8_t pin, float value,
1235+
wippersnapper_sensor_SensorType read_type) {
12341236
JsonDocument doc;
12351237
BuildJSONDoc(doc, pin, value, read_type);
12361238
if (!LogJSONDoc(doc))
@@ -1250,8 +1252,8 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
12501252
@returns True if the event was successfully logged, False otherwise.
12511253
*/
12521254
/**************************************************************************/
1253-
bool ws_sdcard::LogGPIOSensorEventToSD(
1254-
uint8_t pin, uint16_t value, wippersnapper_sensor_SensorType read_type) {
1255+
bool ws_sdcard::LogEventGpio(uint8_t pin, uint16_t value,
1256+
wippersnapper_sensor_SensorType read_type) {
12551257
JsonDocument doc;
12561258
BuildJSONDoc(doc, pin, value, read_type);
12571259
if (!LogJSONDoc(doc))
@@ -1271,8 +1273,8 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
12711273
@returns True if the event was successfully logged, False otherwise.
12721274
*/
12731275
/**************************************************************************/
1274-
bool ws_sdcard::LogGPIOSensorEventToSD(
1275-
uint8_t pin, bool value, wippersnapper_sensor_SensorType read_type) {
1276+
bool ws_sdcard::LogEventGpio(uint8_t pin, bool value,
1277+
wippersnapper_sensor_SensorType read_type) {
12761278
JsonDocument doc;
12771279
BuildJSONDoc(doc, pin, value, read_type);
12781280
if (!LogJSONDoc(doc))
@@ -1292,8 +1294,7 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
12921294
@returns True if the event was successfully logged, False otherwise.
12931295
*/
12941296
/**************************************************************************/
1295-
bool ws_sdcard::LogDS18xSensorEventToSD(
1296-
wippersnapper_ds18x20_Ds18x20Event *event_msg) {
1297+
bool ws_sdcard::LogEventDs18x(wippersnapper_ds18x20_Ds18x20Event *event_msg) {
12971298
JsonDocument doc;
12981299
// Iterate over the event message's sensor events
12991300
// TODO: Standardize this Event with I2C
@@ -1314,7 +1315,7 @@ bool ws_sdcard::LogDS18xSensorEventToSD(
13141315
The I2cDeviceEvent message to log.
13151316
@returns True if the event was successfully logged, False otherwise.
13161317
*/
1317-
bool ws_sdcard::LogI2cDeviceEvent(
1318+
bool ws_sdcard::LogEventI2c(
13181319
wippersnapper_i2c_I2cDeviceEvent *msg_device_event) {
13191320
JsonDocument doc;
13201321
// Pull the DeviceDescriptor out
@@ -1349,56 +1350,85 @@ bool ws_sdcard::LogI2cDeviceEvent(
13491350
The GPSEvent message to log.
13501351
@returns True if the event was successfully logged, False otherwise.
13511352
*/
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;
1353+
bool ws_sdcard::LogEventGps(wippersnapper_gps_GPSEvent *msg_gps_event) {
1354+
JsonDocument doc;
1355+
1356+
// Log RMC responses
1357+
for (pb_size_t rmc_resp = 0; rmc_resp < msg_gps_event->rmc_responses_count;
1358+
rmc_resp++) {
1359+
WS_DEBUG_PRINTLN("[SD] Logging RMC response...");
1360+
// Log GPS DateTime
1361+
if (msg_gps_event->rmc_responses[rmc_resp].has_datetime) {
1362+
wippersnapper_gps_GPSDateTime gps_dt =
1363+
msg_gps_event->rmc_responses[rmc_resp].datetime;
1364+
DateTime gps_datetime(gps_dt.year, gps_dt.month, gps_dt.day, gps_dt.hour,
1365+
gps_dt.minute, gps_dt.seconds);
1366+
doc["timestamp"] = gps_datetime.unixtime();
13751367
}
1368+
// Log GPS data
1369+
doc["fix_status"] = msg_gps_event->rmc_responses[rmc_resp].fix_status;
1370+
doc["latitude"] = msg_gps_event->rmc_responses[rmc_resp].lat;
1371+
doc["lat_dir"] = msg_gps_event->rmc_responses[rmc_resp].lat_dir;
1372+
doc["longitude"] = msg_gps_event->rmc_responses[rmc_resp].lon;
1373+
doc["lon_dir"] = msg_gps_event->rmc_responses[rmc_resp].lon_dir;
1374+
doc["speed"] = msg_gps_event->rmc_responses[rmc_resp].speed;
1375+
doc["angle"] = msg_gps_event->rmc_responses[rmc_resp].angle;
1376+
if (!LogJSONDoc(doc))
1377+
return false;
1378+
}
13761379

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;
1380+
// Log GGA responses
1381+
for (pb_size_t gga_resp = 0; gga_resp < msg_gps_event->gga_responses_count;
1382+
gga_resp++) {
1383+
WS_DEBUG_PRINTLN("[SD] Logging GGA response...");
1384+
// Log GPS DateTime
1385+
if (msg_gps_event->gga_responses[gga_resp].has_datetime) {
1386+
wippersnapper_gps_GPSDateTime gps_dt =
1387+
msg_gps_event->gga_responses[gga_resp].datetime;
1388+
DateTime gps_datetime(gps_dt.year, gps_dt.month, gps_dt.day, gps_dt.hour,
1389+
gps_dt.minute, gps_dt.seconds);
1390+
doc["timestamp"] = gps_datetime.unixtime();
13991391
}
1392+
// Log GPS data
1393+
doc["latitude"] = msg_gps_event->gga_responses[gga_resp].lat;
1394+
doc["lat_dir"] = msg_gps_event->gga_responses[gga_resp].lat_dir;
1395+
doc["longitude"] = msg_gps_event->gga_responses[gga_resp].lon;
1396+
doc["lon_dir"] = msg_gps_event->gga_responses[gga_resp].lon_dir;
1397+
doc["fix_quality"] = msg_gps_event->gga_responses[gga_resp].fix_quality;
1398+
doc["num_satellites"] =
1399+
msg_gps_event->gga_responses[gga_resp].num_satellites;
1400+
doc["hdop"] = msg_gps_event->gga_responses[gga_resp].hdop;
1401+
doc["altitude"] = msg_gps_event->gga_responses[gga_resp].altitude;
1402+
doc["geoid_height"] = msg_gps_event->gga_responses[gga_resp].geoid_height;
1403+
if (!LogJSONDoc(doc))
1404+
return false;
1405+
}
1406+
1407+
return true;
1408+
}
1409+
1410+
/*!
1411+
@brief Logs a UART input event to the SD card.
1412+
@param msg_uart_input_event
1413+
The UartInputEvent message to log.
1414+
@returns True if the event was successfully logged, False otherwise.
1415+
*/
1416+
bool ws_sdcard::LogEventUart(
1417+
wippersnapper_uart_UartInputEvent *msg_uart_input_event) {
1418+
JsonDocument doc;
1419+
doc["timestamp"] = GetTimestamp();
1420+
doc["uart_device_id"] = msg_uart_input_event->device_id;
1421+
doc["uart_port"] = msg_uart_input_event->uart_nbr;
1422+
1423+
// Log each event
1424+
for (pb_size_t i = 0; i < msg_uart_input_event->events_count; i++) {
1425+
doc["value"] = msg_uart_input_event->events[i].value.float_value;
1426+
doc["si_unit"] = SensorTypeToSIUnit(msg_uart_input_event->events[i].type);
1427+
}
14001428

1401-
return true;
1429+
if (!LogJSONDoc(doc))
1430+
return false;
1431+
return true;
14021432
}
14031433

14041434
#ifdef OFFLINE_MODE_DEBUG

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ class ws_sdcard {
6262
bool CreateNewLogFile();
6363
bool isModeOffline() { return is_mode_offline; }
6464
void waitForSerialConfig();
65-
bool LogGPIOSensorEventToSD(uint8_t pin, float value,
66-
wippersnapper_sensor_SensorType read_type);
67-
bool LogGPIOSensorEventToSD(uint8_t pin, bool value,
68-
wippersnapper_sensor_SensorType read_type);
69-
bool LogGPIOSensorEventToSD(uint8_t pin, uint16_t value,
70-
wippersnapper_sensor_SensorType read_type);
71-
bool LogDS18xSensorEventToSD(wippersnapper_ds18x20_Ds18x20Event *event_msg);
72-
bool LogI2cDeviceEvent(wippersnapper_i2c_I2cDeviceEvent *msg_device_event);
73-
bool LogGPSEventToSD(wippersnapper_gps_GPSEvent *msg_gps_event);
65+
bool LogEventGpio(uint8_t pin, float value,
66+
wippersnapper_sensor_SensorType read_type);
67+
bool LogEventGpio(uint8_t pin, bool value,
68+
wippersnapper_sensor_SensorType read_type);
69+
bool LogEventGpio(uint8_t pin, uint16_t value,
70+
wippersnapper_sensor_SensorType read_type);
71+
bool LogEventDs18x(wippersnapper_ds18x20_Ds18x20Event *event_msg);
72+
bool LogEventI2c(wippersnapper_i2c_I2cDeviceEvent *msg_device_event);
73+
bool LogEventGps(wippersnapper_gps_GPSEvent *msg_gps_event);
74+
bool LogEventUart(wippersnapper_uart_UartInputEvent *msg_uart_input_event);
75+
7476
private:
7577
bool ParseExportedFromDevice(JsonDocument &doc);
7678
void ConfigureSDCard();

0 commit comments

Comments
 (0)