SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
repeat.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_SENSESP_TRANSFORMS_REPEAT_H_
2#define SENSESP_SRC_SENSESP_TRANSFORMS_REPEAT_H_
3
4#include <elapsedMillis.h>
5
7#include "sensesp_base_app.h"
8#include "transform.h"
9
10namespace sensesp {
11
27template <typename FROM, typename TO>
28class Repeat : public Transform<FROM, TO> {
29 public:
30 Repeat(unsigned long interval) : Transform<FROM, TO>(), interval_{interval} {
31 if (interval_ == 0) {
32 ESP_LOGW("Repeat", "Interval is 0. This will cause a busy loop.");
33 }
34 }
35
36 virtual void set(const FROM& input) override {
37 this->emit(input);
38 if (repeat_event_ != nullptr) {
39 // Delete the old repeat event
40 repeat_event_->remove(event_loop());
41 }
43 event_loop()->onRepeat(interval_, [this]() { this->notify(); });
44 }
45
46 protected:
47 unsigned long interval_;
48 reactesp::RepeatEvent* repeat_event_ = nullptr;
49};
50
51// For compatibility with the old RepeatReport class
52template <typename T>
53class RepeatReport : public Repeat<T, T> {};
54
61template <typename T>
62class RepeatStopping : public Repeat<T, T> {
63 public:
64 RepeatStopping(unsigned long interval, unsigned long max_age)
65 : Repeat<T, T>(interval), max_age_{max_age} {
66 age_ = max_age;
67
68 if (this->repeat_event_ != nullptr) {
69 // Delete the old repeat event
70 this->repeat_event_->remove();
71 }
72 this->repeat_event_ = event_loop()->onRepeat(
73 this->interval_, [this]() { this->repeat_function(); });
74 }
75
76 virtual void set(const T& input) override {
77 this->emit(input);
78 age_ = 0;
79 if (this->repeat_event_ != nullptr) {
80 // Delete the old repeat event
81 this->repeat_event_->remove();
82 }
83 this->repeat_event_ = event_loop()->onRepeat(
84 this->interval_, [this]() { this->repeat_function(); });
85 }
86
87 protected:
88 elapsedMillis age_;
89 unsigned long max_age_;
90
91 protected:
93 if (age_ < max_age_) {
94 this->notify();
95 } else {
96 if (this->repeat_event_ != nullptr) {
97 // Delete the old repeat event
98 this->repeat_event_->remove();
99 this->repeat_event_ = nullptr;
100 }
101 }
102 };
103};
104
111template <typename T>
112class RepeatExpiring : public Repeat<T, Nullable<T>> {
113 public:
114 RepeatExpiring(unsigned long interval, unsigned long max_age)
115 : Repeat<T, Nullable<T>>(interval), max_age_{max_age} {
116 ESP_LOGD("RepeatExpiring", "interval: %lu, max_age: %lu", interval,
117 max_age);
118
119 age_ = max_age;
120
121 if (this->repeat_event_ != nullptr) {
122 // Delete the old repeat event
123 this->repeat_event_->remove(event_loop());
124 }
125 this->repeat_event_ = event_loop()->onRepeat(
126 this->interval_, [this]() { this->repeat_function(); });
127 }
128
129 virtual void set(const T& input) override {
130 this->emit(input);
131 age_ = 0;
132 if (this->repeat_event_ != nullptr) {
133 // Delete the old repeat event
134 this->repeat_event_->remove(event_loop());
135 }
136 this->repeat_event_ = event_loop()->onRepeat(
137 this->interval_, [this]() { this->repeat_function(); });
138 }
139
140 protected:
141 elapsedMillis age_;
142 unsigned long max_age_;
143
144 protected:
146 // Note: This could've been implemented with the new ExpiringValue class,
147 // but this is more in line with the other Repeat classes.
148 if (age_ < max_age_) {
149 this->notify();
150 } else {
151 this->emit(this->get().invalid());
152 }
153 };
154};
155
167template <typename T>
169 public:
170 RepeatConstantRate(unsigned long interval, unsigned long max_age)
171 : RepeatExpiring<T>(interval, max_age) {
172 if (this->repeat_event_ != nullptr) {
173 // Delete the old repeat event
174 this->repeat_event_->remove();
175 }
176
177 this->repeat_event_ =
178 event_loop()->onRepeat(interval, [this]() { this->repeat_function(); });
179 }
180
181 void set(T input) override {
182 this->output_ = input;
183 this->age_ = 0;
184 }
185};
186
187} // namespace sensesp
188
189#endif // SENSESP_SRC_SENSESP_TRANSFORMS_REPEAT_H_
Template class that supports a special invalid magic value for a type.
Definition nullable.h:15
Repeat transform that emits the last value at a constant interval.
Definition repeat.h:168
void set(T input) override
Definition repeat.h:181
RepeatConstantRate(unsigned long interval, unsigned long max_age)
Definition repeat.h:170
Repeat transform that emits an expired value if the value age exceeds max_age.
Definition repeat.h:112
RepeatExpiring(unsigned long interval, unsigned long max_age)
Definition repeat.h:114
elapsedMillis age_
Definition repeat.h:141
virtual void set(const T &input) override
Definition repeat.h:129
unsigned long max_age_
Definition repeat.h:142
Repeat the input at specific intervals.
Definition repeat.h:28
virtual void set(const FROM &input) override
Definition repeat.h:36
reactesp::RepeatEvent * repeat_event_
Definition repeat.h:48
unsigned long interval_
Definition repeat.h:47
Repeat(unsigned long interval)
Definition repeat.h:30
Repeat transform that stops emitting if the value age exceeds max_age.
Definition repeat.h:62
virtual void set(const T &input) override
Definition repeat.h:76
RepeatStopping(unsigned long interval, unsigned long max_age)
Definition repeat.h:64
unsigned long max_age_
Definition repeat.h:89
elapsedMillis age_
Definition repeat.h:88
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:53
void emit(const TO &new_value)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9