1 /*
2  * Copyright (c) 2023 Google LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_DATA_TYPES_H
8 #define ZEPHYR_INCLUDE_DRIVERS_SENSOR_DATA_TYPES_H
9 
10 #include <zephyr/dsp/types.h>
11 #include <zephyr/dsp/print_format.h>
12 
13 #include <inttypes.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 struct sensor_data_header {
20 	/**
21 	 * The closest timestamp for when the first frame was generated as attained by
22 	 * :c:func:`k_uptime_ticks`.
23 	 */
24 	uint64_t base_timestamp_ns;
25 	/**
26 	 * The number of elements in the 'readings' array.
27 	 *
28 	 * This must be at least 1
29 	 */
30 	uint16_t reading_count;
31 };
32 
33 /**
34  * Data for a sensor channel which reports on three axes. This is used by:
35  * - :c:enum:`SENSOR_CHAN_ACCEL_X`
36  * - :c:enum:`SENSOR_CHAN_ACCEL_Y`
37  * - :c:enum:`SENSOR_CHAN_ACCEL_Z`
38  * - :c:enum:`SENSOR_CHAN_ACCEL_XYZ`
39  * - :c:enum:`SENSOR_CHAN_GYRO_X`
40  * - :c:enum:`SENSOR_CHAN_GYRO_Y`
41  * - :c:enum:`SENSOR_CHAN_GYRO_Z`
42  * - :c:enum:`SENSOR_CHAN_GYRO_XYZ`
43  * - :c:enum:`SENSOR_CHAN_MAGN_X`
44  * - :c:enum:`SENSOR_CHAN_MAGN_Y`
45  * - :c:enum:`SENSOR_CHAN_MAGN_Z`
46  * - :c:enum:`SENSOR_CHAN_MAGN_XYZ`
47  * - :c:enum:`SENSOR_CHAN_POS_DX`
48  * - :c:enum:`SENSOR_CHAN_POS_DY`
49  * - :c:enum:`SENSOR_CHAN_POS_DZ`
50  * - :c:enum:`SENSOR_CHAN_POS_DXYZ`
51  */
52 struct sensor_three_axis_data {
53 	struct sensor_data_header header;
54 	int8_t shift;
55 	struct sensor_three_axis_sample_data {
56 		uint32_t timestamp_delta;
57 		union {
58 			q31_t values[3];
59 			q31_t v[3];
60 			struct {
61 				q31_t x;
62 				q31_t y;
63 				q31_t z;
64 			};
65 		};
66 	} readings[1];
67 };
68 
69 #define PRIsensor_three_axis_data PRIu64 "ns, (%" PRIq(6) ", %" PRIq(6) ", %" PRIq(6) ")"
70 
71 #define PRIsensor_three_axis_data_arg(data_, readings_offset_)                                     \
72 	(data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta,   \
73 		PRIq_arg((data_).readings[(readings_offset_)].x, 6, (data_).shift),                \
74 		PRIq_arg((data_).readings[(readings_offset_)].y, 6, (data_).shift),                \
75 		PRIq_arg((data_).readings[(readings_offset_)].z, 6, (data_).shift)
76 
77 /**
78  * Data from a sensor where we only care about an event occurring. This is used to report triggers.
79  */
80 struct sensor_occurrence_data {
81 	struct sensor_data_header header;
82 	struct sensor_occurrence_sample_data {
83 		uint32_t timestamp_delta;
84 	} readings[1];
85 };
86 
87 #define PRIsensor_occurrence_data PRIu64 "ns"
88 
89 #define PRIsensor_occurrence_data_arg(data_, readings_offset_)                                     \
90 	(data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta
91 
92 struct sensor_q31_data {
93 	struct sensor_data_header header;
94 	int8_t shift;
95 	struct sensor_q31_sample_data {
96 		uint32_t timestamp_delta;
97 		union {
98 			q31_t value;
99 			q31_t light;           /**< Unit: lux */
100 			q31_t pressure;        /**< Unit: kilopascal */
101 			q31_t temperature;     /**< Unit: degrees Celsius */
102 			q31_t percent;         /**< Unit: percent */
103 			q31_t distance;        /**< Unit: meters */
104 			q31_t density;         /**< Unit: ug/m^3 */
105 			q31_t density_ppm;     /**< Unit: parts per million */
106 			q31_t density_ppb;     /**< Unit: parts per billion */
107 			q31_t resistance;      /**< Unit: ohms */
108 			q31_t voltage;         /**< Unit: volts */
109 			q31_t current;         /**< Unit: amps */
110 			q31_t power;           /**< Unit: watts */
111 			q31_t angle;           /**< Unit: degrees */
112 			q31_t electric_charge; /**< Unit: mAh */
113 			q31_t humidity;        /**< Unit: RH */
114 		};
115 	} readings[1];
116 };
117 
118 #define PRIsensor_q31_data PRIu64 "ns (%" PRIq(6) ")"
119 
120 #define PRIsensor_q31_data_arg(data_, readings_offset_)                                            \
121 	(data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta,   \
122 		PRIq_arg((data_).readings[(readings_offset_)].value, 6, (data_).shift)
123 
124 /**
125  * Data from a sensor that produces a byte of data. This is used by:
126  * - :c:enum:`SENSOR_CHAN_PROX`
127  */
128 struct sensor_byte_data {
129 	struct sensor_data_header header;
130 	struct sensor_byte_sample_data {
131 		uint32_t timestamp_delta;
132 		union {
133 			uint8_t value;
134 			struct {
135 				uint8_t is_near: 1;
136 				uint8_t padding: 7;
137 			};
138 		};
139 	} readings[1];
140 };
141 
142 #define PRIsensor_byte_data(field_name_) PRIu64 "ns (" STRINGIFY(field_name_) " = %" PRIu8 ")"
143 
144 #define PRIsensor_byte_data_arg(data_, readings_offset_, field_name_)                              \
145 	(data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta,   \
146 		(data_).readings[(readings_offset_)].field_name_
147 
148 /**
149  * Data from a sensor that produces a count like value. This is used by:
150  * - :c:enum:`SENSOR_CHAN_GAUGE_CYCLE_COUNT`
151  */
152 struct sensor_uint64_data {
153 	struct sensor_data_header header;
154 	struct sensor_uint64_sample_data {
155 		uint32_t timestamp_delta;
156 		uint64_t value;
157 	} readings[1];
158 };
159 
160 #define PRIsensor_uint64_data PRIu64 "ns (%" PRIu64 ")"
161 
162 #define PRIsensor_uint64_data_arg(data_, readings_offset_)                                         \
163 	(data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta,   \
164 		(data_).readings[(readings_offset_)].value
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_DATA_TYPES_H */
171