Controller is default 0

This will ensure compatible with the other controllers
This commit is contained in:
Kristian Sloth Lauszus 2013-05-16 22:41:52 +02:00
parent 1a362c5310
commit 216fce8d95
3 changed files with 78 additions and 78 deletions

View file

@ -325,7 +325,7 @@ void XBOXRECV::readReport(uint8_t controller) {
case 3: led = LED4; case 3: led = LED4;
break; break;
} }
setLedOn(controller, led); setLedOn(led, controller);
} }
#ifdef DEBUG #ifdef DEBUG
else else
@ -381,7 +381,7 @@ void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) { //Uncomment "#d
#endif #endif
} }
uint8_t XBOXRECV::getButtonPress(uint8_t controller, Button b) { uint8_t XBOXRECV::getButtonPress(Button b, uint8_t controller) {
if (b == L2) // These are analog buttons if (b == L2) // These are analog buttons
return (uint8_t)(ButtonState[controller] >> 8); return (uint8_t)(ButtonState[controller] >> 8);
else if (b == R2) else if (b == R2)
@ -389,7 +389,7 @@ uint8_t XBOXRECV::getButtonPress(uint8_t controller, Button b) {
return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16)); return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16));
} }
bool XBOXRECV::getButtonClick(uint8_t controller, Button b) { bool XBOXRECV::getButtonClick(Button b, uint8_t controller) {
if (b == L2) { if (b == L2) {
if (L2Clicked[controller]) { if (L2Clicked[controller]) {
L2Clicked[controller] = false; L2Clicked[controller] = false;
@ -409,7 +409,7 @@ bool XBOXRECV::getButtonClick(uint8_t controller, Button b) {
return click; return click;
} }
int16_t XBOXRECV::getAnalogHat(uint8_t controller, AnalogHat a) { int16_t XBOXRECV::getAnalogHat(AnalogHat a, uint8_t controller) {
return hatValue[controller][a]; return hatValue[controller][a];
} }
@ -462,7 +462,7 @@ void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) {
#endif #endif
} }
void XBOXRECV::setLedRaw(uint8_t controller, uint8_t value) { void XBOXRECV::setLedRaw(uint8_t value, uint8_t controller) {
writeBuf[0] = 0x00; writeBuf[0] = 0x00;
writeBuf[1] = 0x00; writeBuf[1] = 0x00;
writeBuf[2] = 0x08; writeBuf[2] = 0x08;
@ -471,16 +471,16 @@ void XBOXRECV::setLedRaw(uint8_t controller, uint8_t value) {
XboxCommand(controller, writeBuf, 4); XboxCommand(controller, writeBuf, 4);
} }
void XBOXRECV::setLedOn(uint8_t controller, LED led) { void XBOXRECV::setLedOn(LED led, uint8_t controller) {
if (led != ALL) // All LEDs can't be on a the same time if (led != ALL) // All LEDs can't be on a the same time
setLedRaw(controller, (pgm_read_byte(&XBOXLEDS[(uint8_t)led])) + 4); setLedRaw(controller, (pgm_read_byte(&XBOXLEDS[(uint8_t)led])) + 4);
} }
void XBOXRECV::setLedBlink(uint8_t controller, LED led) { void XBOXRECV::setLedBlink(LED led, uint8_t controller) {
setLedRaw(controller, pgm_read_byte(&XBOXLEDS[(uint8_t)led])); setLedRaw(controller, pgm_read_byte(&XBOXLEDS[(uint8_t)led]));
} }
void XBOXRECV::setLedMode(uint8_t controller, LEDMode ledMode) { // This function is used to do some speciel LED stuff the controller supports void XBOXRECV::setLedMode(LEDMode ledMode, uint8_t controller) { // This function is used to do some speciel LED stuff the controller supports
setLedRaw(controller, (uint8_t)ledMode); setLedRaw(controller, (uint8_t)ledMode);
} }
@ -510,7 +510,7 @@ void XBOXRECV::checkStatus() {
} }
} }
void XBOXRECV::setRumbleOn(uint8_t controller, uint8_t lValue, uint8_t rValue) { void XBOXRECV::setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller) {
writeBuf[0] = 0x00; writeBuf[0] = 0x00;
writeBuf[1] = 0x01; writeBuf[1] = 0x01;
writeBuf[2] = 0x0f; writeBuf[2] = 0x0f;

View file

@ -113,93 +113,93 @@ public:
* *
* So you instance if you need to increase a variable once you would use getButtonClick(uint8_t controller, Button b), * So you instance if you need to increase a variable once you would use getButtonClick(uint8_t controller, Button b),
* but if you need to drive a robot forward you would use getButtonPress(uint8_t controller, Button b). * but if you need to drive a robot forward you would use getButtonPress(uint8_t controller, Button b).
* @param controller The controller to read from.
* @param b ::Button to read. * @param b ::Button to read.
* @param controller The controller to read from. Default to 0.
* @return getButtonClick(uint8_t controller, Button b) will return a bool, but getButtonPress(uint8_t controller, Button b) * @return getButtonClick(uint8_t controller, Button b) will return a bool, but getButtonPress(uint8_t controller, Button b)
* will return a byte if reading ::L2 or ::R2. * will return a byte if reading ::L2 or ::R2.
*/ */
uint8_t getButtonPress(uint8_t controller, Button b); uint8_t getButtonPress(Button b, uint8_t controller = 0);
bool getButtonClick(uint8_t controller, Button b); bool getButtonClick(Button b, uint8_t controller = 0);
/**@}*/ /**@}*/
/** @name Xbox Controller functions */ /** @name Xbox Controller functions */
/** /**
* Return the analog value from the joysticks on the controller. * Return the analog value from the joysticks on the controller.
* @param controller The controller to read from.
* @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY. * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
* @param controller The controller to read from. Default to 0.
* @return Returns a signed 16-bit integer. * @return Returns a signed 16-bit integer.
*/ */
int16_t getAnalogHat(uint8_t controller, AnalogHat a); int16_t getAnalogHat(AnalogHat a, uint8_t controller = 0);
/** /**
* Turn rumble off and all the LEDs on the specific controller. * Turn rumble off and all the LEDs on the specific controller.
* @param controller The controller to write to. * @param controller The controller to write to. Default to 0.
*/ */
void setAllOff(uint8_t controller) { void setAllOff(uint8_t controller = 0) {
setRumbleOn(controller, 0, 0); setRumbleOn(0, 0, controller);
setLedOff(controller); setLedOff(controller);
}; };
/** /**
* Turn rumble off the specific controller. * Turn rumble off the specific controller.
* @param controller The controller to write to. * @param controller The controller to write to. Default to 0.
*/ */
void setRumbleOff(uint8_t controller) { void setRumbleOff(uint8_t controller = 0) {
setRumbleOn(controller, 0, 0); setRumbleOn(0, 0, controller);
}; };
/** /**
* Turn rumble on. * Turn rumble on.
* @param controller The controller to write to.
* @param lValue Left motor (big weight) inside the controller. * @param lValue Left motor (big weight) inside the controller.
* @param rValue Right motor (small weight) inside the controller. * @param rValue Right motor (small weight) inside the controller.
* @param controller The controller to write to. Default to 0.
*/ */
void setRumbleOn(uint8_t controller, uint8_t lValue, uint8_t rValue); void setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller = 0);
/** /**
* Set LED value. Without using the ::LED or ::LEDMode enum. * Set LED value. Without using the ::LED or ::LEDMode enum.
* @param controller The controller to write to.
* @param value See: * @param value See:
* setLedOff(uint8_t controller), setLedOn(uint8_t controller, LED l), * setLedOff(uint8_t controller), setLedOn(uint8_t controller, LED l),
* setLedBlink(uint8_t controller, LED l), and setLedMode(uint8_t controller, LEDMode lm). * setLedBlink(uint8_t controller, LED l), and setLedMode(uint8_t controller, LEDMode lm).
* @param controller The controller to write to. Default to 0.
*/ */
void setLedRaw(uint8_t controller, uint8_t value); void setLedRaw(uint8_t value, uint8_t controller = 0);
/** /**
* Turn all LEDs off the specific controller. * Turn all LEDs off the specific controller.
* @param controller The controller to write to. * @param controller The controller to write to. Default to 0.
*/ */
void setLedOff(uint8_t controller) { void setLedOff(uint8_t controller = 0) {
setLedRaw(controller, 0); setLedRaw(0, controller);
}; };
/** /**
* Turn on a LED by using the ::LED enum. * Turn on a LED by using the ::LED enum.
* @param controller The controller to write to.
* @param l ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. * @param l ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
* @param controller The controller to write to. Default to 0.
*/ */
void setLedOn(uint8_t controller, LED l); void setLedOn(LED l, uint8_t controller = 0);
/** /**
* Turn on a LED by using the ::LED enum. * Turn on a LED by using the ::LED enum.
* @param controller The controller to write to.
* @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
* @param controller The controller to write to. Default to 0.
*/ */
void setLedBlink(uint8_t controller, LED l); void setLedBlink(LED l, uint8_t controller = 0);
/** /**
* Used to set special LED modes supported by the Xbox controller. * Used to set special LED modes supported by the Xbox controller.
* @param controller The controller to write to.
* @param lm See ::LEDMode. * @param lm See ::LEDMode.
* @param controller The controller to write to. Default to 0.
*/ */
void setLedMode(uint8_t controller, LEDMode lm); void setLedMode(LEDMode lm, uint8_t controller = 0);
/** /**
* Used to get the battery level from the controller. * Used to get the battery level from the controller.
* @param controller The controller to read from. * @param controller The controller to read from. Default to 0.
* @return Returns the battery level as an integer in the range of 0-3. * @return Returns the battery level as an integer in the range of 0-3.
*/ */
uint8_t getBatteryLevel(uint8_t controller); uint8_t getBatteryLevel(uint8_t controller = 0);
/** /**
* Used to check if a button has changed. * Used to check if a button has changed.
* @param controller The controller to read from. * @param controller The controller to read from. Default to 0.
* @return True if a button has changed. * @return True if a button has changed.
*/ */
bool buttonChanged(uint8_t controller); bool buttonChanged(uint8_t controller = 0);
/**@}*/ /**@}*/
/** True if a wireless receiver is connected. */ /** True if a wireless receiver is connected. */

