/** * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com> * * SPDX-License-Identifier: MIT *//** * ReactiveESP32 Example Overview: * - This example demonstrates the usage of the Signal class with history tracking enabled. * - A Signal<uint8_t> is created, which maintains a history of the last 10 values assigned to it. * - When the history is full, the oldest value is discarded to make room for new values. * * - It's necessary to have RXESP32_ENABLE_SIGNAL_HISTORY enabled in the configuration. * * - The Serial interface is used to interact with the program: * - 'g': Get the current value of the signal. * - 'u': Update the signal by incrementing its value by 1. * - 'h': Print the history of the signal values. * - 's': Sum all values in the signal's history and print the result. * - 'p': Print each value in the signal's history using forEach. * * - Pressing '0' restarts the ESP32. */#include<ReactiveESP32.h>usingnamespaceRxESP32;#if RXESP32_ENABLE_SIGNAL_HISTORY == 0#error "This example requires RXESP32_ENABLE_SIGNAL_HISTORY to be enabled"#else/* ---------------------------------------------------------------------------------------------- */// Define a signal with history enabled, storing the last 10 valuesSignal<uint8_t,/*dependents*/1,/*default filter*/std::not_equal_to<uint8_t>,/*history size*/10>number(0);// Read Serial input and process commandsvoidserialRead();/* ---------------------------------------------------------------------------------------------- */voidsetup(){Serial.begin(115200);delay(1000);Serial.println("===============================");Serial.println("ReactiveESP32 - History 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=number.get();Serial.printf("Number: %u\n",value_number);}break;case'u':{// Update the signal by adding 1 to the last valuenumber.update([](constuint8_t&val){returnval+1;});Serial.printf("Number updated to: %u\n",number.get());}break;case'h':{// Get the history and print all the valuesSerial.printf("History %u/%u:\n",number.getHistoryCount(),number.getHistorySize());for(size_ti=0;i<number.getHistoryCount();i++){Serial.printf("\t[%u]: %u\n",i,number.getHistory(i));}}break;case's':{// Sum all history values using folduint16_tsum=0;sum=number.foldHistory(sum,[](constuint16_t&acc,constuint8_t&val){returnacc+val;});Serial.printf("Sum of history values: %u\n",sum);}break;case'p':{// Print all history values using forEachSerial.printf("History values using forEach:\n");number.forEachHistory([](constuint8_t&val,size_tindex){Serial.printf("\tValue [%u]: %u\n",index,val);});}break;}}#endif