SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensor.h
Go to the documentation of this file.
1#ifndef _sensor_H_
2#define _sensor_H_
3
4#include <set>
5
10
11namespace sensesp {
12
28class Sensor : virtual public Observable,
29 public Configurable,
30 public Startable {
31 public:
33
34 static const std::set<Sensor*>& get_sensors() { return sensors_; }
35
36 private:
37 static std::set<Sensor*> sensors_;
38};
39
44template <typename T>
45class SensorT : public Sensor, public ValueProducer<T> {
46 public:
49};
50
55
56template <class T>
57class RepeatSensor : public SensorT<T> {
58 public:
71 RepeatSensor<T>(unsigned int repeat_interval_ms, std::function<T()> callback)
72 : SensorT<T>(""),
74 returning_callback_(callback) {}
75
90 std::function<void(RepeatSensor<T>*)> callback)
91 : SensorT<T>(""),
93 emitting_callback_(callback) {}
94
96 if (emitting_callback_ != nullptr) {
97 ReactESP::app->onRepeat(repeat_interval_ms_,
98 [this]() { emitting_callback_(this); });
99 } else {
100 ReactESP::app->onRepeat(repeat_interval_ms_,
101 [this]() { this->emit(this->returning_callback_()); });
102 }
103 }
104
105 protected:
107 std::function<T()> returning_callback_ = nullptr;
108 std::function<void(RepeatSensor<T>*)> emitting_callback_ = nullptr;
109};
110
111} // namespace sensesp
112
113#endif
An object that is capable of having configuration data that can be set remotely using a RESTful API,...
Construct a new transform based on a single function.
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:106
void start() override final
Definition sensor.h:95
std::function< void(RepeatSensor< T > *)> emitting_callback_
Definition sensor.h:108
std::function< T()> returning_callback_
Definition sensor.h:107
The base class for all sensors. Used only as a base class - never instantiated directly in a project.
Definition sensor.h:30
static const std::set< Sensor * > & get_sensors()
Definition sensor.h:34
Sensor template class for any sensor producing actual values.
Definition sensor.h:45
Automatic calling of the start() method at startup.
Definition startable.h:20
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(T new_value)
SensorT< String > StringSensor
Definition sensor.h:54
SensorT< float > FloatSensor
Definition sensor.h:51
SensorT< int > IntSensor
Definition sensor.h:52
SensorT< bool > BoolSensor
Definition sensor.h:53