/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates priority-based execution of Effects (also applied to Computed). * - Multiple Effects depend on the same Signal but have different priorities. * - Higher priority Effects execute first when the Signal changes. * - This is useful for ensuring critical operations complete before less important ones. * * - We create a Signal<int> value that triggers three Effects: * - High priority (Priority::Critical): Validates the value * - Medium priority (Priority::High): Processes the value * - Low priority (Priority::Normal): Logs the value * * - It's necessary to have RXESP32_PRIORITY_LEVELS set to at least 3 in the configuration. * * - The Serial interface is used to read commands: * - 'g': Get current value. * - 's': Set value to a specific number (follow with digits). * - 'i': Increment value. * * - Pressing '0' restarts the ESP32. */#include<ReactiveESP32.h>usingnamespaceRxESP32;#if RXESP32_PRIORITY_LEVELS < 3#error "This example requires RXESP32_PRIORITY_LEVELS to be at least 3"#else/* ---------------------------------------------------------------------------------------------- */// Signal that will trigger effects with different prioritiesSignal<int>value(10);// Critical priority effect - runs first (validates)Effect<>validate_effect([](){intval=value.get();if(val<0){Serial.printf("[CRITICAL PRIORITY] Validation: Value %d is negative - may need attention\n",val);}elseif(val>100){Serial.printf("[CRITICAL PRIORITY] Validation: Value %d exceeds threshold\n",val);}else{Serial.printf("[CRITICAL PRIORITY] Validation: Value %d is within range\n",val);}returnnullptr;},{.priority=Priority::Critical});// High priority effect - runs second (processes)Effect<>process_effect([](){intval=value.get();intprocessed=val*2+5;Serial.printf("[HIGH PRIORITY] Processing: %d -> %d\n",val,processed);returnnullptr;},{.priority=Priority::High});// Normal priority effect - runs last (logs)Effect<>log_effect([](){intval=value.get();Serial.printf("[NORMAL PRIORITY] Logging: Value is now %d\n",val);Serial.println("----------------------------------------");returnnullptr;},{.priority=Priority::Normal});// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("================================");Serial.println("ReactiveESP32 - Priority 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':{ESP.restart();}break;case'g':{Serial.printf("Current value: %d\n",value.get());}break;case'u':{value.update([](constint&val){intnew_val=val+1;Serial.printf("Incremented to: %d\n",new_val);returnnew_val;});}break;case'1':{Serial.println("Setting value to 50 (within range)");value.set(50,true);// Force update even if same value}break;case'2':{Serial.println("Setting value to 150 (above threshold)");value.set(150,true);// Force update even if same value}break;case'3':{Serial.println("Setting value to -10 (negative)");value.set(-10,true);// Force update even if same value}break;}}#endif