Add SHARE button support to Xbox One wired

This commit is contained in:
David Madison 2021-07-01 06:03:57 -04:00
parent b6511306f1
commit f0e37202e1
3 changed files with 29 additions and 9 deletions

View file

@ -348,9 +348,9 @@ void XBOXONE::readReport() {
return; return;
} }
uint16_t xbox = ButtonState & pgm_read_word(&XBOX_BUTTONS[ButtonIndex(XBOX)]); // Since the XBOX button is separate, save it and add it back in uint32_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 | (((uint32_t)readBuf[5] & 0xF) << 8) | (readBuf[4] & 0xF0) | (((uint32_t)readBuf[4] & 0x0C) << 10) | ((readBuf[4] & 0x01) << 3) | (((uint32_t)readBuf[5] & 0xC0) << 8) | ((readBuf[5] & 0x30) >> 4) | (((uint32_t)readBuf[22] & 0x01) << ShareButtonIndex);
triggerValue[0] = (uint16_t)(((uint16_t)readBuf[7] << 8) | readBuf[6]); triggerValue[0] = (uint16_t)(((uint16_t)readBuf[7] << 8) | readBuf[6]);
triggerValue[1] = (uint16_t)(((uint16_t)readBuf[9] << 8) | readBuf[8]); triggerValue[1] = (uint16_t)(((uint16_t)readBuf[9] << 8) | readBuf[8]);
@ -383,7 +383,16 @@ uint16_t XBOXONE::getButtonPress(ButtonEnum b) {
return triggerValue[0]; return triggerValue[0];
else if(index == ButtonIndex(R2)) else if(index == ButtonIndex(R2))
return triggerValue[1]; return triggerValue[1];
return (bool)(ButtonState & ((uint16_t)pgm_read_word(&XBOX_BUTTONS[index])));
uint32_t buttonMask;
// special case, as 'SHARE' from the PS4 has the same index
// as 'BACK' on the Xbox controller, so we treat it separately
if (b == SHARE) {
buttonMask = (1 << ShareButtonIndex);
} else {
buttonMask = (uint32_t) pgm_read_word(&XBOX_BUTTONS[index]);
}
return (bool)(ButtonState & buttonMask);
} }
bool XBOXONE::getButtonClick(ButtonEnum b) { bool XBOXONE::getButtonClick(ButtonEnum b) {
@ -401,9 +410,17 @@ bool XBOXONE::getButtonClick(ButtonEnum b) {
} }
return false; return false;
} }
uint16_t button = pgm_read_word(&XBOX_BUTTONS[index]);
bool click = (ButtonClickState & button); uint32_t buttonMask;
ButtonClickState &= ~button; // Clear "click" event // special case, as 'SHARE' from the PS4 has the same index
// as 'BACK' on the Xbox controller, so we treat it separately
if (b == SHARE) {
buttonMask = (1 << ShareButtonIndex);
} else {
buttonMask = pgm_read_word(&XBOX_BUTTONS[index]);
}
bool click = (ButtonClickState & buttonMask);
ButtonClickState &= ~buttonMask; // Clear "click" event
return click; return click;
} }

View file

@ -221,9 +221,10 @@ private:
bool bPollEnable; bool bPollEnable;
/* Variables to store the buttons */ /* Variables to store the buttons */
uint16_t ButtonState; static const uint8_t ShareButtonIndex = 16; // bit offset from right
uint16_t OldButtonState; uint32_t ButtonState;
uint16_t ButtonClickState; uint32_t OldButtonState;
uint32_t ButtonClickState;
int16_t hatValue[4]; int16_t hatValue[4];
uint16_t triggerValue[2]; uint16_t triggerValue[2];
uint16_t triggerValueOld[2]; uint16_t triggerValueOld[2];

View file

@ -93,6 +93,8 @@ void loop() {
Serial.println(F("Xbox")); Serial.println(F("Xbox"));
if (Xbox.getButtonClick(SYNC)) if (Xbox.getButtonClick(SYNC))
Serial.println(F("Sync")); Serial.println(F("Sync"));
if (Xbox.getButtonClick(SHARE))
Serial.println(F("Share"));
if (Xbox.getButtonClick(L1)) if (Xbox.getButtonClick(L1))
Serial.println(F("L1")); Serial.println(F("L1"));