Multi Select Helper Example

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

/**
 * ReactiveESP32 Example Overview:
 * - This example demonstrates the usage of the MultiSelectHelper to select between multiple signals
 *   based on a selector signal.
 * - The library has +30 built-in helpers, and users can define custom helpers for common patterns
 *   (see documentation for more details).
 * - An enum class 'Options' is defined to represent four selectable options.
 * - A Signal<Options> named 'selector' holds the current selected option.
 * - Four Signal<uint8_t> signals represent the values for each option.
 *
 * - The Serial interface is used to interact with the program:
 *   - 'g': Get the current selected option value.
 *   - '1': Set selector to Option1.
 *   - '2': Set selector to Option2.
 *   - '3': Set selector to Option3.
 *   - '4': Set selector to Option4.
 *
 * - Pressing '0' restarts the ESP32.
 */

#include <ReactiveESP32.h>
using namespace RxESP32;

/* ---------------------------------------------------------------------------------------------- */
// Define an enum class to represent a selection of options
enum class Options : uint8_t {
  Option1 = 0,
  Option2,
  Option3,
  Option4,
};

// Define a Signal to hold the current selection using the Options enum
Signal<Options> selector(Options::Option1);

// Define signals to represent whether each option is selected
Signal<uint8_t> is_option1_selected(1);
Signal<uint8_t> is_option2_selected(2);
Signal<uint8_t> is_option3_selected(3);
Signal<uint8_t> is_option4_selected(4);

// Use the multiSelect() helper to link the selector signal to the individual option signals
// The helper creates a computed with the value of the selected option signal
auto selected_option = Helpers::Combinatorial::multiSelect(selector, is_option1_selected,
  is_option2_selected, is_option3_selected, is_option4_selected);

// Effect that prints whenever the selected option changes
Effect<> print_effect([]() {
  uint8_t value = selected_option.get();
  Serial.printf("\tEffect: Selected Option Value: %u\n", value);
  return nullptr; // No cleanup needed
});

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

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

  Serial.println("=========================================");
  Serial.println("ReactiveESP32 - MultiSelectHelper 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 'g':
    {
      // Get the current selected option value
      uint8_t value = selected_option.get();
      Serial.printf("Selected Option Value: %u\n", value);
    } break;

    case '1':
    {
      // Set selector to Option1
      selector.set(Options::Option1);
      Serial.println("Selector set to Option1");
    } break;

    case '2':
    {
      // Set selector to Option2
      selector.set(Options::Option2);
      Serial.println("Selector set to Option2");
    } break;

    case '3':
    {
      // Set selector to Option3
      selector.set(Options::Option3);
      Serial.println("Selector set to Option3");
    } break;

    case '4':
    {
      // Set selector to Option4
      selector.set(Options::Option4);
      Serial.println("Selector set to Option4");
    } break;
  }
}

See Also

  • Core - Core API Reference

  • Filters - Filters API Reference

  • Helpers - Helpers API Reference