SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
press_repeater.cpp
Go to the documentation of this file.
1#include "press_repeater.h"
2
3namespace sensesp {
4
5PressRepeater::PressRepeater(const String& config_path, int integer_false,
6 int repeat_start_interval, int repeat_interval)
7 : BooleanTransform(config_path),
8 integer_false_{integer_false},
9 repeat_start_interval_{repeat_start_interval},
10 repeat_interval_{repeat_interval},
11 pushed_{false},
12 repeating_{false} {
13 load();
14
15 event_loop()->onRepeat(10, [this]() {
16 if (pushed_) {
17 // A press is currently in progress
18 if (repeating_) {
19 if (last_value_sent_ > static_cast<uint64_t>(repeat_interval_)) {
20 ESP_LOGD(__FILENAME__, "Repeating press report");
22 this->emit(true);
23 }
24 } else if (last_value_sent_ >
25 static_cast<uint64_t>(repeat_start_interval_)) {
26 ESP_LOGD(__FILENAME__, "Starting press report repeat");
27 repeating_ = true;
29 this->emit(true);
30 }
31 }
32 });
33}
34
35void PressRepeater::set(const bool& new_value) {
36 if (new_value != pushed_) {
37 pushed_ = new_value;
38
39 if (!pushed_) {
40 repeating_ = false;
41 }
42
44 this->emit(pushed_);
45 }
46}
47
48bool PressRepeater::to_json(JsonObject& root) {
49 root["repeat_start_interval"] = repeat_start_interval_;
50 root["repeat_interval"] = repeat_interval_;
51 return true;
52}
53
54bool PressRepeater::from_json(const JsonObject& config) {
55 String const expected[] = {"repeat_start_interval", "repeat_interval"};
56 for (auto str : expected) {
57 if (!config[str].is<JsonVariant>()) {
58 return false;
59 }
60 }
61 repeat_start_interval_ = config["repeat_start_interval"];
62 repeat_interval_ = config["repeat_interval"];
63 return true;
64}
65
66const String ConfigSchema(const PressRepeater& obj) {
67 return R"###({"type":"object","properties":{"repeat_start_interval":{"title":"Start repeating after (ms)","type":"integer"},"repeat_interval":{"title":"Repeat report interval (ms)","type":"integer"}}})###";
68}
69
70} // namespace sensesp
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
A transform that takes boolean inputs and adds button behaviors familiar to many device end users....
virtual bool from_json(const JsonObject &config) override
PressRepeater(const String &config_path="", int integer_false=0, int repeat_start_interval=1500, int repeat_interval=250)
elapsedMillis last_value_sent_
virtual bool to_json(JsonObject &root) override
virtual void set(const bool &new_value) override
A common type of transform that consumes, transforms, then outputs values of the same data type.
Definition transform.h:94
void emit(const T &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9