/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates the usage of the QueueToSignalHelper to update a signal from a queue. * - The library has +30 built-in helpers, and users can define custom helpers for common patterns * (see documentation for more details). * - A Signal<uint8_t> named 'number' is defined to hold a number. * - A FreeRTOS queue is created to send uint8_t values to update the 'number' signal. * - An Effect is defined to print the value of 'number' whenever it changes. * * - It's necessary to have RXESP32_ENABLE_QUEUE_TO_SIGNAL to be enabled in the configuration. * * - The Serial interface is used to interact with the program: * - 'g': Get the current value of both signals. * - 's': Set the signal to a random value by sending it to the queue. * * - Pressing '0' restarts the ESP32. */#include<ReactiveESP32.h>usingnamespaceRxESP32;#if RXESP32_ENABLE_QUEUE_TO_SIGNAL == 0#error "This example requires RXESP32_ENABLE_QUEUE_TO_SIGNAL to be enabled"#else/* ---------------------------------------------------------------------------------------------- */// Queue handle for sending updates to it. Later we will use a helper to read from this queue and// update a signal accordingly.QueueHandle_tupdate_queue=nullptr;// Define a simple signal to hold a number// For this example, skip_filter_check is enabled to ensure all updates are propagated.Signal<uint8_t>number(0,{.skip_filter_check=true});// Define an effect that prints number whenever it changesEffect<>print_effect([](){uint8_tvalue=number.get();Serial.printf("\tEffect: Number: %u\n",value);returnnullptr;// No cleanup needed});// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("===========================================");Serial.println("ReactiveESP32 - QueueToSignalHelper Example");Serial.println("===========================================");// Start the ReactiveESP32 dispatcherif(!Dispatcher::start()){Serial.println("Failed to start ReactiveESP32 Dispatcher!");while(true){delay(1000);}}// Create the update queue with space for 10 uint8_t itemsupdate_queue=xQueueCreate(10,sizeof(uint8_t));if(update_queue==nullptr){Serial.println("Failed to create update queue!");while(true){delay(1000);}}// Use the QueueToSignal helper to link the queue to the 'number' signalif(!Helpers::Utility::queueToSignal(update_queue,number)){Serial.println("Failed to link queue to signal!");while(true){delay(1000);}}}voidloop(){serialRead();}voidserialRead(){if(!Serial.available())return;charc=Serial.read();if(c=='\r')return;if(c=='\n')c=' ';Serial.printf("> %c\n",c);switch(c){case'0':{// Restart the ESP32ESP.restart();}break;case'g':{// Get the current value of the signaluint8_tvalue_number=number.get();Serial.printf("Number: %u\n",value_number);}break;case's':{// Set a random value to the signal via the queueuint8_tvalue=random(0,256);Serial.printf("Sending value %u to update queue...\n",value);if(xQueueSend(update_queue,&value,pdMS_TO_TICKS(100))!=pdTRUE){Serial.println("Failed to send value to update queue!");break;}Serial.printf("Value %u sent to update queue\n",value);Dispatcher::wake();// Wake dispatcher to process queue}break;}}#endif