Dawn Framework 1.0
Universal data acquisition framework for embedded systems
sensor.hxx
1// dawn/include/dawn/io/sensor.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include "dawn/io/common.hxx"
9#include "dawn/porting/config.hxx"
10#include "dawn/porting/sensors.hxx"
11
12namespace dawn
13{
24 : public CIOCommon
25 , public CIOSensorPorting
26{
27public:
28 enum
29 {
30 IO_SENSOR_CFG_FIRST = 0,
41 IO_SENSOR_CFG_LAST = 31
42 };
43
44 static_assert(IO_SENSOR_CFG_LAST - 1 <= SObjectCfg::ID_MAX);
45
46 explicit CIOSensor(CDescObject &desc)
47 : CIOCommon(desc)
48 , info(getSensorInfo(getCls()))
49 , dsize(4)
52 , fd(-1)
53 {
54 }
55
56 ~CIOSensor() override;
57
58#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
59 const char *getClassNameStr() const override
60 {
61 switch (getCls())
62 {
64 return "accel";
66 return "mag";
68 return "gyro";
70 return "light";
72 return "baro";
74 return "prox";
76 return "humid";
78 return "temp";
80 return "atemp";
82 return "rgb";
84 return "ir";
86 return "uv";
88 return "gas";
90 return "gnss";
92 return "gnss_time";
94 return "gnss_info";
96 return "gnss_sats";
97 default:
98 return "sensor";
99 }
100 }
101#endif
102
103 int configure() override;
104 int deinit() override;
105 int getDataImpl(IODataCmn &data, size_t len) override;
106
107#ifdef CONFIG_DAWN_IO_NOTIFY
108 int getFd() const override;
109#endif
110
111 size_t getDataSize() const override;
112 size_t getDataDim() const override;
113
114 bool isRead() const override
115 {
116 return true;
117 };
118
119 bool isWrite() const override
120 {
121 return false;
122 };
123
124 bool isNotify() const override
125 {
126 return true;
127 };
128
129 bool isBatch() const override
130 {
131 return false;
132 };
133
134 constexpr static SObjectId::ObjectId objectId(uint16_t cls, uint8_t dtype, bool ts, uint16_t inst)
135 {
136 uint8_t flags = 0;
137
138#ifdef CONFIG_DAWN_IO_TIMESTAMP
139 if (ts)
140 {
141 flags |= CIOCommon::IO_FLAGS_TS;
142 }
143#else
144 DAWNASSERT(ts == false, "ts not supported");
145#endif
146
147 return SObjectId::objectId(SObjectId::OBJTYPE_IO, cls, dtype, flags, inst);
148 }
149
150 constexpr static SObjectId::ObjectId objectIdAccel(uint8_t dtype, bool ts, uint16_t inst)
151 {
152 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_ACCELEROMETER, dtype, ts, inst);
153 }
154
155 constexpr static SObjectId::ObjectId objectIdMag(uint8_t dtype, bool ts, uint16_t inst)
156 {
157 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_MAGNETICFIELD, dtype, ts, inst);
158 }
159
160 constexpr static SObjectId::ObjectId objectIdGyro(uint8_t dtype, bool ts, uint16_t inst)
161 {
162 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GYROSCOPE, dtype, ts, inst);
163 }
164
165 constexpr static SObjectId::ObjectId objectIdLight(uint8_t dtype, bool ts, uint16_t inst)
166 {
167 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_LIGHT, dtype, ts, inst);
168 }
169
170 constexpr static SObjectId::ObjectId objectIdBaro(uint8_t dtype, bool ts, uint16_t inst)
171 {
172 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_BAROMETER, dtype, ts, inst);
173 }
174
175 constexpr static SObjectId::ObjectId objectIdProx(uint8_t dtype, bool ts, uint16_t inst)
176 {
177 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_PROXIMITY, dtype, ts, inst);
178 }
179
180 constexpr static SObjectId::ObjectId objectIdHum(uint8_t dtype, bool ts, uint16_t inst)
181 {
182 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_HUMIDITY, dtype, ts, inst);
183 }
184
185 constexpr static SObjectId::ObjectId objectIdTemp(uint8_t dtype, bool ts, uint16_t inst)
186 {
187 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_TEMPERATURE, dtype, ts, inst);
188 }
189
190 constexpr static SObjectId::ObjectId objectIdAtemp(uint8_t dtype, bool ts, uint16_t inst)
191 {
192 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_ATEMPERATURE, dtype, ts, inst);
193 }
194
195 constexpr static SObjectId::ObjectId objectIdRgb(uint8_t dtype, bool ts, uint16_t inst)
196 {
197 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_RGB, dtype, ts, inst);
198 }
199
200 constexpr static SObjectId::ObjectId objectIdIr(uint8_t dtype, bool ts, uint16_t inst)
201 {
202 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_IR, dtype, ts, inst);
203 }
204
205 constexpr static SObjectId::ObjectId objectIdUv(uint8_t dtype, bool ts, uint16_t inst)
206 {
207 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_UV, dtype, ts, inst);
208 }
209
210 constexpr static SObjectId::ObjectId objectIdGas(uint8_t dtype, bool ts, uint16_t inst)
211 {
212 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GAS, dtype, ts, inst);
213 }
214
215 constexpr static SObjectId::ObjectId objectIdGnss(uint8_t dtype, bool ts, uint16_t inst)
216 {
217 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GNSS, dtype, ts, inst);
218 }
219
220 constexpr static SObjectId::ObjectId objectIdGnssTime(uint8_t dtype, bool ts, uint16_t inst)
221 {
222 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GNSS_TIME, dtype, ts, inst);
223 }
224
225 constexpr static SObjectId::ObjectId objectIdGnssInfo(uint8_t dtype, bool ts, uint16_t inst)
226 {
227 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GNSS_INFO, dtype, ts, inst);
228 }
229
230 constexpr static SObjectId::ObjectId objectIdGnssSats(uint8_t dtype, bool ts, uint16_t inst)
231 {
232 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GNSS_SATELLITES, dtype, ts, inst);
233 }
234
235 constexpr static SObjectCfg::ObjectCfgId cfgIdUpdateInterval()
236 {
240 true,
241 1,
243 }
244
245 constexpr static SObjectCfg::ObjectCfgId cfgIdMeasurementPeriod()
246 {
250 true,
251 1,
253 }
254
255protected:
256 // Must fit the largest NuttX sensor event struct read in one go. The GNSS
257 // event (struct sensor_gnss) is the largest at ~72 bytes; keep headroom.
258 constexpr static const size_t DATA_BUFFER_SIZE = 96;
259
260 const SIOSensorMapInfo *info;
261 size_t dsize;
262 uint32_t updateInterval;
264 char path[PATH_MAX] = {};
265 int fd;
266
267 // Validate the configured dtype and set dsize (bytes per element). The base
268 // accepts only the NuttX float sensor_data_t; GNSS subclasses override this
269 // to also accept integer time/satellite fields.
270 virtual int validateDtype();
271
272 int configureDesc(const CDescObject &desc);
273};
274} // Namespace dawn
Descriptor wrapper for individual object configuration.
Base class for all I/O objects.
Definition common.hxx:27
virtual int getFd() const
Get file descriptor for notifications.
Definition common.hxx:444
@ IO_FLAGS_TS
Timestamp support flag.
Definition common.hxx:76
@ IO_CLASS_SENSOR_MAGNETICFIELD
Magnetic field sensor.
Definition common.hxx:114
@ IO_CLASS_ANY
Any I/O class.
Definition common.hxx:86
@ IO_CLASS_SENSOR_LIGHT
Light sensor.
Definition common.hxx:116
@ IO_CLASS_SENSOR_BAROMETER
Barometer sensor.
Definition common.hxx:117
@ IO_CLASS_SENSOR_HUMIDITY
Humidity sensor.
Definition common.hxx:119
@ IO_CLASS_SENSOR_PROXIMITY
Proximity sensor.
Definition common.hxx:118
@ IO_CLASS_SENSOR_GNSS_TIME
GNSS UTC time (seconds since epoch)
Definition common.hxx:148
@ IO_CLASS_SENSOR_TEMPERATURE
Temperature sensor.
Definition common.hxx:120
@ IO_CLASS_SENSOR_GNSS_INFO
GNSS accuracy + DOP (eph/epv/hdop/pdop/vdop)
Definition common.hxx:149
@ IO_CLASS_SENSOR_UV
Ultraviolet sensor.
Definition common.hxx:124
@ IO_CLASS_SENSOR_GNSS
GNSS position+velocity (lat/lon/alt/speed/course)
Definition common.hxx:147
@ IO_CLASS_SENSOR_ACCELEROMETER
Accelerometer sensor.
Definition common.hxx:113
@ IO_CLASS_SENSOR_RGB
RGB color sensor.
Definition common.hxx:122
@ IO_CLASS_SENSOR_GYROSCOPE
Gyroscope sensor.
Definition common.hxx:115
@ IO_CLASS_SENSOR_GNSS_SATELLITES
GNSS satellites used in fix.
Definition common.hxx:150
@ IO_CLASS_SENSOR_IR
Infrared sensor.
Definition common.hxx:123
@ IO_CLASS_SENSOR_ATEMPERATURE
Ambient temperature sensor.
Definition common.hxx:121
@ IO_CLASS_SENSOR_GAS
Gas sensor.
Definition common.hxx:125
Generic sensor interface for various sensor types.
Definition sensor.hxx:26
uint32_t measurementPeriod
Sensor measurement period in milliseconds.
Definition sensor.hxx:263
int fd
File descriptor for sensor device.
Definition sensor.hxx:265
bool isNotify() const
Check if IO supports notifications.
Definition sensor.hxx:124
bool isWrite() const
Check if IO supports write operations.
Definition sensor.hxx:119
int deinit()
De-initialize object.
Definition sensor.cxx:170
@ IO_SENSOR_CFG_GAIN
Analog gain setting.
Definition sensor.hxx:33
@ IO_SENSOR_CFG_UNIT
Measurement unit selection.
Definition sensor.hxx:32
@ IO_SENSOR_CFG_ORIENTATION
Sensor orientation/mounting angle.
Definition sensor.hxx:39
@ IO_SENSOR_CFG_ALERT
Alert threshold configuration.
Definition sensor.hxx:40
@ IO_SENSOR_CFG_BANDWIDTHLP
Low-pass filter bandwidth.
Definition sensor.hxx:37
@ IO_SENSOR_CFG_MEASPERIOD
Measurement period.
Definition sensor.hxx:36
@ IO_SENSOR_CFG_SCALE
Scale/multiplier for raw sensor values.
Definition sensor.hxx:34
@ IO_SENSOR_CFG_BANDWIDTHHP
High-pass filter bandwidth.
Definition sensor.hxx:38
@ IO_SENSOR_CFG_UPDATEINTERVAL
Data update interval in milliseconds.
Definition sensor.hxx:35
@ IO_SENSOR_CFG_ENABLE
Sensor enable/disable configuration.
Definition sensor.hxx:31
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition sensor.cxx:178
bool isBatch() const
Check if IO supports batch operations.
Definition sensor.hxx:129
size_t getDataSize() const
Get data size in bytes.
Definition sensor.cxx:222
size_t dsize
Sensor data size in bytes.
Definition sensor.hxx:261
uint32_t updateInterval
Sensor update interval in milliseconds.
Definition sensor.hxx:262
char path[PATH_MAX]
Sensor device file path.
Definition sensor.hxx:264
const SIOSensorMapInfo * info
Sensor metadata.
Definition sensor.hxx:260
bool isRead() const
Check if IO supports read operations.
Definition sensor.hxx:114
int configure()
Configure object from descriptor data.
Definition sensor.cxx:103
size_t getDataDim() const
Get data vector dimension.
Definition sensor.cxx:227
uint16_t getCls() const
Get object class field.
Definition object.cxx:170
static ObjectCfgId objectCfg(uint8_t type, uint16_t cls, uint8_t dtype, bool rw, uint16_t size, uint8_t id)
Construct 32-bit ConfigID from component fields.
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
static size_t ID_MAX
Configuration ID field constants and bit shift positions.
Definition objectcfg.hxx:45
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ OBJTYPE_IO
Input/Output object type.
Definition objectid.hxx:184
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
static ObjectId objectId(uint8_t type, uint16_t cls, uint8_t dtype, uint8_t flags, uint16_t priv)
Construct 32-bit ObjectID from component fields.
Definition objectid.hxx:290