21namespace bno08x::detail {
24inline constexpr uint8_t kRvcHeader = 0xAA;
27[[nodiscard]]
constexpr uint8_t RvcSum(std::span<const uint8_t> bytes, std::size_t first,
28 std::size_t last)
noexcept {
30 for (std::size_t i = first; i <= last; ++i) {
31 sum +=
static_cast<uint32_t
>(bytes[i]);
33 return static_cast<uint8_t
>(sum & 0xFFU);
37enum class RvcSumRule : uint8_t {
44[[nodiscard]]
constexpr RvcSumRule RvcCheck(std::span<const uint8_t> frame)
noexcept {
45 const uint8_t want = frame[18];
46 if (RvcSum(frame, 2U, 17U) == want) {
47 return RvcSumRule::WithMiMr;
49 const auto alt =
static_cast<uint8_t
>(
50 (
static_cast<uint32_t
>(RvcSum(frame, 2U, 14U)) +
static_cast<uint32_t
>(frame[17])) & 0xFFU);
51 return (alt == want) ? RvcSumRule::WithoutMiMr : RvcSumRule::None;
55[[nodiscard]]
constexpr RvcFrame RvcDecode(std::span<const uint8_t> frame,
bool mi_mr)
noexcept {
58 f.yaw =
static_cast<int16_t
>(S16(Le16(frame, 3U)));
59 f.pitch =
static_cast<int16_t
>(S16(Le16(frame, 5U)));
60 f.roll =
static_cast<int16_t
>(S16(Le16(frame, 7U)));
61 f.ax =
static_cast<int16_t
>(S16(Le16(frame, 9U)));
62 f.ay =
static_cast<int16_t
>(S16(Le16(frame, 11U)));
63 f.az =
static_cast<int16_t
>(S16(Le16(frame, 13U)));
64 f.mi_mr_valid = mi_mr;
82 RvcParser(Stats& stats,
const Config& cfg) noexcept : stats_(stats), cfg_(cfg) {}
85 void Reset() noexcept {
91 bool Feed(uint8_t b)
noexcept {
92 if ((count_ < 2U) && (b != kRvcHeader)) {
94 ++stats_.rvc_banner_bytes;
102 const RvcSumRule rule = RvcCheck(buf_);
103 if (rule == RvcSumRule::None) {
104 ++stats_.rvc_bad_csum;
113 [[nodiscard]]
const RvcFrame& Frame() const noexcept {
119 void slide() noexcept {
122 if ((buf_[p] == kRvcHeader) && (buf_[p + 1U] == kRvcHeader)) {
131 buf_[0] = kRvcHeader;
137 std::copy(buf_.begin() +
static_cast<std::ptrdiff_t
>(p), buf_.end(), buf_.begin()));
142 bool accept(RvcSumRule rule)
noexcept {
143 const uint8_t index = buf_[2];
146 (index !=
static_cast<uint8_t
>((
static_cast<uint32_t
>(last_index_) + 1U) & 0xFFU));
148 ++stats_.rvc_index_gaps;
150 const bool had = have_index_;
153 if (cfg_.rvc_strict && had && gap) {
156 if (rule == RvcSumRule::WithMiMr) {
157 ++stats_.rvc_csum_with_mi;
159 ++stats_.rvc_csum_without_mi;
162 const bool range = (frame_.yaw < -18000) || (frame_.yaw > 18000) || (frame_.pitch < -9000) ||
163 (frame_.pitch > 9000) || (frame_.roll < -18000) || (frame_.roll > 18000);
165 ++stats_.rvc_out_of_range;
173 std::array<uint8_t, reg::kRvcFrameLen> buf_{};
174 std::size_t count_{0};
176 uint8_t last_index_{0};
177 bool have_index_{
false};
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...
constexpr std::size_t kRvcFrameLen
UART-RVC frame length (DS p21).
Definition bno08x_registers.hpp:217
@ Bno086
BNO086: interactive calibration, RVC MI/MR bytes, zero-length I2C packets.
@ Reset
The hub restarted; detail is the reset cause when known.
uint8_t index
Frame index, increments per frame.
Definition bno08x_types.hpp:489