Untracked Example

examples/Basic/Untracked/Untracked.ino
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com>
 *
 * SPDX-License-Identifier: MIT
 */

/**
 * ReactiveESP32 Example Overview:
 * - This example demonstrates the basic usage of untracked nodes.
 * - Two Signal<uint8_t> named 'number1' and 'number2' are defined.
 * - A Computed<uint16_t> named 'sum' is created that computes the sum of 'number1' and 'number2',
 *   but only updates when 'number1' changes, using peek() to read 'number2' without tracking
 *   dependency.
 *
 * - The Serial interface is used to interact with the program:
 *   - '1': Set 'number1' to a new value (triggers 'sum' update).
 *   - '2': Set 'number2' to a new value (does NOT trigger 'sum' update).
 *   - 'g': Get the current values of 'number1', 'number2', and 'sum'.
 *
 * - Pressing '0' restarts the ESP32.
 */

#include <ReactiveESP32.h>
using namespace RxESP32;

/* ---------------------------------------------------------------------------------------------- */
// Define two signals
Signal<uint8_t> number1(0);
Signal<uint8_t> number2(0);

// Define a computed value that sums both signals, but only updates when number1 changes
// Using peek() to read number2 without tracking dependency
Computed<uint16_t> sum([]() {
  uint8_t num1 = number1.get();
  uint8_t num2 = number2.peek();
  uint16_t sum = num1 + num2;

  Serial.printf("\tSum updated: %u + %u = %u\n", num1, num2, sum);
  return sum;
});

// Read Serial input and process commands
void serialRead();
/* ---------------------------------------------------------------------------------------------- */

void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println("=======================================");
  Serial.println("ReactiveESP32 - Basic Untracked Example");
  Serial.println("=======================================");

  // Start the ReactiveESP32 dispatcher
  if (!Dispatcher::start()) {
    Serial.println("Failed to start ReactiveESP32 Dispatcher!");
    while (true) {
      delay(1000);
    }
  }
}

void loop() { serialRead(); }

void serialRead() {
  if (!Serial.available()) return;

  char c = Serial.read();

  if (c == '\r') return;
  if (c == '\n') c = ' ';
  Serial.printf("> %c\n", c);

  switch (c) {
    case '0':
    {
      // Restart the ESP32
      ESP.restart();
    } break;

    case '1':
    {
      // Set the signal 1 to a new value (triggers sum update)
      static uint8_t new_value = 1;

      if (!number1.set(new_value)) {
        Serial.printf("Number 1 unchanged: %u\n", new_value);
        break;
      }

      Serial.printf("Number 1 set to: %u\n", new_value);
      new_value++;
    } break;

    case '2':
    {
      // Set the signal 2 to a new value (does NOT trigger sum update)
      static uint8_t new_value = 1;

      if (!number2.set(new_value)) {
        Serial.printf("Number 2 unchanged: %u\n", new_value);
        break;
      }

      Serial.printf("Number 2 set to: %u\n", new_value);
      new_value++;
    } break;

    case 'g':
    {
      // Get the current value of all nodes
      uint8_t num1       = number1.get();
      uint8_t num2       = number2.get();
      uint16_t sum_value = sum.get();

      Serial.printf("Number 1: %u, Number 2: %u, Sum: %u\n", num1, num2, sum_value);
    } break;
  }
}

See Also

  • Core - Core API Reference

  • Filters - Filters API Reference

  • Helpers - Helpers API Reference