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) {
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) {
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);
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 = getButtonIndexPS3(a); if (index < 0) return 0;
return (uint8_t)(l2capinbuf[pgm_read_byte(&PS3_ANALOG_BUTTONS[index])]);
}
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
};
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

View file

@ -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 = getButtonIndexPS3(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 = 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
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 = 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) {

View file

@ -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::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) {
switch (b) {
case UP:
@ -48,23 +54,26 @@ bool PS4Parser::checkDpad(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);
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 = 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
return click;
}
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];
else if (b == R2)
else if (index == ButtonIndex(R2))
return ps4Data.trigger[1];
return 0;
}

View file

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

View file

@ -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::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) {
switch (b) {
case UP:
@ -52,23 +58,26 @@ bool PS5Parser::checkDpad(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);
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 = 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
return click;
}
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];
else if (b == R2)
else if (index == ButtonIndex(R2))
return ps5Data.trigger[1];
return 0;
}

View file

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

View file

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

View file

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

36
Wii.cpp
View file

@ -1094,19 +1094,39 @@ void WII::readWiiBalanceBoardCalibration() {
/* 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
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 = getButtonIndexWiiPro(b); if (index < 0) return 0;
return (ButtonState & pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[index]));
}
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
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 = getButtonIndexWiiPro(b); if (index < 0) return 0;
button = pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[index]);
}
else {
const int8_t index = getButtonIndexWii(b); if (index < 0) return 0;
button = pgm_read_dword(&WII_BUTTONS[index]);
}
bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event
return click;

2
Wii.h
View file

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

View file

@ -292,26 +292,78 @@ void XBOXOLD::printReport(uint16_t length __attribute__((unused))) { //Uncomment
#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 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) {
const uint8_t buttonIndex = pgm_read_byte(&XBOXOLD_BUTTONS[analogIndex]);
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) {
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) {
const uint8_t buttonIndex = pgm_read_byte(&XBOXOLD_BUTTONS[analogIndex]);
if (buttonClicked[buttonIndex]) {
buttonClicked[buttonIndex] = false;
return true;
}
return false;
}
bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event
return click;
const int8_t digitalIndex = getDigitalIndex(b);
if (digitalIndex >= 0) {
const uint8_t mask = pgm_read_byte(&XBOXOLD_BUTTONS[digitalIndex]);
const bool click = (ButtonClickState & mask);
ButtonClickState &= ~mask;
return click;
}
return 0;
}
int16_t XBOXOLD::getAnalogHat(AnalogHatEnum a) {

View file

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

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[XBOX]);
ButtonState |= pgm_read_word(&XBOX_BUTTONS[ButtonIndex(XBOX)]);
else
ButtonState &= ~pgm_read_word(&XBOX_BUTTONS[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[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,28 +378,30 @@ void XBOXONE::readReport() {
}
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];
else if(b == R2)
else if(index == ButtonIndex(R2))
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) {
if(b == 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(b == R2) {
} else if(index == ButtonIndex(R2)) {
if(R2Clicked) {
R2Clicked = false;
return true;
}
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);
ButtonClickState &= ~button; // Clear "click" event
return click;

View file

@ -54,6 +54,12 @@ enum DPADEnum {
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) {
switch (b) {
case UP:
@ -70,36 +76,38 @@ bool XBOXONESParser::checkDpad(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];
else if (b == R2)
else if (index == ButtonIndex(R2))
return xboxOneSData.trigger[1];
else if (b <= LEFT) // Dpad
else if (index <= LEFT) // Dpad
return checkDpad(b);
else if (b == XBOX)
else if (index == ButtonIndex(XBOX))
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) {
if(b == 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(b == R2) {
} else if(index == ButtonIndex(R2)) {
if(R2Clicked) {
R2Clicked = false;
return true;
}
return false;
} else if (b == XBOX) {
} else if (index == ButtonIndex(XBOX)) {
bool click = xboxbuttonClickState;
xboxbuttonClickState = 0; // Clear "click" event
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;
buttonClickState.val &= ~mask; // Clear "click" event
return click;

View file

@ -111,6 +111,8 @@ protected:
virtual void sendOutputReport(uint8_t *data, uint8_t nbytes) = 0;
private:
static int8_t getButtonIndexXboxOneS(ButtonEnum b);
bool checkDpad(ButtonEnum b); // Used to check Xbox One S DPAD buttons
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) {
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);
else if(b == R2)
else if(index == ButtonIndex(R2))
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) {
if(b == 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(b == R2) {
} else if(index == ButtonIndex(R2)) {
if(R2Clicked[controller]) {
R2Clicked[controller] = false;
return true;
}
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);
ButtonClickState[controller] &= ~button; // clear "click" event
return click;

View file

@ -281,28 +281,30 @@ void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the rep
}
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);
else if(b == R2)
else if(index == ButtonIndex(R2))
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) {
if(b == 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(b == R2) {
} else if(index == ButtonIndex(R2)) {
if(R2Clicked) {
R2Clicked = false;
return true;
}
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);
ButtonClickState &= ~button; // clear "click" event
return click;

View file

@ -77,7 +77,7 @@ enum RumbleEnum {
/** This enum is used to read all the different buttons on the different controllers */
enum ButtonEnum {
/**@{*/
/** These buttons are available on all the the controllers */
/** Directional Pad Buttons - available on most controllers */
UP = 0,
RIGHT = 1,
DOWN = 2,
@ -85,87 +85,130 @@ enum ButtonEnum {
/**@}*/
/**@{*/
/** Wii buttons */
PLUS = 5,
TWO = 6,
ONE = 7,
MINUS = 8,
HOME = 9,
Z = 10,
C = 11,
B = 12,
A = 13,
/** Playstation buttons */
TRIANGLE,
CIRCLE,
CROSS,
SQUARE,
SELECT,
START,
L3,
R3,
L1,
R1,
L2,
R2,
PS,
/**@}*/
/**@{*/
/** These are only available on the Wii U Pro Controller */
L = 16,
R = 17,
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,
/** PS3 Move Controller */
MOVE, // Covers 12 bits - we only need to read the top 8
T, // Covers 12 bits - we only need to read the top 8
/**@}*/
/**@{*/
/** PS Buzz controllers */
RED = 0,
YELLOW = 1,
GREEN = 2,
ORANGE = 3,
BLUE = 4,
RED,
YELLOW,
GREEN,
ORANGE,
BLUE,
/**@}*/
/**@{*/
/** PS4 buttons - SHARE and OPTIONS are present instead of SELECT and START */
SHARE,
OPTIONS,
TOUCHPAD,
/**@}*/
/**@{*/
/** PS5 buttons */
CREATE = 4,
MICROPHONE = 18,
CREATE,
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. */
enum AnalogHatEnum {
/** Left joystick x-axis */

View file

@ -62,4 +62,10 @@ const uint16_t XBOX_BUTTONS[] PROGMEM = {
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