Generalized-Core-Counter 3.20
Particle-based generalized core counter firmware
Loading...
Searching...
No Matches
ISensor.h
Go to the documentation of this file.
1// src/ISensor.h
2#ifndef ISENSOR_H
3#define ISENSOR_H
4
5#include "Particle.h"
6
23struct SensorData {
25 time_t timestamp;
26
28 char sensorType[16];
29
32
34 uint16_t primary;
35
37 uint16_t secondary;
38
40 uint16_t aux1;
41
43 uint16_t aux2;
44
46 bool flag1;
47
49 bool flag2;
50
55 primary(0), secondary(0), aux1(0), aux2(0),
56 flag1(false), flag2(false) {
57 sensorType[0] = '\0';
58 }
59
66 bool toJSON(char* buffer, size_t bufferSize) const;
67};
68
75class ISensor {
76public:
77 virtual ~ISensor() {}
78
83 virtual bool setup() = 0;
84
89 virtual bool loop() = 0;
90
95 virtual SensorData getData() const = 0;
96
101 virtual const char* getSensorType() const = 0;
102
107 virtual bool isReady() const = 0;
108
112 virtual void reset() = 0;
113
120 virtual bool initializeHardware() { return setup(); }
121
127 virtual void onSleep() {}
128
134 virtual bool onWake() { return true; }
135
139 virtual bool usesInterrupt() const { return false; }
140
147 virtual bool isHealthy() const { return true; }
148
152 virtual int lastErrorCode() const { return 0; }
153};
154
155// Implementation of SensorData::toJSON
156inline bool SensorData::toJSON(char* buffer, size_t bufferSize) const {
157 if (!buffer || bufferSize < 100) return false;
158
159 JSONBufferWriter writer(buffer, bufferSize);
160 writer.beginObject();
161
162 writer.name("sensorType").value(sensorType); // Already char*, no .c_str() needed
163 writer.name("timestamp").value((int)timestamp);
164
165 // Only include non-default values to save bandwidth
166 if (primary > 0) {
167 writer.name("primary").value(primary);
168 }
169 if (secondary > 0) {
170 writer.name("secondary").value(secondary);
171 }
172 if (aux1 > 0) {
173 writer.name("aux1").value(aux1);
174 }
175 if (aux2 > 0) {
176 writer.name("aux2").value(aux2);
177 }
178 if (flag1) {
179 writer.name("flag1").value(flag1);
180 }
181 if (flag2) {
182 writer.name("flag2").value(flag2);
183 }
184
185 writer.endObject();
186
187 return writer.dataSize() > 0;
188}
189
190#endif /* ISENSOR_H */
Abstract interface for all sensors.
Definition ISensor.h:75
virtual bool setup()=0
Initialize the sensor hardware.
virtual void reset()=0
Reset sensor state and clear any cached data.
virtual ~ISensor()
Definition ISensor.h:77
virtual void onSleep()
Notification that the device is entering deep sleep.
Definition ISensor.h:127
virtual bool onWake()
Notification that the device is waking from deep sleep.
Definition ISensor.h:134
virtual bool usesInterrupt() const
Whether this sensor uses a hardware interrupt for events.
Definition ISensor.h:139
virtual bool initializeHardware()
Initialize underlying hardware after power-on.
Definition ISensor.h:120
virtual int lastErrorCode() const
Last sensor-specific error code (if any).
Definition ISensor.h:152
virtual SensorData getData() const =0
Get the latest sensor data.
virtual bool loop()=0
Poll the sensor for new data.
virtual const char * getSensorType() const =0
Get sensor type identifier.
virtual bool isReady() const =0
Check if sensor is initialized and ready.
virtual bool isHealthy() const
Health check for the sensor.
Definition ISensor.h:147
Generic sensor data structure.
Definition ISensor.h:23
uint16_t aux2
Auxiliary numeric field 2.
Definition ISensor.h:43
bool hasNewData
Flag indicating if this record contains new data.
Definition ISensor.h:31
bool flag2
Spare boolean flag for future use.
Definition ISensor.h:49
uint16_t secondary
Secondary numeric value (score, quality, ...).
Definition ISensor.h:37
char sensorType[16]
Type of sensor ("PIR", "Ultrasonic", etc.).
Definition ISensor.h:28
SensorData()
Construct a new SensorData with default values.
Definition ISensor.h:54
time_t timestamp
When the data was captured (Unix time).
Definition ISensor.h:25
uint16_t aux1
Auxiliary numeric field 1.
Definition ISensor.h:40
bool toJSON(char *buffer, size_t bufferSize) const
Convert sensor data to JSON string for publishing.
Definition ISensor.h:156
uint16_t primary
Main numeric value (count, distance, faceNumber, ...).
Definition ISensor.h:34
bool flag1
Generic boolean flag (e.g., motionDetected, occupied, inRange).
Definition ISensor.h:46