-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hid-rp): Add gamepad
set_data
and formatters (#373)
* Add formatter for easy printing of gamepad data * Add `set_data` function for setting the raw bytes of the report (e.g. from a received binary input report) for parsing it Helps when you want to parse an input gamepad report of this type and print the values
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
idf_component_register( | ||
INCLUDE_DIRS "include" "../../external/hid-rp/hid-rp" | ||
REQUIRES "format" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include "format.hpp" | ||
|
||
template <size_t BUTTON_COUNT, typename JOYSTICK_TYPE, typename TRIGGER_TYPE, JOYSTICK_TYPE JOYSTICK_MIN, | ||
JOYSTICK_TYPE JOYSTICK_MAX, TRIGGER_TYPE TRIGGER_MIN, TRIGGER_TYPE TRIGGER_MAX, uint8_t REPORT_ID> | ||
struct fmt::formatter<espp::GamepadInputReport<BUTTON_COUNT, JOYSTICK_TYPE, TRIGGER_TYPE, JOYSTICK_MIN, JOYSTICK_MAX, | ||
TRIGGER_MIN, TRIGGER_MAX, REPORT_ID>> { | ||
template <typename ParseContext> constexpr auto parse(ParseContext &ctx) const { | ||
return ctx.begin(); | ||
} | ||
|
||
template <typename FormatContext> | ||
auto format(const espp::GamepadInputReport<BUTTON_COUNT, JOYSTICK_TYPE, TRIGGER_TYPE, JOYSTICK_MIN, | ||
JOYSTICK_MAX, TRIGGER_MIN, TRIGGER_MAX, REPORT_ID> &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<BUTTON_COUNT> 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, "}}"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters