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.
This commit is contained in:
David Madison 2021-03-25 16:18:36 -04:00
parent a321ad36ab
commit 26c66f455e
20 changed files with 331 additions and 115 deletions

View file

@ -47,18 +47,21 @@ BluetoothService(p) // Pointer to USB class instance - mandatory
} }
bool PS3BT::getButtonPress(ButtonEnum b) { 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) { 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); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // Clear "click" event ButtonClickState &= ~button; // Clear "click" event
return click; return click;
} }
uint8_t PS3BT::getAnalogButton(ButtonEnum a) { 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) { uint8_t PS3BT::getAnalogHat(AnalogHatEnum a) {

View file

@ -138,4 +138,10 @@ enum StatusEnum {
Bluetooth = (40 << 8) | 0x16, // Operating by Bluetooth and rumble is turned off 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 #endif

View file

@ -314,18 +314,21 @@ void PS3USB::printReport() { // Uncomment "#define PRINTREPORT" to print the rep
} }
bool PS3USB::getButtonPress(ButtonEnum b) { 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) { 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); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // Clear "click" event ButtonClickState &= ~button; // Clear "click" event
return click; return click;
} }
uint8_t PS3USB::getAnalogButton(ButtonEnum a) { 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) { uint8_t PS3USB::getAnalogHat(AnalogHatEnum a) {

View file

@ -32,6 +32,12 @@ enum DPADEnum {
// To enable serial debugging see "settings.h" // To enable serial debugging see "settings.h"
//#define PRINTREPORT // Uncomment to print the report send by the PS4 Controller //#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) { bool PS4Parser::checkDpad(ButtonEnum b) {
switch (b) { switch (b) {
case UP: case UP:
@ -48,14 +54,16 @@ bool PS4Parser::checkDpad(ButtonEnum b) {
} }
bool PS4Parser::getButtonPress(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); return checkDpad(b);
else 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) { 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; bool click = buttonClickState.val & mask;
buttonClickState.val &= ~mask; // Clear "click" event buttonClickState.val &= ~mask; // Clear "click" event
return click; return click;

View file

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

View file

@ -36,6 +36,12 @@ enum DPADEnum {
// To enable serial debugging see "settings.h" // To enable serial debugging see "settings.h"
//#define PRINTREPORT // Uncomment to print the report send by the PS5 Controller //#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) { bool PS5Parser::checkDpad(ButtonEnum b) {
switch (b) { switch (b) {
case UP: case UP:
@ -52,14 +58,16 @@ bool PS5Parser::checkDpad(ButtonEnum b) {
} }
bool PS5Parser::getButtonPress(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); return checkDpad(b);
else 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) { 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; bool click = buttonClickState.val & mask;
buttonClickState.val &= ~mask; // Clear "click" event buttonClickState.val &= ~mask; // Clear "click" event
return click; return click;

View file

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

View file

@ -49,12 +49,20 @@ uint8_t PSBuzz::OnInitSuccessful() {
return 0; 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) { 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) { 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; bool click = buttonClickState.val & mask;
buttonClickState.val &= ~mask; // Clear "click" event buttonClickState.val &= ~mask; // Clear "click" event
return click; return click;

View file

@ -177,6 +177,7 @@ protected:
private: private:
void (*pFuncOnInit)(void); // Pointer to function called in onInit() void (*pFuncOnInit)(void); // Pointer to function called in onInit()
static int8_t getButtonIndex(ButtonEnum b);
void PSBuzz_Command(uint8_t *data, uint16_t nbytes); void PSBuzz_Command(uint8_t *data, uint16_t nbytes);
PSBUZZButtons psbuzzButtons, oldButtonState, buttonClickState; PSBUZZButtons psbuzzButtons, oldButtonState, buttonClickState;

36
Wii.cpp
View file

@ -1094,19 +1094,39 @@ void WII::readWiiBalanceBoardCalibration() {
/* WII Commands */ /* 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 bool WII::getButtonPress(ButtonEnum b) { // Return true when a button is pressed
if(wiiUProControllerConnected) if (wiiUProControllerConnected) {
return (ButtonState & pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[(uint8_t)b])); const int8_t index = getButtonIndexPro(b); if (index < 0) return 0;
else return (ButtonState & pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[index]));
return (ButtonState & pgm_read_dword(&WII_BUTTONS[(uint8_t)b])); }
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 bool WII::getButtonClick(ButtonEnum b) { // Only return true when a button is clicked
uint32_t button; uint32_t button;
if(wiiUProControllerConnected) if (wiiUProControllerConnected) {
button = pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[(uint8_t)b]); const int8_t index = getButtonIndexPro(b); if (index < 0) return 0;
else button = pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[index]);
button = pgm_read_dword(&WII_BUTTONS[(uint8_t)b]); }
else {
const int8_t index = getButtonIndex(b); if (index < 0) return 0;
button = pgm_read_dword(&WII_BUTTONS[index]);
}
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event ButtonClickState &= ~button; // clear "click" event
return click; return click;

2
Wii.h
View file

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

View file

@ -20,29 +20,6 @@
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report send by the Xbox controller //#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) : XBOXOLD::XBOXOLD(USB *p) :
pUsb(p), // pointer to USB class instance - mandatory pUsb(p), // pointer to USB class instance - mandatory
@ -292,26 +269,96 @@ void XBOXOLD::printReport(uint16_t length __attribute__((unused))) { //Uncomment
#endif #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 XBOXOLD::getButtonPress(ButtonEnum b) {
uint8_t button = pgm_read_byte(&XBOXOLD_BUTTONS[(uint8_t)b]); const int8_t analogIndex = getAnalogIndex(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 (analogIndex >= 0) {
return buttonValues[button]; // Analog buttons return buttonValues[analogIndex];
return (ButtonState & button); // Digital buttons }
const int8_t digitalOffset = getDigitalOffset(b);
if (digitalOffset >= 0) {
return (ButtonState & (1 << digitalOffset));
}
return 0;
} }
bool XBOXOLD::getButtonClick(ButtonEnum b) { bool XBOXOLD::getButtonClick(ButtonEnum b) {
uint8_t button = pgm_read_byte(&XBOXOLD_BUTTONS[(uint8_t)b]); const int8_t analogIndex = getAnalogIndex(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 (analogIndex >= 0) {
if(buttonClicked[button]) { if (buttonClicked[analogIndex]) {
buttonClicked[button] = false; buttonClicked[analogIndex] = false;
return true; return true;
} }
return false; return false;
} }
const int8_t digitalOffset = getDigitalOffset(b);
bool click = (ButtonClickState & button); if (digitalOffset >= 0) {
ButtonClickState &= ~button; // clear "click" event const uint8_t mask = (1 << digitalOffset);
return click; const bool click = (ButtonClickState & mask);
ButtonClickState &= ~mask;
return click;
}
return 0;
} }
int16_t XBOXOLD::getAnalogHat(AnalogHatEnum a) { int16_t XBOXOLD::getAnalogHat(AnalogHatEnum a) {

View file

@ -153,6 +153,9 @@ protected:
EpInfo epInfo[XBOX_MAX_ENDPOINTS]; EpInfo epInfo[XBOX_MAX_ENDPOINTS];
private: private:
static int8_t getAnalogIndex(ButtonEnum b);
static int8_t getDigitalOffset(ButtonEnum b);
/** /**
* Called when the controller is successfully initialized. * Called when the controller is successfully initialized.
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function. * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.

View file

@ -331,9 +331,9 @@ void XBOXONE::readReport() {
if(readBuf[0] == 0x07) { if(readBuf[0] == 0x07) {
// The XBOX button has a separate message // The XBOX button has a separate message
if(readBuf[4] == 1) if(readBuf[4] == 1)
ButtonState |= pgm_read_word(&XBOX_BUTTONS[XBOX]); ButtonState |= pgm_read_word(&XBOX_BUTTONS[legacyButtonValues(XBOX)]);
else else
ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[XBOX]); ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[legacyButtonValues(XBOX)]);
if(ButtonState != OldButtonState) { if(ButtonState != OldButtonState) {
ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
@ -348,7 +348,7 @@ void XBOXONE::readReport() {
return; 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 // 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); 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]; return triggerValue[0];
else if(b == R2) else if(b == R2)
return triggerValue[1]; 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) { bool XBOXONE::getButtonClick(ButtonEnum b) {
@ -399,7 +400,8 @@ bool XBOXONE::getButtonClick(ButtonEnum b) {
} }
return false; 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); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // Clear "click" event ButtonClickState &= ~button; // Clear "click" event
return click; return click;

View file

@ -54,6 +54,12 @@ enum DPADEnum {
DPAD_LEFT_UP = 0x8, 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) { bool XBOXONESParser::checkDpad(ButtonEnum b) {
switch (b) { switch (b) {
case UP: case UP:
@ -78,7 +84,8 @@ uint16_t XBOXONESParser::getButtonPress(ButtonEnum b) {
return checkDpad(b); return checkDpad(b);
else if (b == XBOX) else if (b == XBOX)
return xboxButtonState; 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) { bool XBOXONESParser::getButtonClick(ButtonEnum b) {
@ -99,7 +106,8 @@ bool XBOXONESParser::getButtonClick(ButtonEnum b) {
xboxbuttonClickState = 0; // Clear "click" event xboxbuttonClickState = 0; // Clear "click" event
return click; 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; bool click = buttonClickState.val & mask;
buttonClickState.val &= ~mask; // Clear "click" event buttonClickState.val &= ~mask; // Clear "click" event
return click; return click;

View file

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

View file

@ -412,7 +412,8 @@ uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) {
return (uint8_t)(ButtonState[controller] >> 8); return (uint8_t)(ButtonState[controller] >> 8);
else if(b == R2) else if(b == R2)
return (uint8_t)ButtonState[controller]; 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) { bool XBOXRECV::getButtonClick(ButtonEnum b, uint8_t controller) {
@ -429,7 +430,8 @@ bool XBOXRECV::getButtonClick(ButtonEnum b, uint8_t controller) {
} }
return false; 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); bool click = (ButtonClickState[controller] & button);
ButtonClickState[controller] &= ~button; // clear "click" event ButtonClickState[controller] &= ~button; // clear "click" event
return click; return click;

View file

@ -285,7 +285,8 @@ uint8_t XBOXUSB::getButtonPress(ButtonEnum b) {
return (uint8_t)(ButtonState >> 8); return (uint8_t)(ButtonState >> 8);
else if(b == R2) else if(b == R2)
return (uint8_t)ButtonState; 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) { bool XBOXUSB::getButtonClick(ButtonEnum b) {
@ -302,7 +303,8 @@ bool XBOXUSB::getButtonClick(ButtonEnum b) {
} }
return false; 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); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event ButtonClickState &= ~button; // clear "click" event
return click; return click;

View file

@ -86,86 +86,169 @@ enum ButtonEnum {
/**@{*/ /**@{*/
/** Wii buttons */ /** Wii buttons */
PLUS = 5, PLUS,
TWO = 6, TWO,
ONE = 7, ONE,
MINUS = 8, MINUS,
HOME = 9, HOME,
Z = 10, Z,
C = 11, C,
B = 12, B,
A = 13, A,
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** These are only available on the Wii U Pro Controller */ /** These are only available on the Wii U Pro Controller */
L = 16, L,
R = 17, R,
ZL = 18, ZL,
ZR = 19, ZR,
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** PS3 controllers buttons */ /** PS3 controllers buttons */
SELECT = 4, SELECT,
START = 5, START,
L3 = 6, L3,
R3 = 7, R3,
L2 = 8, L2,
R2 = 9, R2,
L1 = 10, L1,
R1 = 11, R1,
TRIANGLE = 12, TRIANGLE,
CIRCLE = 13, CIRCLE,
CROSS = 14, CROSS,
SQUARE = 15, SQUARE,
PS = 16, PS,
MOVE = 17, // Covers 12 bits - we only need to read the top 8 MOVE, // Covers 12 bits - we only need to read the top 8
T = 18, // 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 */ /** PS4 controllers buttons - SHARE and OPTIONS are present instead of SELECT and START */
SHARE = 4, SHARE,
OPTIONS = 5, OPTIONS,
TOUCHPAD = 17, TOUCHPAD,
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** Xbox buttons */ /** Xbox buttons */
BACK = 4, BACK,
X = 14, X,
Y = 15, Y,
XBOX = 16, XBOX,
SYNC = 17, SYNC,
BLACK = 8, // Available on the original Xbox controller BLACK, // Available on the original Xbox controller
WHITE = 9, // Available on the original Xbox controller WHITE, // Available on the original Xbox controller
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** Xbox One S buttons */ /** Xbox One S buttons */
VIEW = 4, VIEW,
MENU = 5, MENU,
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** PS Buzz controllers */ /** PS Buzz controllers */
RED = 0, RED,
YELLOW = 1, YELLOW,
GREEN = 2, GREEN,
ORANGE = 3, ORANGE,
BLUE = 4, BLUE,
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** PS5 buttons */ /** PS5 buttons */
CREATE = 4, CREATE,
MICROPHONE = 18, 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. */ /** Joysticks on the PS3 and Xbox controllers. */
enum AnalogHatEnum { enum AnalogHatEnum {
/** Left joystick x-axis */ /** Left joystick x-axis */

View file

@ -62,4 +62,10 @@ const uint16_t XBOX_BUTTONS[] PROGMEM = {
0x0008, // SYNC 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 #endif