Merge pull request #618 from dmadison/enum-refactor

Make ButtonEnum values unique
This commit is contained in:
Kristian Sloth Lauszus 2021-05-02 15:26:40 +02:00 committed by GitHub
commit 45bf971f3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 320 additions and 136 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 = getButtonIndexPS3(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 = getButtonIndexPS3(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 = getButtonIndexPS3(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 getButtonIndexPS3(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) 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 = getButtonIndexPS3(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 = getButtonIndexPS3(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 = getButtonIndexPS3(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::getButtonIndexPS4(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) 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,23 +54,26 @@ bool PS4Parser::checkDpad(ButtonEnum b) {
} }
bool PS4Parser::getButtonPress(ButtonEnum b) { bool PS4Parser::getButtonPress(ButtonEnum b) {
if (b <= LEFT) // Dpad const int8_t index = getButtonIndexPS4(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 = getButtonIndexPS4(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;
} }
uint8_t PS4Parser::getAnalogButton(ButtonEnum b) { uint8_t PS4Parser::getAnalogButton(ButtonEnum b) {
if (b == 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]; return ps4Data.trigger[0];
else if (b == R2) else if (index == ButtonIndex(R2))
return ps4Data.trigger[1]; return ps4Data.trigger[1];
return 0; return 0;
} }

View file

@ -362,6 +362,7 @@ protected:
virtual void sendOutputReport(PS4Output *output) = 0; virtual void sendOutputReport(PS4Output *output) = 0;
private: private:
static int8_t getButtonIndexPS4(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::getButtonIndexPS5(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) 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,23 +58,26 @@ bool PS5Parser::checkDpad(ButtonEnum b) {
} }
bool PS5Parser::getButtonPress(ButtonEnum b) { bool PS5Parser::getButtonPress(ButtonEnum b) {
if (b <= LEFT) // Dpad const int8_t index = getButtonIndexPS5(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 = getButtonIndexPS5(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;
} }
uint8_t PS5Parser::getAnalogButton(ButtonEnum b) { uint8_t PS5Parser::getAnalogButton(ButtonEnum b) {
if (b == 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]; return ps5Data.trigger[0];
else if (b == R2) else if (index == ButtonIndex(R2))
return ps5Data.trigger[1]; return ps5Data.trigger[1];
return 0; return 0;
} }

View file

@ -403,6 +403,7 @@ protected:
private: private:
static int8_t getButtonIndexPS5(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::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) { 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 = 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) { 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 = 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; bool click = buttonClickState.val & mask;
buttonClickState.val &= ~mask; // Clear "click" event buttonClickState.val &= ~mask; // Clear "click" event
return click; return click;

View file

@ -175,6 +175,8 @@ protected:
/**@}*/ /**@}*/
private: private:
static int8_t getButtonIndexBuzz(ButtonEnum b);
void (*pFuncOnInit)(void); // Pointer to function called in onInit() void (*pFuncOnInit)(void); // Pointer to function called in onInit()
void PSBuzz_Command(uint8_t *data, uint16_t nbytes); void PSBuzz_Command(uint8_t *data, uint16_t nbytes);

36
Wii.cpp
View file

@ -1094,19 +1094,39 @@ void WII::readWiiBalanceBoardCalibration() {
/* WII Commands */ /* WII Commands */
/************************************************************/ /************************************************************/
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::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 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 = getButtonIndexWiiPro(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 = getButtonIndexWii(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 = getButtonIndexWiiPro(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 = getButtonIndexWii(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 getButtonIndexWii(ButtonEnum b);
static int8_t getButtonIndexWiiPro(ButtonEnum b);
void L2CAP_task(); // L2CAP state machine void L2CAP_task(); // L2CAP state machine

View file

@ -292,26 +292,78 @@ void XBOXOLD::printReport(uint16_t length __attribute__((unused))) { //Uncomment
#endif #endif
} }
int8_t XBOXOLD::getAnalogIndex(ButtonEnum b) {
// A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons
const int8_t index = ButtonIndex(b);
switch (index) {
case ButtonIndex(A):
case ButtonIndex(B):
case ButtonIndex(X):
case ButtonIndex(Y):
case ButtonIndex(BLACK):
case ButtonIndex(WHITE):
case ButtonIndex(L1):
case ButtonIndex(R1):
return index;
default: break;
}
return -1;
}
int8_t XBOXOLD::getDigitalIndex(ButtonEnum b) {
// UP, DOWN, LEFT, RIGHT, START, BACK, L3, and R3 are digital buttons
const int8_t index = ButtonIndex(b);
switch (index) {
case ButtonIndex(UP):
case ButtonIndex(DOWN):
case ButtonIndex(LEFT):
case ButtonIndex(RIGHT):
case ButtonIndex(START):
case ButtonIndex(BACK):
case ButtonIndex(L3):
case ButtonIndex(R3):
return index;
default: break;
}
return -1;
}
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 const uint8_t buttonIndex = pgm_read_byte(&XBOXOLD_BUTTONS[analogIndex]);
return (ButtonState & button); // Digital buttons return buttonValues[buttonIndex];
}
const int8_t digitalIndex = getDigitalIndex(b);
if (digitalIndex >= 0) {
const uint8_t buttonMask = pgm_read_byte(&XBOXOLD_BUTTONS[digitalIndex]);
return (ButtonState & buttonMask);
}
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]) { const uint8_t buttonIndex = pgm_read_byte(&XBOXOLD_BUTTONS[analogIndex]);
buttonClicked[button] = false; if (buttonClicked[buttonIndex]) {
buttonClicked[buttonIndex] = false;
return true; return true;
} }
return false; return false;
} }
const int8_t digitalIndex = getDigitalIndex(b);
bool click = (ButtonClickState & button); if (digitalIndex >= 0) {
ButtonClickState &= ~button; // clear "click" event const uint8_t mask = pgm_read_byte(&XBOXOLD_BUTTONS[digitalIndex]);
const bool click = (ButtonClickState & mask);
ButtonClickState &= ~mask;
return click; 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 getDigitalIndex(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[ButtonIndex(XBOX)]);
else else
ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[XBOX]); ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[ButtonIndex(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[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 // 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);
@ -378,28 +378,30 @@ void XBOXONE::readReport() {
} }
uint16_t XBOXONE::getButtonPress(ButtonEnum b) { uint16_t XBOXONE::getButtonPress(ButtonEnum b) {
if(b == 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]; return triggerValue[0];
else if(b == R2) else if(index == ButtonIndex(R2))
return triggerValue[1]; return triggerValue[1];
return (bool)(ButtonState & ((uint16_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]))); return (bool)(ButtonState & ((uint16_t)pgm_read_word(&XBOX_BUTTONS[index])));
} }
bool XBOXONE::getButtonClick(ButtonEnum b) { bool XBOXONE::getButtonClick(ButtonEnum b) {
if(b == L2) { const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) {
if(L2Clicked) { if(L2Clicked) {
L2Clicked = false; L2Clicked = false;
return true; return true;
} }
return false; return false;
} else if(b == R2) { } else if(index == ButtonIndex(R2)) {
if(R2Clicked) { if(R2Clicked) {
R2Clicked = false; R2Clicked = false;
return true; return true;
} }
return false; return false;
} }
uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]); 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::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;
}
bool XBOXONESParser::checkDpad(ButtonEnum b) { bool XBOXONESParser::checkDpad(ButtonEnum b) {
switch (b) { switch (b) {
case UP: case UP:
@ -70,36 +76,38 @@ bool XBOXONESParser::checkDpad(ButtonEnum b) {
} }
uint16_t XBOXONESParser::getButtonPress(ButtonEnum b) { uint16_t XBOXONESParser::getButtonPress(ButtonEnum b) {
if (b == L2) const int8_t index = getButtonIndexXboxOneS(b); if (index < 0) return 0;
if (index == ButtonIndex(L2))
return xboxOneSData.trigger[0]; return xboxOneSData.trigger[0];
else if (b == R2) else if (index == ButtonIndex(R2))
return xboxOneSData.trigger[1]; return xboxOneSData.trigger[1];
else if (b <= LEFT) // Dpad else if (index <= LEFT) // Dpad
return checkDpad(b); return checkDpad(b);
else if (b == XBOX) else if (index == ButtonIndex(XBOX))
return xboxButtonState; return xboxButtonState;
return xboxOneSData.btn.val & (1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[(uint8_t)b])); return xboxOneSData.btn.val & (1UL << pgm_read_byte(&XBOX_ONE_S_BUTTONS[index]));
} }
bool XBOXONESParser::getButtonClick(ButtonEnum b) { bool XBOXONESParser::getButtonClick(ButtonEnum b) {
if(b == L2) { const int8_t index = getButtonIndexXboxOneS(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) {
if(L2Clicked) { if(L2Clicked) {
L2Clicked = false; L2Clicked = false;
return true; return true;
} }
return false; return false;
} else if(b == R2) { } else if(index == ButtonIndex(R2)) {
if(R2Clicked) { if(R2Clicked) {
R2Clicked = false; R2Clicked = false;
return true; return true;
} }
return false; return false;
} else if (b == XBOX) { } else if (index == ButtonIndex(XBOX)) {
bool click = xboxbuttonClickState; bool click = xboxbuttonClickState;
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]); 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 getButtonIndexXboxOneS(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

@ -408,28 +408,30 @@ void XBOXRECV::printReport(uint8_t controller __attribute__((unused)), uint8_t n
} }
uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) { uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) {
if(b == 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); return (uint8_t)(ButtonState[controller] >> 8);
else if(b == R2) else if(index == ButtonIndex(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)); 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) {
if(b == L2) { const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) {
if(L2Clicked[controller]) { if(L2Clicked[controller]) {
L2Clicked[controller] = false; L2Clicked[controller] = false;
return true; return true;
} }
return false; return false;
} else if(b == R2) { } else if(index == ButtonIndex(R2)) {
if(R2Clicked[controller]) { if(R2Clicked[controller]) {
R2Clicked[controller] = false; R2Clicked[controller] = false;
return true; return true;
} }
return false; return false;
} }
uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]); 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

@ -281,28 +281,30 @@ void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the rep
} }
uint8_t XBOXUSB::getButtonPress(ButtonEnum b) { uint8_t XBOXUSB::getButtonPress(ButtonEnum b) {
if(b == 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); return (uint8_t)(ButtonState >> 8);
else if(b == R2) else if(index == ButtonIndex(R2))
return (uint8_t)ButtonState; return (uint8_t)ButtonState;
return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]) << 16)); return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[index]) << 16));
} }
bool XBOXUSB::getButtonClick(ButtonEnum b) { bool XBOXUSB::getButtonClick(ButtonEnum b) {
if(b == L2) { const int8_t index = getButtonIndexXbox(b); if (index < 0) return 0;
if(index == ButtonIndex(L2)) {
if(L2Clicked) { if(L2Clicked) {
L2Clicked = false; L2Clicked = false;
return true; return true;
} }
return false; return false;
} else if(b == R2) { } else if(index == ButtonIndex(R2)) {
if(R2Clicked) { if(R2Clicked) {
R2Clicked = false; R2Clicked = false;
return true; return true;
} }
return false; return false;
} }
uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]); 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

