Skip to content

Commit

Permalink
Merge pull request #832 from Daft-Freak/hid-zero2
Browse files Browse the repository at this point in the history
pico: 8BitDo Zero 2 HID mapping
  • Loading branch information
Daft-Freak authored Aug 30, 2023
2 parents 248a250 + 78e867a commit ba121b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 13 additions & 3 deletions 32blit-pico/input_usb_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ extern uint8_t hid_keys[6];
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;
};

#define NO 0xFF

static const GamepadMapping gamepad_mappings[]{
{0x15320705, 0, 1, 3, 4, 16, 15, 13}, // Razer Raiju Mobile
{0x20D6A711, 2, 1, 3, 0, 8, 12, 10}, // PowerA wired Switch pro controller
{0x00000000, 0, 1, 2, 3, 4, 5, 6} // probably wrong fallback
{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
};

#undef NO

// hat -> dpad
const uint32_t dpad_map[]{
blit::Button::DPAD_UP,
Expand Down Expand Up @@ -116,6 +122,10 @@ void update_input() {
mapping++;

api.buttons = dpad_map[hid_hat > 8 ? 8 : hid_hat]
| (hid_buttons & (1 << mapping->left) ? uint32_t(Button::DPAD_LEFT) : 0)
| (hid_buttons & (1 << mapping->right) ? uint32_t(Button::DPAD_RIGHT) : 0)
| (hid_buttons & (1 << mapping->up) ? uint32_t(Button::DPAD_UP) : 0)
| (hid_buttons & (1 << mapping->down) ? uint32_t(Button::DPAD_DOWN) : 0)
| (hid_buttons & (1 << mapping->a) ? uint32_t(Button::A) : 0)
| (hid_buttons & (1 << mapping->b) ? uint32_t(Button::B) : 0)
| (hid_buttons & (1 << mapping->x) ? uint32_t(Button::X) : 0)
Expand Down
7 changes: 5 additions & 2 deletions 32blit-pico/usb_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

static int hid_report_id = -1;
static uint16_t buttons_offset = 0, num_buttons = 0;
static uint16_t hat_offset = 0, stick_offset = 0;
static uint16_t hat_offset = 0xFFFF, stick_offset = 0;

uint32_t hid_gamepad_id = 0;
bool hid_keyboard_detected = false;
Expand Down Expand Up @@ -133,7 +133,10 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
// check report id if we have one
if(hid_report_id == -1 || report[0] == hid_report_id) {
// I hope these are reasonably aligned
hid_hat = (report_data[hat_offset / 8] >> (hat_offset % 8)) & 0xF;
if(hat_offset != 0xFFFF)
hid_hat = (report_data[hat_offset / 8] >> (hat_offset % 8)) & 0xF;
else
hid_hat = 8;

hid_joystick[0] = report_data[stick_offset / 8];
hid_joystick[1] = report_data[stick_offset / 8 + 1];
Expand Down

0 comments on commit ba121b2

Please sign in to comment.