SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensor.h
Go to the documentation of this file.
1#ifndef SENSESP_SENSORS_SENSOR_H_
2#define SENSESP_SENSORS_SENSOR_H_
3
4#include <set>
5
9#include "sensesp_base_app.h"
10
11namespace sensesp {
12
28class SensorConfig : virtual public Observable, public FileSystemSaveable {
29 public:
30 SensorConfig(const String& config_path) : FileSystemSaveable(config_path) {}
31
32 private:
33};
34
39template <typename T>
40class Sensor : public SensorConfig, virtual public ValueProducer<T> {
41 public:
42 Sensor<T>(String config_path)
43 : SensorConfig(config_path), ValueProducer<T>() {}
44};
45
50
51template <class T>
52class RepeatSensor : public Sensor<T> {
53 public:
66 RepeatSensor<T>(unsigned int repeat_interval_ms, std::function<T()> callback)
67 : Sensor<T>(""),
68 repeat_interval_ms_(repeat_interval_ms),
69 returning_callback_(callback) {
70 event_loop()->onRepeat(repeat_interval_ms_, [this]() {
71 this->emit(this->returning_callback_());
72 });
73 }
74
88 RepeatSensor<T>(unsigned int repeat_interval_ms,
89 std::function<void(RepeatSensor<T>*)> callback)
90 : Sensor<T>(""),
91 repeat_interval_ms_(repeat_interval_ms),
92 emitting_callback_(callback) {
94 [this]() { emitting_callback_(this); });
95 }
96
97 protected:
98 unsigned int repeat_interval_ms_;
99 std::function<T()> returning_callback_ = nullptr;
100 std::function<void(RepeatSensor<T>*)> emitting_callback_ = nullptr;
101};
102
103} // namespace sensesp
104
105#endif
A base class which allow observers to attach callbacks to themselves. The callbacks will be called wh...
Definition observable.h:16
unsigned int repeat_interval_ms_
Definition sensor.h:98
std::function< void(RepeatSensor< T > *)> emitting_callback_
Definition sensor.h:100
RepeatSensor(unsigned int repeat_interval_ms, std::function< T()> callback)
Construct a new RepeatSensor object.
Definition sensor.h:66
std::function< T()> returning_callback_
Definition sensor.h:99
The base class for all sensors. Used only as a base class - never instantiated directly in a project.
Definition sensor.h:28
SensorConfig(const String &config_path)
Definition sensor.h:30
Sensor template class for any sensor producing actual values.
Definition sensor.h:40
Sensor(String config_path)
Definition sensor.h:42
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(const T &new_value)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
Sensor< float > FloatSensor
Definition sensor.h:46
Sensor< int > IntSensor
Definition sensor.h:47
Sensor< bool > BoolSensor
Definition sensor.h:48
Sensor< String > StringSensor
Definition sensor.h:49