-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsensor_float.cpp
48 lines (40 loc) · 1.68 KB
/
sensor_float.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Base class for sensors that return a float
Note - we don't have any yet as SHT and DHT which return two floats, are subclasses of HT which is a subclass of Sensor
*/
#include "_settings.h" // Settings for what to include etc
#if defined(SENSOR_XYZ_WANT) || defined(SENSOR_MPQ_WANT)
#define SENSOR_FLOAT_WANT
#endif
#ifdef SENSOR_FLOAT_WANT
#include <Arduino.h>
#include <forward_list>
#include "sensor.h"
#include "sensor_float.h"
#include "system_mqtt.h"
#if defined(SENSOR_XYZ_DEBUG) || defined(SENSOR_MPQ_DEBUG) // TODO make this generic,
#define SENSOR_FLOAT_DEBUG
#endif
Sensor_Float::Sensor_Float(const char* topic_init, const unsigned long ms_init) : Sensor(topic_init, ms_init) { };
// TODO_C++_EXPERT this next line is a completely useless one there just to stop the compiler barfing. See https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable
// All subclasses will override this. Note same issue on sensor_float and sensor_uint16 and sensor_ht
float Sensor_Float::read() { Serial.println("Sensor_Float::read must be subclassed"); return -1; }
void Sensor_Float::set(const float newvalue) {
if (changed(newvalue)) {
value = newvalue;
act();
}
}
// TODO_C++_EXPERT is there a (pretty!) way to do these next two pairs of textually similar as one definition or a macro or something.
bool Sensor_Float::changed(const float newvalue) {
return (newvalue == value);
}
void Sensor_Float::act() {
if (topic) {
Mqtt->messageSend(topic, value, retain, qos); // Note messageSend will convert value to String and expand topic
}
}
void Sensor_Float::readAndSet() {
set(read()); // Will also send message via act()
}
#endif // SENSOR_FLOAT_WANT