Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hid): Add switch pro controller reports #378

Merged
merged 3 commits into from
Feb 21, 2025
Merged
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
30 changes: 27 additions & 3 deletions components/hid-rp/example/main/hid_rp_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "logger.hpp"

#include "hid-rp-gamepad.hpp"
#include "hid-rp-switch-pro.hpp"
#include "hid-rp-xbox.hpp"
#include "hid-rp.hpp"

using namespace std::chrono_literals;
Expand All @@ -26,6 +28,9 @@ extern "C" void app_main(void) {
joystick_max, trigger_min, trigger_max, input_report_id>;
GamepadInput gamepad_input_report;

using XboxInput = espp::XboxGamepadInputReport<input_report_id>;
XboxInput xbox_input_report;

using BatteryReport = espp::XboxBatteryInputReport<battery_report_id>;
BatteryReport battery_input_report;

Expand All @@ -51,14 +56,33 @@ extern "C" void app_main(void) {

logger.info("Report Descriptor:");
logger.info(" Size: {}", descriptor.size());
logger.info(" Data: {::#02x}", descriptor);
logger.info(" Data: {::#04X}", descriptor);

using SwitchProInput = espp::SwitchProGamepadInputReport<>;
SwitchProInput switch_pro_input_report;
switch_pro_input_report.reset();
logger.info("{}", switch_pro_input_report);
logger.info("Switch Pro Input Report Size: {}", switch_pro_input_report.get_report().size());
logger.info("Switch Pro Input Report Data: {::#04X}", switch_pro_input_report.get_report());

auto sp_raw_descriptor = espp::switch_pro_descriptor();
auto sp_descriptor = std::vector<uint8_t>(sp_raw_descriptor.begin(), sp_raw_descriptor.end());

logger.info("Switch Report Descriptor:");
logger.info(" Size: {}", sp_descriptor.size());
std::string str = "";
for (auto &byte : sp_descriptor) {
str += fmt::format("0x{:02X}, ", byte);
}
logger.info(" Data: [{}]", str);

GamepadInput::Hat hat = GamepadInput::Hat::UP_RIGHT;
int button_index = 5;
float angle = 2.0f * M_PI * button_index / num_buttons;

// update the gamepad input report
gamepad_input_report.reset();
logger.info("{}", gamepad_input_report);
gamepad_input_report.set_hat(hat);
gamepad_input_report.set_button(button_index, true);
// joystick inputs are in the range [-1, 1] float
Expand All @@ -74,7 +98,7 @@ extern "C" void app_main(void) {
auto report = gamepad_input_report.get_report();
logger.info("Input report:");
logger.info(" Size: {}", report.size());
logger.info(" Data: {::#02x}", report);
logger.info(" Data: {::#02X}", report);

// update the battery input report
battery_input_report.reset();
Expand All @@ -88,7 +112,7 @@ extern "C" void app_main(void) {
report = battery_input_report.get_report();
logger.info("Battery report:");
logger.info(" Size: {}", report.size());
logger.info(" Data: {::#02x}", report);
logger.info(" Data: {::#02X}", report);

//! [hid rp example]
}
Loading