Skip to content

Commit 8800c78

Browse files
committed
SD - Parse UART GPS
1 parent 11062ed commit 8800c78

File tree

4 files changed

+164
-44
lines changed

4 files changed

+164
-44
lines changed

src/Wippersnapper_demo.ino.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmpk62p1kwv"
2+
#include <Arduino.h>
3+
# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
4+
# 11 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
5+
#include "ws_adapters.h"
6+
#if defined(ARDUINO_RASPBERRY_PI_PICO_2) || \
7+
defined(ARDUINO_RASPBERRY_PI_PICO) || \
8+
defined(ARDUINO_ADAFRUIT_FEATHER_RP2040_ADALOGGER) || \
9+
defined(ARDUINO_ADAFRUIT_METRO_RP2350)
10+
ws_adapter_offline wipper;
11+
#else
12+
ws_adapter_wifi wipper;
13+
#endif
14+
15+
#define WS_DEBUG
16+
void setup();
17+
void loop();
18+
#line 23 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
19+
void setup() {
20+
Serial.begin(115200);
21+
wipper.provision();
22+
wipper.connect();
23+
}
24+
25+
void loop() {
26+
wipper.run();
27+
}

src/components/uart/controller.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
8888
cfg_device.device_id, cfg_serial.uart_nbr);
8989
uart_driver->ConfigureDriver(cfg_device);
9090
uart_driver->EnableSensorEvents(
91-
cfg_device.config.generic_uart_input.i2c_device_sensor_types,
92-
cfg_device.config.generic_uart_input.i2c_device_sensor_types_count);
91+
cfg_device.config.generic_uart_input.sensor_types,
92+
cfg_device.config.generic_uart_input.sensor_types_count);
9393
uart_driver->SetSensorPeriod(cfg_device.config.generic_uart_input.period);
9494
WS_DEBUG_PRINT("added!");
9595
} else {
@@ -122,8 +122,8 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
122122
cfg_device.device_id, cfg_serial.uart_nbr);
123123
uart_driver->ConfigureDriver(cfg_device);
124124
uart_driver->EnableSensorEvents(
125-
cfg_device.config.pm25aqi.i2c_device_sensor_types,
126-
cfg_device.config.pm25aqi.i2c_device_sensor_types_count);
125+
cfg_device.config.pm25aqi.sensor_types,
126+
cfg_device.config.pm25aqi.sensor_types_count);
127127
uart_driver->SetSensorPeriod(cfg_device.config.pm25aqi.period);
128128
WS_DEBUG_PRINT("added!");
129129
break;
@@ -173,7 +173,7 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
173173
/* if
174174
(!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_uart_added_tag,
175175
_uart_model->GetUartAddedMsg())) {
176-
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to publish UartAdded message to
176+
WS_DEBUG_PRINTLN("[uart] ERROR: Unable to publish UartAdded message to
177177
IO!"); return false;
178178
} */
179179
WS_DEBUG_PRINTLN("[uart] UartAdded message published successfully!");

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 126 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ uint32_t ws_sdcard::HexStrToInt(const char *hex_str) {
466466
return std::stoi(hex_str, nullptr, 16);
467467
}
468468

