Repository navigation
Add gamepad support #1844
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
Merged
Merged
Add gamepad support #1844
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
102 changes: 102 additions & 0 deletions
102
SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.cpp
This file contains hidden or 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,102 @@ | ||
| /* SDL Gamepad Input | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #ifdef PA_SDL | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <utility> | ||
| #include "GamepadInput_SDL.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
|
|
||
| // Rescale outside a radial deadzone so centered sticks are exactly neutral, | ||
| // while retaining the entire analog range. SDL's positive Y points down. | ||
| std::tuple<double, double> gamepad_stick(int x, int y){ | ||
| constexpr double joystick_max_negative = -SDL_JOYSTICK_AXIS_MIN; | ||
| constexpr double joystick_max_positive = SDL_JOYSTICK_AXIS_MAX; | ||
| constexpr double deadzone = 0.15; | ||
|
|
||
| // First normalize to [-1, 1] | ||
| double normalized_x = x / (x < 0 ? joystick_max_negative : joystick_max_positive); | ||
| double normalized_y = y / (y < 0 ? joystick_max_negative : joystick_max_positive); | ||
|
|
||
| // Then apply a radial deadzone | ||
| double magnitude = std::hypot(normalized_x, normalized_y); | ||
| if (magnitude <= deadzone){ | ||
| return {0, 0}; | ||
| } | ||
|
|
||
| // Rescale to the full range outside the deadzone | ||
| double output_magnitude = (std::min(magnitude, 1.0) - deadzone) / (1.0 - deadzone); | ||
| double scale = output_magnitude / magnitude; | ||
| return {normalized_x * scale, -normalized_y * scale}; | ||
| } | ||
|
|
||
| bool gamepad_trigger(int value){ | ||
| constexpr double trigger_max = SDL_JOYSTICK_AXIS_MAX; | ||
| constexpr double deadzone = 0.20; | ||
|
|
||
| return value > (trigger_max * deadzone); | ||
| } | ||
|
|
||
| GamepadInputState read_gamepad(SDL_Gamepad* pad){ | ||
| using namespace NintendoSwitch; | ||
|
|
||
| GamepadInputState state; | ||
| ProControllerState& output = state.controller_state; | ||
| // Match physical Nintendo positions, regardless of the input device's labels. | ||
| static constexpr std::pair<SDL_GamepadButton, Button> mapping[] = { | ||
| {SDL_GAMEPAD_BUTTON_SOUTH, BUTTON_B}, | ||
| {SDL_GAMEPAD_BUTTON_EAST, BUTTON_A}, | ||
| {SDL_GAMEPAD_BUTTON_WEST, BUTTON_Y}, | ||
| {SDL_GAMEPAD_BUTTON_NORTH, BUTTON_X}, | ||
| {SDL_GAMEPAD_BUTTON_BACK, BUTTON_MINUS}, | ||
| {SDL_GAMEPAD_BUTTON_GUIDE, BUTTON_HOME}, | ||
| {SDL_GAMEPAD_BUTTON_START, BUTTON_PLUS}, | ||
| {SDL_GAMEPAD_BUTTON_LEFT_STICK, BUTTON_LCLICK}, | ||
| {SDL_GAMEPAD_BUTTON_RIGHT_STICK, BUTTON_RCLICK}, | ||
| {SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, BUTTON_L}, | ||
| {SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, BUTTON_R}, | ||
| {SDL_GAMEPAD_BUTTON_MISC1, BUTTON_CAPTURE}, | ||
| }; | ||
| for (const auto& [sdl_button, button] : mapping){ | ||
| if (SDL_GetGamepadButton(pad, sdl_button)){ | ||
| output.buttons |= button; | ||
| } | ||
| } | ||
| if (gamepad_trigger(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER))){ | ||
| output.buttons |= BUTTON_ZL; | ||
| } | ||
| if (gamepad_trigger(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER))){ | ||
| output.buttons |= BUTTON_ZR; | ||
| } | ||
|
|
||
| int dpad_x = (int)SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT) | ||
| - (int)SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_LEFT); | ||
| int dpad_y = (int)SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_DOWN) | ||
| - (int)SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_UP); | ||
| static constexpr DpadPosition dpad_positions[3][3] = { | ||
| {DPAD_UP_LEFT, DPAD_UP, DPAD_UP_RIGHT}, | ||
| {DPAD_LEFT, DPAD_NONE, DPAD_RIGHT}, | ||
| {DPAD_DOWN_LEFT, DPAD_DOWN, DPAD_DOWN_RIGHT}, | ||
| }; | ||
| output.dpad = dpad_positions[dpad_y + 1][dpad_x + 1]; | ||
|
|
||
| std::tie(output.left_joystick.x, output.left_joystick.y) = gamepad_stick( | ||
| SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX), | ||
| SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY) | ||
| ); | ||
| std::tie(output.right_joystick.x, output.right_joystick.y) = gamepad_stick( | ||
| SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTX), | ||
| SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTY) | ||
| ); | ||
| return state; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| #endif | ||
24 changes: 24 additions & 0 deletions
24
SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.h
This file contains hidden or 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,24 @@ | ||
| /* SDL Gamepad Input | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #ifndef PokemonAutomation_ControllerInput_Gamepad_GamepadInput_SDL_H | ||
| #define PokemonAutomation_ControllerInput_Gamepad_GamepadInput_SDL_H | ||
|
|
||
| #ifdef PA_SDL | ||
|
|
||
| #include <tuple> | ||
| #include <SDL3/SDL.h> | ||
| #include "GamepadInput_State.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
|
|
||
| std::tuple<double, double> gamepad_stick(int x, int y); | ||
| bool gamepad_trigger(int value); | ||
| GamepadInputState read_gamepad(SDL_Gamepad* pad); | ||
|
|
||
| } | ||
| #endif | ||
| #endif |
50 changes: 50 additions & 0 deletions
50
SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.cpp
This file contains hidden or 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,50 @@ | ||
| /* Standard Gamepad Input State | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #include "GamepadInput_State.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
|
|
||
| GamepadInputState::GamepadInputState() | ||
| : ControllerInputState(ControllerInputType::StandardGamepad) | ||
| {} | ||
|
|
||
| GamepadInputState& GamepadInputState::operator=(const GamepadInputState& other){ | ||
| controller_state = other.controller_state; | ||
| return *this; | ||
| } | ||
|
|
||
| void GamepadInputState::clear(){ | ||
| controller_state.clear(); | ||
| } | ||
|
|
||
| bool GamepadInputState::is_neutral() const{ | ||
| return controller_state.is_neutral(); | ||
| } | ||
|
|
||
| bool GamepadInputState::operator==(const ControllerInputState& state) const{ | ||
| const auto* other = dynamic_cast<const GamepadInputState*>(&state); | ||
| if (other == nullptr){ | ||
| return false; | ||
| } | ||
| return controller_state == other->controller_state; | ||
| } | ||
|
|
||
| void GamepadInputGate::reset(){ | ||
| m_armed = false; | ||
| } | ||
|
|
||
| void GamepadInputGate::filter(GamepadInputState& state, bool has_listeners){ | ||
| if (!has_listeners){ | ||
| m_armed = false; | ||
| } | ||
| if (!m_armed){ | ||
| m_armed = has_listeners && state.is_neutral(); | ||
| state.clear(); | ||
| } | ||
| } | ||
|
|
||
| } |
40 changes: 40 additions & 0 deletions
40
SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.h
This file contains hidden or 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,40 @@ | ||
| /* Standard Gamepad Input State | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #ifndef PokemonAutomation_ControllerInput_Gamepad_GamepadInput_State_H | ||
| #define PokemonAutomation_ControllerInput_Gamepad_GamepadInput_State_H | ||
|
|
||
| #include "ControllerInput/ControllerInput.h" | ||
| #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProControllerState.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
|
|
||
| class GamepadInputState final : public ControllerInputState{ | ||
| public: | ||
| GamepadInputState(); | ||
|
|
||
| GamepadInputState(const GamepadInputState&) = default; | ||
|
|
||
| GamepadInputState& operator=(const GamepadInputState& other); | ||
|
|
||
| virtual void clear() override; | ||
| virtual bool is_neutral() const override; | ||
| virtual bool operator==(const ControllerInputState& state) const override; | ||
|
|
||
| NintendoSwitch::ProControllerState controller_state; | ||
| }; | ||
|
|
||
| class GamepadInputGate{ | ||
| public: | ||
| void reset(); | ||
| void filter(GamepadInputState& state, bool has_listeners); | ||
|
|
||
| private: | ||
| bool m_armed = false; | ||
| }; | ||
|
|
||
| } | ||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AI review: Minor:
gamepad_stick()(andgamepad_trigger()below) are file-local helpers but have external linkage — they're free functions innamespace PokemonAutomationthat the header doesn't declare. Make themstaticor put them in an anonymous namespace (the codebase's usual convention) so they can't collide with symbols from other translation units.