@@ -443,6 +443,52 @@ uint32_t ws_sdcard::HexStrToInt(const char *hex_str) {
443
443
return std::stoi (hex_str, nullptr , 16 );
444
444
}
445
445
446
+
447
+ /* !
448
+ @brief Parses a Uartadd message from the JSON configuration file.
449
+ @param component
450
+ The JSON object to parse.
451
+ @param msg_uart_add
452
+ The UartAdd message to populate.
453
+ @returns True if the message was successfully populated, False otherwise.
454
+ */
455
+ bool ws_sdcard::ParseUartAdd (JsonObject &component, wippersnapper_uart_UartAdd &msg_uart_add) {
456
+ // Configure the Serial
457
+ msg_uart_add.has_cfg_serial = true ;
458
+ strncpy (msg_uart_add.cfg_serial .pin_rx , component[" pinRx" ] | UNKNOWN_VALUE, sizeof (msg_uart_add.cfg_serial .pin_rx ) - 1 );
459
+ strncpy (msg_uart_add.cfg_serial .pin_tx , component[" pinTx" ] | UNKNOWN_VALUE, sizeof (msg_uart_add.cfg_serial .pin_tx ) - 1 );
460
+ msg_uart_add.cfg_serial .uart_nbr = component[" uartNbr" ] | 0 ;
461
+ msg_uart_add.cfg_serial .baud_rate = component[" baudRate" ] | 9600 ;
462
+ msg_uart_add.cfg_serial .format = wippersnapper_uart_UartPacketFormat_UART_PACKET_FORMAT_8N1; // Stick to default 8N1 for now
463
+ msg_uart_add.cfg_serial .timeout = component[" timeout" ] | 1000 ; // Use a default UART timeout of 1000ms
464
+ msg_uart_add.cfg_serial .use_sw_serial = component[" useSwSerial" ] | false ;
465
+ msg_uart_add.cfg_serial .sw_serial_invert = component[" swSerialInvert" ] | false ;
466
+ // Configure the UART device
467
+ msg_uart_add.has_cfg_device = true ;
468
+ strncpy (msg_uart_add.cfg_device .device_id , component[" deviceId" ] | UNKNOWN_VALUE, sizeof (msg_uart_add.cfg_device .device_id ) - 1 );
469
+ // set UartDeviceType
470
+ const char *device_type = component[" deviceType" ] | " UNKNOWN" ;
471
+ if (strcmp (device_type, " GPS" ) == 0 ) {
472
+ msg_uart_add.cfg_device .device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS;
473
+ // Fill the config field
474
+ } else if (strcmp (device_type, " PM25AQI" ) == 0 ) {
475
+ msg_uart_add.cfg_device .device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI;
476
+ } else if (strcmp (device_type, " GENERIC-INPUT" ) == 0 ) {
477
+ msg_uart_add.cfg_device .device_type = wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GENERIC_INPUT;
478
+ msg_uart_add.cfg_device .config .generic_uart_input .period = component[" period" ] | 0.0 ;
479
+ // Fill sensor types
480
+ // TODO: Needs new PB!
481
+ // msg_uart_add.cfg_device.config.generic_uart_input.i2c_ = 0;
482
+
483
+ } else {
484
+ WS_DEBUG_PRINTLN (" [SD] Parsing Error: Unknown UART device type found: " + String (device_type));
485
+ return false ;
486
+ }
487
+
488
+
489
+ return true ;
490
+ }
491
+
446
492
/* *************************************************************************/
447
493
/* !
448
494
@brief Parses a DS18x20Add message from the JSON configuration file.
@@ -839,6 +885,15 @@ bool ws_sdcard::ParseComponents(JsonArray &components) {
839
885
_cfg_i2c_addresses.push_back (
840
886
msg_add.i2c_device_description .i2c_device_address );
841
887
}
888
+ } else if (strcmp (component_api_type, " uart" ) ==0 ) {
889
+ WS_DEBUG_PRINTLN (" [SD] UART component found in cfg" );
890
+ wippersnapper_uart_UartAdd msg_uart_add = wippersnapper_uart_UartAdd_init_default;
891
+ success = ParseUartAdd (component, msg_uart_add);
892
+ if (success) {
893
+ WS_DEBUG_PRINTLN (" [SD] UART component parsed successfully, adding to shared buffer.." );
894
+ msg_signal_b2d.which_payload = wippersnapper_signal_BrokerToDevice_uart_add_tag;
895
+ msg_signal_b2d.payload .uart_add = msg_uart_add;
896
+ }
842
897
} else {
843
898
WS_DEBUG_PRINTLN (" [SD] Error: Unknown Component API: " +
844
899
String (component_api_type));
0 commit comments