469-
470469
/*!
471470
@brief Parses a Uartadd message from the JSON configuration file.
472471
@param component
@@ -475,56 +474,143 @@ uint32_t ws_sdcard::HexStrToInt(const char *hex_str) {
475474
The UartAdd message to populate.
476475
@returns True if the message was successfully populated, False otherwise.
477476
*/
478-
bool ws_sdcard::ParseUartAdd(JsonObject &component, wippersnapper_uart_UartAdd &msg_uart_add) {
477+
bool ws_sdcard::ParseUartAdd(JsonObject &component,
478+
wippersnapper_uart_UartAdd &msg_uart_add) {
479479
// Configure the Serial
480480
msg_uart_add.has_cfg_serial = true;
481-
strncpy(msg_uart_add.cfg_serial.pin_rx, component["pinRx"] | UNKNOWN_VALUE, sizeof(msg_uart_add.cfg_serial.pin_rx) - 1);
482-
strncpy(msg_uart_add.cfg_serial.pin_tx, component["pinTx"] | UNKNOWN_VALUE, sizeof(msg_uart_add.cfg_serial.pin_tx) - 1);
481+
strncpy(msg_uart_add.cfg_serial.pin_rx, component["pinRx"] | UNKNOWN_VALUE,
482+
sizeof(msg_uart_add.cfg_serial.pin_rx) - 1);
483+
strncpy(msg_uart_add.cfg_serial.pin_tx, component["pinTx"] | UNKNOWN_VALUE,
484+
sizeof(msg_uart_add.cfg_serial.pin_tx) - 1);
483485
msg_uart_add.cfg_serial.uart_nbr = component["uartNbr"] | 0;
484486
msg_uart_add.cfg_serial.baud_rate = component["baudRate"] | 9600;
485-
msg_uart_add.cfg_serial.format = wippersnapper_uart_UartPacketFormat_UART_PACKET_FORMAT_8N1; // Stick to default 8N1 for now
486-
msg_uart_add.cfg_serial.timeout = component["timeout"] | 1000; // Use a default UART timeout of 1000ms
487+
msg_uart_add.cfg_serial.format =
488+
wippersnapper_uart_UartPacketFormat_UART_PACKET_FORMAT_8N1; // Stick to
489+
// default 8N1
490+
// for now
491+
msg_uart_add.cfg_serial.timeout =
492+
component["timeout"] | 1000; // Use a default UART timeout of 1000ms
487493
msg_uart_add.cfg_serial.use_sw_serial = component["useSwSerial"] | false;
488-
msg_uart_add.cfg_serial.sw_serial_invert = component["swSerialInvert"] | false;
489-
// Configure the UART device
494+
msg_uart_add.cfg_serial.sw_serial_invert =
495+
component["swSerialInvert"] | false;
496+
// Configure the Device
490497
msg_uart_add.has_cfg_device = true;
491-
strncpy(msg_uart_add.cfg_device.device_id, component["deviceId"] | UNKNOWN_VALUE, sizeof(msg_uart_add.cfg_device.device_id) - 1);
492-
// set UartDeviceType
498+
strncpy(msg_uart_add.cfg_device.device_id,
499+
component["deviceId"] | UNKNOWN_VALUE,
500+
sizeof(msg_uart_add.cfg_device.device_id) - 1);
493501
const char *device_type = component["deviceType"] | "UNKNOWN";
494502
if (strcmp(device_type, "GPS") == 0) {
495-
msg_uart_add.cfg_device.device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS;
496-
// Fill the config field
503+
msg_uart_add.cfg_device.device_type =
504+
wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS;
505+
msg_uart_add.cfg_device.which_config =
506+
wippersnapper_uart_UartDeviceConfig_gps_tag;
507+
if (!ParseGPSConfig(component, &msg_uart_add.cfg_device.config.gps)) {
508+
WS_DEBUG_PRINTLN(
509+
"[SD] Parsing Error: Failed to parse GPS configuration!");
510+
return false;
511+
}
497512
} else if (strcmp(device_type, "PM25AQI") == 0) {
498-
msg_uart_add.cfg_device.device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI;
499-
msg_uart_add.cfg_device.which_config = wippersnapper_uart_UartDeviceConfig_pm25aqi_tag;
513+
WS_DEBUG_PRINTLN("[SD] Parsing PM25AQI device type");
514+
msg_uart_add.cfg_device.device_type =
515+
wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI;
516+
msg_uart_add.cfg_device.which_config =
517+
wippersnapper_uart_UartDeviceConfig_pm25aqi_tag;
500518
msg_uart_add.cfg_device.config.pm25aqi.period = component["period"] | 0.0;
501-
msg_uart_add.cfg_device.config.pm25aqi.is_pm1006 = component["isPm1006"] | false;
519+
msg_uart_add.cfg_device.config.pm25aqi.is_pm1006 =
520+
component["isPm1006"] | false;
502521
// Fill sensor types
503522
pb_size_t sensor_type_count = 0;
504523
for (JsonObject sensor_type : component["SensorTypes"].as<JsonArray>()) {
505-
msg_uart_add.cfg_device.config.pm25aqi.sensor_types[sensor_type_count] = ParseSensorType(sensor_type["type"]);
506-
sensor_type_count++;
524+
msg_uart_add.cfg_device.config.pm25aqi.sensor_types[sensor_type_count] =
525+
ParseSensorType(sensor_type["type"]);
526+
sensor_type_count++;
507527
}
508-
msg_uart_add.cfg_device.config.pm25aqi.sensor_types_count = sensor_type_count;
528+
msg_uart_add.cfg_device.config.pm25aqi.sensor_types_count =
529+
sensor_type_count;
509530
} else if (strcmp(device_type, "GENERIC-INPUT") == 0) {
510-
// TODO: Fill device name (requires an update to uart.pb.h so it's not a pb_callback field)
511-
msg_uart_add.cfg_device.device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GENERIC_INPUT;
512-
msg_uart_add.cfg_device.which_config = wippersnapper_uart_UartDeviceConfig_generic_uart_input_tag;
513-
msg_uart_add.cfg_device.config.generic_uart_input.line_ending = ParseUartLineEnding(component["lineEnding"] | "LF");
514-
msg_uart_add.cfg_device.config.generic_uart_input.period = component["period"] | 0.0;
531+
WS_DEBUG_PRINTLN("[SD] Parsing GENERIC-INPUT device type");
532+
// TODO: Fill device name (requires an update to uart.pb.h so it's not a
533+
// pb_callback field)
534+
msg_uart_add.cfg_device.device_type =
535+
wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GENERIC_INPUT;
536+
msg_uart_add.cfg_device.which_config =
537+
wippersnapper_uart_UartDeviceConfig_generic_uart_input_tag;
538+
msg_uart_add.cfg_device.config.generic_uart_input.line_ending =
539+
ParseUartLineEnding(component["lineEnding"] | "LF");
540+
msg_uart_add.cfg_device.config.generic_uart_input.period =
541+
component["period"] | 0.0;
515542
// Fill sensor types
516543
pb_size_t sensor_type_count = 0;
517544
for (JsonObject sensor_type : component["SensorTypes"].as<JsonArray>()) {
518-
msg_uart_add.cfg_device.config.generic_uart_input.sensor_types[sensor_type_count] = ParseSensorType(sensor_type["type"]);
519-
sensor_type_count++;
545+
msg_uart_add.cfg_device.config.generic_uart_input
546+
.sensor_types[sensor_type_count] =
547+
ParseSensorType(sensor_type["type"]);
548+
sensor_type_count++;
520549
}
521-
msg_uart_add.cfg_device.config.generic_uart_input.sensor_types_count = sensor_type_count;
550+
msg_uart_add.cfg_device.config.generic_uart_input.sensor_types_count =
551+
sensor_type_count;
522552
} else {
523-
WS_DEBUG_PRINTLN("[SD] Parsing Error: Unknown UART device type found: " + String(device_type));
553+
WS_DEBUG_PRINTLN("[SD] Parsing Error: Unknown UART device type found: " +
554+
String(device_type));
524555
return false;
525556
}
557+
return true;
558+
}
526559

527-
560+
/*!
561+
@brief Parses a GPS configuration from the JSON configuration file.
562+
@param gps_config
563+
The JSON object containing GPS configuration.
564+
@param cfg_gps
565+
The GPSConfig structure to populate.
566+
@returns True if the GPS configuration was successfully parsed, False
567+
otherwise.
568+
*/
569+
bool ws_sdcard::ParseGPSConfig(JsonObject &gps_config,
570+
wippersnapper_gps_GPSConfig *cfg_gps) {
571+
cfg_gps->period = gps_config["period"] | 15000; // Default period 15 seconds
572+
// Parse PMTK or UBX commands if they exist
573+
if (gps_config["commands_pmtks"].is<JsonArray>() &&
574+
sizeof(cfg_gps->commands_pmtks) > 0) {
575+
WS_DEBUG_PRINTLN("[SD] Parsing PMTK commands...");
576+
pb_size_t pmtk_cmd_count = 0;
577+
JsonArray pmtk_array = gps_config["commands_pmtks"];
578+
for (JsonVariant pmtk_cmd : pmtk_array) {
579+
if (pmtk_cmd_count >= 8) {
580+
WS_DEBUG_PRINTLN("[SD] Warning: Too many PMTK commands, skipping...");
581+
break;
582+
}
583+
strlcpy(cfg_gps->commands_pmtks[pmtk_cmd_count],
584+
pmtk_cmd.as<const char *>(), 90);
585+
pmtk_cmd_count++;
586+
}
587+
cfg_gps->commands_pmtks_count = pmtk_cmd_count;
588+
} else if (gps_config["commands_ubxes"].is<JsonArray>() &&
589+
sizeof(cfg_gps->commands_ubxes) > 0) {
590+
WS_DEBUG_PRINTLN("[SD] Parsing UBX commands...");
591+
pb_size_t ubx_cmd_count = 0;
592+
JsonArray ubx_array = gps_config["commands_ubxes"];
593+
for (JsonVariant ubx_cmd : ubx_array) {
594+
if (ubx_cmd_count >= 8) {
595+
WS_DEBUG_PRINTLN("[SD] Warning: Too many UBX commands, skipping...");
596+
break;
597+
}
598+
JsonArray ubx_bytes = ubx_cmd.as<JsonArray>();
599+
for (JsonVariant byte_val : ubx_bytes) {
600+
if (cfg_gps->commands_ubxes[ubx_cmd_count].size >= 128) {
601+
WS_DEBUG_PRINTLN("[SD] Warning: UBX command too long, skipping...");
602+
break;
603+
}
604+
cfg_gps->commands_ubxes[ubx_cmd_count]
605+
.bytes[cfg_gps->commands_ubxes[ubx_cmd_count].size] =
606+
byte_val.as<uint8_t>();
607+
cfg_gps->commands_ubxes[ubx_cmd_count].size++;
608+
}
609+
}
610+
cfg_gps->commands_ubxes_count = ubx_cmd_count;
611+
} else {
612+
WS_DEBUG_PRINTLN("[SD] No PMTK or UBX commands found for GPS!");
613+
}
528614
return true;
529615
}
530616

@@ -924,15 +1010,18 @@ bool ws_sdcard::ParseComponents(JsonArray &components) {
9241010
_cfg_i2c_addresses.push_back(
9251011
msg_add.i2c_device_description.i2c_device_address);
9261012
}
927-
} else if (strcmp(component_api_type, "uart") ==0) {
928-
WS_DEBUG_PRINTLN("[SD] UART component found in cfg");
929-
wippersnapper_uart_UartAdd msg_uart_add = wippersnapper_uart_UartAdd_init_default;
930-
success = ParseUartAdd(component, msg_uart_add);
931-
if (success) {
932-
WS_DEBUG_PRINTLN("[SD] UART component parsed successfully, adding to shared buffer..");
933-
msg_signal_b2d.which_payload = wippersnapper_signal_BrokerToDevice_uart_add_tag;
934-
msg_signal_b2d.payload.uart_add = msg_uart_add;
935-
}
1013+
} else if (strcmp(component_api_type, "uart") == 0) {
1014+
WS_DEBUG_PRINTLN("[SD] UART component found in cfg");
1015+
wippersnapper_uart_UartAdd msg_uart_add =
1016+
wippersnapper_uart_UartAdd_init_default;
1017+
success = ParseUartAdd(component, msg_uart_add);
1018+
if (success) {
1019+
WS_DEBUG_PRINTLN("[SD] UART component parsed successfully, adding to "
1020+
"shared buffer..");
1021+
msg_signal_b2d.which_payload =
1022+
wippersnapper_signal_BrokerToDevice_uart_add_tag;
1023+
msg_signal_b2d.payload.uart_add = msg_uart_add;
1024+
}
9361025
} else {
9371026
WS_DEBUG_PRINTLN("[SD] Error: Unknown Component API: " +
9381027
String(component_api_type));

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class ws_sdcard {
8686
void TickSoftRTC();
8787
uint32_t GetSoftRTCTime();
8888
wippersnapper_sensor_SensorType ParseSensorType(const char *sensor_type);
89-
wippersnapper_uart_GenericDeviceLineEnding ParseUartLineEnding(const char *line_ending);
89+
wippersnapper_uart_GenericDeviceLineEnding
90+
ParseUartLineEnding(const char *line_ending);
9091
bool ParseComponents(JsonArray &components);
9192
bool
9293
ParseDigitalIOAdd(JsonObject &component,
@@ -95,7 +96,10 @@ class ws_sdcard {
9596
wippersnapper_analogio_AnalogIOAdd &msg_AnalogIOAdd);
9697
bool ParseDS18xAdd(JsonObject &component,
9798
wippersnapper_ds18x20_Ds18x20Add &msg_ds18x20_add);
98-
bool ParseUartAdd(JsonObject &component, wippersnapper_uart_UartAdd &msg_uart_add);
99+
bool ParseUartAdd(JsonObject &component,
100+
wippersnapper_uart_UartAdd &msg_uart_add);
101+
bool ParseGPSConfig(JsonObject &gps_config,
102+
wippersnapper_gps_GPSConfig *cfg_gps);
99103
bool ParseI2cDeviceAddReplace(
100104
JsonObject &component,
101105
wippersnapper_i2c_I2cDeviceAddOrReplace &msg_i2c_device_add_replace);

0 commit comments

Comments
 (0)