Skip to content

Commit

Permalink
pico: add some code for handling a stick that is actually a dpad and …
Browse files Browse the repository at this point in the history
…use it for the zero 2
  • Loading branch information
Daft-Freak committed Jul 19, 2023
1 parent 1fa5b36 commit dfaef9f
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions 32blit-pico/input_usb_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,31 @@ extern uint8_t hid_hat;
extern uint32_t hid_buttons;
extern uint8_t hid_keys[6];


enum GamePadFlags {
Gamepad_StickIsDPAD = 1 << 0,
};

struct GamepadMapping {
uint32_t id; // vid:pid
uint8_t a, b, x, y;
uint8_t up, down, left, right; // if no hat
uint8_t menu, home, joystick;
uint8_t flags;
};

#define NO 0xFF
#define SD Gamepad_StickIsDPAD

static const GamepadMapping gamepad_mappings[]{
{0x057E2009, 3, 2, 1, 0, 17, 16, 19, 18, 8, 12, 11}, // Switch Pro Controller
{0x15320705, 0, 1, 3, 4, NO, NO, NO, NO, 16, 15, 13}, // Razer Raiju Mobile
{0x20D6A711, 2, 1, 3, 0, NO, NO, NO, NO, 8, 12, 10}, // PowerA wired Switch pro controller
{0x2DC89018, 0, 1, 3, 4, NO, NO, NO, NO, 10, 11, NO}, // 8BitDo Zero 2
{0x00000000, 0, 1, 2, 3, NO, NO, NO, NO, 4, 5, 6} // probably wrong fallback
{0x057E2009, 3, 2, 1, 0, 17, 16, 19, 18, 8, 12, 11, 0}, // Switch Pro Controller
{0x15320705, 0, 1, 3, 4, NO, NO, NO, NO, 16, 15, 13, 0}, // Razer Raiju Mobile
{0x20D6A711, 2, 1, 3, 0, NO, NO, NO, NO, 8, 12, 10, 0}, // PowerA wired Switch pro controller
{0x2DC89018, 0, 1, 3, 4, NO, NO, NO, NO, 10, 11, NO, SD}, // 8BitDo Zero 2
{0x00000000, 0, 1, 2, 3, NO, NO, NO, NO, 4, 5, 6, 0} // probably wrong fallback
};

#undef SD
#undef NO

// hat -> dpad
Expand Down Expand Up @@ -135,6 +143,19 @@ void update_input() {
| (hid_buttons & (1 << mapping->home) ? uint32_t(Button::HOME) : 0)
| (hid_buttons & (1 << mapping->joystick) ? uint32_t(Button::JOYSTICK) : 0);

api_data.joystick.x = (float(hid_joystick[0]) - 0x80) / 0x80;
api_data.joystick.y = (float(hid_joystick[1]) - 0x80) / 0x80;
if(mapping->flags & Gamepad_StickIsDPAD) {
// stick is actually a D-PAD
if(hid_joystick[0] < 0x40)
api_data.buttons.state |= uint32_t(Button::DPAD_LEFT);
else if(hid_joystick[0] > 0xC0)
api_data.buttons.state |= uint32_t(Button::DPAD_RIGHT);

if(hid_joystick[1] < 0x40)
api_data.buttons.state |= uint32_t(Button::DPAD_UP);
else if(hid_joystick[1] > 0xC0)
api_data.buttons.state |= uint32_t(Button::DPAD_DOWN);
} else {
api_data.joystick.x = (float(hid_joystick[0]) - 0x80) / 0x80;
api_data.joystick.y = (float(hid_joystick[1]) - 0x80) / 0x80;
}
}

0 comments on commit dfaef9f

Please sign in to comment.