/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates the usage of the MultiSelectHelper to select between multiple signals * based on a selector signal. * - The library has +30 built-in helpers, and users can define custom helpers for common patterns * (see documentation for more details). * - An enum class 'Options' is defined to represent four selectable options. * - A Signal<Options> named 'selector' holds the current selected option. * - Four Signal<uint8_t> signals represent the values for each option. * * - The Serial interface is used to interact with the program: * - 'g': Get the current selected option value. * - '1': Set selector to Option1. * - '2': Set selector to Option2. * - '3': Set selector to Option3. * - '4': Set selector to Option4. * * - Pressing '0' restarts the ESP32. */#include<ReactiveESP32.h>usingnamespaceRxESP32;/* ---------------------------------------------------------------------------------------------- */// Define an enum class to represent a selection of optionsenumclassOptions:uint8_t{Option1=0,Option2,Option3,Option4,};// Define a Signal to hold the current selection using the Options enumSignal<Options>selector(Options::Option1);// Define signals to represent whether each option is selectedSignal<uint8_t>is_option1_selected(1);Signal<uint8_t>is_option2_selected(2);Signal<uint8_t>is_option3_selected(3);Signal<uint8_t>is_option4_selected(4);// Use the multiSelect() helper to link the selector signal to the individual option signals// The helper creates a computed with the value of the selected option signalautoselected_option=Helpers::Combinatorial::multiSelect(selector,is_option1_selected,is_option2_selected,is_option3_selected,is_option4_selected);// Effect that prints whenever the selected option changesEffect<>print_effect([](){uint8_tvalue=selected_option.get();Serial.printf("\tEffect: Selected Option Value: %u\n",value);returnnullptr;// No cleanup needed});// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("=========================================");Serial.println("ReactiveESP32 - MultiSelectHelper 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 selected option valueuint8_tvalue=selected_option.get();Serial.printf("Selected Option Value: %u\n",value);}break;case'1':{// Set selector to Option1selector.set(Options::Option1);Serial.println("Selector set to Option1");}break;case'2':{// Set selector to Option2selector.set(Options::Option2);Serial.println("Selector set to Option2");}break;case'3':{// Set selector to Option3selector.set(Options::Option3);Serial.println("Selector set to Option3");}break;case'4':{// Set selector to Option4selector.set(Options::Option4);Serial.println("Selector set to Option4");}break;}}