From 52858d502b802551d95c34ae171ba50b48c9a5e0 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Mon, 11 Nov 2013 00:38:56 +0100 Subject: [PATCH] Added delay before bus reset and renamed some variables and cleanup example --- XBOXRECV.cpp | 10 ++- XBOXRECV.h | 2 +- examples/Xbox/XBOXRECV/XBOXRECV.ino | 103 ++++++++++++++-------------- 3 files changed, 59 insertions(+), 56 deletions(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index e0bcc55f..dfa6b2a4 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -18,7 +18,7 @@ */ #include "XBOXRECV.h" -// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h +// To enable serial debugging see "settings.h" //#define EXTRADEBUG // Uncomment to get even more debugging data //#define PRINTREPORT // Uncomment to print the report send by the Xbox 360 Controller @@ -106,6 +106,8 @@ uint8_t XBOXRECV::ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) { epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0; // Extract Max Packet Size from device descriptor epInfo[1].epAddr = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations; // Steal and abuse from epInfo structure to save memory + delay(20); // Wait a little before resetting device + return USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET; /* Diagnostic messages */ @@ -254,6 +256,7 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) { #endif XboxReceiverConnected = true; bPollEnable = true; + checkStatusTimer = 0; // Reset timer return 0; // Successful configuration /* Diagnostic messages */ @@ -298,10 +301,11 @@ uint8_t XBOXRECV::Release() { uint8_t XBOXRECV::Poll() { if (!bPollEnable) return 0; - if (!timer || ((millis() - timer) > 3000)) { // Run checkStatus every 3 seconds - timer = millis(); + if (!checkStatusTimer || ((millis() - checkStatusTimer) > 3000)) { // Run checkStatus every 3 seconds + checkStatusTimer = millis(); checkStatus(); } + uint8_t inputPipe; uint16_t bufferSize; for (uint8_t i = 0; i < 4; i++) { diff --git a/XBOXRECV.h b/XBOXRECV.h index 02e4ef07..bb0b53d7 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -259,7 +259,7 @@ private: bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not bool R2Clicked[4]; - unsigned long timer; // Timing for checkStatus() signals + uint32_t checkStatusTimer; // Timing for checkStatus() signals uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data uint8_t writeBuf[7]; // General purpose buffer for output data diff --git a/examples/Xbox/XBOXRECV/XBOXRECV.ino b/examples/Xbox/XBOXRECV/XBOXRECV.ino index 6bd33b0e..2143098e 100644 --- a/examples/Xbox/XBOXRECV/XBOXRECV.ino +++ b/examples/Xbox/XBOXRECV/XBOXRECV.ino @@ -1,7 +1,7 @@ /* Example sketch for the Xbox Wireless Reciver library - developed by Kristian Lauszus It supports up to four controllers wirelessly - For more information see the blog post: http://blog.tkjelectronics.dk/2012/12/xbox-360-receiver-added-to-the-usb-host-library/ or + For more information see the blog post: http://blog.tkjelectronics.dk/2012/12/xbox-360-receiver-added-to-the-usb-host-library/ or send me an e-mail: kristianl@tkjelectronics.com */ @@ -15,81 +15,81 @@ void setup() { while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection if (Usb.Init() == -1) { Serial.print(F("\r\nOSC did not start")); - while(1); //halt + while (1); //halt } Serial.print(F("\r\nXbox Wireless Receiver Library Started")); } void loop() { Usb.Task(); - if(Xbox.XboxReceiverConnected) { - for(uint8_t i=0;i<4;i++) { - if(Xbox.Xbox360Connected[i]) { - if(Xbox.getButtonPress(L2,i) || Xbox.getButtonPress(R2,i)) { + if (Xbox.XboxReceiverConnected) { + for (uint8_t i = 0; i < 4; i++) { + if (Xbox.Xbox360Connected[i]) { + if (Xbox.getButtonPress(L2, i) || Xbox.getButtonPress(R2, i)) { Serial.print("L2: "); - Serial.print(Xbox.getButtonPress(L2,i)); + Serial.print(Xbox.getButtonPress(L2, i)); Serial.print("\tR2: "); - Serial.println(Xbox.getButtonPress(R2,i)); - Xbox.setRumbleOn(Xbox.getButtonPress(L2,i),Xbox.getButtonPress(R2,i),i); + Serial.println(Xbox.getButtonPress(R2, i)); + Xbox.setRumbleOn(Xbox.getButtonPress(L2, i), Xbox.getButtonPress(R2, i), i); } - 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(LeftHatX,i) > 7500 || Xbox.getAnalogHat(LeftHatX,i) < -7500) { - Serial.print(F("LeftHatX: ")); - Serial.print(Xbox.getAnalogHat(LeftHatX,i)); + 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(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500) { + Serial.print(F("LeftHatX: ")); + Serial.print(Xbox.getAnalogHat(LeftHatX, i)); Serial.print("\t"); - } - if(Xbox.getAnalogHat(LeftHatY,i) > 7500 || Xbox.getAnalogHat(LeftHatY,i) < -7500) { - Serial.print(F("LeftHatY: ")); - Serial.print(Xbox.getAnalogHat(LeftHatY,i)); + } + if (Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500) { + Serial.print(F("LeftHatY: ")); + Serial.print(Xbox.getAnalogHat(LeftHatY, i)); Serial.print("\t"); - } - if(Xbox.getAnalogHat(RightHatX,i) > 7500 || Xbox.getAnalogHat(RightHatX,i) < -7500) { - Serial.print(F("RightHatX: ")); - Serial.print(Xbox.getAnalogHat(RightHatX,i)); - Serial.print("\t"); - } - if(Xbox.getAnalogHat(RightHatY,i) > 7500 || Xbox.getAnalogHat(RightHatY,i) < -7500) { - Serial.print(F("RightHatY: ")); - Serial.print(Xbox.getAnalogHat(RightHatY,i)); + } + if (Xbox.getAnalogHat(RightHatX, i) > 7500 || Xbox.getAnalogHat(RightHatX, i) < -7500) { + Serial.print(F("RightHatX: ")); + Serial.print(Xbox.getAnalogHat(RightHatX, i)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500) { + Serial.print(F("RightHatY: ")); + Serial.print(Xbox.getAnalogHat(RightHatY, i)); } Serial.println(); } - if(Xbox.getButtonClick(UP,i)) { - Xbox.setLedOn(LED1,i); + if (Xbox.getButtonClick(UP, i)) { + Xbox.setLedOn(LED1, i); Serial.println(F("Up")); - } - if(Xbox.getButtonClick(DOWN,i)) { - Xbox.setLedOn(LED4,i); + } + if (Xbox.getButtonClick(DOWN, i)) { + Xbox.setLedOn(LED4, i); Serial.println(F("Down")); } - if(Xbox.getButtonClick(LEFT,i)) { - Xbox.setLedOn(LED3,i); + if (Xbox.getButtonClick(LEFT, i)) { + Xbox.setLedOn(LED3, i); Serial.println(F("Left")); } - if(Xbox.getButtonClick(RIGHT,i)) { - Xbox.setLedOn(LED2,i); + if (Xbox.getButtonClick(RIGHT, i)) { + Xbox.setLedOn(LED2, i); Serial.println(F("Right")); - } + } - if(Xbox.getButtonClick(START,i)) { - Xbox.setLedMode(ALTERNATING,i); + if (Xbox.getButtonClick(START, i)) { + Xbox.setLedMode(ALTERNATING, i); Serial.println(F("Start")); } - if(Xbox.getButtonClick(BACK,i)) { - Xbox.setLedBlink(ALL,i); + if (Xbox.getButtonClick(BACK, i)) { + Xbox.setLedBlink(ALL, i); Serial.println(F("Back")); } - if(Xbox.getButtonClick(L3,i)) + if (Xbox.getButtonClick(L3, i)) Serial.println(F("L3")); - if(Xbox.getButtonClick(R3,i)) + if (Xbox.getButtonClick(R3, i)) Serial.println(F("R3")); - if(Xbox.getButtonClick(L1,i)) + if (Xbox.getButtonClick(L1, i)) Serial.println(F("L1")); - if(Xbox.getButtonClick(R1,i)) + if (Xbox.getButtonClick(R1, i)) Serial.println(F("R1")); - if(Xbox.getButtonClick(XBOX,i)) { - Xbox.setLedMode(ROTATING,i); + if (Xbox.getButtonClick(XBOX, i)) { + Xbox.setLedMode(ROTATING, i); Serial.print(F("Xbox (Battery: ")); Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 Serial.println(F(")")); @@ -97,16 +97,15 @@ void loop() { if(Xbox.getButtonClick(SYNC,i)) Serial.println(F("Sync")); - if(Xbox.getButtonClick(A,i)) + if (Xbox.getButtonClick(A, i)) Serial.println(F("A")); - if(Xbox.getButtonClick(B,i)) + if (Xbox.getButtonClick(B, i)) Serial.println(F("B")); - if(Xbox.getButtonClick(X,i)) + if (Xbox.getButtonClick(X, i)) Serial.println(F("X")); - if(Xbox.getButtonClick(Y,i)) + if (Xbox.getButtonClick(Y, i)) Serial.println(F("Y")); } } - } - delay(1); + } }