/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates the use of the cleanup function in an Effect node. * - A Signal<uint8_t> named 'number' is defined. * - An Effect is created that prints the value of 'number' whenever it changes, along with a * cleanup function that prints the previous value before the next execution. * * - The Serial interface is used to interact with the program: * - 'g': Get the current value of the signal. * - '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);// Create an effect with a cleanup functionEffect<>cleanup_effect([](){uint8_tvalue=number.get();Serial.printf("\tEffect executed. Current number value: %u\n",value);// Define cleanup function// This will be called before the next effect execution// This is a lambda that captures 'value' by value, so it retains the previous valuereturn[value](){Serial.printf("\t\tCleanup executed. Previous number value was: %u\n",value);};});// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("======================================");Serial.println("ReactiveESP32 - Effect Cleanup 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'g':{// Get the current value of the signaluint8_tvalue=number.get();Serial.printf("Current number value: %u\n",value);}break;case's':{// Set the signal to a new value using set()staticuint8_tnew_value=1;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;}}