Refactor button index function names

It's not really fair to refer to the main index function as "legacy" if it's actively being used. All controller-specific 'get' filtering functions are now appended with the name of the controller rather than having it in the middle.
This commit is contained in:
David Madison 2021-03-27 08:35:55 -04:00
parent 6e0748d097
commit 2120adf283
18 changed files with 75 additions and 74 deletions

View file

@ -47,12 +47,12 @@ BluetoothService(p) // Pointer to USB class instance - mandatory
}
bool PS3BT::getButtonPress(ButtonEnum b) {
const int8_t index = getPS3ButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexPS3(b); if (index < 0) return 0;
return (ButtonState & pgm_read_dword(&PS3_BUTTONS[index]));
}
bool PS3BT::getButtonClick(ButtonEnum b) {
const int8_t index = getPS3ButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexPS3(b); if (index < 0) return 0;
uint32_t button = pgm_read_dword(&PS3_BUTTONS[index]);
bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // Clear "click" event
@ -60,7 +60,7 @@ bool PS3BT::getButtonClick(ButtonEnum b) {
}
uint8_t PS3BT::getAnalogButton(ButtonEnum a) {
const int8_t index = getPS3ButtonIndex(a); if (index < 0) return 0;
const int8_t index = getButtonIndexPS3(a); if (index < 0) return 0;
return (uint8_t)(l2capinbuf[pgm_read_byte(&PS3_ANALOG_BUTTONS[index])]);
}

View file

@ -138,8 +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);
inline int8_t getButtonIndexPS3(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) index >= (sizeof(PS3_BUTTONS) / sizeof(PS3_BUTTONS[0]))) return -1;
return index;
}

View file

@ -314,12 +314,12 @@ void PS3USB::printReport() { // Uncomment "#define PRINTREPORT" to print the rep
}
bool PS3USB::getButtonPress(ButtonEnum b) {
const int8_t index = getPS3ButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexPS3(b); if (index < 0) return 0;
return (ButtonState & pgm_read_dword(&PS3_BUTTONS[index]));
}
bool PS3USB::getButtonClick(ButtonEnum b) {
const int8_t index = getPS3ButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexPS3(b); if (index < 0) return 0;
uint32_t button = pgm_read_dword(&PS3_BUTTONS[index]);
bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // Clear "click" event
@ -327,7 +327,7 @@ bool PS3USB::getButtonClick(ButtonEnum b) {
}
uint8_t PS3USB::getAnalogButton(ButtonEnum a) {
const int8_t index = getPS3ButtonIndex(a); if (index < 0) return 0;
const int8_t index = getButtonIndexPS3(a); if (index < 0) return 0;
return (uint8_t)(readBuf[(pgm_read_byte(&PS3_ANALOG_BUTTONS[index])) - 9]);
}

View file

@ -32,8 +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);
int8_t PS4Parser::getButtonIndexPS4(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) index >= (sizeof(PS4_BUTTONS) / sizeof(PS4_BUTTONS[0]))) return -1;
return index;
}
@ -54,7 +54,7 @@ bool PS4Parser::checkDpad(ButtonEnum b) {
}
bool PS4Parser::getButtonPress(ButtonEnum b) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexPS4(b); if (index < 0) return 0;
if (index <= LEFT) // Dpad
return checkDpad(b);
else
@ -62,7 +62,7 @@ bool PS4Parser::getButtonPress(ButtonEnum b) {
}
bool PS4Parser::getButtonClick(ButtonEnum b) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexPS4(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
@ -70,10 +70,10 @@ bool PS4Parser::getButtonClick(ButtonEnum b) {
}
uint8_t PS4Parser::getAnalogButton(ButtonEnum b) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
if (index == legacyButtonValues(L2)) // These are the only analog buttons on the controller
const int8_t index = getButtonIndexPS4(b); if (index < 0) return 0;
if (index == ButtonIndex(L2)) // These are the only analog buttons on the controller
return ps4Data.trigger[0];
else if (index == legacyButtonValues(R2))
else if (index == ButtonIndex(R2))
return ps4Data.trigger[1];
return 0;
}

View file

@ -362,7 +362,7 @@ protected:
virtual void sendOutputReport(PS4Output *output) = 0;
private:
static int8_t getButtonIndex(ButtonEnum b);
static int8_t getButtonIndexPS4(ButtonEnum b);
bool checkDpad(ButtonEnum b); // Used to check PS4 DPAD buttons
PS4Data ps4Data;

View file

