SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
debounce.h
Go to the documentation of this file.
1#ifndef debounce_H
2#define debounce_H
3
4#include "transform.h"
5
6namespace sensesp {
7
8static const char DEBOUNCE_SCHEMA[] PROGMEM = R"###({
9 "type": "object",
10 "properties": {
11 "min_delay": { "title": "Minimum delay", "type": "number", "description": "The minimum time in ms between inputs for output to happen" }
12 }
13 })###";
14
37template <class T>
39 public:
42 ms_min_delay_{ms_min_delay} {
43 this->load_configuration();
44 }
45
46 virtual void set_input(T input, uint8_t input_channel = 0) override {
47
48 // Input has changed since the last emit, or this is the first
49 // input since the program started to run.
50
51 if (input != debounced_value_ || !value_received_) {
52 debounced_value_ = input;
53
54 if (reaction_) {
55 reaction_->remove();
56 reaction_ = nullptr;
57 }
58 reaction_ = ReactESP::app->onDelay(ms_min_delay_, [this, input]() {
59 this->reaction_ = nullptr;
60 this->debounced_value_ = input;
61 this->emit(input);
62 });
63 value_received_ = true;
64 }
65 }
66
67 private:
68 int ms_min_delay_;
69 bool value_received_ = false;
70 T debounced_value_;
71 DelayReaction* reaction_ = nullptr;
72 virtual void get_configuration(JsonObject& doc) override {
73 doc["min_delay"] = ms_min_delay_;
74 }
75
76 virtual bool set_configuration(const JsonObject& config) override {
77 String expected[] = {"min_delay"};
78 for (auto str : expected) {
79 if (!config.containsKey(str)) {
80 return false;
81 }
82 }
83 ms_min_delay_ = config["min_delay"];
84 return true;
85 }
86
87 virtual String get_config_schema() override { return FPSTR(DEBOUNCE_SCHEMA); }
88};
89
92 Debounce; // for backward-compatibility with original class
95 DebounceFloat; // not sure this works - test it if you use it
96
97}
98
99#endif
virtual void load_configuration()
Implements debounce code for a button or switch.
Definition debounce.h:38
DebounceTemplate(int ms_min_delay=15, String config_path="")
Definition debounce.h:40
virtual void set_input(T input, uint8_t input_channel=0) override
Definition debounce.h:46
Construct a new transform based on a single function.
A common type of transform that consumes, transforms, then outputs values of the same data type.
Definition transform.h:95
void emit(T new_value)
const uint8_t PAGE_css_bootstrap[] PROGMEM
DebounceTemplate< float > DebounceFloat
Definition debounce.h:95
DebounceTemplate< bool > Debounce
Definition debounce.h:92
DebounceTemplate< bool > DebounceBool
Definition debounce.h:90
DebounceTemplate< int > DebounceInt
Definition debounce.h:93