@@ -1659,7 +1659,8 @@ void cbSignalUARTReq(char *data, uint16_t len) {
1659
1659
}
1660
1660
1661
1661
/* !
1662
- @brief Deserializes a DisplayRequest message and sends it to the display component.
1662
+ @brief Deserializes a DisplayRequest message and sends it to the display
1663
+ component.
1663
1664
@param stream
1664
1665
Incoming data stream from buffer.
1665
1666
@param field
@@ -1668,11 +1669,13 @@ void cbSignalUARTReq(char *data, uint16_t len) {
1668
1669
Optional arguments from decoder calling function.
1669
1670
@returns True if decoded successfully, False otherwise.
1670
1671
*/
1671
- bool cbDecodeDisplayMsg (pb_istream_t *stream, const pb_field_t *field, void **arg) {
1672
+ bool cbDecodeDisplayMsg (pb_istream_t *stream, const pb_field_t *field,
1673
+ void **arg) {
1672
1674
if (field->tag == wippersnapper_signal_v1_DisplayRequest_display_add_tag) {
1673
1675
1674
1676
// Decode message into a DisplayAddRequest
1675
- wippersnapper_display_v1_DisplayAddOrReplace msgAddReq = wippersnapper_display_v1_DisplayAddOrReplace_init_zero;
1677
+ wippersnapper_display_v1_DisplayAddOrReplace msgAddReq =
1678
+ wippersnapper_display_v1_DisplayAddOrReplace_init_zero;
1676
1679
if (!ws_pb_decode (stream,
1677
1680
wippersnapper_display_v1_DisplayAddOrReplace_fields,
1678
1681
&msgAddReq)) {
@@ -1681,8 +1684,10 @@ bool cbDecodeDisplayMsg(pb_istream_t *stream, const pb_field_t *field, void **ar
1681
1684
}
1682
1685
1683
1686
// Attempt to add or replace a display component
1684
- bool did_add = WS._displayController ->Handle_Display_AddOrReplace (&msgAddReq);
1685
- // TODO: Add response handling and publishing here, for now it always returns true and doesnt publish back to the broker
1687
+ bool did_add =
1688
+ WS._displayController ->Handle_Display_AddOrReplace (&msgAddReq);
1689
+ // TODO: Add response handling and publishing here, for now it always
1690
+ // returns true and doesnt publish back to the broker
1686
1691
}
1687
1692
return true ;
1688
1693
}
@@ -2406,57 +2411,56 @@ bool Wippersnapper::generateWSTopics() {
2406
2411
return false ;
2407
2412
}
2408
2413
2409
- // Create d2b display topic
2414
+ // /display topic //
2415
+
2416
+ // Pre-determine topic size
2417
+ topicLen = strlen (WS._config .aio_user ) + strlen (" /" ) + strlen (_device_uid) +
2418
+ strlen (" /wprsnpr/" ) + strlen (TOPIC_SIGNALS) + strlen (" broker" ) +
2419
+ strlen (TOPIC_DISPLAY) + 1 ;
2420
+
2421
+ // Pre-allocate memory for topic
2410
2422
#ifdef USE_PSRAM
2411
- WS._topic_signal_display_brkr = (char *)ps_malloc (
2412
- sizeof (char ) * strlen (WS._config .aio_user ) + strlen (" /" ) +
2413
- strlen (_device_uid) + strlen (" /wprsnpr/" ) + strlen (TOPIC_SIGNALS) +
2414
- strlen (" broker" ) + strlen (TOPIC_DISPLAY) + 1 );
2423
+ WS._topic_signal_display_brkr = (char *)ps_malloc (topicLen);
2415
2424
#else
2416
- WS._topic_signal_display_brkr = (char *)malloc (
2417
- sizeof (char ) * strlen (WS._config .aio_user ) + strlen (" /" ) +
2418
- strlen (_device_uid) + strlen (" /wprsnpr/" ) + strlen (TOPIC_SIGNALS) +
2419
- strlen (" broker" ) + strlen (TOPIC_DISPLAY) + 1 );
2425
+ WS._topic_signal_display_brkr = (char *)malloc (topicLen);
2420
2426
#endif
2427
+
2428
+ // Generate the topic
2421
2429
if (WS._topic_signal_display_brkr != NULL ) {
2422
- strcpy (WS._topic_signal_display_brkr , WS._config .aio_user );
2423
- strcat (WS._topic_signal_display_brkr , TOPIC_WS);
2424
- strcat (WS._topic_signal_display_brkr , _device_uid);
2425
- strcat (WS._topic_signal_display_brkr , TOPIC_SIGNALS);
2426
- strcat (WS._topic_signal_display_brkr , " broker" );
2427
- strcat (WS._topic_signal_display_brkr , TOPIC_DISPLAY);
2428
- } else { // malloc failed
2429
- WS_DEBUG_PRINTLN (" ERROR: Failed to add a display topic!" );
2430
+ snprintf (WS._topic_signal_display_brkr , topicLen, " %s/wprsnpr/%s%sbroker%s" ,
2431
+ WS._config .aio_user , _device_uid, TOPIC_SIGNALS, TOPIC_DISPLAY);
2432
+ } else {
2433
+ WS_DEBUG_PRINTLN (
2434
+ " FATAL ERROR: Failed to allocate memory for DISPLAY topic!" );
2430
2435
return false ;
2431
2436
}
2432
2437
2433
- // Subscribe to the display sub-topic
2438
+ // Subscribe to signal's DISPLAY sub-topic and set callback
2434
2439
_topic_signal_display_sub =
2435
2440
new Adafruit_MQTT_Subscribe (WS._mqtt , WS._topic_signal_display_brkr , 1 );
2436
2441
WS._mqtt ->subscribe (_topic_signal_display_sub);
2437
2442
_topic_signal_display_sub->setCallback (cbDisplayMessage);
2438
2443
2439
- // Create a b2d display topic
2444
+ // Calculate length of the topic for device-to-broker DISPLAY topic
2445
+ topicLen = strlen (WS._config .aio_user ) + strlen (" /" ) + strlen (_device_uid) +
2446
+ strlen (" /wprsnpr/" ) + strlen (TOPIC_SIGNALS) + strlen (" device" ) +
2447
+ strlen (TOPIC_DISPLAY) + 1 ;
2448
+
2449
+ // Allocate memory for dynamic MQTT topic
2440
2450
#ifdef USE_PSRAM
2441
- WS._topic_signal_display_device = (char *)ps_malloc (
2442
- sizeof (char ) * strlen (WS._config .aio_user ) + strlen (" /" ) +
2443
- strlen (_device_uid) + strlen (" /wprsnpr/" ) + strlen (TOPIC_SIGNALS) +
2444
- strlen (" device" ) + strlen (TOPIC_DISPLAY) + 1 );
2451
+ WS._topic_signal_display_device = (char *)ps_malloc (topicLen);
2445
2452
#else
2446
- WS._topic_signal_display_device = (char *)malloc (
2447
- sizeof (char ) * strlen (WS._config .aio_user ) + strlen (" /" ) +
2448
- strlen (_device_uid) + strlen (" /wprsnpr/" ) + strlen (TOPIC_SIGNALS) +
2449
- strlen (" device" ) + strlen (TOPIC_DISPLAY) + 1 );
2453
+ WS._topic_signal_display_device = (char *)malloc (topicLen);
2450
2454
#endif
2455
+
2456
+ // Generate the topic if memory was allocated successfully
2451
2457
if (WS._topic_signal_display_device != NULL ) {
2452
- strcpy (WS._topic_signal_display_device , WS._config .aio_user );
2453
- strcat (WS._topic_signal_display_device , TOPIC_WS);
2454
- strcat (WS._topic_signal_display_device , _device_uid);
2455
- strcat (WS._topic_signal_display_device , TOPIC_SIGNALS);
2456
- strcat (WS._topic_signal_display_device , " device" );
2457
- strcat (WS._topic_signal_display_device , TOPIC_DISPLAY);
2458
- } else { // malloc failed
2459
- WS_DEBUG_PRINTLN (" ERROR: Failed to add a display topic!" );
2458
+ snprintf (WS._topic_signal_display_device , topicLen,
2459
+ " %s/wprsnpr/%s%sdevice%s" , WS._config .aio_user , _device_uid,
2460
+ TOPIC_SIGNALS, TOPIC_DISPLAY);
2461
+ } else {
2462
+ WS_DEBUG_PRINTLN (
2463
+ " FATAL ERROR: Failed to allocate memory for DISPLAY topic!" );
2460
2464
return false ;
2461
2465
}
2462
2466
@@ -2953,6 +2957,14 @@ void Wippersnapper::connect() {
2953
2957
WS._ui_helper ->build_scr_monitor ();
2954
2958
#endif
2955
2959
2960
+ WS.pinCfgCompleted = true ;
2961
+
2962
+ // Initialize Digital IO class
2963
+ WS._digitalGPIO = new Wippersnapper_DigitalGPIO (20 );
2964
+ // Initialize Analog IO class
2965
+ WS._analogIO = new Wippersnapper_AnalogIO (5 , 3.3 );
2966
+ WS._boardStatus = WS_BOARD_DEF_OK;
2967
+
2956
2968
// Configure hardware
2957
2969
while (!WS.pinCfgCompleted ) {
2958
2970
WS_DEBUG_PRINTLN (
@@ -2968,6 +2980,13 @@ void Wippersnapper::connect() {
2968
2980
statusLEDFade (GREEN, 3 );
2969
2981
WS_DEBUG_PRINTLN (
2970
2982
" Registration and configuration complete!\n Running application..." );
2983
+
2984
+ // Print out display topics
2985
+ WS_DEBUG_PRINTLN (" Device-to-Broker DISPLAY topic: " );
2986
+ WS_DEBUG_PRINTLN (WS._topic_signal_display_device );
2987
+ WS_DEBUG_PRINTLN (" Broker-to-Device DISPLAY topic: " );
2988
+ WS_DEBUG_PRINTLN (WS._topic_signal_display_brkr );
2989
+ delay (500 );
2971
2990
}
2972
2991
2973
2992
/* *************************************************************************/
0 commit comments