diff --git a/PS3Enums.h b/PS3Enums.h index 2c0116e4..08d6ae0b 100644 --- a/PS3Enums.h +++ b/PS3Enums.h @@ -138,10 +138,8 @@ enum StatusEnum { Bluetooth = (40 << 8) | 0x16, // Operating by Bluetooth and rumble is turned off }; -inline int8_t getPS3ButtonIndex(ButtonEnum b) { - const int8_t index = legacyButtonValues(b); - if ((uint8_t) index >= (sizeof(PS3_BUTTONS) / sizeof(PS3_BUTTONS[0]))) return -1; - return index; +inline constexpr int8_t getPS3ButtonIndex(ButtonEnum b) { + return (uint8_t) legacyButtonValues(b) >= (sizeof(PS3_BUTTONS) / sizeof(PS3_BUTTONS[0])) ? -1 : legacyButtonValues(b); } #endif diff --git a/PS4Parser.cpp b/PS4Parser.cpp index aee04d2c..ff685293 100644 --- a/PS4Parser.cpp +++ b/PS4Parser.cpp @@ -32,10 +32,8 @@ 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 int8_t index = legacyButtonValues(b); - if ((uint8_t) index >= (sizeof(PS4_BUTTONS) / sizeof(PS4_BUTTONS[0]))) return -1; - return index; +inline constexpr int8_t PS4Parser::getButtonIndex(ButtonEnum b) { + return (uint8_t) legacyButtonValues(b) >= (sizeof(PS4_BUTTONS) / sizeof(PS4_BUTTONS[0])) ? -1 : legacyButtonValues(b); } bool PS4Parser::checkDpad(ButtonEnum b) { diff --git a/PS4Parser.h b/PS4Parser.h index 4d6088ba..bd5d9d6f 100644 --- a/PS4Parser.h +++ b/PS4Parser.h @@ -362,7 +362,7 @@ protected: virtual void sendOutputReport(PS4Output *output) = 0; private: - static int8_t getButtonIndex(ButtonEnum b); + static inline constexpr 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 8d53997c..a29b7486 100644 --- a/PS5Parser.cpp +++ b/PS5Parser.cpp @@ -36,10 +36,8 @@ 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 int8_t index = legacyButtonValues(b); - if ((uint8_t) index >= (sizeof(PS5_BUTTONS) / sizeof(PS5_BUTTONS[0]))) return -1; - return index; +inline constexpr int8_t PS5Parser::getButtonIndex(ButtonEnum b) { + return (uint8_t) legacyButtonValues(b) >= (sizeof(PS5_BUTTONS) / sizeof(PS5_BUTTONS[0])) ? -1 : legacyButtonValues(b); } bool PS5Parser::checkDpad(ButtonEnum b) { diff --git a/PS5Parser.h b/PS5Parser.h index 7c0c29c9..121f710f 100644 --- a/PS5Parser.h +++ b/PS5Parser.h @@ -403,7 +403,7 @@ protected: private: - static int8_t getButtonIndex(ButtonEnum b); + static inline constexpr int8_t getButtonIndex(ButtonEnum b); bool checkDpad(ButtonEnum b); // Used to check PS5 DPAD buttons PS5Data ps5Data; diff --git a/Wii.cpp b/Wii.cpp index 6073f250..0a69de69 100644 --- a/Wii.cpp +++ b/Wii.cpp @@ -1094,16 +1094,12 @@ void WII::readWiiBalanceBoardCalibration() { /* WII Commands */ /************************************************************/ -int8_t WII::getButtonIndex(ButtonEnum b) { - const int8_t index = legacyButtonValues(b); - if ((uint8_t) index >= (sizeof(WII_BUTTONS) / sizeof(WII_BUTTONS[0]))) return -1; - return index; +inline constexpr int8_t WII::getButtonIndex(ButtonEnum b) { + return (uint8_t)legacyButtonValues(b) >= (sizeof(WII_BUTTONS) / sizeof(WII_BUTTONS[0])) ? -1 : legacyButtonValues(b); } -int8_t WII::getButtonIndexPro(ButtonEnum b) { - const int8_t index = legacyButtonValues(b); - if ((uint8_t) index >= (sizeof(WII_PROCONTROLLER_BUTTONS) / sizeof(WII_PROCONTROLLER_BUTTONS[0]))) return -1; - return index; +inline constexpr int8_t WII::getButtonIndexPro(ButtonEnum b) { + return (uint8_t)legacyButtonValues(b) >= (sizeof(WII_PROCONTROLLER_BUTTONS) / sizeof(WII_PROCONTROLLER_BUTTONS[0])) ? -1 : legacyButtonValues(b); } bool WII::getButtonPress(ButtonEnum b) { // Return true when a button is pressed diff --git a/Wii.h b/Wii.h index d70b2510..343edf18 100644 --- a/Wii.h +++ b/Wii.h @@ -431,8 +431,8 @@ protected: /**@}*/ private: - static int8_t getButtonIndex(ButtonEnum b); - static int8_t getButtonIndexPro(ButtonEnum b); + static inline constexpr int8_t getButtonIndex(ButtonEnum b); + static inline constexpr int8_t getButtonIndexPro(ButtonEnum b); void L2CAP_task(); // L2CAP state machine diff --git a/XBOXONESParser.cpp b/XBOXONESParser.cpp index b92bbeb7..20e25833 100644 --- a/XBOXONESParser.cpp +++ b/XBOXONESParser.cpp @@ -54,10 +54,8 @@ enum DPADEnum { DPAD_LEFT_UP = 0x8, }; -int8_t XBOXONESParser::getButtonIndex(ButtonEnum b) { - const int8_t index = legacyButtonValues(b); - if ((uint8_t) index >= (sizeof(XBOX_ONE_S_BUTTONS) / sizeof(XBOX_ONE_S_BUTTONS[0]))) return -1; - return index; +inline constexpr int8_t XBOXONESParser::getButtonIndex(ButtonEnum b) { + return (uint8_t) legacyButtonValues(b) >= (sizeof(XBOX_ONE_S_BUTTONS) / sizeof(XBOX_ONE_S_BUTTONS[0])) ? -1 : legacyButtonValues(b); } bool XBOXONESParser::checkDpad(ButtonEnum b) { diff --git a/XBOXONESParser.h b/XBOXONESParser.h index 531bb3eb..67c3c34d 100644 --- a/XBOXONESParser.h +++ b/XBOXONESParser.h @@ -111,7 +111,7 @@ protected: virtual void sendOutputReport(uint8_t *data, uint8_t nbytes) = 0; private: - static int8_t getButtonIndex(ButtonEnum b); + static inline constexpr int8_t getButtonIndex(ButtonEnum b); bool checkDpad(ButtonEnum b); // Used to check Xbox One S DPAD buttons diff --git a/xboxEnums.h b/xboxEnums.h index 037be594..791f0cd4 100644 --- a/xboxEnums.h +++ b/xboxEnums.h @@ -62,10 +62,8 @@ const uint16_t XBOX_BUTTONS[] PROGMEM = { 0x0008, // SYNC }; -inline int8_t getXboxButtonIndex(ButtonEnum b) { - const int8_t index = legacyButtonValues(b); - if ((uint8_t) index >= (sizeof(XBOX_BUTTONS) / sizeof(XBOX_BUTTONS[0]))) return -1; - return index; +inline constexpr int8_t getXboxButtonIndex(ButtonEnum b) { + return (uint8_t) legacyButtonValues(b) >= (sizeof(XBOX_BUTTONS) / sizeof(XBOX_BUTTONS[0])) ? -1 : legacyButtonValues(b); } #endif