Add Second Keyboard mode

There a now two keyboard modes to easily allow two players on the
same computer. Player 1 is mapped to 'DFJK', player 2 to 'CBN,'.
This commit is contained in:
Frederik Walk 2023-12-29 22:49:25 +01:00
parent c41af9c82d
commit 0f9d433253
8 changed files with 48 additions and 18 deletions

View File

@ -20,7 +20,8 @@ typedef enum {
USB_MODE_DUALSHOCK3,
USB_MODE_PS4_TATACON,
USB_MODE_DUALSHOCK4,
USB_MODE_KEYBOARD,
USB_MODE_KEYBOARD_P1,
USB_MODE_KEYBOARD_P2,
USB_MODE_XBOX360,
USB_MODE_MIDI,
USB_MODE_DEBUG,

View File

@ -43,6 +43,12 @@ struct InputState {
Drum drum;
Controller controller;
private:
enum class Player {
One,
Two,
};
private:
hid_switch_report_t m_switch_report;
hid_ps3_report_t m_ps3_report;
@ -55,7 +61,7 @@ struct InputState {
usb_report_t getSwitchReport();
usb_report_t getPS3InputReport();
usb_report_t getPS4InputReport();
usb_report_t getKeyboardReport();
usb_report_t getKeyboardReport(Player player);
usb_report_t getXinputReport();
usb_report_t getMidiReport();
usb_report_t getDebugReport();

View File

@ -63,7 +63,8 @@ class Menu {
ChangeUsbModeDS3,
ChangeUsbModePS4Tatacon,
ChangeUsbModeDS4,
ChangeUsbModeKeyboard,
ChangeUsbModeKeyboardP1,
ChangeUsbModeKeyboardP2,
ChangeUsbModeXbox360,
ChangeUsbModeMidi,
ChangeUsbModeDebug,

View File

@ -39,8 +39,10 @@ static std::string modeToString(usb_mode_t mode) {
return "PS4 Tatacon";
case USB_MODE_DUALSHOCK4:
return "Dualshock 4";
case USB_MODE_KEYBOARD:
return "Keyboard";
case USB_MODE_KEYBOARD_P1:
return "Keyboard P1";
case USB_MODE_KEYBOARD_P2:
return "Keyboard P2";
case USB_MODE_XBOX360:
return "Xbox 360";
case USB_MODE_MIDI:

View File

@ -16,7 +16,8 @@ uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t
case USB_MODE_PS4_TATACON:
case USB_MODE_DUALSHOCK4:
return hid_ps4_get_report_cb(itf, report_id, report_type, buffer, reqlen);
case USB_MODE_KEYBOARD:
case USB_MODE_KEYBOARD_P1:
case USB_MODE_KEYBOARD_P2:
return hid_keyboard_get_report_cb(itf, report_id, report_type, buffer, reqlen);
default:
}
@ -38,7 +39,8 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t rep
case USB_MODE_DUALSHOCK4:
hid_ps4_set_report_cb(itf, report_id, report_type, buffer, bufsize);
break;
case USB_MODE_KEYBOARD:
case USB_MODE_KEYBOARD_P1:
case USB_MODE_KEYBOARD_P2:
hid_keyboard_set_report_cb(itf, report_id, report_type, buffer, bufsize);
break;
default:

View File

@ -77,7 +77,8 @@ void usb_driver_init(usb_mode_t mode) {
usbd_send_report = send_hid_ps4_report;
usbd_receive_report = NULL;
break;
case USB_MODE_KEYBOARD:
case USB_MODE_KEYBOARD_P1:
case USB_MODE_KEYBOARD_P2:
usbd_desc_device = &keyboard_desc_device;
usbd_desc_cfg = keyboard_desc_cfg;
usbd_desc_hid_report = keyboard_desc_hid_report;

View File

@ -23,8 +23,10 @@ usb_report_t InputState::getReport(usb_mode_t mode) {
case USB_MODE_PS4_TATACON:
case USB_MODE_DUALSHOCK4:
return getPS4InputReport();
case USB_MODE_KEYBOARD:
return getKeyboardReport();
case USB_MODE_KEYBOARD_P1:
return getKeyboardReport(Player::One);
case USB_MODE_KEYBOARD_P2:
return getKeyboardReport(Player::Two);
case USB_MODE_XBOX360:
return getXinputReport();
case USB_MODE_MIDI:
@ -187,7 +189,7 @@ usb_report_t InputState::getPS4InputReport() {
return {(uint8_t *)&m_ps4_report, sizeof(hid_ps4_report_t)};
}
usb_report_t InputState::getKeyboardReport() {
usb_report_t InputState::getKeyboardReport(InputState::Player player) {
m_keyboard_report = {.keycodes = {0}};
auto set_key = [&](const bool input, const uint8_t keycode) {
@ -196,10 +198,20 @@ usb_report_t InputState::getKeyboardReport() {
}
};
set_key(drum.ka_left.triggered, HID_KEY_D);
set_key(drum.don_left.triggered, HID_KEY_F);
set_key(drum.don_right.triggered, HID_KEY_J);
set_key(drum.ka_right.triggered, HID_KEY_K);
switch (player) {
case Player::One: {
set_key(drum.ka_left.triggered, HID_KEY_D);
set_key(drum.don_left.triggered, HID_KEY_F);
set_key(drum.don_right.triggered, HID_KEY_J);
set_key(drum.ka_right.triggered, HID_KEY_K);
} break;
case Player::Two: {
set_key(drum.ka_left.triggered, HID_KEY_C);
set_key(drum.don_left.triggered, HID_KEY_B);
set_key(drum.don_right.triggered, HID_KEY_N);
set_key(drum.ka_right.triggered, HID_KEY_COMMA);
} break;
}
set_key(controller.dpad.up, HID_KEY_ARROW_UP);
set_key(controller.dpad.down, HID_KEY_ARROW_DOWN);

View File

@ -22,7 +22,8 @@ const std::map<Menu::Page, const Menu::Descriptor> Menu::descriptors = {
{"Dualshock3", Menu::Descriptor::Action::ChangeUsbModeDS3}, //
{"PS4 Tata", Menu::Descriptor::Action::ChangeUsbModePS4Tatacon}, //
{"Dualshock4", Menu::Descriptor::Action::ChangeUsbModeDS4}, //
{"Keyboard", Menu::Descriptor::Action::ChangeUsbModeKeyboard}, //
{"Keybrd P1", Menu::Descriptor::Action::ChangeUsbModeKeyboardP1}, //
{"Keybrd P2", Menu::Descriptor::Action::ChangeUsbModeKeyboardP2}, //
{"Xbox 360", Menu::Descriptor::Action::ChangeUsbModeXbox360}, //
{"MIDI", Menu::Descriptor::Action::ChangeUsbModeMidi}, //
{"Debug", Menu::Descriptor::Action::ChangeUsbModeDebug}}, //
@ -274,8 +275,12 @@ void Menu::performSelectionAction(Menu::Descriptor::Action action) {
m_store->setUsbMode(USB_MODE_DUALSHOCK4);
gotoParent();
break;
case Descriptor::Action::ChangeUsbModeKeyboard:
m_store->setUsbMode(USB_MODE_KEYBOARD);
case Descriptor::Action::ChangeUsbModeKeyboardP1:
m_store->setUsbMode(USB_MODE_KEYBOARD_P1);
gotoParent();
break;
case Descriptor::Action::ChangeUsbModeKeyboardP2:
m_store->setUsbMode(USB_MODE_KEYBOARD_P2);
gotoParent();
break;
case Descriptor::Action::ChangeUsbModeXbox360: