-
Notifications
You must be signed in to change notification settings - Fork 0
/
barometric_sensor.h
430 lines (405 loc) · 18.6 KB
/
barometric_sensor.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// SPDX-FileCopyrightText: © 2022 Embedded Artistry LLC <[email protected]>
// SPDX-License-Identifier: MIT
#ifndef VIRTUAL_BAROMETRIC_SENSOR_H_
#define VIRTUAL_BAROMETRIC_SENSOR_H_
#include <stdbool.h>
#include <stdint.h>
/** @file barometric_sensor.h
* Example barometric pressure sensor interfaces that also support altitude calculations.
*
* This header defines three variations of a barometric sensor:
* - A simple interface, which only provides the capabilities of reading pressure/altitude
* (BarometricSensor).
* - A variation which supports callbacks (BarometricSensor_withCb).
* - A variation that supports callbacks and expects to be used in an asynchronous
* system (BarometricSensor_asyncWithCb).
*
* Note that there are differences in fundamental assumptions and function behaviors
* across the variations. Even small changes in an interface can impact expected
* behaviors.
*
* ## Modifying the Interfaces
*
* There are a number of ways you might modify this interface to suit your needs:
*
* - Change the type of the pressure parameter (e.g., to a float)
* - Change the fixed-point format of the parameter (e.g., to 24.8, giving a wider range
* reduced resolution. Or adopt a standard like Q16 across all values in your program
* so you can simplify math operations)
* - Return the pressure sample, and use a separate function to determine validity.
* - Specifying additional callback parameters
* - Varying the types of callbacks used
* - Separating callbacks for altitude and pressure updates
* - Eliminating the ability to unregister a callback
* - Supporting only a single callback function (this is primarily a change in
* documentation to note the change in assumption)
*/
#pragma mark - Basic Interface -
/** Virtual Barometric Pressure/Altimeter Sensor Interface
*
* A standard interface for a device which can measure barometric pressure.
*
* This is a simple interface, which only provides the ability to read the current
* sample.
*
* ## Fundamental Assumptions
*
* - The device produces barometric pressure readings
* - This device reports barometric pressure in hectopascal (hPa)
* - The reported barometric pressure reading will be compensated for ambient temperature
* by the implementation if it is required.
* - Pressure will be formatted as a 32-bit fixed-point integer with format UQ22.10,
* giving a resolution of 0.001 hPa.
* - This device produces barometric altitude readings
* - This device will report barometric altitude in meters
* - Altitude will be formatted as a 32-bit fixed-point integer with format Q21.10,
* giving a resolution of 0.001 m.
* - Altitude will be corrected for Sea Level Pressure. If no value for SLP has been supplied,
* calculations will assume 1013.25 hPa.
* - The device will indicate whether the current sample is valid or invalid
*
* - This interface, at its core, appears to be blocking. However, you can still implement this
* interface in a non-blocking way. For example, you could always return the most recent altitude
* measurement, while there is another thread (or a timer) that handles updates to that
* measurement asynchronously.
*/
typedef struct
{
/** Read the current pressure from the device.
*
* @pre The pressure sensor has been properly initialized by the system.
* @pre The pressure parameter is not NULL.
* @post If the measurement is valid, the data pointed to by the pressure parameter
* will be updated with the latest reading.
* @post If the measurement is invalid, the data pointed to by the pressure parameter
* will remain unchanged.
*
* @param[inout] pressure
* Pointer which will be used for storing the latest pressure reading. This pointer
* must not be null.
*
* Pressure will be formatted as a 32-bit fixed-point integer with format UQ22.10,
* giving a resolution of 0.001 hPa.
*
* @returns True if the sample is valid, false if invalid (e.g., an error occurred)
*/
bool (*readPressure)(uint32_t* const pressure);
/** Get the current altitude, corrected for Sea Level Pressure
*
* If no value for SLP has been supplied, calculations will assume 1013.25 hPa.
*
* @pre The pressure sensor has been properly initialized by the system.
* @pre The altitude parameter is not NULL.
* @post If the measurement is valid, the data pointed to by the altitude parameter
* will be updated with the latest reading.
* @post If the measurement is invalid, the data pointed to by the altitude parameter
* will remain unchanged.
*
* @returns Current altitude in meters (m), corrected for sea level pressure.
* Altitude is specified as a signed 32-bit fixed-point number in format Q21.10.
*/
bool (*readAltitude)(int32_t* const altitude);
/** Set the sea level pressure
*
* @param[in] slp The current sea level pressure in hPa.
* slp should be specified as an unsigned 32-bit fixed-point number in format UQ22.10.
*/
void (*setSeaLevelPressure)(uint32_t slp);
} BarometricSensor;
#pragma mark - Interface with Callback Support -
/** Callback function prototype for processing new barometric samples
*
* When a new (and valid) barometric pressure/altitude sample is available, this callback
* function will be invoked.
*
* The callback is not guaranteed to run on its own thread of control. We recommend
* keeping the implementation small. Your function implementation could take the
* new sample and perform some dispatching operation (e.g., add the value to a queue),
* ensuring that any "heavy" processing happens on a new thread.
*
* @param[in] pressure The latest pressure sample.
*
* Pressure will be formatted as a 32-bit fixed-point integer with format UQ22.10,
* giving a resolution of 0.001 hPa.
*
* @param[in] altitude The latest altitude sample.
*
* Altitude will be formatted as a signed 32-bit fixed-point number in format Q21.10.
*
*/
typedef void (*NewBarometricSampleCb)(uint32_t pressure, int32_t altitude);
/** Callback function prototype for barometric sensor errors
*
* When an error in the virtual barometric device occurs, this callback function
* will be invoked. The virtual device itself does not support error handling
* capabilities, so we recommend using this callback in tightly-coupled system
* code to take the appropriate recovery action (restart the device, restart the
* system, etc.).
*
* The callback is not guaranteed to run on its own thread of control. We recommend
* keeping the implementation small. Your function implementation could take the
* new sample and perform some dispatching operation (e.g., add the value to a queue),
* ensuring that any "heavy" processing happens on a new thread.
*/
typedef void (*BarometricErrorCb)(void);
/** Virtual Barometric Pressure/Altimeter Interface (with callback support)
*
* A standard interface for a device which can measure barometric pressure. In this variant, the
* readPressure() and readAltitude() functions can be used in the following ways:
* - Have the caller receive the current sample, and trigger registered callbacks
* - Have the caller trigger a reading (input parameter == NULL), but only have registered
* callbacks receive the data.
*
* ## Fundamental Assumptions
*
* - The device produces barometric pressure readings
* - This device reports barometric pressure in hectopascal (hPa)
* - The reported barometric pressure reading will be compensated for ambient temperature
* by the implementation if it is required.
* - Pressure will be formatted as a 32-bit fixed-point integer with format UQ22.10,
* giving a resolution of 0.001 hPa.
* - This device produces barometric altitude readings
* - This device will report barometric altitude in meters
* - Altitude will be formatted as a 32-bit fixed-point integer with format Q21.10,
* giving a resolution of 0.001 m.
* - Altitude will be corrected for Sea Level Pressure. If no value for SLP has been supplied,
* calculations will assume 1013.25 hPa.
* - The device will indicate whether the current sample is valid or invalid
* - The device will notify interested parties when a new valid sample is available.
*
* ## Undesired event assumptions
* - If an error occurs internally, the virtual device will notify interested parties
* by issuing an error callback. The registered parties can take desired action
* when this occurs (e.g., attempt recovery, stop querying the sensor).
*
* ## Implementation Notes
*
* - This interface, at its core, appears to be blocking. However, you can still implement this
* interface in a non-blocking way. For example, you could always return the most recent pressure
* measurement, while there is another thread that handles updates to that measurement
* asynchronously.
* - Note that the callback registration functions do not support error handling.
* We recommend that implementers trigger an assert() or other crash if a callback
* cannot be added to a list due to exceeding fixed size constraints.
*/
typedef struct
{
/** Request an pressure sample from the device.
*
* @pre The pressure sensor has been properly initialized by the system.
* @post If the measurement is valid and pressure is not NULL, the data pointed
* to by the pressure parameter will be updated with the latest reading
* @post If the measurement is invalid, the data pointed to by the pressure parameter
* will remain unchanged.
* @post If the measurement is valid, registered New Sample callbacks will be invoked
* or dispatched with the new measurement.
* @post If the measurement is not valid, registered Error callbacks will be invoked
* or dispatched.
*
* @param[inout] pressure
* Pointer which will be used for storing the latest pressure reading.
*
* If pressure is NULL, the function will only supply the pressure sample to
* registered callback functions. I
*
* Pressure will be formatted as a 32-bit fixed-point integer with format UQ22.10,
* giving a resolution of 0.001 hPa.
*
* @returns True if the sample is valid, false if invalid (e.g., an error occurred)
*/
bool (*readPressure)(uint32_t* const pressure);
/** Request an altitude sample from the device.
*
* Altitude will be corrected for Sea Level Pressure. If no value for SLP has been supplied,
* calculations will assume 1013.25 hPa.
*
* @pre The altitude sensor has been properly initialized by the system.
* @post If the measurement is valid and the altitude parameter is not NULL, the data pointed
* to will be updated with the latest reading
* @post If the measurement is invalid, the data pointed to by the altitude parameter
* will remain unchanged.
* @post If the measurement is valid, registered New Sample callbacks will be invoked
* or dispatched with the new measurement.
* @post If the measurement is not valid, registered Error callbacks will be invoked
* or dispatched.
*
* @param[inout] altitude
* Pointer which will be used for storing the latest altitude reading.
*
* If altitude is NULL, the function will only supply the altitude sample to
* registered callback functions.
*
* Altitude is specified as a signed 32-bit fixed-point number in format Q21.10
*
* Altitude will be corrected for Sea Level Pressure.
*
* @returns True if the sample is valid, false if invalid (e.g., an error occurred)
*/
bool (*readAltitude)(int32_t* const altitude);
/** Set the sea level pressure
*
* @param[in] slp The current sea level pressure in hPa.
* slp should be specified as an unsigned 32-bit fixed-point number in format UQ22.10.
*/
void (*setSeaLevelPressure)(uint32_t slp);
/** Register a NewBarometricSampleCb function
*
* This function will add the callback input to a list of functions to execute
* when a new and valid sample is available.
*
* @pre callback is not NULL
* @post callback is added to the list of "new sample" callbacks.
*
* @param[in] callback
* The callback function pointer to register on the "new sample" callback list.
*/
void (*registerNewSampleCb)(const NewBarometricSampleCb callback);
/** Remove a registered NewBarometricSampleCb function
*
* This function will remove a callback function from the registered list of
* "new sample" callbacks. If the function has not been previously registered,
* the parameter will be ignored and the list will be unchanged.
*
* @post callback function pointer is not present on the list of "new sample" callbacks.
*
* @param[in] callback
* The callback function pointer to remove from the "new sample" callback list.
*/
void (*unregisterNewSampleCb)(const NewBarometricSampleCb callback);
/** Register a BarometricErrorCb function
*
* This function will add the callback input to a list of functions to execute
* when a pressure sensor error occurs.
*
* @pre callback is not NULL
* @post callback is added to the list of error callbacks.
*
* @param[in] callback
* The callback function pointer to register on the "error" callback list.
*/
void (*registerErrorCb)(const BarometricErrorCb callback);
/** Remove a registered BarometricErrorCb function
*
* This function will remove a callback function from the registered list of
* "error" callbacks. If the function has not been previously registered,
* the parameter will be ignored and the list will be unchanged.
*
* @post callback function pointer is not present on the list of "error" callbacks.
*
* @param[in] callback
* The callback function pointer to remove from the "error" callback list.
*/
void (*unregisterErrorCb)(const BarometricErrorCb callback);
} BarometricSensor_withCb;
#pragma mark - Asynchronous Processing Support -
/** Virtual Barometric Pressure/Altimeter Interface (asynchronous mode)
*
* A standard interface for a device which can measure barometric pressure. This interface
* is intended for use with _asynchronous_ implementations. That is, the readPressure() function
* cannot be a blocking function, but will instead be expected to enqueue a request to geneate
* a new pressure sample. The pressure sample, whenver it becomes available, will be passed
* along to interested parties via the registered callback functions.
*
* ## Fundamental Assumptions
*
* - The device produces barometric pressure readings
* - This device reports barometric pressure in hectopascal (hPa)
* - The reported barometric pressure reading will be compensated for ambient temperature
* by the implementation if it is required.
* - Pressure will be formatted as a 32-bit fixed-point integer with format UQ22.10,
* giving a resolution of 0.001 hPa.
* - This device produces barometric altitude readings
* - This device will report barometric altitude in meters
* - Altitude will be formatted as a 32-bit fixed-point integer with format Q21.10,
* giving a resolution of 0.001 m.
* - Altitude will be corrected for Sea Level Pressure. If no value for SLP has been supplied,
* calculations will assume 1013.25 hPa.
* - readPressure() will be used to request a new pressure sample from the device.
* - The device will notify interested parties when a new valid sample is available after
* a successful readPressure() request has completed.
*
* ## Undesired event assumptions
*
* - If an error occurs internally, the virtual device will notify interested parties
* by issuing an error callback. The registered parties can take desired action
* when this occurs (e.g., attempt recovery, stop querying the sensor).
* - The readPressure() function will provide inidication if the request cannot be made
* (e.g., queue full)
*
* ## Implementation Notes
*
* - readPressure() should be a non-blocking call. It should not directly query the sensor and
* wait for a response, but should instead send a request to a queue of some type.
* - Note that the callback registration functions do not support error handling.
* We recommend that implementers trigger an assert() or other crash if a callback
* cannot be added to a list due to exceeding fixed size constraints.
*/
typedef struct
{
/** Request a new current pressure/altitude sample from the device.
*
* Submit a request to the device for a new sample. When available, the data will be provided
* to registered callback functions.
*
* @pre The barometric sensor has been properly initialized by the system.
*
* @returns True if the request was successful, False if the request could not be successfully
* enqueued (e.g., the queue is full).
*/
bool (*readSample)(void);
/** Set the sea level pressure
*
* @param[in] slp The current sea level pressure in hPa.
* slp should be specified as an unsigned 32-bit fixed-point number in format UQ22.10.
*/
void (*setSeaLevelPressure)(uint32_t slp);
/** Register a NewBarometricSampleCb function
*
* This function will add the callback input to a list of functions to execute
* when a new and valid sample is available.
*
* @pre callback is not NULL
* @post callback is added to the list of "new sample" callbacks.
*
* @param[in] callback
* The callback function pointer to register on the "new sample" callback list.
*/
void (*registerNewSampleCb)(const NewBarometricSampleCb callback);
/** Remove a registered NewBarometricSampleCb function
*
* This function will remove a callback function from the registered list of
* "new sample" callbacks. If the function has not been previously registered,
* the parameter will be ignored and the list will be unchanged.
*
* @post callback function pointer is not present on the list of "new sample" callbacks.
*
* @param[in] callback
* The callback function pointer to remove from the "new sample" callback list.
*/
void (*unregisterNewSampleCb)(const NewBarometricSampleCb callback);
/** Register a BarometricErrorCb function
*
* This function will add the callback input to a list of functions to execute
* when a pressure sensor error occurs.
*
* @pre callback is not NULL
* @post callback is added to the list of error callbacks.
*
* @param[in] callback
* The callback function pointer to register on the "error" callback list.
*/
void (*registerErrorCb)(const BarometricErrorCb callback);
/** Remove a registered BarometricErrorCb function
*
* This function will remove a callback function from the registered list of
* "error" callbacks. If the function has not been previously registered,
* the parameter will be ignored and the list will be unchanged.
*
* @post callback function pointer is not present on the list of "error" callbacks.
*
* @param[in] callback
* The callback function pointer to remove from the "error" callback list.
*/
void (*unregisterErrorCb)(const BarometricErrorCb callback);
} BarometricSensor_asyncWithCb;
#endif // VIRTUAL_BAROMETRIC_SENSOR_H_