-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throttle limits the update rate of its input.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef SENSESP_SRC_SENSESP_TRANSFORMS_THROTTLE_H_ | ||
#define SENSESP_SRC_SENSESP_TRANSFORMS_THROTTLE_H_ | ||
|
||
#include <climits> | ||
#include <elapsedMillis.h> | ||
|
||
#include "transform.h" | ||
|
||
namespace sensesp { | ||
|
||
/** | ||
* @brief Throttle the input rate. | ||
* | ||
* Make sure that a frequently changing value is only | ||
* reported at a specified minimum interval. If, for example, | ||
* the input value changes every 100ms, but you only want to process it | ||
* every 1000ms, you can use this transform. It does not average or buffer | ||
* any values, it just throws away values that are too frequent. | ||
* | ||
* In many cases, you might want to consider the `moving_average` transform | ||
* instead of this one. The `moving_average` transform will smooth out | ||
* the input values, while this one will only emit the last value. | ||
* | ||
* @param min_interval Minimum time, in ms, before a new value | ||
* is emitted again. | ||
* | ||
*/ | ||
template <typename T> | ||
class Throttle : public SymmetricTransform<T> { | ||
public: | ||
Throttle(long min_interval) | ||
: SymmetricTransform<T>(), min_interval_{min_interval} {} | ||
|
||
void set(T input, uint8_t inputChannel = 0) override { | ||
if (age_ < min_interval_) { | ||
return; | ||
} | ||
age_ = 0; | ||
this->emit(input); | ||
} | ||
|
||
protected: | ||
long min_interval_; | ||
elapsedMillis age_ = LONG_MAX; | ||
}; | ||
|
||
} // namespace sensesp | ||
|
||
#endif // SENSESP_SRC_SENSESP_TRANSFORMS_THROTTLE_H_ |