Line data Source code
1 0 : /*
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 0 : 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 1 : 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 1 : 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 1 : struct sensor_three_axis_data {
53 0 : struct sensor_data_header header;
54 0 : int8_t shift;
55 0 : struct sensor_three_axis_sample_data {
56 0 : uint32_t timestamp_delta;
57 : union {
58 0 : q31_t values[3];
59 0 : q31_t v[3];
60 : struct {
61 0 : q31_t x;
62 0 : q31_t y;
63 0 : q31_t z;
64 : };
65 0 : };
66 0 : } readings[1];
67 : };
68 :
69 0 : #define PRIsensor_three_axis_data PRIu64 "ns, (%" PRIq(6) ", %" PRIq(6) ", %" PRIq(6) ")"
70 :
71 0 : #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 for a sensor channel which reports game rotation vector data. This is used by:
79 : * - :c:enum:`SENSOR_CHAN_GAME_ROTATION_VECTOR`
80 : */
81 1 : struct sensor_game_rotation_vector_data {
82 0 : struct sensor_data_header header;
83 0 : int8_t shift;
84 0 : struct sensor_game_rotation_vector_sample_data {
85 0 : uint32_t timestamp_delta;
86 : union {
87 0 : q31_t values[4];
88 0 : q31_t v[4];
89 : struct {
90 0 : q31_t x;
91 0 : q31_t y;
92 0 : q31_t z;
93 0 : q31_t w;
94 : };
95 0 : };
96 0 : } readings[1];
97 : };
98 :
99 0 : #define PRIsensor_game_rotation_vector_data PRIu64 \
100 : "ns, (%" PRIq(6) ", %" PRIq(6) ", %" PRIq(6) ", %" PRIq(6) ")"
101 :
102 0 : #define PRIsensor_game_rotation_vector_data_arg(data_, readings_offset_) \
103 : (data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta, \
104 : PRIq_arg((data_).readings[(readings_offset_)].x, 6, (data_).shift), \
105 : PRIq_arg((data_).readings[(readings_offset_)].y, 6, (data_).shift), \
106 : PRIq_arg((data_).readings[(readings_offset_)].z, 6, (data_).shift), \
107 : PRIq_arg((data_).readings[(readings_offset_)].w, 6, (data_).shift)
108 :
109 : /**
110 : * Data from a sensor where we only care about an event occurring. This is used to report triggers.
111 : */
112 1 : struct sensor_occurrence_data {
113 0 : struct sensor_data_header header;
114 0 : struct sensor_occurrence_sample_data {
115 0 : uint32_t timestamp_delta;
116 0 : } readings[1];
117 : };
118 :
119 0 : #define PRIsensor_occurrence_data PRIu64 "ns"
120 :
121 0 : #define PRIsensor_occurrence_data_arg(data_, readings_offset_) \
122 : (data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta
123 :
124 0 : struct sensor_q31_data {
125 0 : struct sensor_data_header header;
126 0 : int8_t shift;
127 0 : struct sensor_q31_sample_data {
128 0 : uint32_t timestamp_delta;
129 : union {
130 0 : q31_t value;
131 1 : q31_t light; /**< Unit: lux */
132 1 : q31_t pressure; /**< Unit: kilopascal */
133 1 : q31_t temperature; /**< Unit: degrees Celsius */
134 1 : q31_t percent; /**< Unit: percent */
135 1 : q31_t distance; /**< Unit: meters */
136 1 : q31_t density; /**< Unit: ug/m^3 */
137 1 : q31_t density_ppm; /**< Unit: parts per million */
138 1 : q31_t density_ppb; /**< Unit: parts per billion */
139 1 : q31_t resistance; /**< Unit: ohms */
140 1 : q31_t voltage; /**< Unit: volts */
141 1 : q31_t current; /**< Unit: amps */
142 1 : q31_t power; /**< Unit: watts */
143 1 : q31_t angle; /**< Unit: degrees */
144 1 : q31_t electric_charge; /**< Unit: mAh */
145 1 : q31_t humidity; /**< Unit: RH */
146 0 : };
147 0 : } readings[1];
148 : };
149 :
150 0 : #define PRIsensor_q31_data PRIu64 "ns (%" PRIq(6) ")"
151 :
152 0 : #define PRIsensor_q31_data_arg(data_, readings_offset_) \
153 : (data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta, \
154 : PRIq_arg((data_).readings[(readings_offset_)].value, 6, (data_).shift)
155 :
156 : /**
157 : * Data from a sensor that produces a byte of data. This is used by:
158 : * - :c:enum:`SENSOR_CHAN_PROX`
159 : */
160 1 : struct sensor_byte_data {
161 0 : struct sensor_data_header header;
162 0 : struct sensor_byte_sample_data {
163 0 : uint32_t timestamp_delta;
164 : union {
165 0 : uint8_t value;
166 : struct {
167 0 : uint8_t is_near: 1;
168 0 : uint8_t padding: 7;
169 : };
170 0 : };
171 0 : } readings[1];
172 : };
173 :
174 0 : #define PRIsensor_byte_data(field_name_) PRIu64 "ns (" STRINGIFY(field_name_) " = %" PRIu8 ")"
175 :
176 0 : #define PRIsensor_byte_data_arg(data_, readings_offset_, field_name_) \
177 : (data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta, \
178 : (data_).readings[(readings_offset_)].field_name_
179 :
180 : /**
181 : * Data from a sensor that produces a count like value. This is used by:
182 : * - :c:enum:`SENSOR_CHAN_GAUGE_CYCLE_COUNT`
183 : */
184 1 : struct sensor_uint64_data {
185 0 : struct sensor_data_header header;
186 0 : struct sensor_uint64_sample_data {
187 0 : uint32_t timestamp_delta;
188 0 : uint64_t value;
189 0 : } readings[1];
190 : };
191 :
192 0 : #define PRIsensor_uint64_data PRIu64 "ns (%" PRIu64 ")"
193 :
194 0 : #define PRIsensor_uint64_data_arg(data_, readings_offset_) \
195 : (data_).header.base_timestamp_ns + (data_).readings[(readings_offset_)].timestamp_delta, \
196 : (data_).readings[(readings_offset_)].value
197 :
198 : #ifdef __cplusplus
199 : }
200 : #endif
201 :
202 : #endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_DATA_TYPES_H */
|