/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates the basic usage of nested nodes. * - A Signal<uint8_t> named 'number' is defined and updated every second. * - A Computed<uint16_t> named 'doubled' is created that computes double the value of 'number'. * - A second Computed<uint16_t> named 'quadrupled' computes double the value of 'doubled'. * - An Effect is defined to print the value of 'quadrupled' whenever it changes. * * - The Serial interface is used to interact with the program: * - 'g': Get the current value of all nodes. * - 's': Set the signal to a new value. * - 'm': Mutate the signal value by adding 5. * * - Pressing '0' restarts the ESP32. */#include<ReactiveESP32.h>usingnamespaceRxESP32;/* ---------------------------------------------------------------------------------------------- */// Define a simple signalSignal<uint8_t>number(0);// Define a computed value that doubles the signalComputed<uint16_t>doubled([](){uint16_tvalue=number.get()*2;returnvalue;});// Define a computed value that doubles the doubled value (number quadrupled)Computed<uint16_t>quadrupled([](){uint16_tvalue=doubled.get()*2;returnvalue;});// Define an effect that prints when the quadrupled value changesEffect<>print_quadrupled([](){uint16_tvalue=quadrupled.get();Serial.printf("\tQuadrupled value updated: %u\n",value);returnnullptr;// No cleanup function needed});// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("=========================================");Serial.println("ReactiveESP32 - Basic NestedNodes Example");Serial.println("=========================================");// Start the ReactiveESP32 dispatcherif(!Dispatcher::start()){Serial.println("Failed to start ReactiveESP32 Dispatcher!");while(true){delay(1000);}}}voidloop(){serialRead();// Update the signal every secondstaticuint32_tstart=0;if(millis()-start>=1000){start=millis();// Update the signal using update()number.update([](constuint8_t&value){uint8_tnew_value=value+1;Serial.printf("Number updated: %u\n",new_value);returnnew_value;});}}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 all signals and computedsuint8_tvalue_number=number.get();uint16_tvalue_doubled=doubled.get();uint16_tvalue_quadrupled=quadrupled.get();Serial.printf("Number - Doubled - Quadrupled: %u - %u - %u\n",value_number,value_doubled,value_quadrupled);}break;case's':{// Set the signal to a new value using set()staticuint8_tnew_value=0;if(!number.set(new_value)){Serial.printf("Number unchanged: %u\n",new_value);break;}Serial.printf("Number set to: %u\n",new_value);new_value++;}break;case'm':{// Mutate the signal value using mutate()number.mutate([](uint8_t&value){value+=5;Serial.printf("Number mutated to: %u\n",value);});}break;}}