From 26c66f455e5b51653dbbfe530ca3dc139e9ca794 Mon Sep 17 00:00:00 2001 From: David Madison Date: Thu, 25 Mar 2021 16:18:36 -0400 Subject: [PATCH] Remove shared identifiers in ButtonEnum Move shared button identifiers from enum to an inline function, so formerly shared identifiers can be separately filtered and processed on a class-by-class basis. Fixes #611. --- PS3BT.cpp | 9 ++- PS3Enums.h | 6 ++ PS3USB.cpp | 9 ++- PS4Parser.cpp | 14 +++- PS4Parser.h | 1 + PS5Parser.cpp | 14 +++- PS5Parser.h | 1 + PSBuzz.cpp | 12 ++- PSBuzz.h | 1 + Wii.cpp | 36 +++++++-- Wii.h | 2 + XBOXOLD.cpp | 117 +++++++++++++++++++++--------- XBOXOLD.h | 3 + XBOXONE.cpp | 12 +-- XBOXONESParser.cpp | 12 ++- XBOXONESParser.h | 2 + XBOXRECV.cpp | 6 +- XBOXUSB.cpp | 6 +- controllerEnums.h | 177 +++++++++++++++++++++++++++++++++------------ xboxEnums.h | 6 ++ 20 files changed, 331 insertions(+), 115 deletions(-) diff --git a/PS3BT.cpp b/PS3BT.cpp index 1e4e7f41..3326f1f6 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -47,18 +47,21 @@ BluetoothService(p) // Pointer to USB class instance - mandatory } bool PS3BT::getButtonPress(ButtonEnum b) { - return (ButtonState & pgm_read_dword(&PS3_BUTTONS[(uint8_t)b])); + const int8_t index = getPS3ButtonIndex(b); if (index < 0) return 0; + return (ButtonState & pgm_read_dword(&PS3_BUTTONS[index])); } bool PS3BT::getButtonClick(ButtonEnum b) { - uint32_t button = pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]); + const int8_t index = getPS3ButtonIndex(b); if (index < 0) return 0; + uint32_t button = pgm_read_dword(&PS3_BUTTONS[index]); bool click = (ButtonClickState & button); ButtonClickState &= ~button; // Clear "click" event return click; } uint8_t PS3BT::getAnalogButton(ButtonEnum a) { - return (uint8_t)(l2capinbuf[pgm_read_byte(&PS3_ANALOG_BUTTONS[(uint8_t)a])]); + const int8_t index = getPS3ButtonIndex(a); if (index < 0) return 0; + return (uint8_t)(l2capinbuf[pgm_read_byte(&PS3_ANALOG_BUTTONS[index])]); } uint8_t PS3BT::getAnalogHat(AnalogHatEnum a) { diff --git a/PS3Enums.h b/PS3Enums.h index 77801945..77c7c536 100644 --- a/PS3Enums.h +++ b/PS3Enums.h @@ -138,4 +138,10 @@ enum StatusEnum { Bluetooth = (40 << 8) | 0x16, // Operating by Bluetooth and rumble is turned off }; +inline int8_t getPS3ButtonIndex(ButtonEnum b) { + const uint8_t index = legacyButtonValues(b); + if (index >= sizeof(PS3_BUTTONS) / sizeof(PS3_BUTTONS[0])) return -1; + return index; +} + #endif diff --git a/PS3USB.cpp b/PS3USB.cpp index 081a7a20..4aba0e54 100644 --- a/PS3USB.cpp +++ b/PS3USB.cpp @@ -314,18 +314,21 @@ void PS3USB::printReport() { // Uncomment "#define PRINTREPORT" to print the rep } bool PS3USB::getButtonPress(ButtonEnum b) { - return (ButtonState & pgm_read_dword(&PS3_BUTTONS[(uint8_t)b])); + const int8_t index = getPS3ButtonIndex(b); if (index < 0) return 0; + return (ButtonState & pgm_read_dword(&PS3_BUTTONS[index])); } bool PS3USB::getButtonClick(ButtonEnum b) { - uint32_t button = pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]); + const int8_t index = getPS3ButtonIndex(b); if (index < 0) return 0; + uint32_t button = pgm_read_dword(&PS3_BUTTONS[index]); bool click = (ButtonClickState & button); ButtonClickState &= ~button; // Clear "click" event return click; } uint8_t PS3USB::getAnalogButton(ButtonEnum a) { - return (uint8_t)(readBuf[(pgm_read_byte(&PS3_ANALOG_BUTTONS[(uint8_t)a])) - 9]); + const int8_t index = getPS3ButtonIndex(a); if (index < 0) return 0; + return (uint8_t)(readBuf[(pgm_read_byte(&PS3_ANALOG_BUTTONS[index])) - 9]); } uint8_t PS3USB::getAnalogHat(AnalogHatEnum a) { diff --git a/PS4Parser.cpp b/PS4Parser.cpp index c484b83e..ed619a51 100644 --- a/PS4Parser.cpp +++ b/PS4Parser.cpp @@ -32,6 +32,12 @@ enum DPADEnum { // To enable serial debugging see "settings.h" //#define PRINTREPORT // Uncomment to print the report send by the PS4 Controller +int8_t PS4Parser::getButtonIndex(ButtonEnum b) { + const uint8_t index = legacyButtonValues(b); + if (index >= sizeof(PS4_BUTTONS) / sizeof(PS4_BUTTONS[0])) return -1; + return index; +} + bool PS4Parser::checkDpad(ButtonEnum b) { switch (b) { case UP: @@ -48,14 +54,16 @@ bool PS4Parser::checkDpad(ButtonEnum b) { } bool PS4Parser::getButtonPress(ButtonEnum b) { - if (b <= LEFT) // Dpad + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + if (index <= LEFT) // Dpad return checkDpad(b); else - return ps4Data.btn.val & (1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b])); + return ps4Data.btn.val & (1UL << pgm_read_byte(&PS4_BUTTONS[index])); } bool PS4Parser::getButtonClick(ButtonEnum b) { - uint32_t mask = 1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]); + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + uint32_t mask = 1UL << pgm_read_byte(&PS4_BUTTONS[index]); bool click = buttonClickState.val & mask; buttonClickState.val &= ~mask; // Clear "click" event return click; diff --git a/PS4Parser.h b/PS4Parser.h index 86999623..4d6088ba 100644 --- a/PS4Parser.h +++ b/PS4Parser.h @@ -362,6 +362,7 @@ protected: virtual void sendOutputReport(PS4Output *output) = 0; private: + static int8_t getButtonIndex(ButtonEnum b); bool checkDpad(ButtonEnum b); // Used to check PS4 DPAD buttons PS4Data ps4Data; diff --git a/PS5Parser.cpp b/PS5Parser.cpp index fa3576c2..061ab8fe 100644 --- a/PS5Parser.cpp +++ b/PS5Parser.cpp @@ -36,6 +36,12 @@ enum DPADEnum { // To enable serial debugging see "settings.h" //#define PRINTREPORT // Uncomment to print the report send by the PS5 Controller +int8_t PS5Parser::getButtonIndex(ButtonEnum b) { + const uint8_t index = legacyButtonValues(b); + if (index >= sizeof(PS5_BUTTONS) / sizeof(PS5_BUTTONS[0])) return -1; + return index; +} + bool PS5Parser::checkDpad(ButtonEnum b) { switch (b) { case UP: @@ -52,14 +58,16 @@ bool PS5Parser::checkDpad(ButtonEnum b) { } bool PS5Parser::getButtonPress(ButtonEnum b) { - if (b <= LEFT) // Dpad + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + if (index <= LEFT) // Dpad return checkDpad(b); else - return ps5Data.btn.val & (1UL << pgm_read_byte(&PS5_BUTTONS[(uint8_t)b])); + return ps5Data.btn.val & (1UL << pgm_read_byte(&PS5_BUTTONS[index])); } bool PS5Parser::getButtonClick(ButtonEnum b) { - uint32_t mask = 1UL << pgm_read_byte(&PS5_BUTTONS[(uint8_t)b]); + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + uint32_t mask = 1UL << pgm_read_byte(&PS5_BUTTONS[index]); bool click = buttonClickState.val & mask; buttonClickState.val &= ~mask; // Clear "click" event return click; diff --git a/PS5Parser.h b/PS5Parser.h index 49fadd7b..7c0c29c9 100644 --- a/PS5Parser.h +++ b/PS5Parser.h @@ -403,6 +403,7 @@ protected: private: + static int8_t getButtonIndex(ButtonEnum b); bool checkDpad(ButtonEnum b); // Used to check PS5 DPAD buttons PS5Data ps5Data; diff --git a/PSBuzz.cpp b/PSBuzz.cpp index 2d4f2123..0ec452d6 100644 --- a/PSBuzz.cpp +++ b/PSBuzz.cpp @@ -49,12 +49,20 @@ uint8_t PSBuzz::OnInitSuccessful() { return 0; }; +int8_t PSBuzz::getButtonIndex(ButtonEnum b) { + const uint8_t index = legacyButtonValues(b); + if (index > 4) return -1; // 5 buttons, 0-4 inclusive + return index; +} + bool PSBuzz::getButtonPress(ButtonEnum b, uint8_t controller) { - return psbuzzButtons.val & (1UL << (b + 5 * controller)); // Each controller uses 5 bits, so the value is shifted 5 for each controller + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + return psbuzzButtons.val & (1UL << (index + 5 * controller)); // Each controller uses 5 bits, so the value is shifted 5 for each controller }; bool PSBuzz::getButtonClick(ButtonEnum b, uint8_t controller) { - uint32_t mask = (1UL << (b + 5 * controller)); // Each controller uses 5 bits, so the value is shifted 5 for each controller + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + uint32_t mask = (1UL << (index + 5 * controller)); // Each controller uses 5 bits, so the value is shifted 5 for each controller bool click = buttonClickState.val & mask; buttonClickState.val &= ~mask; // Clear "click" event return click; diff --git a/PSBuzz.h b/PSBuzz.h index da83dff0..8f891c35 100644 --- a/PSBuzz.h +++ b/PSBuzz.h @@ -177,6 +177,7 @@ protected: private: void (*pFuncOnInit)(void); // Pointer to function called in onInit() + static int8_t getButtonIndex(ButtonEnum b); void PSBuzz_Command(uint8_t *data, uint16_t nbytes); PSBUZZButtons psbuzzButtons, oldButtonState, buttonClickState; diff --git a/Wii.cpp b/Wii.cpp index 5f79104c..48ff3739 100644 --- a/Wii.cpp +++ b/Wii.cpp @@ -1094,19 +1094,39 @@ void WII::readWiiBalanceBoardCalibration() { /* WII Commands */ /************************************************************/ +int8_t WII::getButtonIndex(ButtonEnum b) { + const uint8_t index = legacyButtonValues(b); + if (index >= sizeof(WII_BUTTONS) / sizeof(WII_BUTTONS[0])) return -1; + return index; +} + +int8_t WII::getButtonIndexPro(ButtonEnum b) { + const uint8_t index = legacyButtonValues(b); + if (index >= sizeof(WII_PROCONTROLLER_BUTTONS) / sizeof(WII_PROCONTROLLER_BUTTONS[0])) return -1; + return index; +} + bool WII::getButtonPress(ButtonEnum b) { // Return true when a button is pressed - if(wiiUProControllerConnected) - return (ButtonState & pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[(uint8_t)b])); - else - return (ButtonState & pgm_read_dword(&WII_BUTTONS[(uint8_t)b])); + if (wiiUProControllerConnected) { + const int8_t index = getButtonIndexPro(b); if (index < 0) return 0; + return (ButtonState & pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[index])); + } + else { + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + return (ButtonState & pgm_read_dword(&WII_BUTTONS[index])); + } } bool WII::getButtonClick(ButtonEnum b) { // Only return true when a button is clicked uint32_t button; - if(wiiUProControllerConnected) - button = pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[(uint8_t)b]); - else - button = pgm_read_dword(&WII_BUTTONS[(uint8_t)b]); + if (wiiUProControllerConnected) { + const int8_t index = getButtonIndexPro(b); if (index < 0) return 0; + button = pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[index]); + } + else { + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + button = pgm_read_dword(&WII_BUTTONS[index]); + } bool click = (ButtonClickState & button); ButtonClickState &= ~button; // clear "click" event return click; diff --git a/Wii.h b/Wii.h index dde886b3..d70b2510 100644 --- a/Wii.h +++ b/Wii.h @@ -431,6 +431,8 @@ protected: /**@}*/ private: + static int8_t getButtonIndex(ButtonEnum b); + static int8_t getButtonIndexPro(ButtonEnum b); void L2CAP_task(); // L2CAP state machine diff --git a/XBOXOLD.cpp b/XBOXOLD.cpp index bcc353b3..440f8e0d 100644 --- a/XBOXOLD.cpp +++ b/XBOXOLD.cpp @@ -20,29 +20,6 @@ //#define EXTRADEBUG // Uncomment to get even more debugging data //#define PRINTREPORT // Uncomment to print the report send by the Xbox controller -/** Buttons on the controllers */ -const uint8_t XBOXOLD_BUTTONS[] PROGMEM = { - 0x01, // UP - 0x08, // RIGHT - 0x02, // DOWN - 0x04, // LEFT - - 0x20, // BACK - 0x10, // START - 0x40, // L3 - 0x80, // R3 - - // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons - 4, // BLACK - 5, // WHTIE - 6, // L1 - 7, // R1 - - 1, // B - 0, // A - 2, // X - 3, // Y -}; XBOXOLD::XBOXOLD(USB *p) : pUsb(p), // pointer to USB class instance - mandatory @@ -292,26 +269,96 @@ void XBOXOLD::printReport(uint16_t length __attribute__((unused))) { //Uncomment #endif } +int8_t XBOXOLD::getAnalogIndex(ButtonEnum b) { + // A, B, X, Y, BLACK, WHITE, L, and R are analog buttons + uint8_t out; + + switch (b) { + case A: + out = 0; break; + case B: + out = 1; break; + case X: + out = 2; break; + case Y: + out = 3; break; + case WHITE: + out = 5; break; + case BLACK: + out = 4; break; + case L: + case L1: + case L2: + out = 6; break; + case R: + case R1: + case R2: + out = 7; break; + default: + out = -1; break; + } + + return out; +} + +int8_t XBOXOLD::getDigitalOffset(ButtonEnum b) { + // UP, DOWN, LEFT, RIGHT, START, BACK, L3, and R3 are digital buttons + // (these are offets for the bitshift) + uint8_t out; + + switch (b) { + case UP: + out = 0; break; + case DOWN: + out = 1; break; + case LEFT: + out = 2; break; + case RIGHT: + out = 3; break; + case START: + out = 4; break; + case BACK: + out = 5; break; + case L3: + out = 6; break; + case R3: + out = 7; break; + default: + out = -1; break; + } + + return out; +} + uint8_t XBOXOLD::getButtonPress(ButtonEnum b) { - uint8_t button = pgm_read_byte(&XBOXOLD_BUTTONS[(uint8_t)b]); - if(b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons - return buttonValues[button]; // Analog buttons - return (ButtonState & button); // Digital buttons + const int8_t analogIndex = getAnalogIndex(b); + if (analogIndex >= 0) { + return buttonValues[analogIndex]; + } + const int8_t digitalOffset = getDigitalOffset(b); + if (digitalOffset >= 0) { + return (ButtonState & (1 << digitalOffset)); + } + return 0; } bool XBOXOLD::getButtonClick(ButtonEnum b) { - uint8_t button = pgm_read_byte(&XBOXOLD_BUTTONS[(uint8_t)b]); - if(b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) { // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons - if(buttonClicked[button]) { - buttonClicked[button] = false; + const int8_t analogIndex = getAnalogIndex(b); + if (analogIndex >= 0) { + if (buttonClicked[analogIndex]) { + buttonClicked[analogIndex] = false; return true; } return false; } - - bool click = (ButtonClickState & button); - ButtonClickState &= ~button; // clear "click" event - return click; + const int8_t digitalOffset = getDigitalOffset(b); + if (digitalOffset >= 0) { + const uint8_t mask = (1 << digitalOffset); + const bool click = (ButtonClickState & mask); + ButtonClickState &= ~mask; + return click; + } + return 0; } int16_t XBOXOLD::getAnalogHat(AnalogHatEnum a) { diff --git a/XBOXOLD.h b/XBOXOLD.h index 6b0757b2..319e435d 100644 --- a/XBOXOLD.h +++ b/XBOXOLD.h @@ -153,6 +153,9 @@ protected: EpInfo epInfo[XBOX_MAX_ENDPOINTS]; private: + static int8_t getAnalogIndex(ButtonEnum b); + static int8_t getDigitalOffset(ButtonEnum b); + /** * Called when the controller is successfully initialized. * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. diff --git a/XBOXONE.cpp b/XBOXONE.cpp index e84be5b4..336618be 100644 --- a/XBOXONE.cpp +++ b/XBOXONE.cpp @@ -331,9 +331,9 @@ void XBOXONE::readReport() { if(readBuf[0] == 0x07) { // The XBOX button has a separate message if(readBuf[4] == 1) - ButtonState |= pgm_read_word(&XBOX_BUTTONS[XBOX]); + ButtonState |= pgm_read_word(&XBOX_BUTTONS[legacyButtonValues(XBOX)]); else - ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[XBOX]); + ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[legacyButtonValues(XBOX)]); if(ButtonState != OldButtonState) { ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable @@ -348,7 +348,7 @@ void XBOXONE::readReport() { return; } - uint16_t xbox = ButtonState & pgm_read_word(&XBOX_BUTTONS[XBOX]); // Since the XBOX button is separate, save it and add it back in + uint16_t xbox = ButtonState & pgm_read_word(&XBOX_BUTTONS[legacyButtonValues(XBOX)]); // Since the XBOX button is separate, save it and add it back in // xbox button from before, dpad, abxy, start/back, sync, stick click, shoulder buttons ButtonState = xbox | (((uint16_t)readBuf[5] & 0xF) << 8) | (readBuf[4] & 0xF0) | (((uint16_t)readBuf[4] & 0x0C) << 10) | ((readBuf[4] & 0x01) << 3) | (((uint16_t)readBuf[5] & 0xC0) << 8) | ((readBuf[5] & 0x30) >> 4); @@ -382,7 +382,8 @@ uint16_t XBOXONE::getButtonPress(ButtonEnum b) { return triggerValue[0]; else if(b == R2) return triggerValue[1]; - return (bool)(ButtonState & ((uint16_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]))); + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + return (bool)(ButtonState & ((uint16_t)pgm_read_word(&XBOX_BUTTONS[index]))); } bool XBOXONE::getButtonClick(ButtonEnum b) { @@ -399,7 +400,8 @@ bool XBOXONE::getButtonClick(ButtonEnum b) { } return false; } - uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]); + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + uint16_t button = pgm_read_word(&XBOX_BUTTONS[index]); bool click = (ButtonClickState & button); ButtonClickState &= ~button; // Clear "click" event return click; diff --git a/XBOXONESParser.cpp b/XBOXONESParser.cpp index 9e5edd59..fce6d776 100644 --- a/XBOXONESParser.cpp +++ b/XBOXONESParser.cpp @@ -54,6 +54,12 @@ enum DPADEnum { DPAD_LEFT_UP = 0x8, }; +int8_t XBOXONESParser::getButtonIndex(ButtonEnum b) { + const uint8_t index = legacyButtonValues(b); + if (index >= sizeof(XBOX_ONE_S_BUTTONS) / sizeof(XBOX_ONE_S_BUTTONS[0])) return -1; + return index; +} + bool XBOXONESParser::checkDpad(ButtonEnum b) { switch (b) { case UP: @@ -78,7 +84,8 @@ uint16_t XBOXONESParser::getButtonPress(ButtonEnum b) { return checkDpad(b); else if (b == XBOX) return xboxButtonState; - return xboxOneSData.btn.val & (1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[(uint8_t)b])); + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + return xboxOneSData.btn.val & (1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[index])); } bool XBOXONESParser::getButtonClick(ButtonEnum b) { @@ -99,7 +106,8 @@ bool XBOXONESParser::getButtonClick(ButtonEnum b) { xboxbuttonClickState = 0; // Clear "click" event return click; } - uint32_t mask = 1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[(uint8_t)b]); + const int8_t index = getButtonIndex(b); if (index < 0) return 0; + uint32_t mask = 1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[index]); bool click = buttonClickState.val & mask; buttonClickState.val &= ~mask; // Clear "click" event return click; diff --git a/XBOXONESParser.h b/XBOXONESParser.h index d031631a..531bb3eb 100644 --- a/XBOXONESParser.h +++ b/XBOXONESParser.h @@ -111,6 +111,8 @@ protected: virtual void sendOutputReport(uint8_t *data, uint8_t nbytes) = 0; private: + static int8_t getButtonIndex(ButtonEnum b); + bool checkDpad(ButtonEnum b); // Used to check Xbox One S DPAD buttons XboxOneSData xboxOneSData; diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 031713d4..d00c1fa2 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -412,7 +412,8 @@ uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) { return (uint8_t)(ButtonState[controller] >> 8); else if(b == R2) return (uint8_t)ButtonState[controller]; - return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]) << 16)); + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[index]) << 16)); } bool XBOXRECV::getButtonClick(ButtonEnum b, uint8_t controller) { @@ -429,7 +430,8 @@ bool XBOXRECV::getButtonClick(ButtonEnum b, uint8_t controller) { } return false; } - uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]); + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + uint16_t button = pgm_read_word(&XBOX_BUTTONS[index]); bool click = (ButtonClickState[controller] & button); ButtonClickState[controller] &= ~button; // clear "click" event return click; diff --git a/XBOXUSB.cpp b/XBOXUSB.cpp index 6799029d..3749a5ad 100644 --- a/XBOXUSB.cpp +++ b/XBOXUSB.cpp @@ -285,7 +285,8 @@ uint8_t XBOXUSB::getButtonPress(ButtonEnum b) { return (uint8_t)(ButtonState >> 8); else if(b == R2) return (uint8_t)ButtonState; - return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]) << 16)); + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[index]) << 16)); } bool XBOXUSB::getButtonClick(ButtonEnum b) { @@ -302,7 +303,8 @@ bool XBOXUSB::getButtonClick(ButtonEnum b) { } return false; } - uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]); + const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0; + uint16_t button = pgm_read_word(&XBOX_BUTTONS[index]); bool click = (ButtonClickState & button); ButtonClickState &= ~button; // clear "click" event return click; diff --git a/controllerEnums.h b/controllerEnums.h index 3b45d5ef..cda84253 100644 --- a/controllerEnums.h +++ b/controllerEnums.h @@ -86,86 +86,169 @@ enum ButtonEnum { /**@{*/ /** Wii buttons */ - PLUS = 5, - TWO = 6, - ONE = 7, - MINUS = 8, - HOME = 9, - Z = 10, - C = 11, - B = 12, - A = 13, + PLUS, + TWO, + ONE, + MINUS, + HOME, + Z, + C, + B, + A, /**@}*/ /**@{*/ /** These are only available on the Wii U Pro Controller */ - L = 16, - R = 17, - ZL = 18, - ZR = 19, + L, + R, + ZL, + ZR, /**@}*/ /**@{*/ /** PS3 controllers buttons */ - SELECT = 4, - START = 5, - L3 = 6, - R3 = 7, + SELECT, + START, + L3, + R3, - L2 = 8, - R2 = 9, - L1 = 10, - R1 = 11, - TRIANGLE = 12, - CIRCLE = 13, - CROSS = 14, - SQUARE = 15, + L2, + R2, + L1, + R1, + TRIANGLE, + CIRCLE, + CROSS, + SQUARE, - PS = 16, + PS, - MOVE = 17, // Covers 12 bits - we only need to read the top 8 - T = 18, // Covers 12 bits - we only need to read the top 8 + MOVE, // Covers 12 bits - we only need to read the top 8 + T, // Covers 12 bits - we only need to read the top 8 /**@}*/ /** PS4 controllers buttons - SHARE and OPTIONS are present instead of SELECT and START */ - SHARE = 4, - OPTIONS = 5, - TOUCHPAD = 17, + SHARE, + OPTIONS, + TOUCHPAD, /**@}*/ /**@{*/ /** Xbox buttons */ - BACK = 4, - X = 14, - Y = 15, - XBOX = 16, - SYNC = 17, - BLACK = 8, // Available on the original Xbox controller - WHITE = 9, // Available on the original Xbox controller + BACK, + X, + Y, + XBOX, + SYNC, + BLACK, // Available on the original Xbox controller + WHITE, // Available on the original Xbox controller /**@}*/ /**@{*/ /** Xbox One S buttons */ - VIEW = 4, - MENU = 5, + VIEW, + MENU, /**@}*/ /**@{*/ /** PS Buzz controllers */ - RED = 0, - YELLOW = 1, - GREEN = 2, - ORANGE = 3, - BLUE = 4, + RED, + YELLOW, + GREEN, + ORANGE, + BLUE, /**@}*/ /**@{*/ /** PS5 buttons */ - CREATE = 4, - MICROPHONE = 18, + CREATE, + MICROPHONE, /**@}*/ }; +inline uint8_t legacyButtonValues(ButtonEnum key) { + uint8_t out; + + switch (key) { + case UP: + case RED: + out = 0; break; + case RIGHT: + case YELLOW: + out = 1; break; + case DOWN: + case GREEN: + out = 2; break; + case LEFT: + case ORANGE: + out = 3; break; + case SELECT: + case SHARE: + case BACK: + case VIEW: + case BLUE: + case CREATE: + out = 4; break; + case PLUS: + case START: + case OPTIONS: + case MENU: + out = 5; break; + case TWO: + case L3: + out = 6; break; + case ONE: + case R3: + out = 7; break; + case MINUS: + case L2: + case BLACK: + out = 8; break; + case HOME: + case R2: + case WHITE: + out = 9; break; + case Z: + case L1: + out = 10; break; + case C: + case R1: + out = 11; break; + case B: + case TRIANGLE: + out = 12; break; + case A: + case CIRCLE: + out = 13; break; + case CROSS: + case X: + out = 14; break; + case SQUARE: + case Y: + out = 15; break; + case L: + case PS: + case XBOX: + out = 16; break; + case R: + case MOVE: + case TOUCHPAD: + out = 17; break; + case SYNC: + out = 17; break; + case ZL: + case T: + case MICROPHONE: + out = 18; break; + case ZR: + out = 19; break; + default: + out = 0; break; + } + + return out; +} + /** Joysticks on the PS3 and Xbox controllers. */ enum AnalogHatEnum { /** Left joystick x-axis */ diff --git a/xboxEnums.h b/xboxEnums.h index 84b137bb..309a3a8d 100644 --- a/xboxEnums.h +++ b/xboxEnums.h @@ -62,4 +62,10 @@ const uint16_t XBOX_BUTTONS[] PROGMEM = { 0x0008, // SYNC }; +inline int8_t getXboxButtonIndex(ButtonEnum b) { + const uint8_t index = legacyButtonValues(b); + if (index >= sizeof(XBOX_BUTTONS) / sizeof(XBOX_BUTTONS[0])) return -1; + return index; +} + #endif