@ -36,8 +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);
int8_t PS5Parser::getButtonIndexPS5(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) index >= (sizeof(PS5_BUTTONS) / sizeof(PS5_BUTTONS[0]))) return -1;
return index;
}
@ -58,7 +58,7 @@ bool PS5Parser::checkDpad(ButtonEnum b) {
}
bool PS5Parser::getButtonPress(ButtonEnum b) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexPS5(b); if (index < 0) return 0;
if (index <= LEFT) // Dpad
return checkDpad(b);
else
@ -66,7 +66,7 @@ bool PS5Parser::getButtonPress(ButtonEnum b) {
}
bool PS5Parser::getButtonClick(ButtonEnum b) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexPS5(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
@ -74,10 +74,10 @@ bool PS5Parser::getButtonClick(ButtonEnum b) {
}
uint8_t PS5Parser::getAnalogButton(ButtonEnum b) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
if (index == legacyButtonValues(L2)) // These are the only analog buttons on the controller
const int8_t index = getButtonIndexPS5(b); if (index < 0) return 0;
if (index == ButtonIndex(L2)) // These are the only analog buttons on the controller
return ps5Data.trigger[0];
else if (index == legacyButtonValues(R2))
else if (index == ButtonIndex(R2))
return ps5Data.trigger[1];
return 0;
}

View file

@ -403,7 +403,7 @@ protected:
private:
static int8_t getButtonIndex(ButtonEnum b);
static int8_t getButtonIndexPS5(ButtonEnum b);
bool checkDpad(ButtonEnum b); // Used to check PS5 DPAD buttons
PS5Data ps5Data;

View file

@ -49,19 +49,19 @@ uint8_t PSBuzz::OnInitSuccessful() {
return 0;
};
int8_t PSBuzz::getButtonIndex(ButtonEnum b) {
const int8_t index = legacyButtonValues(b);
int8_t PSBuzz::getButtonIndexBuzz(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if (index > 4) return -1; // 5 buttons, 0-4 inclusive
return index;
}
bool PSBuzz::getButtonPress(ButtonEnum b, uint8_t controller) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexBuzz(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) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
const int8_t index = getButtonIndexBuzz(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

View file

@ -175,9 +175,10 @@ protected:
/**@}*/
private:
static int8_t getButtonIndexBuzz(ButtonEnum b);
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;

16
Wii.cpp
View file

@ -1094,25 +1094,25 @@ void WII::readWiiBalanceBoardCalibration() {
/* WII Commands */
/************************************************************/
int8_t WII::getButtonIndex(ButtonEnum b) {
const int8_t index = legacyButtonValues(b);
int8_t WII::getButtonIndexWii(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) index >= (sizeof(WII_BUTTONS) / sizeof(WII_BUTTONS[0]))) return -1;
return index;
}
int8_t WII::getButtonIndexPro(ButtonEnum b) {
const int8_t index = legacyButtonValues(b);
int8_t WII::getButtonIndexWiiPro(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) 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) {
const int8_t index = getButtonIndexPro(b); if (index < 0) return 0;
const int8_t index = getButtonIndexWiiPro(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;
const int8_t index = getButtonIndexWii(b); if (index < 0) return 0;
return (ButtonState & pgm_read_dword(&WII_BUTTONS[index]));
}
}
@ -1120,11 +1120,11 @@ bool WII::getButtonPress(ButtonEnum b) { // Return true when a button is pressed
bool WII::getButtonClick(ButtonEnum b) { // Only return true when a button is clicked
uint32_t button;
if (wiiUProControllerConnected) {
const int8_t index = getButtonIndexPro(b); if (index < 0) return 0;
const int8_t index = getButtonIndexWiiPro(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;
const int8_t index = getButtonIndexWii(b); if (index < 0) return 0;
button = pgm_read_dword(&WII_BUTTONS[index]);
}
bool click = (ButtonClickState & button);

4
Wii.h
View file

@ -431,8 +431,8 @@ protected:
/**@}*/
private:
static int8_t getButtonIndex(ButtonEnum b);
static int8_t getButtonIndexPro(ButtonEnum b);
static int8_t getButtonIndexWii(ButtonEnum b);
static int8_t getButtonIndexWiiPro(ButtonEnum b);
void L2CAP_task(); // L2CAP state machine

View file

@ -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[legacyButtonValues(XBOX)]);
ButtonState |= pgm_read_word(&XBOX_BUTTONS[ButtonIndex(XBOX)]);
else
ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[legacyButtonValues(XBOX)]);
ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[ButtonIndex(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[legacyButtonValues(XBOX)]); // Since the XBOX button is separate, save it and add it back in
uint16_t xbox = ButtonState & pgm_read_word(&XBOX_BUTTONS[ButtonIndex(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);
@ -378,23 +378,23 @@ void XBOXONE::readReport() {
}
uint16_t XBOXONE::getButtonPress(ButtonEnum b) {
const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0;
if(index == legacyButtonValues(L2)) // These are analog buttons
const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) // These are analog buttons
return triggerValue[0];
else if(index == legacyButtonValues(R2))
else if(index == ButtonIndex(R2))
return triggerValue[1];
return (bool)(ButtonState & ((uint16_t)pgm_read_word(&XBOX_BUTTONS[index])));
}
bool XBOXONE::getButtonClick(ButtonEnum b) {
const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0;
if(index == legacyButtonValues(L2)) {
const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) {
if(L2Clicked) {
L2Clicked = false;
return true;
}
return false;
} else if(index == legacyButtonValues(R2)) {
} else if(index == ButtonIndex(R2)) {
if(R2Clicked) {
R2Clicked = false;
return true;

View file

@ -54,8 +54,8 @@ enum DPADEnum {
DPAD_LEFT_UP = 0x8,
};
int8_t XBOXONESParser::getButtonIndex(ButtonEnum b) {
const int8_t index = legacyButtonValues(b);
int8_t XBOXONESParser::getButtonIndexXboxOneS(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) index >= (sizeof(XBOX_ONE_S_BUTTONS) / sizeof(XBOX_ONE_S_BUTTONS[0]))) return -1;
return index;
}
@ -76,33 +76,33 @@ bool XBOXONESParser::checkDpad(ButtonEnum b) {
}
uint16_t XBOXONESParser::getButtonPress(ButtonEnum b) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
if (index == legacyButtonValues(L2))
const int8_t index = getButtonIndexXboxOneS(b); if (index < 0) return 0;
if (index == ButtonIndex(L2))
return xboxOneSData.trigger[0];
else if (index == legacyButtonValues(R2))
else if (index == ButtonIndex(R2))
return xboxOneSData.trigger[1];
else if (index <= LEFT) // Dpad
return checkDpad(b);
else if (index == legacyButtonValues(XBOX))
else if (index == ButtonIndex(XBOX))
return xboxButtonState;
return xboxOneSData.btn.val & (1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[index]));
}
bool XBOXONESParser::getButtonClick(ButtonEnum b) {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
if(index == legacyButtonValues(L2)) {
const int8_t index = getButtonIndexXboxOneS(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) {
if(L2Clicked) {
L2Clicked = false;
return true;
}
return false;
} else if(index == legacyButtonValues(R2)) {
} else if(index == ButtonIndex(R2)) {
if(R2Clicked) {
R2Clicked = false;
return true;
}
return false;
} else if (index == legacyButtonValues(XBOX)) {
} else if (index == ButtonIndex(XBOX)) {
bool click = xboxbuttonClickState;
xboxbuttonClickState = 0; // Clear "click" event
return click;

View file

@ -111,7 +111,7 @@ protected:
virtual void sendOutputReport(uint8_t *data, uint8_t nbytes) = 0;
private:
static int8_t getButtonIndex(ButtonEnum b);
static int8_t getButtonIndexXboxOneS(ButtonEnum b);
bool checkDpad(ButtonEnum b); // Used to check Xbox One S DPAD buttons

View file

@ -408,23 +408,23 @@ void XBOXRECV::printReport(uint8_t controller __attribute__((unused)), uint8_t n
}
uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) {
const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0;
if(index == legacyButtonValues(L2)) // These are analog buttons
const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) // These are analog buttons
return (uint8_t)(ButtonState[controller] >> 8);
else if(index == legacyButtonValues(R2))
else if(index == ButtonIndex(R2))
return (uint8_t)ButtonState[controller];
return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[index]) << 16));
}
bool XBOXRECV::getButtonClick(ButtonEnum b, uint8_t controller) {
const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0;
if(index == legacyButtonValues(L2)) {
const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) {
if(L2Clicked[controller]) {
L2Clicked[controller] = false;
return true;
}
return false;
} else if(index == legacyButtonValues(R2)) {
} else if(index == ButtonIndex(R2)) {
if(R2Clicked[controller]) {
R2Clicked[controller] = false;
return true;

View file

@ -281,23 +281,23 @@ void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the rep
}
uint8_t XBOXUSB::getButtonPress(ButtonEnum b) {
const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0;
if(index == legacyButtonValues(L2)) // These are analog buttons
const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) // These are analog buttons
return (uint8_t)(ButtonState >> 8);
else if(index == legacyButtonValues(R2))
else if(index == ButtonIndex(R2))
return (uint8_t)ButtonState;
return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[index]) << 16));
}
bool XBOXUSB::getButtonClick(ButtonEnum b) {
const int8_t index = getXboxButtonIndex(b); if (index < 0) return 0;
if(index == legacyButtonValues(L2)) {
const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) {
if(L2Clicked) {
L2Clicked = false;
return true;
}
return false;
} else if(index == legacyButtonValues(R2)) {
} else if(index == ButtonIndex(R2)) {
if(R2Clicked) {
R2Clicked = false;
return true;

View file

@ -183,7 +183,7 @@ enum ButtonEnum {
/**@}*/
};
inline constexpr int8_t legacyButtonValues(ButtonEnum key) {
inline constexpr int8_t ButtonIndex(ButtonEnum key) {
// using a chained ternary in place of a switch for constexpr on older compilers
return
(key == UP || key == RED) ? 0 :

View file

@ -62,8 +62,8 @@ const uint16_t XBOX_BUTTONS[] PROGMEM = {
0x0008, // SYNC
};
inline int8_t getXboxButtonIndex(ButtonEnum b) {
const int8_t index = legacyButtonValues(b);
inline int8_t getButtonIndexXbox(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) index >= (sizeof(XBOX_BUTTONS) / sizeof(XBOX_BUTTONS[0]))) return -1;
return index;
}