/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates the usage of the Dependency Graph feature in ReactiveESP32. * - Various nodes are created: a Signal, two Computeds, and two Effects. * - The nodes are named for easier identification in the dependency graph. * - Pressing 'p' in the Serial monitor will print the current dependency graph in mermaid format. * You can visualize it using online tools like https://mermaid.live/. * * - It's necessary to have RXESP32_ENABLE_DEPENDENCY_GRAPH enabled in the configuration. * * - The Serial interface is used to interact with the program: * - 's': Set the signal to a new value. * - 'p': Print the current dependency graph in mermaid format. * * - Pressing '0' restarts the ESP32. */#include<ReactiveESP32.h>usingnamespaceRxESP32;#if RXESP32_ENABLE_DEPENDENCY_GRAPH == 0#error "This example requires RXESP32_ENABLE_DEPENDENCY_GRAPH to be enabled"#else/* ---------------------------------------------------------------------------------------------- */// Define a simple signalSignal<uint8_t>number(0,{.name="Number Signal"});// Define a computed value that doubles the signalComputed<uint16_t>doubled([](){uint16_tvalue=number.get()*2;returnvalue;},{.name="Doubled Computed"});// Define a computed value that prints whenever 'doubled' changesEffect<>print_doubled_effect([](){uint16_tvalue=doubled.get();Serial.printf("\tComputed doubled updated: %u\n",value);returnnullptr;// No cleanup needed},{.name="Print Doubled Effect"});// Define a computed value that quadruples the signalComputed<uint16_t>quadrupled([](){uint16_tvalue=number.get()*4;returnvalue;},{.name="Quadrupled Computed"});// Define an effect that prints whenever 'quadrupled' changesEffect<>print_quad_effect([](){uint16_tvalue=quadrupled.get();Serial.printf("\tComputed quadrupled updated: %u\n",value);returnnullptr;// No cleanup needed},{.name="Print Quadrupled Effect"});// These nodes create the following dependency graph:// graph TD// S0["Number Signal"]// C1["Doubled Computed"]// E2["Print Doubled Effect"]// C3["Quadrupled Computed"]// E4["Print Quadrupled Effect"]// S0 --> C1// C1 --> E2// S0 --> C3// C3 --> E4// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("=======================================");Serial.println("ReactiveESP32 - DependencyGraph Example");Serial.println("=======================================");// Start the ReactiveESP32 dispatcherif(!Dispatcher::start()){Serial.println("Failed to start ReactiveESP32 Dispatcher!");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'p':{// Print the dependency graph to Serial// This will output a mermaid diagram representation// You can paste the output to https://mermaid.live/ to visualize itUtils::printGraphMermaidToStream();}break;case's':{// Update the signal to current + 1number.update([](constuint8_t&value){uint8_tnew_value=value+1;Serial.printf("%s updated to: %u\n",number.getName(),new_value);returnnew_value;});}break;}}#endif