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 20, 2023
1 parent 78e867a commit e740d21
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions 32blit-pico/input_usb_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@ 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[]{
{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
{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 @@ -134,6 +141,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.joystick.x = (float(hid_joystick[0]) - 0x80) / 0x80;
api.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.buttons.state |= uint32_t(Button::DPAD_LEFT);
else if(hid_joystick[0] > 0xC0)
api.buttons.state |= uint32_t(Button::DPAD_RIGHT);

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

0 comments on commit e740d21

Please sign in to comment.