BNO08X Driver 0.1.0
API reference
Loading...
Searching...
No Matches
bno08x_reports.hpp
Go to the documentation of this file.
1
12#ifndef BNO08X_REPORTS_HPP
13#define BNO08X_REPORTS_HPP
14
15#include <array>
16#include <cstddef>
17#include <cstdint>
18#include <span>
19
20#include "bno08x_detail.hpp"
21#include "bno08x_registers.hpp"
22#include "bno08x_shtp.hpp"
23#include "bno08x_types.hpp"
24
25namespace bno08x {
26
27namespace detail {
28
30enum class FieldKind : uint8_t {
31 U8,
32 S16,
33 U16,
34 U32,
35 S32,
36};
37
39struct FieldDef {
40 uint8_t off{0};
41 FieldKind kind{FieldKind::U8};
42};
43
45inline constexpr std::size_t kMaxFields = 14;
46
48struct SensorSpec {
49 uint8_t count{0};
50 uint8_t hub_ts{0xFF};
51 bool lsb_only{false};
52 std::array<FieldDef, kMaxFields> f{};
53};
54
56constexpr SensorSpec Run(FieldKind kind, uint8_t first, uint8_t step, uint8_t n) noexcept {
57 SensorSpec s{};
58 s.count = n;
59 for (uint8_t i = 0; i < n; ++i) {
60 s.f[i] = FieldDef{.off = static_cast<uint8_t>(first + (i * step)), .kind = kind};
61 }
62 return s;
63}
64
66constexpr SensorSpec Plus(SensorSpec base, uint8_t off, FieldKind kind) noexcept {
67 base.f[base.count] = FieldDef{.off = off, .kind = kind};
68 ++base.count;
69 return base;
70}
71
74constexpr void FillRare(std::array<SensorSpec, reg::detail::kSensorTableSize>& t) noexcept {
75 const SensorSpec quat = Run(FieldKind::S16, 4, 2, 4);
76 const SensorSpec quat_acc = Plus(quat, 12, FieldKind::S16);
77 t[0x28] = quat_acc;
78 t[0x29] = quat;
79 t[0x2B] = Run(FieldKind::U8, 4, 1, 2);
80 SensorSpec flow = Run(FieldKind::S16, 4, 2, 3);
81 flow = Plus(
82 Plus(
83 Plus(Plus(Plus(Plus(Plus(flow, 10, FieldKind::U8), 11, FieldKind::U8), 12, FieldKind::U8),
84 13, FieldKind::U8),
85 14, FieldKind::U8),
86 15, FieldKind::U8),
87 16, FieldKind::U8);
88 t[0x2C] = Plus(Plus(flow, 18, FieldKind::U16), 20, FieldKind::U32);
89 SensorSpec pose = Run(FieldKind::U32, 4, 4, 1);
90 pose.hub_ts = 0;
91 SensorSpec words = Run(FieldKind::S32, 8, 4, 13);
92 for (uint8_t i = 0; i < 13; ++i) {
93 pose = Plus(pose, words.f[i].off, FieldKind::S32);
94 }
95 t[0x2D] = pose;
96}
97
99constexpr std::array<SensorSpec, reg::detail::kSensorTableSize> MakeSpecs() noexcept {
100 std::array<SensorSpec, reg::detail::kSensorTableSize> t{};
101 const SensorSpec vec3 = Run(FieldKind::S16, 4, 2, 3);
102 const SensorSpec quat = Run(FieldKind::S16, 4, 2, 4);
103 const SensorSpec quat_acc = Plus(quat, 12, FieldKind::S16);
104 const SensorSpec six = Run(FieldKind::S16, 4, 2, 6);
105 const SensorSpec word16 = Run(FieldKind::U16, 4, 2, 1);
106 SensorSpec lsb16 = word16;
107 lsb16.lsb_only = true;
108 SensorSpec raw = Plus(vec3, 12, FieldKind::U32);
109 raw.hub_ts = 3;
110 SensorSpec raw_gyro = Plus(Plus(vec3, 10, FieldKind::S16), 12, FieldKind::U32);
111 raw_gyro.hub_ts = 4;
112 for (const std::size_t id : std::array<std::size_t, 5>{0x01, 0x02, 0x03, 0x04, 0x06}) {
113 t[id] = vec3;
114 }
115 t[0x05] = quat_acc;
116 t[0x07] = six;
117 t[0x08] = quat;
118 t[0x09] = quat_acc;
119 t[0x0A] = Run(FieldKind::U32, 4, 4, 1);
120 t[0x0B] = Run(FieldKind::U32, 4, 4, 1);
121 for (const std::size_t id : std::array<std::size_t, 3>{0x0C, 0x0D, 0x0E}) {
122 t[id] = Run(FieldKind::S16, 4, 2, 1);
123 }
124 t[0x0F] = six;
125 t[0x10] = Run(FieldKind::U8, 4, 1, 1);
126 t[0x11] = Plus(Run(FieldKind::U32, 4, 4, 1), 8, FieldKind::U16);
127 t[0x12] = lsb16;
128 t[0x13] = Run(FieldKind::U8, 4, 1, 1);
129 t[0x14] = raw;
130 t[0x15] = raw_gyro;
131 t[0x16] = raw;
132 t[0x18] = Run(FieldKind::U32, 4, 4, 1);
133 for (const std::size_t id :
134 std::array<std::size_t, 7>{0x19, 0x1A, 0x1B, 0x1C, 0x20, 0x21, 0x22}) {
135 t[id] = lsb16;
136 }
137 t[0x1E] = Run(FieldKind::U8, 4, 1, 12);
138 t[0x1F] = Run(FieldKind::U8, 4, 1, 1);
139 t[0x23] = word16;
140 FillRare(t);
141 return t;
142}
143
145inline constexpr std::array<SensorSpec, reg::detail::kSensorTableSize> kSpecs = MakeSpecs();
146
148constexpr std::array<uint8_t, kSensorSlots> MakeQPoints() noexcept {
149 std::array<uint8_t, kSensorSlots> q{};
150 const std::array<std::array<uint8_t, 2>, 17> rows{{{{0x01, 8}},
151 {{0x02, 9}},
152 {{0x03, 4}},
153 {{0x04, 8}},
154 {{0x05, 14}},
155 {{0x06, 8}},
156 {{0x07, 9}},
157 {{0x08, 14}},
158 {{0x09, 14}},
159 {{0x0A, 20}},
160 {{0x0B, 8}},
161 {{0x0C, 8}},
162 {{0x0D, 4}},
163 {{0x0E, 7}},
164 {{0x0F, 4}},
165 {{0x28, 14}},
166 {{0x29, 14}}}};
167 for (const auto& r : rows) {
168 q[r[0]] = r[1];
169 }
170 q[0x2A] = 14;
171 q[0x2D] = 15;
172 return q;
173}
174
176inline constexpr std::array<uint8_t, kSensorSlots> kQPoints = MakeQPoints();
177
179constexpr std::array<uint16_t, kSensorSlots> MakeMetadataIds() noexcept {
180 std::array<uint16_t, kSensorSlots> m{};
181 const std::array<std::array<uint16_t, 2>, 37> rows{{
182 {{0x01, 0xE302}}, {{0x02, 0xE306}}, {{0x03, 0xE309}}, {{0x04, 0xE303}}, {{0x05, 0xE30B}},
183 {{0x06, 0xE304}}, {{0x07, 0xE307}}, {{0x08, 0xE30C}}, {{0x09, 0xE30D}}, {{0x0A, 0xE30E}},
184 {{0x0B, 0xE30F}}, {{0x0C, 0xE310}}, {{0x0D, 0xE311}}, {{0x0E, 0xE312}}, {{0x0F, 0xE30A}},
185 {{0x10, 0xE313}}, {{0x11, 0xE315}}, {{0x12, 0xE316}}, {{0x13, 0xE317}}, {{0x14, 0xE301}},
186 {{0x15, 0xE305}}, {{0x16, 0xE308}}, {{0x18, 0xE314}}, {{0x19, 0xE318}}, {{0x1A, 0xE319}},
187 {{0x1B, 0xE31A}}, {{0x1C, 0xE31B}}, {{0x1E, 0xE31C}}, {{0x1F, 0xE31D}}, {{0x20, 0xE31E}},
188 {{0x21, 0xE31F}}, {{0x22, 0xE320}}, {{0x23, 0xE321}}, {{0x28, 0xE322}}, {{0x29, 0xE323}},
189 {{0x2A, 0xE324}}, {{0x2B, 0xE325}},
190 }};
191 for (const auto& r : rows) {
192 m[r[0]] = r[1];
193 }
194 return m;
195}
196
198inline constexpr std::array<uint16_t, kSensorSlots> kMetadataIds = MakeMetadataIds();
199
201inline constexpr std::array<const char*, kSensorSlots> kSensorNames{nullptr,
202 "Accelerometer",
203 "GyroscopeCalibrated",
204 "MagneticFieldCalibrated",
205 "LinearAcceleration",
206 "RotationVector",
207 "Gravity",
208 "GyroscopeUncalibrated",
209 "GameRotationVector",
210 "GeomagneticRotationVector",
211 "Pressure",
212 "AmbientLight",
213 "Humidity",
214 "Proximity",
215 "Temperature",
216 "MagneticFieldUncalibrated",
217 "TapDetector",
218 "StepCounter",
219 "SignificantMotion",
220 "StabilityClassifier",
221 "RawAccelerometer",
222 "RawGyroscope",
223 "RawMagnetometer",
224 nullptr,
225 "StepDetector",
226 "ShakeDetector",
227 "FlipDetector",
228 "PickupDetector",
229 "StabilityDetector",
230 nullptr,
231 "PersonalActivityClassifier",
232 "SleepDetector",
233 "TiltDetector",
234 "PocketDetector",
235 "CircleDetector",
236 "HeartRateMonitor",
237 nullptr,
238 nullptr,
239 nullptr,
240 nullptr,
241 "ArvrRotationVector",
242 "ArvrGameRotationVector",
243 "GyroIntegratedRotationVector",
244 "MotionRequest",
245 "OpticalFlow",
246 "DeadReckoningPose",
247 "WheelEncoder"};
248
250[[nodiscard]] constexpr int32_t ReadField(std::span<const uint8_t> report, std::size_t base,
251 const FieldDef& fld) noexcept {
252 const std::size_t o = static_cast<std::size_t>(fld.off) - base;
253 int32_t v = 0;
254 switch (fld.kind) {
255 case FieldKind::U8:
256 v = static_cast<int32_t>(report[o]);
257 break;
258 case FieldKind::S16:
259 v = S16(Le16(report, o));
260 break;
261 case FieldKind::U16:
262 v = static_cast<int32_t>(Le16(report, o));
263 break;
264 case FieldKind::U32:
265 v = static_cast<int32_t>(
266 Le32(report, o)); // bit pattern; read back with `static_cast<uint32_t>`
267 break;
268 case FieldKind::S32:
269 v = S32(Le32(report, o));
270 break;
271 default:
272 break;
273 }
274 return v;
275}
276
278[[nodiscard]] constexpr std::size_t FieldWidth(FieldKind kind) noexcept {
279 std::size_t w = 1;
280 if ((kind == FieldKind::S16) || (kind == FieldKind::U16)) {
281 w = 2;
282 } else if ((kind == FieldKind::U32) || (kind == FieldKind::S32)) {
283 w = 4;
284 } else {
285 // one byte
286 }
287 return w;
288}
289
290} // namespace detail
291
297[[nodiscard]] constexpr uint8_t QPoint(SensorId id) noexcept {
298 const auto slot = static_cast<std::size_t>(detail::ToUnderlying(id));
299 return (slot < kSensorSlots) ? detail::kQPoints[slot] : uint8_t{0};
300}
301
307[[nodiscard]] constexpr uint16_t MetadataRecord(SensorId id) noexcept {
308 const auto slot = static_cast<std::size_t>(detail::ToUnderlying(id));
309 return (slot < kSensorSlots) ? detail::kMetadataIds[slot] : uint16_t{0};
310}
311
317[[nodiscard]] constexpr const char* SensorName(SensorId id) noexcept {
318 const auto slot = static_cast<std::size_t>(detail::ToUnderlying(id));
319 const char* n = (slot < kSensorSlots) ? detail::kSensorNames[slot] : nullptr;
320 return (n != nullptr) ? n : "Unknown";
321}
322
329[[nodiscard]] constexpr bool InDatasheet(SensorId id) noexcept {
330 const auto v = static_cast<uint32_t>(detail::ToUnderlying(id));
331 const bool manual_only = (v == 0x1AU) || (v == 0x1BU) || ((v >= 0x1FU) && (v <= 0x23U)) ||
332 (v == 0x2BU) || (v == 0x2CU) || (v == 0x2DU) || (v == 0x2EU);
333 return !manual_only;
334}
335
341[[nodiscard]] constexpr bool IsRawSensor(SensorId id) noexcept {
342 return (id == SensorId::RawAccelerometer) || (id == SensorId::RawGyroscope) ||
344}
345
346namespace detail {
347
351[[nodiscard]] inline bool DecodeFields(const SensorSpec& spec, std::span<const uint8_t> report,
352 std::size_t base, Event& ev) noexcept {
353 for (std::size_t i = 0; i < spec.count; ++i) {
354 const FieldDef& fld = spec.f[i];
355 if (((static_cast<std::size_t>(fld.off) - base) + FieldWidth(fld.kind)) > report.size()) {
356 return false;
357 }
358 ev.v[i] = ReadField(report, base, fld);
359 }
360 if (spec.hub_ts != 0xFFU) {
361 ev.hub_t_us = static_cast<uint32_t>(ev.v[spec.hub_ts]);
362 }
363 return true;
364}
365
366} // namespace detail
367
368} // namespace bno08x
369
370#endif // BNO08X_REPORTS_HPP
Compile-time helpers of the BNO08X driver: typed integer literals, exact hook matching,...
BNO08x protocol constants: SHTP channels, SH-2 report, command and record identifiers,...
The SHTP frame engine: header codec, strict receive validation (rules V1 to V12), the UART framer and...
Public types of the BNO08x driver: errors, Result<T>, configuration, events and the integer unit help...
SensorId
Every sensor report ID any CEVA document defines (RM S6.5; matrix section 1.8 of the design).
Definition bno08x_types.hpp:205
constexpr std::size_t kSensorSlots
Number of slots of the per-sensor tables: sensor IDs 0x01 to 0x2E.
Definition bno08x_types.hpp:250
@ RawMagnetometer
Raw magnetometer ADC counts with hub timestamp.
@ RawAccelerometer
Raw accelerometer ADC counts with hub timestamp.
@ RawGyroscope
Raw gyroscope ADC counts, temperature and hub timestamp.
The BNO08x driver: everything it declares lives here; internals in detail.
Definition bno08x.hpp:36
constexpr uint8_t QPoint(SensorId id) noexcept
The Q point of the first value of a sensor's event (0 when not fixed point).
Definition bno08x_reports.hpp:297
constexpr const char * SensorName(SensorId id) noexcept
The name of a sensor.
Definition bno08x_reports.hpp:317
constexpr bool InDatasheet(SensorId id) noexcept
Whether the data sheet itself describes the sensor ("D" in the coverage matrix); the others come from...
Definition bno08x_reports.hpp:329
constexpr uint16_t MetadataRecord(SensorId id) noexcept
The metadata record that describes a sensor (RM Figure 27).
Definition bno08x_reports.hpp:307
constexpr bool IsRawSensor(SensorId id) noexcept
Whether the sensor delivers raw ADC data (it needs its base sensor enabled, DS p30).
Definition bno08x_reports.hpp:341