SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
click_type.cpp
Go to the documentation of this file.
1#include "click_type.h"
2
3#include "ReactESP.h"
4
5namespace sensesp {
6
11 click_count_{0},
12 long_click_delay_{long_click_delay},
13 ultra_long_click_delay_{ultra_long_click_delay},
14 double_click_interval_{double_click_interval},
15 delayed_click_report_{NULL} {
17}
18
20 if (input) {
22 } else {
24 }
25}
26
28 return (value != ClickTypes::ButtonPress &&
30}
31
33 debugD(
34 "ClickType received PRESS on click count %d (millis: %ld, last release: "
35 "%ld ms ago)",
37
38 if (click_count_ == 0) {
39 // This is a new, isolated "click" that we have not yet processed.
42 } else {
43 // One or more presses is already in progress...
44
46 // The button down is the second one reported in a row without a button
47 // release and the press is now long enough to qualify as an "ultra long
48 // click"
51 // This is the start of a second click to come in prior to the expiration
52 // of the double_click_interval. Remove any "SingleClick" report that may
53 // have been queued up....
55 delayed_click_report_->remove();
57 debugD(
58 "ClickType press is double click. Removed queued SingleClick "
59 "report");
60 }
62 }
63 }
64
66}
67
69 debugD(
70 "ClickType received UNPRESS for click count %d (millis: %ld, press "
71 "duration: %ld ms)",
73
74 if (click_count_ > 0) {
75 // This is the "release" of a click we are tracking...
78 this->on_click_completed();
79 } else if (press_duration_ >= this->long_click_delay_) {
80 debugD(
81 "ClickType detected LongSingleClick (millis: %ld, press duration %ld "
82 "ms)",
83 millis(), (long)press_duration_);
85 this->on_click_completed();
86 } else if (this->click_count_ > 1) {
87 // We have just ended a double click. Sent it immediately...
88 debugD(
89 "ClickType detected DoubleClick (millis: %ld, press duration %ld ms)",
90 millis(), (long)press_duration_);
92 this->on_click_completed();
93 } else {
94 // This is the end of a potential single click. Queue up the send of a
95 // SingleClick report, but delay it in case another click comes in prior
96 // to the double_click_interval, which would turn this click into a
97 // DoubleClick
98 unsigned long time_of_event = millis();
99 long pd = (long)press_duration_;
101 ReactESP::app->onDelay(double_click_interval_ + 20, [this, pd, time_of_event]() {
102 debugD(
103 "ClickType detected SingleClick (millis: %ld, queue time: %ld, "
104 "press duration %ld ms)",
107 this->on_click_completed();
108 });
109 }
110
113
114 } else {
115 // A press release with no initial press should happen only when
116 // an UltraLongClick has already been sent, or the producer
117 // is feeding us weird values...
119 debugW("ClickType detected UNPRESS with no pending PRESS (millis=%ld)",
120 millis());
121 }
122}
123
125 ReactESP::app->onDelay(5, [this, value]() { this->emit(value); });
126}
127
134
136 debugD(
137 "ClickType detected UltraLongSingleClick (millis: %ld, press duration "
138 "%ld ms)",
139 millis(), (long)press_duration_);
142}
143
145 root["long_click_delay"] = long_click_delay_;
146 root["ultra_long_click_delay"] = ultra_long_click_delay_;
147 root["double_click_interval"] = double_click_interval_;
148}
149
150static const char SCHEMA[] PROGMEM = R"({
151 "type": "object",
152 "properties": {
153 "long_click_delay": { "title": "Long click milliseconds", "type": "integer" },
154 "ultra_long_click_delay": { "title": "Ultra long click milliseconds", "type": "integer" },
155 "double_click_interval": { "title": "Max millisecond interval between double clicks", "type" : "integer" }
156 }
157 })";
158
160
162 String expected[] = {"long_click_delay", "ultra_long_click_delay",
163 "double_click_interval"};
164 for (auto str : expected) {
165 if (!config.containsKey(str)) {
166 return false;
167 }
168 }
169 long_click_delay_ = config["long_click_delay"];
170 ultra_long_click_delay_ = config["ultra_long_click_delay"];
171 double_click_interval_ = config["double_click_interval"];
172 return true;
173}
174
175} // namespace sensesp
void emitDelayed(ClickTypes value)
ClickType(String config_path="", uint16_t long_click_delay=1300, uint16_t double_click_interval=400, uint16_t ultra_long_click_delay=5000)
Definition click_type.cpp:7
elapsedMillis press_duration_
Timmer to time button presses.
Definition click_type.h:87
elapsedMillis release_duration_
Timer to time interval between button releases.
Definition click_type.h:90
uint16_t long_click_delay_
Definition click_type.h:76
virtual void get_configuration(JsonObject &doc) override
DelayReaction * delayed_click_report_
Definition click_type.h:96
uint16_t double_click_interval_
Definition click_type.h:84
virtual String get_config_schema() override
void on_button_press()
Processes incoming values that represent a "ButonPress" event.
void on_button_release()
Processes incoming value that represent a "ButtonRelease" event.
virtual void set_input(bool input, uint8_t input_channel=0) override
virtual bool set_configuration(const JsonObject &config) override
static bool is_click(ClickTypes value)
uint16_t ultra_long_click_delay_
Definition click_type.h:80
virtual void load_configuration()
Construct a new transform based on a single function.
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:54
void emit(T new_value)
const uint8_t PAGE_css_bootstrap[] PROGMEM
#define debugD(fmt,...)
Definition local_debug.h:47
#define debugW(fmt,...)
Definition local_debug.h:49