View file

@ -22,86 +22,86 @@ void loop() {
if(Xbox.XboxReceiverConnected) { if(Xbox.XboxReceiverConnected) {
for(uint8_t i=0;i<4;i++) { for(uint8_t i=0;i<4;i++) {
if(Xbox.Xbox360Connected[i]) { if(Xbox.Xbox360Connected[i]) {
if(Xbox.getButtonPress(i,L2) || Xbox.getButtonPress(i,R2)) { if(Xbox.getButtonPress(L2,i) || Xbox.getButtonPress(R2,i)) {
Serial.print("L2: "); Serial.print("L2: ");
Serial.print(Xbox.getButtonPress(i,L2)); Serial.print(Xbox.getButtonPress(L2,i));
Serial.print("\tR2: "); Serial.print("\tR2: ");
Serial.println(Xbox.getButtonPress(i,R2)); Serial.println(Xbox.getButtonPress(R2,i));
Xbox.setRumbleOn(i,Xbox.getButtonPress(i,L2),Xbox.getButtonPress(i,R2)); Xbox.setRumbleOn(Xbox.getButtonPress(L2,i),Xbox.getButtonPress(R2,i));
} }
if(Xbox.getAnalogHat(i,LeftHatX) > 7500 || Xbox.getAnalogHat(i,LeftHatX) < -7500 || Xbox.getAnalogHat(i,LeftHatY) > 7500 || Xbox.getAnalogHat(i,LeftHatY) < -7500 || Xbox.getAnalogHat(i,RightHatX) > 7500 || Xbox.getAnalogHat(i,RightHatX) < -7500 || Xbox.getAnalogHat(i,RightHatY) > 7500 || Xbox.getAnalogHat(i,RightHatY) < -7500) { if(Xbox.getAnalogHat(LeftHatX,i) > 7500 || Xbox.getAnalogHat(LeftHatX,i) < -7500 || Xbox.getAnalogHat(LeftHatY,i) > 7500 || Xbox.getAnalogHat(LeftHatY,i) < -7500 || Xbox.getAnalogHat(RightHatX,i) > 7500 || Xbox.getAnalogHat(RightHatX,i) < -7500 || Xbox.getAnalogHat(RightHatY,i) > 7500 || Xbox.getAnalogHat(RightHatY,i) < -7500) {
if(Xbox.getAnalogHat(i,LeftHatX) > 7500 || Xbox.getAnalogHat(i,LeftHatX) < -7500) { if(Xbox.getAnalogHat(LeftHatX,i) > 7500 || Xbox.getAnalogHat(LeftHatX,i) < -7500) {
Serial.print(F("LeftHatX: ")); Serial.print(F("LeftHatX: "));
Serial.print(Xbox.getAnalogHat(i,LeftHatX)); Serial.print(Xbox.getAnalogHat(LeftHatX,i));
Serial.print("\t"); Serial.print("\t");
} }
if(Xbox.getAnalogHat(i,LeftHatY) > 7500 || Xbox.getAnalogHat(i,LeftHatY) < -7500) { if(Xbox.getAnalogHat(LeftHatY,i) > 7500 || Xbox.getAnalogHat(LeftHatY,i) < -7500) {
Serial.print(F("LeftHatY: ")); Serial.print(F("LeftHatY: "));
Serial.print(Xbox.getAnalogHat(i,LeftHatY)); Serial.print(Xbox.getAnalogHat(LeftHatY,i));
Serial.print("\t"); Serial.print("\t");
} }
if(Xbox.getAnalogHat(i,RightHatX) > 7500 || Xbox.getAnalogHat(i,RightHatX) < -7500) { if(Xbox.getAnalogHat(RightHatX,i) > 7500 || Xbox.getAnalogHat(RightHatX,i) < -7500) {
Serial.print(F("RightHatX: ")); Serial.print(F("RightHatX: "));
Serial.print(Xbox.getAnalogHat(i,RightHatX)); Serial.print(Xbox.getAnalogHat(RightHatX,i));
Serial.print("\t"); Serial.print("\t");
} }
if(Xbox.getAnalogHat(i,RightHatY) > 7500 || Xbox.getAnalogHat(i,RightHatY) < -7500) { if(Xbox.getAnalogHat(RightHatY,i) > 7500 || Xbox.getAnalogHat(RightHatY,i) < -7500) {
Serial.print(F("RightHatY: ")); Serial.print(F("RightHatY: "));
Serial.print(Xbox.getAnalogHat(i,RightHatY)); Serial.print(Xbox.getAnalogHat(RightHatY,i));
} }
Serial.println(); Serial.println();
} }
if(Xbox.getButtonClick(i,UP)) { if(Xbox.getButtonClick(UP,i)) {
Xbox.setLedOn(i,LED1); Xbox.setLedOn(LED1,i);
Serial.println(F("Up")); Serial.println(F("Up"));
} }
if(Xbox.getButtonClick(i,DOWN)) { if(Xbox.getButtonClick(DOWN,i)) {
Xbox.setLedOn(i,LED4); Xbox.setLedOn(LED4,i);
Serial.println(F("Down")); Serial.println(F("Down"));
} }
if(Xbox.getButtonClick(i,LEFT)) { if(Xbox.getButtonClick(LEFT,i)) {
Xbox.setLedOn(i,LED3); Xbox.setLedOn(LED3,i);
Serial.println(F("Left")); Serial.println(F("Left"));
} }
if(Xbox.getButtonClick(i,RIGHT)) { if(Xbox.getButtonClick(RIGHT,i)) {
Xbox.setLedOn(i,LED2); Xbox.setLedOn(LED2,i);
Serial.println(F("Right")); Serial.println(F("Right"));
} }
if(Xbox.getButtonClick(i,START)) { if(Xbox.getButtonClick(START,i)) {
Xbox.setLedMode(i,ALTERNATING); Xbox.setLedMode(ALTERNATING,i);
Serial.println(F("Start")); Serial.println(F("Start"));
} }
if(Xbox.getButtonClick(i,BACK)) { if(Xbox.getButtonClick(BACK,i)) {
Xbox.setLedBlink(i,ALL); Xbox.setLedBlink(ALL,i);
Serial.println(F("Back")); Serial.println(F("Back"));
} }
if(Xbox.getButtonClick(i,L3)) if(Xbox.getButtonClick(L3,i))
Serial.println(F("L3")); Serial.println(F("L3"));
if(Xbox.getButtonClick(i,R3)) if(Xbox.getButtonClick(R3,i))
Serial.println(F("R3")); Serial.println(F("R3"));
if(Xbox.getButtonClick(i,L1)) if(Xbox.getButtonClick(L1,i))
Serial.println(F("L1")); Serial.println(F("L1"));
if(Xbox.getButtonClick(i,R1)) if(Xbox.getButtonClick(R1,i))
Serial.println(F("R1")); Serial.println(F("R1"));
if(Xbox.getButtonClick(i,XBOX)) { if(Xbox.getButtonClick(XBOX,i)) {
Xbox.setLedMode(i,ROTATING); Xbox.setLedMode(ROTATING,i);
Serial.print(F("Xbox (Battery: ")); Serial.print(F("Xbox (Battery: "));
Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3
Serial.println(F(")")); Serial.println(F(")"));
} }
if(Xbox.getButtonClick(i,SYNC)) if(Xbox.getButtonClick(SYNC,i))
Serial.println(F("Sync")); Serial.println(F("Sync"));
if(Xbox.getButtonClick(i,A)) if(Xbox.getButtonClick(A,i))
Serial.println(F("A")); Serial.println(F("A"));
if(Xbox.getButtonClick(i,B)) if(Xbox.getButtonClick(B,i))
Serial.println(F("B")); Serial.println(F("B"));
if(Xbox.getButtonClick(i,X)) if(Xbox.getButtonClick(X,i))
Serial.println(F("X")); Serial.println(F("X"));
if(Xbox.getButtonClick(i,Y)) if(Xbox.getButtonClick(Y,i))
Serial.println(F("Y")); Serial.println(F("Y"));
} }
} }