LeftTrigger/RightTrigger => L2/R2 for consistency

This commit is contained in:
Fred 2015-02-15 19:47:06 +00:00
parent 519e2a5309
commit 9979e50426
5 changed files with 63 additions and 59 deletions

View file

@ -281,8 +281,8 @@ void XBOXONE::readReport() {
// 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);
triggerValue[LeftTrigger] = (uint16_t)(((uint16_t)readBuf[7] << 8) | readBuf[6]); triggerValue[0] = (uint16_t)(((uint16_t)readBuf[7] << 8) | readBuf[6]);
triggerValue[RightTrigger] = (uint16_t)(((uint16_t)readBuf[9] << 8) | readBuf[8]); triggerValue[1] = (uint16_t)(((uint16_t)readBuf[9] << 8) | readBuf[8]);
hatValue[LeftHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]); hatValue[LeftHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]);
hatValue[LeftHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]); hatValue[LeftHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]);
@ -296,6 +296,15 @@ void XBOXONE::readReport() {
ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
OldButtonState = ButtonState; OldButtonState = ButtonState;
} }
if (triggerValue[0] != triggerValueOld[0]) {
triggerValueOld[0] = triggerValue[0];
L2Clicked = true;
}
if (triggerValue[1] != triggerValueOld[1]) {
triggerValueOld[1] = triggerValue[1];
R2Clicked = true;
}
} }
void XBOXONE::printReport() { //Uncomment "#define PRINTREPORT" to print the report send by the Xbox ONE Controller void XBOXONE::printReport() { //Uncomment "#define PRINTREPORT" to print the report send by the Xbox ONE Controller
@ -310,11 +319,28 @@ void XBOXONE::printReport() { //Uncomment "#define PRINTREPORT" to print the rep
#endif #endif
} }
uint8_t XBOXONE::getButtonPress(ButtonEnum b) { uint16_t XBOXONE::getButtonPress(ButtonEnum b) {
if(b == L2) // These are analog buttons
return triggerValue[0];
else if(b == 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[(uint8_t)b])));
} }
bool XBOXONE::getButtonClick(ButtonEnum b) { bool XBOXONE::getButtonClick(ButtonEnum b) {
if(b == L2) {
if(L2Clicked) {
L2Clicked = false;
return true;
}
return false;
} else if(b == 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[(uint8_t)b]);
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event ButtonClickState &= ~button; // clear "click" event
@ -325,10 +351,6 @@ int16_t XBOXONE::getAnalogHat(AnalogHatEnum a) {
return hatValue[a]; return hatValue[a];
} }
uint16_t XBOXONE::getTrigger(TriggerEnum a) {
return triggerValue[a];
}
/* Xbox Controller commands */ /* Xbox Controller commands */
uint8_t XBOXONE::XboxCommand(uint8_t* data, uint16_t nbytes) { uint8_t XBOXONE::XboxCommand(uint8_t* data, uint16_t nbytes) {
uint8_t rcode = pUsb->outTransfer(bAddress, epInfo[ XBOX_OUTPUT_PIPE ].epAddr, nbytes, data); uint8_t rcode = pUsb->outTransfer(bAddress, epInfo[ XBOX_OUTPUT_PIPE ].epAddr, nbytes, data);

View file

@ -42,7 +42,7 @@
#define XBOX_MAX_ENDPOINTS 3 #define XBOX_MAX_ENDPOINTS 3
/** This class implements support for a Xbox wired controller via USB. */ /** This class implements support for a Xbox ONE controller connected via USB. */
class XBOXONE : public USBDeviceConfig { class XBOXONE : public USBDeviceConfig {
public: public:
/** /**
@ -107,9 +107,9 @@ public:
* So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b), * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
* but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b). * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
* @param b ::ButtonEnum to read. * @param b ::ButtonEnum to read.
* @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2. * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a word if reading ::L2 or ::R2.
*/ */
uint8_t getButtonPress(ButtonEnum b); uint16_t getButtonPress(ButtonEnum b);
bool getButtonClick(ButtonEnum b); bool getButtonClick(ButtonEnum b);
/** /**
@ -119,14 +119,6 @@ public:
*/ */
int16_t getAnalogHat(AnalogHatEnum a); int16_t getAnalogHat(AnalogHatEnum a);
/** @name Xbox Controller functions */
/**
* Return the analog value from the triggers on the controller.
* @param a Either ::LeftTrigger, or ::RightTrigger.
* @return Returns an unsigned 16-bit integer.
*/
uint16_t getTrigger(TriggerEnum a);
/** /**
* Used to call your own function when the controller is successfully initialized. * Used to call your own function when the controller is successfully initialized.
* @param funcOnInit Function to call. * @param funcOnInit Function to call.
@ -163,6 +155,10 @@ private:
uint16_t ButtonClickState; uint16_t ButtonClickState;
int16_t hatValue[4]; int16_t hatValue[4];
int16_t triggerValue[2]; int16_t triggerValue[2];
int16_t triggerValueOld[2];
bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
bool R2Clicked;
uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
uint8_t writeBuf[12]; // General purpose buffer for output data uint8_t writeBuf[12]; // General purpose buffer for output data

View file

@ -159,14 +159,6 @@ enum AnalogHatEnum {
RightHatY = 3, RightHatY = 3,
}; };
/** Triggers on Xbox One controller. */
enum TriggerEnum {
/** Left trigger */
LeftTrigger = 0,
/** Right trigger */
RightTrigger = 1,
};
/** /**
* Sensors inside the Sixaxis Dualshock 3, Move controller and PS4 controller. * Sensors inside the Sixaxis Dualshock 3, Move controller and PS4 controller.
* <B>Note:</B> that the location is shifted 9 when it's connected via USB on the PS3 controller. * <B>Note:</B> that the location is shifted 9 when it's connected via USB on the PS3 controller.

View file

@ -47,54 +47,51 @@ void loop() {
Serial.println(); Serial.println();
} }
if (Xbox.getTrigger(LeftTrigger) > 0 || Xbox.getTrigger(RightTrigger) > 0) { if (Xbox.getButtonPress(L2) > 0 || Xbox.getButtonPress(R2) > 0) {
if (Xbox.getTrigger(LeftTrigger) > 0) { if (Xbox.getButtonPress(L2) > 0) {
Serial.print(F("LeftTrigger: ")); Serial.print(F("L2: "));
Serial.print(Xbox.getTrigger(LeftTrigger)); Serial.print(Xbox.getButtonPress(L2));
Serial.print("\t"); Serial.print("\t");
} }
if (Xbox.getTrigger(RightTrigger) > 0) { if (Xbox.getButtonPress(R2) > 0) {
Serial.print(F("RightTrigger: ")); Serial.print(F("R2: "));
Serial.print(Xbox.getTrigger(RightTrigger)); Serial.print(Xbox.getButtonPress(R2));
Serial.print("\t"); Serial.print("\t");
} }
Serial.println(); Serial.println();
} }
if (Xbox.getButtonClick(UP)) { if (Xbox.getButtonClick(UP))
Serial.println(F("Up")); Serial.println(F("Up"));
} if (Xbox.getButtonClick(DOWN))
if (Xbox.getButtonClick(DOWN)) {
Serial.println(F("Down")); Serial.println(F("Down"));
} if (Xbox.getButtonClick(LEFT))
if (Xbox.getButtonClick(LEFT)) {
Serial.println(F("Left")); Serial.println(F("Left"));
} if (Xbox.getButtonClick(RIGHT))
if (Xbox.getButtonClick(RIGHT)) {
Serial.println(F("Right")); Serial.println(F("Right"));
}
if (Xbox.getButtonClick(START)) { if (Xbox.getButtonClick(START))
Serial.println(F("Start")); Serial.println(F("Start"));
} if (Xbox.getButtonClick(BACK))
if (Xbox.getButtonClick(BACK)) {
Serial.println(F("Back")); Serial.println(F("Back"));
} if (Xbox.getButtonClick(XBOX))
if (Xbox.getButtonClick(L3)) Serial.println(F("Xbox"));
Serial.println(F("L3")); if (Xbox.getButtonClick(SYNC))
if (Xbox.getButtonClick(R3)) Serial.println(F("Sync"));
Serial.println(F("R3"));
if (Xbox.getButtonClick(L1)) if (Xbox.getButtonClick(L1))
Serial.println(F("L1")); Serial.println(F("L1"));
if (Xbox.getButtonClick(R1)) if (Xbox.getButtonClick(R1))
Serial.println(F("R1")); Serial.println(F("R1"));
if (Xbox.getButtonClick(XBOX)) { if (Xbox.getButtonClick(L2))
Serial.println(F("Xbox")); Serial.println(F("L2"));
} if (Xbox.getButtonClick(R2))
if (Xbox.getButtonClick(SYNC)) { Serial.println(F("R2"));
Serial.println(F("Sync")); if (Xbox.getButtonClick(L3))
} Serial.println(F("L3"));
if (Xbox.getButtonClick(R3))
Serial.println(F("R3"));
if (Xbox.getButtonClick(A)) if (Xbox.getButtonClick(A))
Serial.println(F("A")); Serial.println(F("A"));

View file

@ -140,9 +140,6 @@ LeftHatY LITERAL1
RightHatX LITERAL1 RightHatX LITERAL1
RightHatY LITERAL1 RightHatY LITERAL1
LeftTrigger LITERAL1
RightTrigger LITERAL1
aX LITERAL1 aX LITERAL1
aY LITERAL1 aY LITERAL1
aZ LITERAL1 aZ LITERAL1
@ -364,4 +361,4 @@ RED LITERAL1
YELLOW LITERAL1 YELLOW LITERAL1
GREEN LITERAL1 GREEN LITERAL1
ORANGE LITERAL1 ORANGE LITERAL1
BLUE LITERAL1 BLUE LITERAL1