SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
digital_input.cpp
Go to the documentation of this file.
1#include "sensesp.h"
2
3#include "digital_input.h"
4
5#include <FunctionalInterrupt.h>
6#include <elapsedMillis.h>
7
8namespace sensesp {
9
10bool DigitalInputState::to_json(JsonObject& root) {
11 root["read_delay"] = read_delay_;
12 return true;
13}
14
15bool DigitalInputState::from_json(const JsonObject& config) {
16 String const expected[] = {"read_delay"};
17 for (auto str : expected) {
18 if (!config[str].is<JsonVariant>()) {
19 return false;
20 }
21 }
22 read_delay_ = config["read_delay"];
23 return true;
24}
25
26bool DigitalInputCounter::to_json(JsonObject& root) {
27 root["read_delay"] = read_delay_;
28 return true;
29}
30
31bool DigitalInputCounter::from_json(const JsonObject& config) {
32 String const expected[] = {"read_delay"};
33 for (auto str : expected) {
34 if (!config[str].is<JsonVariant>()) {
35 return false;
36 }
37 }
38 read_delay_ = config["read_delay"];
39 return true;
40}
41
42void DigitalInputDebounceCounter::handleInterrupt() {
43 if (since_last_event_ > ignore_interval_ms_) {
44 counter_++;
45 since_last_event_ = 0;
46 }
47}
48
49bool DigitalInputDebounceCounter::to_json(JsonObject& root) {
50 root["read_delay"] = read_delay_;
51 root["ignore_interval"] = ignore_interval_ms_;
52 return true;
53}
54
55bool DigitalInputDebounceCounter::from_json(const JsonObject& config) {
56 String const expected[] = {"read_delay", "ignore_interval"};
57 for (auto str : expected) {
58 if (!config[str].is<JsonVariant>()) {
59 ESP_LOGE(
60 __FILENAME__,
61 "Cannot set DigitalInputDebounceConfiguration configuration: missing "
62 "json field %s",
63 str.c_str());
64 return false;
65 }
66 }
67
68 read_delay_ = config["read_delay"];
69 ignore_interval_ms_ = config["ignore_interval"];
70 return true;
71}
72
73const String ConfigSchema(DigitalInputState& obj) {
74 return R"###({"type":"object","properties":{"read_delay":{"title":"Read delay","type":"number","description":"The time, in milliseconds, between each read of the input"}} })###";
75}
76
78 return true;
79}
80
81const String ConfigSchema(const DigitalInputCounter& obj) {
82 return R"###({"type":"object","properties":{"read_delay":{"title":"Read delay","type":"number","description":"The time, in milliseconds, between each read of the input"}} })###";
83}
84
86 return true;
87}
88
89const String ConfigSchema(const DigitalInputDebounceCounter& obj) {
90 return R"###({"type":"object","properties":{"read_delay":{"title":"Read delay","type":"number","description":"The time, in milliseconds, between each read of the input"},"ignore_interval":{"title":"Ignore interval","type":"number","description":"The time, in milliseconds, to ignore events after a recorded event"}} })###";
91}
92
94 return true;
95}
96
97} // namespace sensesp
DigitalInputCounter counts interrupts and reports the count every read_delay ms.
volatile unsigned int counter_
virtual bool to_json(JsonObject &root) override
virtual bool from_json(const JsonObject &config) override
DigitalInputDebounceCounter counts interrupts and reports the count every read_delay ms,...
DigitalInputState polls the state of an input pin every read_delay ms.
const String ConfigSchema(const SmartSwitchController &obj)
bool ConfigRequiresRestart(const HTTPServer &obj)