SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
digital_input.h
Go to the documentation of this file.
1#ifndef SENSESP_SENSORS_DIGITAL_INPUT_H_
2#define SENSESP_SENSORS_DIGITAL_INPUT_H_
3
4#include <elapsedMillis.h>
5
7#include "sensesp_base_app.h"
8#include "sensor.h"
9
10namespace sensesp {
11
21 public:
22 DigitalInput(uint8_t pin, int pin_mode) : pin_{pin} {
23 pinMode(pin_, pin_mode);
24 };
25
26 protected:
27 uint8_t pin_;
28};
29
50class DigitalInputState : public DigitalInput, public Sensor<bool> {
51 public:
52 DigitalInputState(uint8_t pin, int pin_mode, int read_delay = 1000,
53 String config_path = "")
54 : DigitalInput{pin, pin_mode},
55 Sensor<bool>(config_path),
56 read_delay_{read_delay},
57 triggered_{false} {
58 load();
59
60 event_loop()->onRepeat(read_delay_,
61 [this]() { this->emit(digitalRead(pin_)); });
62 }
63
64 private:
65 int read_delay_;
66 bool triggered_;
67 virtual bool to_json(JsonObject& root) override;
68 virtual bool from_json(const JsonObject& config) override;
69};
70
71const String ConfigSchema(const DigitalInputState& obj);
72
74
93class DigitalInputCounter : public DigitalInput, public Sensor<int> {
94 public:
95 DigitalInputCounter(uint8_t pin, int pin_mode, int interrupt_type,
96 unsigned int read_delay, String config_path = "")
97 : DigitalInputCounter(pin, pin_mode, interrupt_type, read_delay,
98 config_path, [this]() { this->counter_++; }) {
99 event_loop()->onInterrupt(pin_, interrupt_type_, interrupt_handler_);
100
101 event_loop()->onRepeat(read_delay_, [this]() {
102 noInterrupts();
104 counter_ = 0;
105 interrupts();
106 notify();
107 });
108 }
109
110 virtual bool to_json(JsonObject& root) override;
111 virtual bool from_json(const JsonObject& config) override;
112
113 protected:
114 DigitalInputCounter(uint8_t pin, int pin_mode, int interrupt_type,
115 unsigned int read_delay, String config_path,
116 std::function<void()> interrupt_handler)
117 : DigitalInput{pin, pin_mode},
118 Sensor<int>(config_path),
119 read_delay_{read_delay},
120 interrupt_type_{interrupt_type},
121 interrupt_handler_{interrupt_handler} {
122 load();
123 }
124
125 unsigned int read_delay_;
126 volatile unsigned int counter_ = 0;
127
128 private:
129 int interrupt_type_;
130 std::function<void()> interrupt_handler_;
131};
132
133const String ConfigSchema(const DigitalInputCounter& obj);
134
136
159 public:
160 DigitalInputDebounceCounter(uint8_t pin, int pin_mode, int interrupt_type,
161 unsigned int read_delay,
162 unsigned int ignore_interval_ms,
163 String config_path = "")
164 : DigitalInputCounter(pin, pin_mode, interrupt_type, read_delay,
165 config_path, [this]() { this->handleInterrupt(); }),
166 ignore_interval_ms_{ignore_interval_ms} {}
167
168 private:
169 void handleInterrupt();
170
171 unsigned int ignore_interval_ms_;
172 elapsedMillis since_last_event_;
173 virtual bool to_json(JsonObject& root) override;
174 virtual bool from_json(const JsonObject& config) override;
175};
176
177const String ConfigSchema(const DigitalInputDebounceCounter& obj);
178
179bool ConfigRequiresRestart(const DigitalInputDebounceCounter& obj);
180
201class DigitalInputChange : public DigitalInput, public Sensor<bool> {
202 public:
203 DigitalInputChange(uint8_t pin, int pin_mode, int interrupt_type,
204 String config_path = "")
205 : DigitalInput{pin, pin_mode},
206 Sensor<bool>(config_path),
207 interrupt_type_{interrupt_type},
208 triggered_{true} {
209 load();
210 output_ = (bool)digitalRead(pin_);
211 last_output_ = !output_; // ensure that we always send the first output_
212
213 event_loop()->onInterrupt(pin_, interrupt_type_, [this]() {
214 output_ = (bool)digitalRead(pin_);
215 triggered_ = true;
216 });
217
218 event_loop()->onTick([this]() {
219 if (triggered_ && (output_ != last_output_)) {
220 noInterrupts();
221 triggered_ = false;
222 last_output_ = output_;
223 interrupts();
224 notify();
225 }
226 });
227 }
228
229 private:
230 int interrupt_type_;
231 bool triggered_;
232 bool last_output_;
233 virtual bool to_json(JsonObject& doc) override { return true; }
234 virtual bool from_json(const JsonObject& config) override { return true; }
235};
236
237} // namespace sensesp
238
239#endif
DigitalInputChange provides output whenever a digital pin changes its state.
DigitalInputChange(uint8_t pin, int pin_mode, int interrupt_type, String config_path="")
DigitalInputCounter counts interrupts and reports the count every read_delay ms.
volatile unsigned int counter_
DigitalInputCounter(uint8_t pin, int pin_mode, int interrupt_type, unsigned int read_delay, String config_path, std::function< void()> interrupt_handler)
virtual bool to_json(JsonObject &root) override
virtual bool from_json(const JsonObject &config) override
DigitalInputCounter(uint8_t pin, int pin_mode, int interrupt_type, unsigned int read_delay, String config_path="")
DigitalInputDebounceCounter counts interrupts and reports the count every read_delay ms,...
DigitalInputDebounceCounter(uint8_t pin, int pin_mode, int interrupt_type, unsigned int read_delay, unsigned int ignore_interval_ms, String config_path="")
DigitalInput is the base class for reading a digital GPIO pin.
DigitalInput(uint8_t pin, int pin_mode)
DigitalInputState polls the state of an input pin every read_delay ms.
DigitalInputState(uint8_t pin, int pin_mode, int read_delay=1000, String config_path="")
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
Sensor template class for any sensor producing actual values.
Definition sensor.h:40
void emit(const bool &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
bool ConfigRequiresRestart(const HTTPServer &obj)