@ -77,7 +77,7 @@ enum RumbleEnum {
/** This enum is used to read all the different buttons on the different controllers */ /** This enum is used to read all the different buttons on the different controllers */
enum ButtonEnum { enum ButtonEnum {
/**@{*/ /**@{*/
/** These buttons are available on all the the controllers */ /** Directional Pad Buttons - available on most controllers */
UP = 0, UP = 0,
RIGHT = 1, RIGHT = 1,
DOWN = 2, DOWN = 2,
@ -85,87 +85,130 @@ enum ButtonEnum {
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** Wii buttons */ /** Playstation buttons */
PLUS = 5, TRIANGLE,
TWO = 6, CIRCLE,
ONE = 7, CROSS,
MINUS = 8, SQUARE,
HOME = 9,
Z = 10, SELECT,
C = 11, START,
B = 12,
A = 13, L3,
R3,
L1,
R1,
L2,
R2,
PS,
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** These are only available on the Wii U Pro Controller */ /** PS3 Move Controller */
L = 16, MOVE, // Covers 12 bits - we only need to read the top 8
R = 17, T, // Covers 12 bits - we only need to read the top 8
ZL = 18,
ZR = 19,
/**@}*/
/**@{*/
/** PS3 controllers buttons */
SELECT = 4,
START = 5,
L3 = 6,
R3 = 7,
L2 = 8,
R2 = 9,
L1 = 10,
R1 = 11,
TRIANGLE = 12,
CIRCLE = 13,
CROSS = 14,
SQUARE = 15,
PS = 16,
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
/**@}*/
/** PS4 controllers buttons - SHARE and OPTIONS are present instead of SELECT and START */
SHARE = 4,
OPTIONS = 5,
TOUCHPAD = 17,
/**@}*/
/**@{*/
/** 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
/**@}*/
/**@{*/
/** Xbox One S buttons */
VIEW = 4,
MENU = 5,
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** PS Buzz controllers */ /** PS Buzz controllers */
RED = 0, RED,
YELLOW = 1, YELLOW,
GREEN = 2, GREEN,
ORANGE = 3, ORANGE,
BLUE = 4, BLUE,
/**@}*/
/**@{*/
/** PS4 buttons - SHARE and OPTIONS are present instead of SELECT and START */
SHARE,
OPTIONS,
TOUCHPAD,
/**@}*/ /**@}*/
/**@{*/ /**@{*/
/** PS5 buttons */ /** PS5 buttons */
CREATE = 4, CREATE,
MICROPHONE = 18, MICROPHONE,
/**@}*/
/**@{*/
/** Xbox buttons */
A,
B,
X,
Y,
BACK,
// START, // listed under Playstation buttons
// L1, // listed under Playstation buttons
// R1, // listed under Playstation buttons
// L2, // listed under Playstation buttons
// R2, // listed under Playstation buttons
XBOX,
SYNC,
BLACK, // Available on the original Xbox controller
WHITE, // Available on the original Xbox controller
/**@}*/
/**@{*/
/** Xbox One S buttons */
VIEW,
MENU,
/**@}*/
/**@{*/
/** Wii buttons */
PLUS,
TWO,
ONE,
MINUS,
HOME,
Z,
C,
// B, // listed under Xbox buttons
// A, // listed under Xbox buttons
/**@}*/
/**@{*/
/** Wii U Pro Controller */
L,
R,
ZL,
ZR,
/**@}*/ /**@}*/
}; };
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 :
(key == RIGHT || key == YELLOW) ? 1 :
(key == DOWN || key == GREEN) ? 2 :
(key == LEFT || key == ORANGE) ? 3 :
(key == SELECT || key == SHARE || key == BACK || key == VIEW || key == BLUE || key == CREATE) ? 4 :
(key == START || key == OPTIONS || key == MENU || key == PLUS) ? 5 :
(key == L3 || key == TWO) ? 6 :
(key == R3 || key == ONE) ? 7 :
(key == L2 || key == MINUS || key == BLACK) ? 8 :
(key == R2 || key == HOME || key == WHITE) ? 9 :
(key == L1 || key == Z) ? 10 :
(key == R1 || key == C) ? 11 :
(key == TRIANGLE || key == B) ? 12 :
(key == CIRCLE || key == A) ? 13 :
(key == CROSS || key == X) ? 14 :
(key == SQUARE || key == Y) ? 15 :
(key == L || key == PS || key == XBOX) ? 16 :
(key == R || key == MOVE || key == TOUCHPAD || key == SYNC) ? 17 :
(key == ZL || key == T || key == MICROPHONE) ? 18 :
(key == ZR) ? 19 :
-1; // not a match
}
/** 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 getButtonIndexXbox(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) index >= (sizeof(XBOX_BUTTONS) / sizeof(XBOX_BUTTONS[0]))) return -1;
return index;
}
#endif #endif