-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsensor.cpp
62 lines (52 loc) · 1.53 KB
/
sensor.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Base class for sensors
*/
#include "_settings.h" // Settings for what to include etc
#include <Arduino.h>
#include <vector>
#include "sensor.h"
#include "system_mqtt.h"
std::vector<Sensor*> sensors; // TODO_C++_EXPERT I wanted this to be a static inside class Sensor but compiler barfs on it.
#ifdef SENSOR_DEBUG
void Sensor_debug(const char * const msg) {
Serial.print(msg);
for (Sensor* s: sensors) {
Serial.print(s->topic); Serial.print(F(" "));
}
Serial.println();
delay(1000);
} // Allow Serial to stabilize
#endif // SENSOR_DEBUG
Sensor::Sensor(const char* const t, const unsigned long m) : Frugal_Base(), topic(t), ms(m) {
sensors.push_back(this);
}
void Sensor::setup() { } // Default to do nothing
void Sensor::setupAll() {
for (Sensor* s: sensors) {
s->setup();
}
}
// TODO_C++_EXPERT - unclear why this is needed, all objects in "sensors" will be subclasses either Sensor_Uint16 or Sensor_Float each of which has a readAndSet method.
void Sensor::readAndSet() {
Serial.println(F("XXX25 Shouldnt be calling Sensor::readAndSet - should be a subclass"));
}
void Sensor::loop() {
if (nextLoopTime <= millis()) {
readAndSet(); // Will also send message via act()
nextLoopTime = millis() + ms;
}
}
void Sensor::loopAll() {
for (Sensor* s: sensors) {
s->loop();
}
}
/*
At this point no dispatching for sensors as none have INCOMING messages
void Sensor::dispatch() {String &topic, String &payload }
void Sensor::dispatchAll() {
for (Sensor* s: sensors) {
s->dispatch();
}
}
*/