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)
50 , updateInterval(0)
51 , measurementPeriod(0)
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";
89 default:
90 return "sensor";
91 }
92 }
93#endif
94
95 int configure() override;
96 int deinit() override;
97 int getDataImpl(IODataCmn &data, size_t len) override;
98
99#ifdef CONFIG_DAWN_IO_NOTIFY
100 int getFd() const override;
101#endif
102
103 size_t getDataSize() const override;
104 size_t getDataDim() const override;
105
106 bool isRead() const override
107 {
108 return true;
109 };
110
111 bool isWrite() const override
112 {
113 return false;
114 };
115
116 bool isNotify() const override
117 {
118 return true;
119 };
120
121 bool isBatch() const override
122 {
123 return false;
124 };
125
126 constexpr static SObjectId::ObjectId objectId(uint16_t cls, uint8_t dtype, bool ts, uint16_t inst)
127 {
128 uint8_t flags = 0;
129
130#ifdef CONFIG_DAWN_IO_TIMESTAMP
131 if (ts)
132 {
133 flags |= CIOCommon::IO_FLAGS_TS;
134 }
135#else
136 DAWNASSERT(ts == false, "ts not supported");
137#endif
138
139 return SObjectId::objectId(SObjectId::OBJTYPE_IO, cls, dtype, flags, inst);
140 }
141
142 constexpr static SObjectId::ObjectId objectIdAccel(uint8_t dtype, bool ts, uint16_t inst)
143 {
144 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_ACCELEROMETER, dtype, ts, inst);
145 }
146
147 constexpr static SObjectId::ObjectId objectIdMag(uint8_t dtype, bool ts, uint16_t inst)
148 {
149 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_MAGNETICFIELD, dtype, ts, inst);
150 }
151
152 constexpr static SObjectId::ObjectId objectIdGyro(uint8_t dtype, bool ts, uint16_t inst)
153 {
154 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GYROSCOPE, dtype, ts, inst);
155 }
156
157 constexpr static SObjectId::ObjectId objectIdLight(uint8_t dtype, bool ts, uint16_t inst)
158 {
159 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_LIGHT, dtype, ts, inst);
160 }
161
162 constexpr static SObjectId::ObjectId objectIdBaro(uint8_t dtype, bool ts, uint16_t inst)
163 {
164 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_BAROMETER, dtype, ts, inst);
165 }
166
167 constexpr static SObjectId::ObjectId objectIdProx(uint8_t dtype, bool ts, uint16_t inst)
168 {
169 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_PROXIMITY, dtype, ts, inst);
170 }
171
172 constexpr static SObjectId::ObjectId objectIdHum(uint8_t dtype, bool ts, uint16_t inst)
173 {
174 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_HUMIDITY, dtype, ts, inst);
175 }
176
177 constexpr static SObjectId::ObjectId objectIdTemp(uint8_t dtype, bool ts, uint16_t inst)
178 {
179 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_TEMPERATURE, dtype, ts, inst);
180 }
181
182 constexpr static SObjectId::ObjectId objectIdAtemp(uint8_t dtype, bool ts, uint16_t inst)
183 {
184 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_ATEMPERATURE, dtype, ts, inst);
185 }
186
187 constexpr static SObjectId::ObjectId objectIdRgb(uint8_t dtype, bool ts, uint16_t inst)
188 {
189 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_RGB, dtype, ts, inst);
190 }
191
192 constexpr static SObjectId::ObjectId objectIdIr(uint8_t dtype, bool ts, uint16_t inst)
193 {
194 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_IR, dtype, ts, inst);
195 }
196
197 constexpr static SObjectId::ObjectId objectIdUv(uint8_t dtype, bool ts, uint16_t inst)
198 {
199 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_UV, dtype, ts, inst);
200 }
201
202 constexpr static SObjectId::ObjectId objectIdGas(uint8_t dtype, bool ts, uint16_t inst)
203 {
204 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GAS, dtype, ts, inst);
205 }
206
207 constexpr static SObjectCfg::ObjectCfgId cfgIdUpdateInterval()
208 {
212 true,
213 1,
215 }
216
217 constexpr static SObjectCfg::ObjectCfgId cfgIdMeasurementPeriod()
218 {
222 true,
223 1,
225 }
226
227private:
228 constexpr static const size_t DATA_BUFFER_SIZE = 32;
229
230 const SIOSensorMapInfo *info;
231 size_t dsize;
232 uint32_t updateInterval;
233 uint32_t measurementPeriod;
234 char path[PATH_MAX] = {};
235 int fd;
236
237 int configureDesc(const CDescObject &desc);
238};
239} // 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:425
@ 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_TEMPERATURE
Temperature sensor.
Definition common.hxx:120
@ IO_CLASS_SENSOR_UV
Ultraviolet sensor.
Definition common.hxx:124
@ 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_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
bool isNotify() const
Check if IO supports notifications.
Definition sensor.hxx:116
bool isWrite() const
Check if IO supports write operations.
Definition sensor.hxx:111
int deinit()
De-initialize object.
Definition sensor.cxx:144
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition sensor.cxx:152
bool isBatch() const
Check if IO supports batch operations.
Definition sensor.hxx:121
size_t getDataSize() const
Get data size in bytes.
Definition sensor.cxx:196
@ 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
bool isRead() const
Check if IO supports read operations.
Definition sensor.hxx:106
int configure()
Configure object from descriptor data.
Definition sensor.cxx:71
size_t getDataDim() const
Get data vector dimension.
Definition sensor.cxx:201
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