SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
digital_input.cpp
Go to the documentation of this file.
1#include "digital_input.h"
2
3#include <elapsedMillis.h>
4#include <FunctionalInterrupt.h>
5
6#include "sensesp.h"
7
8namespace sensesp {
9
10void DigitalInputState::get_configuration(JsonObject& root) {
11 root["read_delay"] = read_delay_;
12}
13
14static const char SCHEMA2[] PROGMEM = R"###({
15 "type": "object",
16 "properties": {
17 "read_delay": { "title": "Read delay", "type": "number", "description": "The time, in milliseconds, between each read of the input" }
18 }
19 })###";
20
21String DigitalInputState::get_config_schema() { return FPSTR(SCHEMA2); }
22
23bool DigitalInputState::set_configuration(const JsonObject& config) {
24 String expected[] = {"read_delay"};
25 for (auto str : expected) {
26 if (!config.containsKey(str)) {
27 return false;
28 }
29 }
30 read_delay_ = config["read_delay"];
31 return true;
32}
33
34void DigitalInputCounter::get_configuration(JsonObject& root) {
35 root["read_delay"] = read_delay_;
36}
37
38static const char SCHEMA[] PROGMEM = R"###({
39 "type": "object",
40 "properties": {
41 "read_delay": { "title": "Read delay", "type": "number", "description": "The time, in milliseconds, between each read of the input" }
42 }
43 })###";
44
45String DigitalInputCounter::get_config_schema() { return FPSTR(SCHEMA); }
46
47bool DigitalInputCounter::set_configuration(const JsonObject& config) {
48 String expected[] = {"read_delay"};
49 for (auto str : expected) {
50 if (!config.containsKey(str)) {
51 return false;
52 }
53 }
54 read_delay_ = config["read_delay"];
55 return true;
56}
57
58void DigitalInputDebounceCounter::handleInterrupt() {
59 if (since_last_event_ > ignore_interval_ms_) {
60 counter_++;
61 since_last_event_ = 0;
62 }
63}
64
65void DigitalInputDebounceCounter::get_configuration(JsonObject& root) {
66 root["read_delay"] = read_delay_;
67 root["ignore_interval"] = ignore_interval_ms_;
68}
69
70static const char DEBOUNCE_SCHEMA[] PROGMEM = R"###({
71 "type": "object",
72 "properties": {
73 "read_delay": { "title": "Read delay", "type": "number", "description": "The time, in milliseconds, between each read of the input" },
74 "ignore_interval": { "title": "Ignore interval", "type": "number", "description": "The time, in milliseconds, to ignore events after a recorded event" }
75 }
76 })###";
77
78String DigitalInputDebounceCounter::get_config_schema() {
79 return FPSTR(DEBOUNCE_SCHEMA);
80}
81
82bool DigitalInputDebounceCounter::set_configuration(const JsonObject& config) {
83 String expected[] = {"read_delay", "ignore_interval"};
84 for (auto str : expected) {
85 if (!config.containsKey(str)) {
86 debugE(
87 "Cannot set DigitalInputDebounceConfiguration configuration: missing "
88 "json field %s",
89 str.c_str());
90 return false;
91 }
92 }
93
94 read_delay_ = config["read_delay"];
95 ignore_interval_ms_ = config["ignore_interval"];
96 return true;
97}
98
100 ReactESP::app->onInterrupt(pin_, interrupt_type_, [this]() {
102 triggered_ = true;
103 });
104
105 ReactESP::app->onTick([this]() {
106 if (triggered_ && (output != last_output_)) {
107 noInterrupts();
108 triggered_ = false;
109 last_output_ = output;
110 interrupts();
111 notify();
112 }
113 });
114}
115
116} // namespace sensesp
virtual void start() override final
volatile unsigned int counter_
Construct a new transform based on a single function.
const uint8_t PAGE_css_bootstrap[] PROGMEM
#define debugE(fmt,...)
Definition local_debug.h:50