|
| 1 | +/** |
| 2 | + * @file repeat_transform.cpp |
| 3 | + * @brief Example of different Repeat transforms. |
| 4 | + * |
| 5 | + * Repeat transforms transmit their input value at regular intervals, ensuring |
| 6 | + * output even at the absence of input. |
| 7 | + * |
| 8 | + * Try running this code with the serial monitor open ("Upload and Monitor" |
| 9 | + * in PlatformIO menu). The program will produce capital letters every second, |
| 10 | + * lowercase letters every 3 seconds, and integers every 10 seconds. Comment |
| 11 | + * out and enable different Repeat transform variants to see how they affect the |
| 12 | + * output. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#include "sensesp.h" |
| 17 | + |
| 18 | +#include <math.h> |
| 19 | + |
| 20 | +#include "sensesp/sensors/sensor.h" |
| 21 | +#include "sensesp/system/lambda_consumer.h" |
| 22 | +#include "sensesp/transforms/repeat.h" |
| 23 | +#include "sensesp_minimal_app_builder.h" |
| 24 | + |
| 25 | +using namespace sensesp; |
| 26 | + |
| 27 | +ReactESP app; |
| 28 | + |
| 29 | +SensESPMinimalApp* sensesp_app; |
| 30 | + |
| 31 | +void setup() { |
| 32 | + SetupLogging(); |
| 33 | + |
| 34 | + // Note: SensESPMinimalAppBuilder is used to build the app. This creates |
| 35 | + // a minimal app with no networking or other bells and whistles which |
| 36 | + // would be distracting in this example. In normal use, this is not what |
| 37 | + // you would use. Unless, of course, you know that is what you want. |
| 38 | + SensESPMinimalAppBuilder builder; |
| 39 | + |
| 40 | + sensesp_app = builder.get_app(); |
| 41 | + |
| 42 | + // Produce capital letters every second |
| 43 | + auto sensor_A = new RepeatSensor<char>(1000, []() { |
| 44 | + static char value = 'Z'; |
| 45 | + if (value == 'Z') { |
| 46 | + value = 'A'; |
| 47 | + } else { |
| 48 | + value += 1; |
| 49 | + } |
| 50 | + return value; |
| 51 | + }); |
| 52 | + |
| 53 | + sensor_A->connect_to(new LambdaConsumer<char>( |
| 54 | + [](char value) { ESP_LOGD("App", " %c", value); })); |
| 55 | + |
| 56 | + // Produce lowercase letters every 3 seconds |
| 57 | + auto sensor_a = new RepeatSensor<char>(3000, []() { |
| 58 | + static char value = 'z'; |
| 59 | + if (value == 'z') { |
| 60 | + value = 'a'; |
| 61 | + } else { |
| 62 | + value += 1; |
| 63 | + } |
| 64 | + return value; |
| 65 | + }); |
| 66 | + |
| 67 | + sensor_a->connect_to(new LambdaConsumer<char>( |
| 68 | + [](char value) { ESP_LOGD("App", " %c", value); })); |
| 69 | + |
| 70 | + // Produce integers every 10 seconds |
| 71 | + auto sensor_int = new RepeatSensor<int>(10000, []() { |
| 72 | + static int value = 0; |
| 73 | + value += 1; |
| 74 | + return value; |
| 75 | + }); |
| 76 | + |
| 77 | + sensor_int->connect_to(new LambdaConsumer<int>( |
| 78 | + [](int value) { ESP_LOGD("App", " %d", value); })); |
| 79 | + |
| 80 | + // Repeat the values every 2 seconds |
| 81 | + |
| 82 | + auto repeat_A = new Repeat<char>(2000); |
| 83 | + auto repeat_a = new Repeat<char>(2000); |
| 84 | + auto repeat_int = new Repeat<int>(2000); |
| 85 | + |
| 86 | + // Pay attention to the individual columns of the program console output. |
| 87 | + // Capital letters are produced every second. Repeat gets always triggered as |
| 88 | + // a result, but never on its own because the repeat timer always is reset |
| 89 | + // before it expires. Lowercase letters are produced every 3 seconds. Repeat |
| 90 | + // gets triggered immediately and then again after 2 seconds. Integers are |
| 91 | + // produced every 10 seconds. Repeat gets triggered immediately and then |
| 92 | + // again after every 2 seconds until the next integer is produced. |
| 93 | + |
| 94 | + // Try commenting out the Repeat lines above and uncommenting the |
| 95 | + // RepeatStopping lines below. |
| 96 | + |
| 97 | + // auto repeat_A = new RepeatStopping<char>(2000, 5000); |
| 98 | + // auto repeat_a = new RepeatStopping<char>(2000, 5000); |
| 99 | + // auto repeat_int = new RepeatStopping<int>(2000, 5000); |
| 100 | + |
| 101 | + // The maximum age is set to 5 seconds. Both the capital and lowercase |
| 102 | + // letters are produced like before because their repetition rates are |
| 103 | + // faster than the expiration time. However, the integers are produced |
| 104 | + // only every 10 seconds, and they stop being repeated after 5 seconds |
| 105 | + // until a new integer is produced. |
| 106 | + |
| 107 | + // auto repeat_A = new RepeatExpiring<char>(2000, 5000, '?'); |
| 108 | + // auto repeat_a = new RepeatExpiring<char>(2000, 5000, '?'); |
| 109 | + // auto repeat_int = new RepeatExpiring<int>(2000, 5000, -1); |
| 110 | + |
| 111 | + // The expiration time is set to 5 seconds. Both the capital and lowercase |
| 112 | + // letters are produced like before because their repetition rates are |
| 113 | + // faster than the expiration time. However, the integers are produced |
| 114 | + // only every 10 seconds, and the value does expire after 5 seconds, indicated |
| 115 | + // by the -1 value that gets output after the expiry, until a new integer is |
| 116 | + // produced. |
| 117 | + |
| 118 | + // Finally, try commenting out the RepeatExpiring lines above and uncommenting |
| 119 | + // the RepeatConstantRate lines below. |
| 120 | + |
| 121 | + // auto repeat_A = new RepeatConstantRate<char>(2000, 5000, '?'); |
| 122 | + // auto repeat_a = new RepeatConstantRate<char>(2000, 5000, '?'); |
| 123 | + // auto repeat_int = new RepeatConstantRate<int>(2000, 5000, -1); |
| 124 | + |
| 125 | + // Notice how the repetitions are no longer triggered by the sensors but |
| 126 | + // are produced at a constant rate, in clusters. The letters still |
| 127 | + // never expire, but the integers do. |
| 128 | + |
| 129 | + sensor_A->connect_to(repeat_A); |
| 130 | + sensor_a->connect_to(repeat_a); |
| 131 | + sensor_int->connect_to(repeat_int); |
| 132 | + |
| 133 | + repeat_A->connect_to(new LambdaConsumer<char>( |
| 134 | + [](char value) { ESP_LOGD("App", "Repeat: %c", value); })); |
| 135 | + |
| 136 | + repeat_a->connect_to(new LambdaConsumer<char>( |
| 137 | + [](char value) { ESP_LOGD("App", "Repeat: %c", value); })); |
| 138 | + |
| 139 | + repeat_int->connect_to(new LambdaConsumer<int>( |
| 140 | + [](int value) { ESP_LOGD("App", "Repeat: %d", value); })); |
| 141 | +} |
| 142 | + |
| 143 | +void loop() { app.tick(); } |
0 commit comments