diff --git a/components/hid-rp/CMakeLists.txt b/components/hid-rp/CMakeLists.txt index fe5ef9077..e7064de5b 100644 --- a/components/hid-rp/CMakeLists.txt +++ b/components/hid-rp/CMakeLists.txt @@ -1,3 +1,4 @@ idf_component_register( INCLUDE_DIRS "include" "../../external/hid-rp/hid-rp" + REQUIRES "format" ) diff --git a/components/hid-rp/include/hid-rp-gamepad-formatters.hpp b/components/hid-rp/include/hid-rp-gamepad-formatters.hpp new file mode 100644 index 000000000..9b81f42fc --- /dev/null +++ b/components/hid-rp/include/hid-rp-gamepad-formatters.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "format.hpp" + +template +struct fmt::formatter> { + template constexpr auto parse(ParseContext &ctx) const { + return ctx.begin(); + } + + template + auto format(const espp::GamepadInputReport &report, FormatContext &ctx) const { + auto out = ctx.out(); + fmt::format_to(out, "GamepadInputReport<{}> {{", BUTTON_COUNT); + fmt::format_to(out, "joystick_axes: ["); + for (size_t i = 0; i < 4; i++) { + fmt::format_to(out, "{}", report.joystick_axes[i]); + if (i < 3) { + fmt::format_to(out, ", "); + } + } + fmt::format_to(out, "], trigger_axes: ["); + for (size_t i = 0; i < 2; i++) { + fmt::format_to(out, "{}", report.trigger_axes[i]); + if (i < 1) { + fmt::format_to(out, ", "); + } + } + fmt::format_to(out, "], hat_switch: {}, buttons: [", report.hat_switch); + std::bitset buttons; + for (size_t i = 1; i <= BUTTON_COUNT; i++) { + buttons.set(i - 1, report.buttons.test(hid::page::button(i))); + } + fmt::format_to(out, "{}", buttons); + fmt::format_to(out, "], consumer_record: {}", (bool)report.consumer_record); + return fmt::format_to(out, "}}"); + } +}; diff --git a/components/hid-rp/include/hid-rp-gamepad.hpp b/components/hid-rp/include/hid-rp-gamepad.hpp index 4d732fd62..b9423a93a 100644 --- a/components/hid-rp/include/hid-rp-gamepad.hpp +++ b/components/hid-rp/include/hid-rp-gamepad.hpp @@ -174,6 +174,15 @@ class GamepadInputReport : public hid::report::base(report_data, report_data + report_size); } + /// Set the output report data from a vector of bytes + /// \param data The data to set the output report to. + constexpr void set_data(const std::vector &data) { + // the first two bytes are the id and the selector, which we don't want + size_t offset = 2; + // copy the data into our data array + std::copy(data.begin(), data.end(), this->data() + offset); + } + /// Get the report descriptor as a hid::rdf::descriptor /// \return The report descriptor as a hid::rdf::descriptor. /// \note This is an incomplete descriptor, you will need to add it to a @@ -277,6 +286,10 @@ class GamepadInputReport : public hid::report::base>; + }; /// HID Gamepad Output Report @@ -696,3 +709,5 @@ class GamepadLedOutputReport : public hid::report::base