Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions components/hid-rp/include/hid-rp-3dconnexion-formatters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ struct fmt::formatter<espp::SpaceMouseTranslationInputReport<LOGICAL_MIN, LOGICA
auto
format(const espp::SpaceMouseTranslationInputReport<LOGICAL_MIN, LOGICAL_MAX, REPORT_ID> &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());
}
};

Expand All @@ -33,7 +35,7 @@ struct fmt::formatter<espp::SpaceMouseRotationInputReport<LOGICAL_MIN, LOGICAL_M
format(const espp::SpaceMouseRotationInputReport<LOGICAL_MIN, LOGICAL_MAX, REPORT_ID> &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());
}
};

Expand Down
78 changes: 43 additions & 35 deletions components/hid-rp/include/hid-rp-3dconnexion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<std::int16_t, 3> 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
Expand All @@ -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].
Expand All @@ -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
Expand Down Expand Up @@ -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<std::int16_t, 3> 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
Expand All @@ -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].
Expand All @@ -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
Expand Down
26 changes: 23 additions & 3 deletions components/hid-rp/include/hid-rp-playstation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ union PlaystationDualsenseVendorDefinedData {
struct {
std::uint32_t coarse_timestamp; ///< Coarse timestamp
union {
std::array<std::int16_t, 6> 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<std::int16_t, 6> imu;
struct {
Gyroscope gyro; ///< 3 axes of gyroscope data
Accelerometer accel; ///< 3 axes of accelerometer data
Expand Down Expand Up @@ -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<int16_t, 6> &data) { vendor_defined.imu = data; }
constexpr const std::array<int16_t, 6> &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<int16_t, 6> &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<int16_t, 6> 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};
}
Comment thread
finger563 marked this conversation as resolved.
Resolved

/// Set the battery level as a percentage (0-100) and if it is charging
/// @param level The battery level as a percentage (0-100)
Expand Down
30 changes: 30 additions & 0 deletions components/hid-rp/test/hid_rp_reports_host_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//
// No ESP-IDF headers required.

#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
Expand All @@ -20,6 +21,7 @@

#include <doctest/doctest.h>

#include "hid-rp-3dconnexion-formatters.hpp"
#include "hid-rp-3dconnexion.hpp"
#include "hid-rp-gamepad.hpp"
#include "hid-rp-playstation.hpp"
Expand Down Expand Up @@ -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<uint8_t>{0, 0, 0, 0, 0, 0}));
using RotationNoId = espp::SpaceMouseRotationInputReport<-350, 350, 0>;
RotationNoId rot0;
rot0.set_rotation(0x010B, static_cast<std::int16_t>(-300), 0x0105);
Expand All @@ -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<int16_t, 6> 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<uint8_t> 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);
Expand Down
Loading