SensESP 3.0.1
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#include "sensesp_base_app.h"
5
6namespace sensesp {
7
8ClickType::ClickType(const String& config_path, uint16_t long_click_delay,
9 uint16_t double_click_interval,
10 uint16_t ultra_long_click_delay)
11 : Transform<bool, ClickTypes>(config_path),
12 click_count_{0},
13 long_click_delay_{long_click_delay},
14 ultra_long_click_delay_{ultra_long_click_delay},
15 double_click_interval_{double_click_interval},
16 delayed_click_report_{NULL} {
17 load();
18}
19
20void ClickType::set(const bool& input) {
21 if (input) {
23 } else {
25 }
26}
27
29 return (value != ClickTypes::ButtonPress &&
31}
32
34 ESP_LOGD(
35 __FILENAME__,
36 "ClickType received PRESS on click count %d (millis: %ld, last release: "
37 "%ld ms ago)",
38 click_count_, millis(), (long)release_duration_);
39
40 if (click_count_ == 0) {
41 // This is a new, isolated "click" that we have not yet processed.
44 } else {
45 // One or more presses is already in progress...
46
48 // The button down is the second one reported in a row without a button
49 // release and the press is now long enough to qualify as an "ultra long
50 // click"
53 // This is the start of a second click to come in prior to the expiration
54 // of the double_click_interval. Remove any "SingleClick" report that may
55 // have been queued up....
56 if (delayed_click_report_ != NULL) {
59 ESP_LOGD(__FILENAME__,
60 "ClickType press is double click. Removed queued SingleClick "
61 "report");
62 }
64 }
65 }
66
68}
69
71 ESP_LOGD(__FILENAME__,
72 "ClickType received UNPRESS for click count %d (millis: %ld, press "
73 "duration: %ld ms)",
74 click_count_, millis(), (long)press_duration_);
75
76 if (click_count_ > 0) {
77 // This is the "release" of a click we are tracking...
80 this->on_click_completed();
81 } else if (press_duration_ >= this->long_click_delay_) {
82 ESP_LOGD(
83 __FILENAME__,
84 "ClickType detected LongSingleClick (millis: %ld, press duration %ld "
85 "ms)",
86 millis(), (long)press_duration_);
88 this->on_click_completed();
89 } else if (this->click_count_ > 1) {
90 // We have just ended a double click. Sent it immediately...
91 ESP_LOGD(
92 __FILENAME__,
93 "ClickType detected DoubleClick (millis: %ld, press duration %ld ms)",
94 millis(), (long)press_duration_);
96 this->on_click_completed();
97 } else {
98 // This is the end of a potential single click. Queue up the send of a
99 // SingleClick report, but delay it in case another click comes in prior
100 // to the double_click_interval, which would turn this click into a
101 // DoubleClick
102 uint64_t const time_of_event = millis();
103 int64_t const pd = (long)press_duration_;
105 double_click_interval_ + 20, [this, pd, time_of_event]() {
106 ESP_LOGD(
107 __FILENAME__,
108 "ClickType detected SingleClick (millis: %ld, queue time: %ld, "
109 "press duration %ld ms)",
110 millis(), static_cast<long>(time_of_event),
111 static_cast<long>(pd));
113 this->on_click_completed();
114 });
115 }
116
119
120 } else {
121 // A press release with no initial press should happen only when
122 // an UltraLongClick has already been sent, or the producer
123 // is feeding us weird values...
125 ESP_LOGW(__FILENAME__,
126 "ClickType detected UNPRESS with no pending PRESS (millis=%ld)",
127 millis());
128 }
129}
130
132 event_loop()->onDelay(
133 5, [this, value]() { this->emit(value); });
134}
135
142
144 ESP_LOGD(
145 __FILENAME__,
146 "ClickType detected UltraLongSingleClick (millis: %ld, press duration "
147 "%ld ms)",
148 millis(), (long)press_duration_);
151}
152
153bool ClickType::to_json(JsonObject& root) {
154 root["long_click_delay"] = long_click_delay_;
155 root["ultra_long_click_delay"] = ultra_long_click_delay_;
156 root["double_click_interval"] = double_click_interval_;
157 return true;
158}
159
160bool ClickType::from_json(const JsonObject& config) {
161 String const expected[] = {"long_click_delay", "ultra_long_click_delay",
162 "double_click_interval"};
163 for (auto str : expected) {
164 if (!config[str].is<JsonVariant>()) {
165 return false;
166 }
167 }
168 long_click_delay_ = config["long_click_delay"];
169 ultra_long_click_delay_ = config["ultra_long_click_delay"];
170 double_click_interval_ = config["double_click_interval"];
171 return true;
172}
173
174const String ConfigSchema(const ClickType& obj) {
175 return R"({"type":"object","properties":{"long_click_delay":{"title":"Long Click Delay","type":"integer"},"double_click_interval":{"title":"Double Click Interval","type":"integer"},"ultra_long_click_delay":{"title":"Ultra Long Click Delay","type":"integer"}}})";
176}
177
178} // namespace sensesp
ClickType is a transform that consumes button clicks and translates them as events of type ClickTypes...
Definition click_type.h:29
void emitDelayed(ClickTypes value)
elapsedMillis press_duration_
Timmer to time button presses.
Definition click_type.h:87
virtual bool from_json(const JsonObject &config) override
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
reactesp::DelayEvent * delayed_click_report_
Definition click_type.h:96
uint16_t double_click_interval_
Definition click_type.h:84
ClickType(const 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:8
virtual void set(const bool &input) 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.
static bool is_click(ClickTypes value)
virtual bool to_json(JsonObject &root) override
uint16_t ultra_long_click_delay_
Definition click_type.h:80
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:53
void emit(const ClickTypes &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9