diff --git a/components/hid-rp/include/hid-rp-3dconnexion-formatters.hpp b/components/hid-rp/include/hid-rp-3dconnexion-formatters.hpp index 753d5568cb..8745411d54 100644 --- a/components/hid-rp/include/hid-rp-3dconnexion-formatters.hpp +++ b/components/hid-rp/include/hid-rp-3dconnexion-formatters.hpp @@ -17,8 +17,10 @@ struct fmt::formatter &report, FormatContext &ctx) const { + // by value: fmt takes its arguments by reference, and the axes are members + // of a packed class return fmt::format_to(ctx.out(), "SpaceMouseTranslationInputReport {{x: {}, y: {}, z: {}}}", - report.axes[0], report.axes[1], report.axes[2]); + report.get_x(), report.get_y(), report.get_z()); } }; @@ -33,7 +35,7 @@ struct fmt::formatter &report, FormatContext &ctx) const { return fmt::format_to(ctx.out(), "SpaceMouseRotationInputReport {{rx: {}, ry: {}, rz: {}}}", - report.axes[0], report.axes[1], report.axes[2]); + report.get_rx(), report.get_ry(), report.get_rz()); } }; diff --git a/components/hid-rp/include/hid-rp-3dconnexion.hpp b/components/hid-rp/include/hid-rp-3dconnexion.hpp index 818bc60a2c..bd51b2a76d 100644 --- a/components/hid-rp/include/hid-rp-3dconnexion.hpp +++ b/components/hid-rp/include/hid-rp-3dconnexion.hpp @@ -51,7 +51,7 @@ namespace espp { // // NOTE: the whole class (including the base `hid::report::base`) must be // defined with 1-byte packing so that no alignment padding is inserted -// before the 2-byte-aligned `axes` array below; get_report()/set_data() rely +// before the axis members below; get_report()/set_data() rely // on the wire layout being exactly // [report id][x_lo][x_hi][y_lo][y_hi][z_lo][z_hi] with no gaps. The pragma // must precede the `class` keyword itself (not just appear inside the class @@ -91,7 +91,13 @@ class SpaceMouseTranslationInputReport "SpaceMouseTranslationInputReport serializes std::int16_t axes to the wire via " "direct object-representation copy, which assumes a little-endian host"); - std::array axes{0, 0, 0}; // X, Y, Z + // Plain members rather than a std::array: the class is 1-byte packed, so + // they sit at odd offsets. Direct member access in a packed class is + // well-defined (the compiler emits misalignment-safe loads and stores); + // a std::array's member functions, and references to its elements, are not. + std::int16_t x_{0}; ///< X + std::int16_t y_{0}; ///< Y + std::int16_t z_{0}; ///< Z public: /// Construct a new Translation Input Report object @@ -103,25 +109,23 @@ class SpaceMouseTranslationInputReport } /// Reset the translation axes to their centered (0) value - constexpr void reset() { axes.fill(0); } + constexpr void reset() { + x_ = 0; + y_ = 0; + z_ = 0; + } /// Set the X (left/right) translation axis value /// \param value The value to set the X axis to, in the range [logical_min, logical_max]. - constexpr void set_x(std::int16_t value) { - axes[0] = std::clamp(value, logical_min, logical_max); - } + constexpr void set_x(std::int16_t value) { x_ = std::clamp(value, logical_min, logical_max); } /// Set the Y (forward/backward) translation axis value /// \param value The value to set the Y axis to, in the range [logical_min, logical_max]. - constexpr void set_y(std::int16_t value) { - axes[1] = std::clamp(value, logical_min, logical_max); - } + constexpr void set_y(std::int16_t value) { y_ = std::clamp(value, logical_min, logical_max); } /// Set the Z (up/down) translation axis value /// \param value The value to set the Z axis to, in the range [logical_min, logical_max]. - constexpr void set_z(std::int16_t value) { - axes[2] = std::clamp(value, logical_min, logical_max); - } + constexpr void set_z(std::int16_t value) { z_ = std::clamp(value, logical_min, logical_max); } /// Set all three translation axes at once /// \param x The X axis value, in the range [logical_min, logical_max]. @@ -135,24 +139,24 @@ class SpaceMouseTranslationInputReport /// Get the X (left/right) translation axis value /// \return The X axis value. - constexpr std::int16_t get_x() const { return axes[0]; } + constexpr std::int16_t get_x() const { return x_; } /// Get the Y (forward/backward) translation axis value /// \return The Y axis value. - constexpr std::int16_t get_y() const { return axes[1]; } + constexpr std::int16_t get_y() const { return y_; } /// Get the Z (up/down) translation axis value /// \return The Z axis value. - constexpr std::int16_t get_z() const { return axes[2]; } + constexpr std::int16_t get_z() const { return z_; } /// Get all three translation axes at once /// \param x[out] The X axis value. /// \param y[out] The Y axis value. /// \param z[out] The Z axis value. constexpr void get_translation(std::int16_t &x, std::int16_t &y, std::int16_t &z) const { - x = axes[0]; - y = axes[1]; - z = axes[2]; + x = x_; + y = y_; + z = z_; } /// Get the input report as a vector of bytes @@ -251,7 +255,13 @@ class SpaceMouseRotationInputReport "SpaceMouseRotationInputReport serializes std::int16_t axes to the wire via " "direct object-representation copy, which assumes a little-endian host"); - std::array axes{0, 0, 0}; // Rx, Ry, Rz + // Plain members rather than a std::array: the class is 1-byte packed, so + // they sit at odd offsets. Direct member access in a packed class is + // well-defined (the compiler emits misalignment-safe loads and stores); + // a std::array's member functions, and references to its elements, are not. + std::int16_t rx_{0}; ///< Rx + std::int16_t ry_{0}; ///< Ry + std::int16_t rz_{0}; ///< Rz public: /// Construct a new Rotation Input Report object @@ -263,25 +273,23 @@ class SpaceMouseRotationInputReport } /// Reset the rotation axes to their centered (0) value - constexpr void reset() { axes.fill(0); } + constexpr void reset() { + rx_ = 0; + ry_ = 0; + rz_ = 0; + } /// Set the Rx (pitch, tilt forward/back) rotation axis value /// \param value The value to set the Rx axis to, in the range [logical_min, logical_max]. - constexpr void set_rx(std::int16_t value) { - axes[0] = std::clamp(value, logical_min, logical_max); - } + constexpr void set_rx(std::int16_t value) { rx_ = std::clamp(value, logical_min, logical_max); } /// Set the Ry (yaw, twist left/right) rotation axis value /// \param value The value to set the Ry axis to, in the range [logical_min, logical_max]. - constexpr void set_ry(std::int16_t value) { - axes[1] = std::clamp(value, logical_min, logical_max); - } + constexpr void set_ry(std::int16_t value) { ry_ = std::clamp(value, logical_min, logical_max); } /// Set the Rz (roll, tilt left/right) rotation axis value /// \param value The value to set the Rz axis to, in the range [logical_min, logical_max]. - constexpr void set_rz(std::int16_t value) { - axes[2] = std::clamp(value, logical_min, logical_max); - } + constexpr void set_rz(std::int16_t value) { rz_ = std::clamp(value, logical_min, logical_max); } /// Set all three rotation axes at once /// \param rx The Rx axis value, in the range [logical_min, logical_max]. @@ -295,24 +303,24 @@ class SpaceMouseRotationInputReport /// Get the Rx (pitch) rotation axis value /// \return The Rx axis value. - constexpr std::int16_t get_rx() const { return axes[0]; } + constexpr std::int16_t get_rx() const { return rx_; } /// Get the Ry (yaw) rotation axis value /// \return The Ry axis value. - constexpr std::int16_t get_ry() const { return axes[1]; } + constexpr std::int16_t get_ry() const { return ry_; } /// Get the Rz (roll) rotation axis value /// \return The Rz axis value. - constexpr std::int16_t get_rz() const { return axes[2]; } + constexpr std::int16_t get_rz() const { return rz_; } /// Get all three rotation axes at once /// \param rx[out] The Rx axis value. /// \param ry[out] The Ry axis value. /// \param rz[out] The Rz axis value. constexpr void get_rotation(std::int16_t &rx, std::int16_t &ry, std::int16_t &rz) const { - rx = axes[0]; - ry = axes[1]; - rz = axes[2]; + rx = rx_; + ry = ry_; + rz = rz_; } /// Get the input report as a vector of bytes diff --git a/components/hid-rp/include/hid-rp-playstation.hpp b/components/hid-rp/include/hid-rp-playstation.hpp index 613fdde362..09a0f35bee 100644 --- a/components/hid-rp/include/hid-rp-playstation.hpp +++ b/components/hid-rp/include/hid-rp-playstation.hpp @@ -85,7 +85,10 @@ union PlaystationDualsenseVendorDefinedData { struct { std::uint32_t coarse_timestamp; ///< Coarse timestamp union { - std::array imu; ///< IMU data as array + /// IMU data as an array. It sits at an odd offset in a packed report: + /// read and write it with get_imu_data() / set_imu_data() or through + /// gyro / accel, not with std::array's member functions or references. + std::array imu; struct { Gyroscope gyro; ///< 3 axes of gyroscope data Accelerometer accel; ///< 3 axes of accelerometer data @@ -815,8 +818,25 @@ class PlaystationDualsenseBLEComplexInputReport } constexpr uint32_t get_coarse_timestamp() const { return vendor_defined.coarse_timestamp; } - constexpr void set_imu_data(const std::array &data) { vendor_defined.imu = data; } - constexpr const std::array &get_imu_data() const { return vendor_defined.imu; } + /// Set the IMU data: gyroscope X / Y / Z, then accelerometer X / Y / Z. + /// \param data The six IMU values. + /// \note Written through the (1-byte packed) gyro / accel members: the + /// std::array view of the same bytes sits at an odd offset. + constexpr void set_imu_data(const std::array &data) { + vendor_defined.gyro.X = data[0]; + vendor_defined.gyro.Y = data[1]; + vendor_defined.gyro.Z = data[2]; + vendor_defined.accel.X = data[3]; + vendor_defined.accel.Y = data[4]; + vendor_defined.accel.Z = data[5]; + } + /// Get the IMU data: gyroscope X / Y / Z, then accelerometer X / Y / Z. + /// \return A copy of the six IMU values (a reference into the packed report + /// would be misaligned). + constexpr std::array get_imu_data() const { + return {vendor_defined.gyro.X, vendor_defined.gyro.Y, vendor_defined.gyro.Z, + vendor_defined.accel.X, vendor_defined.accel.Y, vendor_defined.accel.Z}; + } /// Set the battery level as a percentage (0-100) and if it is charging /// @param level The battery level as a percentage (0-100) diff --git a/components/hid-rp/test/hid_rp_reports_host_test.cpp b/components/hid-rp/test/hid_rp_reports_host_test.cpp index 39cc20fa06..de038d42e9 100644 --- a/components/hid-rp/test/hid_rp_reports_host_test.cpp +++ b/components/hid-rp/test/hid_rp_reports_host_test.cpp @@ -11,6 +11,7 @@ // // No ESP-IDF headers required. +#include #include #include #include @@ -20,6 +21,7 @@ #include +#include "hid-rp-3dconnexion-formatters.hpp" #include "hid-rp-3dconnexion.hpp" #include "hid-rp-gamepad.hpp" #include "hid-rp-playstation.hpp" @@ -79,6 +81,15 @@ TEST_CASE("spacemouse") { CHECK((rot.get_report()[0] == 0x0B && rot.get_report()[1] == 0x01)); CHECK((rot.get_report()[2] == 0xD4 && rot.get_report()[3] == 0xFE)); // -300 CHECK((rot.get_report()[4] == 0x05 && rot.get_report()[5] == 0x01)); + // the getters and the formatters read the (packed, odd-offset) axes back + CHECK(t.get_x() == 0x0102); + CHECK(t.get_y() == -2); + CHECK(t.get_z() == 0x0103); + CHECK(rot.get_ry() == -300); + CHECK(fmt::format("{}", t) == "SpaceMouseTranslationInputReport {x: 258, y: -2, z: 259}"); + CHECK(fmt::format("{}", rot) == "SpaceMouseRotationInputReport {rx: 267, ry: -300, rz: 261}"); + t.reset(); + CHECK(t.get_report() == (std::vector{0, 0, 0, 0, 0, 0})); using RotationNoId = espp::SpaceMouseRotationInputReport<-350, 350, 0>; RotationNoId rot0; rot0.set_rotation(0x010B, static_cast(-300), 0x0105); @@ -105,6 +116,25 @@ TEST_CASE("spacemouse") { CHECK(led0.get_led()); } +// The IMU sits at an odd offset in the packed report: the accessors copy it +// in and out through the packed gyro / accel members. +TEST_CASE("dualsense imu") { + espp::PlaystationDualsenseBLEComplexInputReport<> r; + const std::array imu{0x0102, -2, 0x0304, 0x0506, -300, 0x0708}; + r.set_imu_data(imu); + CHECK(r.get_imu_data() == imu); + // on the wire: the six values back to back, little-endian, after the + // timestamp, sticks, triggers, counter, buttons, the unknown byte and the + // vendor block's 4-byte coarse timestamp + constexpr std::size_t imu_offset = + 1 + 4 + 2 + 1 + sizeof(espp::PlaystationDualsenseGamepadButtons) + 1 + 4; + const std::vector le{0x02, 0x01, 0xFE, 0xFF, 0x04, 0x03, + 0x06, 0x05, 0xD4, 0xFE, 0x08, 0x07}; + const auto bytes = r.get_report(); + REQUIRE(bytes.size() >= imu_offset + le.size()); + CHECK(std::equal(le.begin(), le.end(), bytes.begin() + imu_offset)); +} + TEST_CASE("gamepad leds") { espp::GamepadLedOutputReport<> leds; leds.set_led(1, true);