Priority Example

examples/Intermediate/Priority/Priority.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * 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>
using namespace RxESP32;

#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 priorities
Signal<int> value(10);

// Critical priority effect - runs first (validates)
Effect<> validate_effect(
  []() {
    int val = value.get();
    if (val < 0) {
      Serial.printf("[CRITICAL PRIORITY] Validation: Value %d is negative - may need attention\n",
        val);
    } else if (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);
    }
    return nullptr;
  },
  {.priority = Priority::Critical});

// High priority effect - runs second (processes)
Effect<> process_effect(
  []() {
    int val       = value.get();
    int processed = val * 2 + 5;
    Serial.printf("[HIGH PRIORITY] Processing: %d -> %d\n", val, processed);
    return nullptr;
  },
  {.priority = Priority::High});

// Normal priority effect - runs last (logs)
Effect<> log_effect(
  []() {
    int val = value.get();
    Serial.printf("[NORMAL PRIORITY] Logging: Value is now %d\n", val);
    Serial.println("----------------------------------------");
    return nullptr;
  },
  {.priority = Priority::Normal});

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

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

  Serial.println("================================");
  Serial.println("ReactiveESP32 - Priority 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':
    {
      ESP.restart();
    } break;

    case 'g':
    {
      Serial.printf("Current value: %d\n", value.get());
    } break;

    case 'u':
    {
      value.update([](const int& val) {
        int new_val = val + 1;
        Serial.printf("Incremented to: %d\n", new_val);
        return new_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

See Also

  • Core - Core API Reference

  • Filters - Filters API Reference

  • Helpers - Helpers API Reference