Skip Filter Check Example

examples/Intermediate/SkipFilterCheck/SkipFilterCheck.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
/**
 * SPDX-FileCopyrightText: 2026 Maximiliano Ramirez <maximiliano.ramirezbravo@gmail.com>
 *
 * SPDX-License-Identifier: MIT
 */

/**
 * ReactiveESP32 Example Overview:
 * - This example demonstrates the use of the `skip_filter_check` option in Signals and Computeds.
 * - It shows how to configure Signals and Computeds to either respect or skip filter checks when
 * propagating changes.
 * - The program defines a Signal and two Computeds, each with different filter check behaviors.
 * - Two Effects are set up to print messages when the Computeds change, illustrating the impact of
 * the filter check option.
 *
 * - The Serial interface is used to interact with the program:
 *   - 't': Trigger the Signal to its current value, demonstrating the effect of filter checks.
 *
 * - Pressing '0' restarts the ESP32.
 */

#include <ReactiveESP32.h>
using namespace RxESP32;

/* ---------------------------------------------------------------------------------------------- */
// Define a simple signal with options:
// - name: "trigger_signal"
// - skip_filter_check: true to always propagate changes, even if the value is the same
Signal<uint8_t> trigger(0, {.name = "trigger_signal", .skip_filter_check = true});

// Define a computed value that just returns the signal's value
Computed<uint8_t> computed_trigger([]() { return trigger.get(); });

// Define an effect with options that prints when the computed value changes
// This Effect will NOT run if the computed value remains the same
// - name: "print_computed_trigger"
Effect<> print_computed_trigger(
  []() {
    uint8_t value = computed_trigger.get();
    Serial.printf("Computed Trigger changed: %u\n", value);
    return nullptr; // No cleanup function
  },
  {.name = "print_computed_trigger"});

// Define a computed value with options that just returns the signal's value, but skips filter
// checks
// - skip_filter_check: true to always propagate changes, even if the value is the same
Computed<uint8_t> computed_trigger_no_filter([]() { return trigger.get(); },
  {.skip_filter_check = true});

// Define an effect with options that prints when the computed value changes
// This Effect WILL run even if the computed value remains the same
// - name: "print_computed_trigger_no_filter"
Effect<> print_computed_trigger_no_filter(
  []() {
    uint8_t value = computed_trigger_no_filter.get();
    Serial.printf("Computed Trigger (no filter) changed: %u\n", value);
    return nullptr; // No cleanup function
  },
  {.name = "print_computed_trigger_no_filter"});

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

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

  Serial.println("=======================================");
  Serial.println("ReactiveESP32 - SkipFilterCheck 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 't':
    {
      // Trigger the signal to the same value
      uint8_t current = trigger.get();

      Serial.printf("Triggering %s to same value: %u\n", trigger.getName(), current);
      Serial.printf("\t%s will NOT execute\n", print_computed_trigger.getName());
      Serial.printf("\t%s WILL execute\n", print_computed_trigger_no_filter.getName());

      if (!trigger.set(current)) {
        Serial.printf("Failed to trigger %s\n", trigger.getName());
      } else {
        Serial.printf("Signal %s triggered\n", trigger.getName());
      }
    } break;
  }
}

See Also

  • Core - Core API Reference

  • Filters - Filters API Reference

  • Helpers - Helpers API Reference