/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates the use of signals with filters. * - The library has +50 built-in filters, and users can define custom filters as well (see * documentation for more details). * - A Signal<uint8_t> named 'number' is defined, with a WithinRange filter that only allows values * between 10 and 20 (inclusive). * - A Computed<uint8_t> named 'doubled' is defined, which computes double the value of 'number'. It * has skip_filter_check enabled to ensure it recalculates even if 'number' is set to the same * value. * * - The Serial interface is used to interact with the program: * - 'g': Get the current value of the signal and computed. * - 'y': Set the signal to a new value that would pass the filter (between 10 and 20). * - 'n': Set the signal to a new value that would fail the filter (outside 10 to 20). * * - Pressing '0' restarts the ESP32. */#include<ReactiveESP32.h>usingnamespaceRxESP32;/* ---------------------------------------------------------------------------------------------- */// Define a simple signal with the WithinRange filter// This will only allow values between 10 and 20 (inclusive)Signal<uint8_t,1,Filters::Numerical::WithinRange<uint8_t,10,20>>number(0);// Define a computed that doubles the signal value, with skip_filter_check enabled to recompute even// if 'number' was set to the same valueComputed<uint8_t,1>doubled([](){uint8_tval=number.get();uint8_tdoubled_val=val*2;Serial.printf("\tComputed doubled recalculated: %u\n",doubled_val);returndoubled_val;},{.skip_filter_check=true});// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("=======================================");Serial.println("ReactiveESP32 - NumericalFilter 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 signal and computeduint8_tvalue_number=number.get();uint16_tvalue_doubled=doubled.get();Serial.printf("Number - Doubled: %u - %u\n",value_number,value_doubled);}break;case'y':{// Set the signal to a random value within the filter rangeuint8_tnew_value=random(10,21);// 10 to 20 inclusiveSerial.printf("Setting number to %u (within range)\n",new_value);Statusstatus=number.set(new_value);switch(status){caseStatus::Ok:Serial.println("Signal updated successfully");break;caseStatus::Unchanged:Serial.println("Signal value unchanged (filtered out)");break;default:Serial.printf("Error updating signal: %d\n",static_cast<int>(status));break;}}break;case'n':{// Set the signal to a random value outside the filter rangeuint8_tnew_value=random(0,10);// 0 to 9Serial.printf("Setting number to %u (outside range)\n",new_value);Statusstatus=number.set(new_value);switch(status){caseStatus::Ok:Serial.println("Signal updated successfully");break;caseStatus::Unchanged:Serial.println("Signal value unchanged (filtered out)");break;default:Serial.printf("Error updating signal: %d\n",static_cast<int>(status));break;}}break;}}