@@ -1659,7 +1659,8 @@ void cbSignalUARTReq(char *data, uint16_t len) {
16591659}
16601660
16611661/* !
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.
16631664 @param stream
16641665 Incoming data stream from buffer.
16651666 @param field
@@ -1668,11 +1669,13 @@ void cbSignalUARTReq(char *data, uint16_t len) {
16681669 Optional arguments from decoder calling function.
16691670 @returns True if decoded successfully, False otherwise.
16701671*/
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) {
16721674 if (field->tag == wippersnapper_signal_v1_DisplayRequest_display_add_tag) {
16731675
16741676 // 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;
16761679 if (!ws_pb_decode (stream,
16771680 wippersnapper_display_v1_DisplayAddOrReplace_fields,
16781681 &msgAddReq)) {
@@ -1681,8 +1684,10 @@ bool cbDecodeDisplayMsg(pb_istream_t *stream, const pb_field_t *field, void **ar
16811684 }
16821685
16831686 // 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
16861691 }
16871692 return true ;
16881693}
@@ -2406,57 +2411,56 @@ bool Wippersnapper::generateWSTopics() {
24062411 return false ;
24072412 }
24082413
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
24102422#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);
24152424#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);
24202426#endif
2427+
2428+ // Generate the topic
24212429 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!" );
24302435 return false ;
24312436 }
24322437
2433- // Subscribe to the display sub-topic
2438+ // Subscribe to signal's DISPLAY sub-topic and set callback
24342439 _topic_signal_display_sub =
24352440 new Adafruit_MQTT_Subscribe (WS._mqtt , WS._topic_signal_display_brkr , 1 );
24362441 WS._mqtt ->subscribe (_topic_signal_display_sub);
24372442 _topic_signal_display_sub->setCallback (cbDisplayMessage);
24382443
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
24402450#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);
24452452#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);
24502454#endif
2455+
2456+ // Generate the topic if memory was allocated successfully
24512457 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!" );
24602464 return false ;
24612465 }
24622466
@@ -2953,6 +2957,14 @@ void Wippersnapper::connect() {
29532957 WS._ui_helper ->build_scr_monitor ();
29542958#endif
29552959
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+
29562968 // Configure hardware
29572969 while (!WS.pinCfgCompleted ) {
29582970 WS_DEBUG_PRINTLN (
@@ -2968,6 +2980,13 @@ void Wippersnapper::connect() {
29682980 statusLEDFade (GREEN, 3 );
29692981 WS_DEBUG_PRINTLN (
29702982 " 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 );
29712990}
29722991
29732992/* *************************************************************************/
0 commit comments