Merge pull request #4 from adafruit/gamepad_gb
Some checks failed
Build mcume / bins (push) Has been cancelled
Some checks failed
Build mcume / bins (push) Has been cancelled
add usb gamepad to gameboy emulator
This commit is contained in:
commit
40ed46a3a1
1 changed files with 43 additions and 49 deletions
|
|
@ -374,54 +374,48 @@ static void process_kbd_report (hid_keyboard_report_t const *report)
|
||||||
prev_report = *report;
|
prev_report = *report;
|
||||||
}
|
}
|
||||||
|
|
||||||
// this mapping is hard coded for a certain snes-style gamepad and picogb!
|
|
||||||
static void process_gamepad_report(const uint8_t *report) {
|
static void process_gamepad_report(const uint8_t *report) {
|
||||||
uint16_t decoded_report = 0;
|
uint16_t decoded_report = 0;
|
||||||
|
|
||||||
// X coordinate
|
// Directional Controls
|
||||||
if (report[0] == 0) {
|
// Left is when byte 2 is close to 0x00
|
||||||
decoded_report |= MASK_JOY2_RIGHT;
|
if (report[0] < 0x40) {
|
||||||
} else if(report[0] == 0xff) {
|
decoded_report |= MASK_JOY2_RIGHT; // Note: swapped due to gameboy mapping
|
||||||
decoded_report |= MASK_JOY2_LEFT;
|
|
||||||
}
|
}
|
||||||
|
// Right is when byte 2 is close to 0xFF
|
||||||
// Y coordinate
|
if (report[0] > 0xB0) {
|
||||||
if (report[1] == 0) {
|
decoded_report |= MASK_JOY2_LEFT; // Note: swapped due to gameboy mapping
|
||||||
|
}
|
||||||
|
// Up is when byte 1 is close to 0x00
|
||||||
|
if (report[1] < 0x40) {
|
||||||
decoded_report |= MASK_JOY2_UP;
|
decoded_report |= MASK_JOY2_UP;
|
||||||
} else if(report[1] == 0xff) {
|
}
|
||||||
|
// Down is when byte 1 is close to 0xFF
|
||||||
|
if (report[1] > 0xB0) {
|
||||||
decoded_report |= MASK_JOY2_DOWN;
|
decoded_report |= MASK_JOY2_DOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
// X A B Y = top bits of byte 5 . Let's just have X/Y double A/B
|
// A Button (check for 0x2F or 0x1F in byte 6)
|
||||||
if (report[5] & 0x10) {
|
if ((report[5] & 0x20) || (report[5] & 0x10)) {
|
||||||
decoded_report |= MASK_JOY2_BTN; // X
|
decoded_report |= MASK_KEY_USER3;
|
||||||
}
|
|
||||||
if (report[5] & 0x20) {
|
|
||||||
decoded_report |= MASK_KEY_USER3; // A -- this is picogb numbering
|
|
||||||
}
|
|
||||||
if (report[5] & 0x40) {
|
|
||||||
decoded_report |= MASK_JOY2_BTN; // B
|
|
||||||
}
|
|
||||||
if (report[5] & 0x80) {
|
|
||||||
decoded_report |= MASK_KEY_USER3; // Y
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SELECT START = top bits of byte 6
|
// B Button (check for 0x4F or 0x8F in byte 6)
|
||||||
|
if ((report[5] & 0x40) || (report[5] & 0x80)) {
|
||||||
|
decoded_report |= MASK_JOY2_BTN;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select Button (byte 7, bit 0x10)
|
||||||
if (report[6] & 0x10) {
|
if (report[6] & 0x10) {
|
||||||
decoded_report |= MASK_KEY_USER1; // SELECT
|
decoded_report |= MASK_KEY_USER1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start Button (byte 7, bit 0x20)
|
||||||
if (report[6] & 0x20) {
|
if (report[6] & 0x20) {
|
||||||
decoded_report |= MASK_KEY_USER2; // START
|
decoded_report |= MASK_KEY_USER2;
|
||||||
}
|
|
||||||
|
|
||||||
// Decode shoulder buttons into B/A too
|
|
||||||
if (report[6] & 0x1) {
|
|
||||||
decoded_report |= MASK_JOY2_BTN; // B
|
|
||||||
}
|
|
||||||
if (report[6] & 0x2) {
|
|
||||||
decoded_report |= MASK_KEY_USER3; // Y
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send the decoded gamepad state
|
||||||
kbd_signal_raw_gamepad(decoded_report);
|
kbd_signal_raw_gamepad(decoded_report);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -487,7 +481,7 @@ int proto = tuh_hid_interface_protocol (dev_addr, instance);
|
||||||
process_kbd_report ((hid_keyboard_report_t const*) report);
|
process_kbd_report ((hid_keyboard_report_t const*) report);
|
||||||
break;
|
break;
|
||||||
case HID_ITF_PROTOCOL_NONE:
|
case HID_ITF_PROTOCOL_NONE:
|
||||||
if (len == 8) {
|
if (len >= 8) {
|
||||||
process_gamepad_report(report);
|
process_gamepad_report(report);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue