diff --git a/BTD.cpp b/BTD.cpp index 7cccbe8e..85a75183 100644 --- a/BTD.cpp +++ b/BTD.cpp @@ -497,6 +497,7 @@ void BTD::HCI_task() { case HCI_RESET_STATE: hci_counter++; if (hci_cmd_complete) { + hci_counter = 0; #ifdef DEBUG Notify(PSTR("\r\nHCI Reset complete")); #endif @@ -661,26 +662,17 @@ void BTD::HCI_task() { } PrintHex(disc_bdaddr[0]); #endif - hci_write_scan_disable(); - hci_state = HCI_DISABLE_SCAN_STATE; - } - break; - - case HCI_DISABLE_SCAN_STATE: - if (hci_cmd_complete) { -#ifdef DEBUG - Notify(PSTR("\r\nScan Disabled")); -#endif + l2capConnectionClaimed = false; hci_event_flag = 0; hci_state = HCI_DONE_STATE; } break; - + case HCI_DONE_STATE: hci_counter++; - if (hci_counter > 250) { // Wait until we have looped 250 times to make sure that the L2CAP connection has been started - hci_state = HCI_SCANNING_STATE; - l2capConnectionClaimed = false; + if (hci_counter > 1000) { // Wait until we have looped 1000 times to make sure that the L2CAP connection has been started + hci_counter = 0; + hci_state = HCI_SCANNING_STATE; } break; diff --git a/PS3BT.cpp b/PS3BT.cpp index 65697af5..544b71fc 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -60,20 +60,13 @@ pBtd(p) // pointer to USB class instance - mandatory Reset(); } -bool PS3BT::getButton(Button b) { - if (l2capinbuf == NULL) - return false; - if(PS3MoveConnected) { - if((l2capinbuf[((uint16_t)b >> 8)-1] & ((uint8_t)b & 0xff))) // All the buttons locations are shifted one back on the Move controller - return true; - else - return false; - } else { - if((l2capinbuf[(uint16_t)b >> 8] & ((uint8_t)b & 0xff))) - return true; - else - return false; - } +bool PS3BT::getButtonPress(Button b) { + return (ButtonState & (uint32_t)b); +} +bool PS3BT::getButtonClick(Button b) { + bool click = (ButtonClickState & (uint32_t)b); + ButtonClickState &= ~((uint32_t)b); // clear "click" event + return click; } uint8_t PS3BT::getAnalogButton(AnalogButton a) { if (l2capinbuf == NULL) @@ -90,27 +83,18 @@ int16_t PS3BT::getSensor(Sensor a) { return 0; if (a == aX || a == aY || a == aZ || a == gZ) return ((l2capinbuf[(uint16_t)a] << 8) | l2capinbuf[(uint16_t)a + 1]); - else if (a == mXmove || a == mYmove || a == mZmove) // These are all 12-bits long - { - // Might not be correct, haven't tested it yet - /*if (a == mXmove) - return ((l2capinbuf[(uint16_t)a + 1] << 0x04) | (l2capinbuf[(uint16_t)a] << 0x0C)); - else if (a == mYmove) - return ((l2capinbuf[(uint16_t)a + 1] & 0xF0) | (l2capinbuf[(uint16_t)a] << 0x08)); - else if (a == mZmove) - return ((l2capinbuf[(uint16_t)a + 1] << 0x0F) | (l2capinbuf[(uint16_t)a] << 0x0C)); - */ + else if (a == mXmove || a == mYmove || a == mZmove) { // These are all 12-bits long if (a == mXmove || a == mYmove) return (((l2capinbuf[(uint16_t)a] & 0x0F) << 8) | (l2capinbuf[(uint16_t)a + 1])); else // mZmove - return ((l2capinbuf[(uint16_t)a] << 4) | (l2capinbuf[(uint16_t)a + 1] >> 4)); + return ((l2capinbuf[(uint16_t)a] << 4) | (l2capinbuf[(uint16_t)a + 1] & 0xF0 ) >> 4); } else if (a == tempMove) // The tempearature is 12 bits long too return ((l2capinbuf[(uint16_t)a] << 4) | ((l2capinbuf[(uint16_t)a + 1] & 0xF0) >> 4)); else // aXmove, aYmove, aZmove, gXmove, gYmove and gZmove return (l2capinbuf[(uint16_t)a] | (l2capinbuf[(uint16_t)a + 1] << 8)); } -double PS3BT::getAngle(Angle a) { +double PS3BT::getAngle(Angle a) { double accXval; double accYval; double accZval; @@ -121,24 +105,48 @@ double PS3BT::getAngle(Angle a) { accXval = -((double)getSensor(aX)-zeroG); accYval = -((double)getSensor(aY)-zeroG); accZval = -((double)getSensor(aZ)-zeroG); - } else if(PS3MoveConnected) { + } else if(PS3MoveConnected) { // It's a Kionix KXSC4 inside the Motion controller - const uint16_t zeroG = 0x8000; + const uint16_t zeroG = 0x8000; accXval = -(int16_t)(getSensor(aXmove)-zeroG); accYval = (int16_t)(getSensor(aYmove)-zeroG); - accZval = (int16_t)(getSensor(aZmove)-zeroG); + accZval = (int16_t)(getSensor(aZmove)-zeroG); } // Convert to 360 degrees resolution // atan2 outputs the value of -π to π (radians) - // We are then converting it to 0 to 2π and then to degrees - if (a == Pitch) { + // We are then converting it to 0 to 2π and then to degrees + if (a == Pitch) { double angle = (atan2(accYval,accZval)+PI)*RAD_TO_DEG; return angle; } else { double angle = (atan2(accXval,accZval)+PI)*RAD_TO_DEG; return angle; - } + } +} +double PS3BT::get9DOFValues(Sensor a) { // Thanks to Manfred Piendl + int16_t value = getSensor(a); + if (a == mXmove || a == mYmove || a == mZmove) { + if (value > 2047) + value -= 0x1000; + return (double)value/3.2; // unit: muT = 10^(-6) Tesla + } + else if (a == aXmove || a == aYmove || a == aZmove) { + if (value < 0) + value += 0x8000; + else + value -= 0x8000; + return (double)value/442.0; // unit: m/(s^2) + } + else if (a == gXmove || a == gYmove || a == gZmove) { + if (value < 0) + value += 0x8000; + else + value -= 0x8000; + return (double)value/9.6; // unit: deg/s + } + else + return 0; } String PS3BT::getTemperature() { if(PS3MoveConnected) { @@ -231,7 +239,7 @@ void PS3BT::disconnect() { // Use this void to disconnect any of the controllers void PS3BT::ACLData(uint8_t* ACLData) { if(!pBtd->l2capConnectionClaimed && !PS3Connected && !PS3MoveConnected && !PS3NavigationConnected) { if (ACLData[8] == L2CAP_CMD_CONNECTION_REQUEST) { - if((ACLData[12] | (ACLData[13] << 8)) == HID_CTRL_PSM) { + if((ACLData[12] | (ACLData[13] << 8)) == HID_CTRL_PSM) { pBtd->l2capConnectionClaimed = true; // Claim that the incoming connection belongs to this service hci_handle = pBtd->hci_handle; // Store the HCI Handle for the connection l2cap_state = L2CAP_WAIT; @@ -367,21 +375,9 @@ void PS3BT::ACLData(uint8_t* ACLData) { //PrintHex(ButtonState); if(ButtonState != OldButtonState) { - buttonChanged = true; - if(ButtonState != 0x00) { - buttonPressed = true; - buttonReleased = false; - } else { - buttonPressed = false; - buttonReleased = true; - } - } - else { - buttonChanged = false; - buttonPressed = false; - buttonReleased = false; - } - OldButtonState = ButtonState; + ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable + OldButtonState = ButtonState; + } #ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers for(uint8_t i = 10; i < 58;i++) { @@ -583,22 +579,17 @@ void PS3BT::setRumbleOn(Rumble mode) { * 5 - duration_left * 6 - power_left */ - if ((mode & 0x30) > 0) - { + if ((mode & 0x30) > 0) { HIDBuffer[3] = 0xfe; - HIDBuffer[5] = 0xfe; - - if (mode == RumbleHigh) - { + HIDBuffer[5] = 0xfe; + if (mode == RumbleHigh) { HIDBuffer[4] = 0;//low mode off HIDBuffer[6] = 0xff;//high mode on } - else - { + else { HIDBuffer[4] = 0xff;//low mode on HIDBuffer[6] = 0;//high mode off - } - + } HID_Command(HIDBuffer, HID_BUFFERSIZE); } } diff --git a/PS3BT.h b/PS3BT.h index 806193df..54edad59 100644 --- a/PS3BT.h +++ b/PS3BT.h @@ -19,6 +19,7 @@ #define _ps3bt_h_ #include "BTD.h" +#include "PS3Enums.h" #define HID_BUFFERSIZE 50 // size of the buffer for the Playstation Motion Controller #define OUTPUT_REPORT_BUFFER_SIZE 48 //Size of the output report buffer for the controllers @@ -56,159 +57,6 @@ #define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE) #define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) -enum LED { - LED1 = 0x01, - LED2 = 0x02, - LED3 = 0x04, - LED4 = 0x08, - - LED5 = 0x09, - LED6 = 0x0A, - LED7 = 0x0C, - LED8 = 0x0D, - LED9 = 0x0E, - LED10 = 0x0F, -}; -enum Colors { - // Used to set the colors of the move controller - Red = 0xFF0000, // r = 255, g = 0, b = 0 - Green = 0xFF00, // r = 0, g = 255, b = 0 - Blue = 0xFF, // r = 0, g = 0, b = 255 - - Yellow = 0xFFEB04, // r = 255, g = 235, b = 4 - Lightblue = 0xFFFF, // r = 0, g = 255, b = 255 - Purble = 0xFF00FF, // r = 255, g = 0, b = 255 - - White = 0xFFFFFF, // r = 255, g = 255, b = 255 - Off = 0x00, // r = 0, g = 0, b = 0 -}; - -enum Button { - // byte location | bit location - - // Sixaxis Dualshcock 3 & Navigation controller - SELECT = (11 << 8) | 0x01, - L3 = (11 << 8) | 0x02, - R3 = (11 << 8) | 0x04, - START = (11 << 8) | 0x08, - UP = (11 << 8) | 0x10, - RIGHT = (11 << 8) | 0x20, - DOWN = (11 << 8) | 0x40, - LEFT = (11 << 8) | 0x80, - - L2 = (12 << 8) | 0x01, - R2 = (12 << 8) | 0x02, - L1 = (12 << 8) | 0x04, - R1 = (12 << 8) | 0x08, - TRIANGLE = (12 << 8) | 0x10, - CIRCLE = (12 << 8) | 0x20, - CROSS = (12 << 8) | 0x40, - SQUARE = (12 << 8) | 0x80, - - PS = (13 << 8) | 0x01, - - MOVE = (13/*12*/ << 8) | 0x08, // covers 12 bits - we only need to read the top 8 - T = (13/*12*/ << 8) | 0x10, // covers 12 bits - we only need to read the top 8 - - - // These are the true locations for the Move controller, but to make the same syntax for all controllers, it is handled by getButton() -/* - // Playstation Move Controller - SELECT_MOVE = (10 << 8) | 0x01, - START_MOVE = (10 << 8) | 0x08, - - TRIANGLE_MOVE = (11 << 8) | 0x10, - CIRCLE_MOVE = (11 << 8) | 0x20, - CROSS_MOVE = (11 << 8) | 0x40, - SQUARE_MOVE = (11 << 8) | 0x80, - - PS_MOVE = (12 << 8) | 0x01, - MOVE_MOVE = (12 << 8) | 0x08, // covers 12 bits - we only need to read the top 8 - T_MOVE = (12 << 8) | 0x10, // covers 12 bits - we only need to read the top 8 - */ -}; -enum AnalogButton { - //Sixaxis Dualshcock 3 & Navigation controller - UP_ANALOG = 23, - RIGHT_ANALOG = 24, - DOWN_ANALOG = 25, - LEFT_ANALOG = 26, - - L2_ANALOG = 27, - R2_ANALOG = 28, - L1_ANALOG = 29, - R1_ANALOG = 30, - TRIANGLE_ANALOG = 31, - CIRCLE_ANALOG = 32, - CROSS_ANALOG = 33, - SQUARE_ANALOG = 34, - - //Playstation Move Controller - T_ANALOG = 15, // Both at byte 14 (last reading) and byte 15 (current reading) -}; -enum AnalogHat { - LeftHatX = 15, - LeftHatY = 16, - RightHatX = 17, - RightHatY = 18, -}; -enum Sensor { - //Sensors inside the Sixaxis Dualshock 3 controller - aX = 50, - aY = 52, - aZ = 54, - gZ = 56, - - //Sensors inside the move motion controller - it only reads the 2nd frame - aXmove = 28, - aZmove = 30, - aYmove = 32, - - gXmove = 40, - gZmove = 42, - gYmove = 44, - - tempMove = 46, - - mXmove = 47, - mZmove = 49, - mYmove = 50, -}; -enum Angle { - Pitch = 0x01, - Roll = 0x02, -}; -enum Status { - // byte location | bit location - Plugged = (38 << 8) | 0x02, - Unplugged = (38 << 8) | 0x03, - - Charging = (39 << 8) | 0xEE, - NotCharging = (39 << 8) | 0xF1, - Shutdown = (39 << 8) | 0x01, - Dying = (39 << 8) | 0x02, - Low = (39 << 8) | 0x03, - High = (39 << 8) | 0x04, - Full = (39 << 8) | 0x05, - - MoveCharging = (21 << 8) | 0xEE, - MoveNotCharging = (21 << 8) | 0xF1, - MoveShutdown = (21 << 8) | 0x01, - MoveDying = (21 << 8) | 0x02, - MoveLow = (21 << 8) | 0x03, - MoveHigh = (21 << 8) | 0x04, - MoveFull = (21 << 8) | 0x05, - - CableRumble = (40 << 8) | 0x10,//Opperating by USB and rumble is turned on - Cable = (40 << 8) | 0x12,//Opperating by USB and rumble is turned off - BluetoothRumble = (40 << 8) | 0x14,//Opperating by bluetooth and rumble is turned on - Bluetooth = (40 << 8) | 0x16,//Opperating by bluetooth and rumble is turned off -}; -enum Rumble { - RumbleHigh = 0x10, - RumbleLow = 0x20, -}; - class PS3BT : public BluetoothService { public: PS3BT(BTD *pBtd, uint8_t btadr5=0, uint8_t btadr4=0, uint8_t btadr3=0, uint8_t btadr2=0, uint8_t btadr1=0, uint8_t btadr0=0); @@ -219,12 +67,21 @@ public: virtual void Reset(); // Use this to reset the service virtual void disconnect(); // Use this void to disconnect any of the controllers - /* PS3 Controller Commands */ - bool getButton(Button b); + /* PS3 Controller Commands */ + /* + getButtonPress will return true as long as the button is held down + While getButtonClick will only return it once + So you instance if you need to increase a variable once you would use getButtonClick, + but if you need to drive a robot forward you would use getButtonPress + */ + bool getButtonPress(Button b); + bool getButtonClick(Button b); + uint8_t getAnalogButton(AnalogButton a); uint8_t getAnalogHat(AnalogHat a); int16_t getSensor(Sensor a); double getAngle(Angle a); + double get9DOFValues(Sensor a); bool getStatus(Status c); String getStatusString(); String getTemperature(); @@ -246,9 +103,6 @@ public: bool PS3Connected;// Variable used to indicate if the normal playstation controller is successfully connected bool PS3MoveConnected;// Variable used to indicate if the move controller is successfully connected bool PS3NavigationConnected;// Variable used to indicate if the navigation controller is successfully connected - bool buttonChanged;//Indicate if a button has been changed - bool buttonPressed;//Indicate if a button has been pressed - bool buttonReleased;//Indicate if a button has been released private: /* mandatory members */ @@ -268,6 +122,8 @@ private: uint32_t ButtonState; uint32_t OldButtonState; + uint32_t ButtonClickState; + uint32_t timerHID;// timer used see if there has to be a delay before a new HID command uint32_t timerBulbRumble;// used to continuously set PS3 Move controller Bulb and rumble values diff --git a/PS3Enums.h b/PS3Enums.h new file mode 100644 index 00000000..d5e1906f --- /dev/null +++ b/PS3Enums.h @@ -0,0 +1,158 @@ +/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved. + + This software may be distributed and modified under the terms of the GNU + General Public License version 2 (GPL2) as published by the Free Software + Foundation and appearing in the file GPL2.TXT included in the packaging of + this file. Please note that GPL2 Section 2[b] requires that all works based + on this software must also be made publicly available under the terms of + the GPL2 ("Copyleft"). + + Contact information + ------------------- + + Kristian Lauszus, TKJ Electronics + Web : http://www.tkjelectronics.com + e-mail : kristianl@tkjelectronics.com + */ + +#ifndef _ps3enums_h +#define _ps3enums_h + +enum LED { + LED1 = 0x01, + LED2 = 0x02, + LED3 = 0x04, + LED4 = 0x08, + + LED5 = 0x09, + LED6 = 0x0A, + LED7 = 0x0C, + LED8 = 0x0D, + LED9 = 0x0E, + LED10 = 0x0F, +}; +enum Colors { + // Used to set the colors of the move controller + Red = 0xFF0000, // r = 255, g = 0, b = 0 + Green = 0xFF00, // r = 0, g = 255, b = 0 + Blue = 0xFF, // r = 0, g = 0, b = 255 + + Yellow = 0xFFEB04, // r = 255, g = 235, b = 4 + Lightblue = 0xFFFF, // r = 0, g = 255, b = 255 + Purble = 0xFF00FF, // r = 255, g = 0, b = 255 + + White = 0xFFFFFF, // r = 255, g = 255, b = 255 + Off = 0x00, // r = 0, g = 0, b = 0 +}; +enum Button { + SELECT = 0x01, + L3 = 0x02, + R3 = 0x04, + START = 0x08, + UP = 0x10, + RIGHT = 0x20, + DOWN = 0x40, + LEFT = 0x80, + + L2 = 0x0100, + R2 = 0x0200, + L1 = 0x0400, + R1 = 0x0800, + TRIANGLE = 0x1000, + CIRCLE = 0x2000, + CROSS = 0x4000, + SQUARE = 0x8000, + + PS = 0x010000, + + MOVE = 0x080000, // covers 12 bits - we only need to read the top 8 + T = 0x100000, // covers 12 bits - we only need to read the top 8 +}; +enum AnalogButton { + // Note that the location is shiftet 9 when it's connected via USB + // Sixaxis Dualshcock 3 & Navigation controller + UP_ANALOG = 23, + RIGHT_ANALOG = 24, + DOWN_ANALOG = 25, + LEFT_ANALOG = 26, + + L2_ANALOG = 27, + R2_ANALOG = 28, + L1_ANALOG = 29, + R1_ANALOG = 30, + TRIANGLE_ANALOG = 31, + CIRCLE_ANALOG = 32, + CROSS_ANALOG = 33, + SQUARE_ANALOG = 34, + + //Playstation Move Controller + T_ANALOG = 15, // Both at byte 14 (last reading) and byte 15 (current reading) +}; +enum AnalogHat { + // Note that the location is shiftet 9 when it's connected via USB + LeftHatX = 15, + LeftHatY = 16, + RightHatX = 17, + RightHatY = 18, +}; +enum Sensor { + // Note that the location is shiftet 9 when it's connected via USB + // Sensors inside the Sixaxis Dualshock 3 controller + aX = 50, + aY = 52, + aZ = 54, + gZ = 56, + + // Sensors inside the Move Motion controller + aXmove = 28, + aZmove = 30, + aYmove = 32, + + gXmove = 40, + gZmove = 42, + gYmove = 44, + + tempMove = 46, + + mXmove = 47, + mZmove = 49, + mYmove = 50, +}; +enum Angle { + // Used to get the angle calculated using atan2 + Pitch = 0x01, + Roll = 0x02, +}; +enum Status { + // Note that the location is shiftet 9 when it's connected via USB + // Byte location | bit location + Plugged = (38 << 8) | 0x02, + Unplugged = (38 << 8) | 0x03, + + Charging = (39 << 8) | 0xEE, + NotCharging = (39 << 8) | 0xF1, + Shutdown = (39 << 8) | 0x01, + Dying = (39 << 8) | 0x02, + Low = (39 << 8) | 0x03, + High = (39 << 8) | 0x04, + Full = (39 << 8) | 0x05, + + MoveCharging = (21 << 8) | 0xEE, + MoveNotCharging = (21 << 8) | 0xF1, + MoveShutdown = (21 << 8) | 0x01, + MoveDying = (21 << 8) | 0x02, + MoveLow = (21 << 8) | 0x03, + MoveHigh = (21 << 8) | 0x04, + MoveFull = (21 << 8) | 0x05, + + CableRumble = (40 << 8) | 0x10,//Opperating by USB and rumble is turned on + Cable = (40 << 8) | 0x12,//Opperating by USB and rumble is turned off + BluetoothRumble = (40 << 8) | 0x14,//Opperating by bluetooth and rumble is turned on + Bluetooth = (40 << 8) | 0x16,//Opperating by bluetooth and rumble is turned off +}; +enum Rumble { + RumbleHigh = 0x10, + RumbleLow = 0x20, +}; + +#endif diff --git a/PS3USB.cpp b/PS3USB.cpp index 91ff9574..9e739ca4 100644 --- a/PS3USB.cpp +++ b/PS3USB.cpp @@ -309,57 +309,43 @@ void PS3USB::readReport() { //PrintHex(ButtonState); if(ButtonState != OldButtonState) { - buttonChanged = true; - if(ButtonState != 0x00) { - buttonPressed = true; - buttonReleased = false; - } else { - buttonPressed = false; - buttonReleased = true; - } - } - else { - buttonChanged = false; - buttonPressed = false; - buttonReleased = false; + ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable + OldButtonState = ButtonState; } - - OldButtonState = ButtonState; -} +} void PS3USB::printReport() { //Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers if (readBuf == NULL) return; - for(uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE;i++) - { + for(uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE;i++) { PrintHex(readBuf[i]); Serial.print(" "); } - Serial.println(""); + Serial.println(); } -bool PS3USB::getButton(Button b) { - if (readBuf == NULL) - return false; - if ((readBuf[(uint16_t)b >> 8] & ((uint8_t)b & 0xff)) > 0) - return true; - else - return false; +bool PS3USB::getButtonPress(Button b) { + return (ButtonState & (uint32_t)b); +} +bool PS3USB::getButtonClick(Button b) { + bool click = (ButtonClickState & (uint32_t)b); + ButtonClickState &= ~((uint32_t)b); // clear "click" event + return click; } uint8_t PS3USB::getAnalogButton(AnalogButton a) { if (readBuf == NULL) return 0; - return (uint8_t)(readBuf[(uint16_t)a]); + return (uint8_t)(readBuf[((uint16_t)a)-9]); } uint8_t PS3USB::getAnalogHat(AnalogHat a) { if (readBuf == NULL) return 0; - return (uint8_t)(readBuf[(uint16_t)a]); + return (uint8_t)(readBuf[((uint16_t)a)-9]); } uint16_t PS3USB::getSensor(Sensor a) { if (readBuf == NULL) return 0; - return ((readBuf[(uint16_t)a] << 8) | readBuf[(uint16_t)a + 1]); + return ((readBuf[((uint16_t)a)-9] << 8) | readBuf[((uint16_t)a + 1)-9]); } double PS3USB::getAngle(Angle a) { if(PS3Connected) { @@ -384,19 +370,17 @@ double PS3USB::getAngle(Angle a) { return angle; } } else - return 0; - + return 0; } bool PS3USB::getStatus(Status c) { if (readBuf == NULL) return false; - if (readBuf[(uint16_t)c >> 8] == ((uint8_t)c & 0xff)) + if (readBuf[((uint16_t)c >> 8)-9] == ((uint8_t)c & 0xff)) return true; return false; } String PS3USB::getStatusString() { - if (PS3Connected || PS3NavigationConnected) - { + if (PS3Connected || PS3NavigationConnected) { char statusOutput[100]; strcpy(statusOutput,"ConnectionStatus: "); @@ -455,22 +439,17 @@ void PS3USB::setRumbleOn(Rumble mode) { * 5 - duration_left * 6 - power_left */ - if ((mode & 0x30) > 0) - { + if ((mode & 0x30) > 0) { writeBuf[1] = 0xfe; - writeBuf[3] = 0xfe; - - if (mode == RumbleHigh) - { + writeBuf[3] = 0xfe; + if (mode == RumbleHigh) { writeBuf[2] = 0;//low mode off writeBuf[4] = 0xff;//high mode on } - else - { + else { writeBuf[2] = 0xff;//low mode on writeBuf[4] = 0;//high mode off } - PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); } } @@ -498,8 +477,7 @@ void PS3USB::setBdaddr(uint8_t* BDADDR) { pUsb->ctrlReq(bAddress,epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL); #ifdef DEBUG Notify(PSTR("\r\nBluetooth Address was set to: ")); - for(int8_t i = 5; i > 0; i--) - { + for(int8_t i = 5; i > 0; i--) { PrintHex(my_bdaddr[i]); Serial.print(":"); } @@ -560,8 +538,7 @@ void PS3USB::setMoveBdaddr(uint8_t* BDADDR) { pUsb->ctrlReq(bAddress,epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00,11,11, buf, NULL); #ifdef DEBUG Notify(PSTR("\r\nBluetooth Address was set to: ")); - for(int8_t i = 5; i > 0; i--) - { + for(int8_t i = 5; i > 0; i--) { PrintHex(my_bdaddr[i]); Serial.print(":"); } diff --git a/PS3USB.h b/PS3USB.h index 35cae686..f434e268 100644 --- a/PS3USB.h +++ b/PS3USB.h @@ -25,6 +25,7 @@ #endif #include "Usb.h" +#include "PS3Enums.h" /* PS3 data taken from descriptors */ #define EP_MAXPKTSIZE 64 // max size for data via USB @@ -52,113 +53,6 @@ #define PS3_MAX_ENDPOINTS 3 -enum LED { - LED1 = 0x01, - LED2 = 0x02, - LED3 = 0x04, - LED4 = 0x08, - - LED5 = 0x09, - LED6 = 0x0A, - LED7 = 0x0C, - LED8 = 0x0D, - LED9 = 0x0E, - LED10 = 0x0F, -}; -enum Colors { - // Used to set the colors of the move controller - Red = 0xFF0000, // r = 255, g = 0, b = 0 - Green = 0xFF00, // r = 0, g = 255, b = 0 - Blue = 0xFF, // r = 0, g = 0, b = 255 - - Yellow = 0xFFEB04, // r = 255, g = 235, b = 4 - Lightblue = 0xFFFF, // r = 0, g = 255, b = 255 - Purble = 0xFF00FF, // r = 255, g = 0, b = 255 - - White = 0xFFFFFF, // r = 255, g = 255, b = 255 - Off = 0x00, // r = 0, g = 0, b = 0 -}; - -enum Button { - // byte location | bit location - - // Sixaxis Dualshcock 3 & Navigation controller - SELECT = (2 << 8) | 0x01, - L3 = (2 << 8) | 0x02, - R3 = (2 << 8) | 0x04, - START = (2 << 8) | 0x08, - UP = (2 << 8) | 0x10, - RIGHT = (2 << 8) | 0x20, - DOWN = (2 << 8) | 0x40, - LEFT = (2 << 8) | 0x80, - - L2 = (3 << 8) | 0x01, - R2 = (3 << 8) | 0x02, - L1 = (3 << 8) | 0x04, - R1 = (3 << 8) | 0x08, - TRIANGLE = (3 << 8) | 0x10, - CIRCLE = (3 << 8) | 0x20, - CROSS = (3 << 8) | 0x40, - SQUARE = (3 << 8) | 0x80, - - PS = (4 << 8) | 0x01, -}; -enum AnalogButton { - // Sixaxis Dualshcock 3 & Navigation controller - UP_ANALOG = 14, - RIGHT_ANALOG = 15, - DOWN_ANALOG = 16, - LEFT_ANALOG = 17, - - L2_ANALOG = 18, - R2_ANALOG = 19, - L1_ANALOG = 20, - R1_ANALOG = 21, - TRIANGLE_ANALOG = 22, - CIRCLE_ANALOG = 23, - CROSS_ANALOG = 24, - SQUARE_ANALOG = 25, -}; -enum AnalogHat { - LeftHatX = 6, - LeftHatY = 7, - RightHatX = 8, - RightHatY = 9, -}; -enum Sensor { - // Sensors inside the Sixaxis Dualshock 3 controller - aX = 41, - aY = 43, - aZ = 45, - gZ = 47, -}; -enum Angle { - Pitch = 0x01, - Roll = 0x02, -}; -enum Status { - // byte location | bit location - Plugged = (29 << 8) | 0x02, - Unplugged = (29 << 8) | 0x03, - - Charging = (30 << 8) | 0xEE, - NotCharging = (30 << 8) | 0xF1, - Shutdown = (30 << 8) | 0x01, - Dying = (30 << 8) | 0x02, - Low = (30 << 8) | 0x03, - High = (30 << 8) | 0x04, - Full = (30 << 8) | 0x05, - - CableRumble = (31 << 8) | 0x10, // Opperating by USB and rumble is turned on - Cable = (31 << 8) | 0x12, // Opperating by USB and rumble is turned off - BluetoothRumble = (31 << 8) | 0x14, // Opperating by bluetooth and rumble is turned on - Bluetooth = (31 << 8) | 0x16, // Opperating by bluetooth and rumble is turned off -}; -enum Rumble { - RumbleHigh = 0x10, - RumbleLow = 0x20, -}; - class PS3USB : public USBDeviceConfig { public: PS3USB(USB *pUsb, uint8_t btadr5=0, uint8_t btadr4=0, uint8_t btadr3=0, uint8_t btadr2=0, uint8_t btadr1=0, uint8_t btadr0=0); @@ -174,7 +68,15 @@ public: void setMoveBdaddr(uint8_t* BDADDR); /* PS3 Controller Commands */ - bool getButton(Button b); + /* + getButtonPress will return true as long as the button is held down + While getButtonClick will only return it once + So you instance if you need to increase a variable once you would use getButtonClick, + but if you need to drive a robot forward you would use getButtonPress + */ + bool getButtonPress(Button b); + bool getButtonClick(Button b); + uint8_t getAnalogButton(AnalogButton a); uint8_t getAnalogHat(AnalogHat a); uint16_t getSensor(Sensor a); @@ -198,9 +100,6 @@ public: bool PS3Connected;// Variable used to indicate if the normal playstation controller is successfully connected bool PS3MoveConnected;// Variable used to indicate if the move controller is successfully connected bool PS3NavigationConnected;// Variable used to indicate if the navigation controller is successfully connected */ - bool buttonChanged;//Indicate if a button has been changed - bool buttonPressed;//Indicate if a button has been pressed - bool buttonReleased;//Indicate if a button has been released protected: /* mandatory members */ @@ -214,7 +113,8 @@ private: uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values uint32_t ButtonState; - uint32_t OldButtonState; + uint32_t OldButtonState; + uint32_t ButtonClickState; uint8_t my_bdaddr[6]; // Change to your dongles Bluetooth address in the constructor uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data @@ -225,7 +125,7 @@ private: /* Private commands */ void PS3_Command(uint8_t* data, uint16_t nbytes); - void enable_sixaxis();//Command used to enable the Dualshock 3 and Navigation controller to send data via USB + void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via USB void Move_Command(uint8_t* data, uint16_t nbytes); }; #endif diff --git a/SPP.cpp b/SPP.cpp index 74631ad0..08ca0241 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -379,6 +379,7 @@ void SPP::SDP_task() { { case L2CAP_SDP_WAIT: if (l2cap_connection_request_sdp_flag) { + l2cap_event_flag &= ~L2CAP_FLAG_CONNECTION_SDP_REQUEST; // Clear flag #ifdef DEBUG Notify(PSTR("\r\nSDP Incoming Connection Request")); #endif @@ -393,6 +394,7 @@ void SPP::SDP_task() { break; case L2CAP_SDP_REQUEST: if (l2cap_config_request_sdp_flag) { + l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_SDP_REQUEST; // Clear flag #ifdef DEBUG Notify(PSTR("\r\nSDP Configuration Request")); #endif @@ -402,6 +404,7 @@ void SPP::SDP_task() { break; case L2CAP_SDP_SUCCESS: if (l2cap_config_success_sdp_flag) { + l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_SDP_SUCCESS; // Clear flag #ifdef DEBUG Notify(PSTR("\r\nSDP Successfully Configured")); #endif @@ -412,12 +415,12 @@ void SPP::SDP_task() { break; case L2CAP_SDP_DONE: if(l2cap_disconnect_request_sdp_flag) { + l2cap_event_flag &= ~L2CAP_FLAG_DISCONNECT_SDP_REQUEST; // Clear flag SDPConnected = false; #ifdef DEBUG Notify(PSTR("\r\nDisconnected SDP Channel")); #endif - pBtd->l2cap_disconnection_response(hci_handle,identifier,sdp_dcid,sdp_scid); - l2cap_event_flag = 0; // Reset flags + pBtd->l2cap_disconnection_response(hci_handle,identifier,sdp_dcid,sdp_scid); l2cap_sdp_state = L2CAP_SDP_WAIT; } break; @@ -442,6 +445,7 @@ void SPP::RFCOMM_task() { case L2CAP_RFCOMM_WAIT: if(l2cap_connection_request_rfcomm_flag) { + l2cap_event_flag &= ~L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST; // Clear flag #ifdef DEBUG Notify(PSTR("\r\nRFCOMM Incoming Connection Request")); #endif @@ -456,6 +460,7 @@ void SPP::RFCOMM_task() break; case L2CAP_RFCOMM_REQUEST: if (l2cap_config_request_rfcomm_flag) { + l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_RFCOMM_REQUEST; // Clear flag #ifdef DEBUG Notify(PSTR("\r\nRFCOMM Configuration Request")); #endif @@ -465,6 +470,7 @@ void SPP::RFCOMM_task() break; case L2CAP_RFCOMM_SUCCESS: if (l2cap_config_success_rfcomm_flag) { + l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS; // Clear flag #ifdef DEBUG Notify(PSTR("\r\nRFCOMM Successfully Configured")); #endif @@ -476,13 +482,13 @@ void SPP::RFCOMM_task() break; case L2CAP_RFCOMM_DONE: if(l2cap_disconnect_request_rfcomm_flag) { + l2cap_event_flag &= ~L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST; // Clear flag RFCOMMConnected = false; connected = false; #ifdef DEBUG Notify(PSTR("\r\nDisconnected RFCOMM Channel")); #endif pBtd->l2cap_disconnection_response(hci_handle,identifier,rfcomm_dcid,rfcomm_scid); - l2cap_event_flag = 0; // Reset flags l2cap_rfcomm_state = L2CAP_RFCOMM_WAIT; } break; @@ -789,8 +795,7 @@ void SPP::printNumber(uint16_t n) { else { uint8_t buf[digits]; for(uint8_t i = 1; i < digits+1; i++) { - buf[digits-i] = n%10; // Get number and convert to ASCII Character - buf[digits-i] += 48; + buf[digits-i] = n%10 + '0'; // Get number and convert to ASCII Character n /= 10; } print(buf,digits); @@ -808,8 +813,7 @@ void SPP::printNumberln(uint16_t n) { else { uint8_t buf[digits+2]; for(uint8_t i = 1; i < digits+1; i++) { - buf[digits-i] = n%10; // Get number and convert to ASCII Character - buf[digits-i] += 48; + buf[digits-i] = n%10 + '0'; // Get number and convert to ASCII Character n /= 10; } buf[digits] = '\r'; diff --git a/Wii.cpp b/Wii.cpp index dd54db50..fbf2e7f9 100644 --- a/Wii.cpp +++ b/Wii.cpp @@ -165,11 +165,11 @@ void WII::ACLData(uint8_t* l2capinbuf) { if(l2capinbuf[20] & 0x02) // Only update the wiimote buttons, since the extension bytes are from the Motion Plus ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)(ButtonState & 0xFFFF0000))); else if (nunchuckConnected) // Update if it's a report from the Nunchuck - ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)(l2capinbuf[20] & 0x0C) << 14)); + ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)((~l2capinbuf[20]) & 0x0C) << 14)); //else if(classicControllerConnected) // Update if it's a report from the Classic Controller } else if(nunchuckConnected) // The Nunchuck is directly connected - ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)(l2capinbuf[20] & 0x03) << 16)); + ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)((~l2capinbuf[20]) & 0x03) << 16)); //else if(classicControllerConnected) // The Classic Controller is directly connected else if(!unknownExtensionConnected) ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8)); @@ -178,9 +178,10 @@ void WII::ACLData(uint8_t* l2capinbuf) { PrintHex(ButtonState); Notify(PSTR("\r\n")); #endif - if(ButtonState != OldButtonState) - ButtonClickState = ButtonState; // Update click state variable - OldButtonState = ButtonState; + if(ButtonState != OldButtonState) { + ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable + OldButtonState = ButtonState; + } } if(l2capinbuf[9] == 0x31 || l2capinbuf[9] == 0x35) { // Read the accelerometer accX = ((l2capinbuf[12] << 2) | (l2capinbuf[10] & 0x60 >> 5))-500; @@ -249,11 +250,6 @@ void WII::ACLData(uint8_t* l2capinbuf) { activateNunchuck = false; motionPlusConnected = true; nunchuckConnected = true; - - ButtonState |= (Z | C); - ButtonState |= ((Z | C)<<2); - ButtonClickState = ButtonState; - OldButtonState = ButtonState; } #ifdef DEBUG else { @@ -571,13 +567,8 @@ void WII::Run() { break; case L2CAP_LED_STATE: - if(nunchuck_connected_flag) { + if(nunchuck_connected_flag) nunchuckConnected = true; - ButtonState |= (Z | C); // Since the Nunchuck button are cleared when pressed we set the buttonstates like so - ButtonState |= ((Z | C)<<2); // And like this when it's connected since the bytes are shifter two to the left - ButtonClickState = ButtonState; - OldButtonState = ButtonState; - } setLedOn(LED1); l2cap_state = L2CAP_DONE; break; @@ -607,10 +598,6 @@ void WII::Run() { #endif activateNunchuck = true; nunchuckConnected = true; - ButtonState |= (Z | C); // Since the Nunchuck button are cleared when pressed we set the buttonstates like so - ButtonState |= ((Z | C)<<2); // And like this when it's connected since the bytes are shifter two to the left - ButtonClickState = ButtonState; - OldButtonState = ButtonState; } if(!motionPlusConnected) stateCounter = 449; @@ -793,11 +780,7 @@ bool WII::getButtonPress(Button b) { // Return true when a button is pressed } bool WII::getButtonClick(Button b) { // Only return true when a button is clicked bool click = (ButtonClickState & (uint32_t)b); - if(b == Z || b == C) { - click = !click; // The nunchuck buttons are cleared when pressed - ButtonClickState |= (uint32_t)b; // clear "click" event - } else - ButtonClickState &= ~((uint32_t)b); // clear "click" event + ButtonClickState &= ~((uint32_t)b); // clear "click" event return click; } uint8_t WII::getAnalogHat(AnalogHat a) { diff --git a/Wii.h b/Wii.h index 048143cf..bed254f8 100644 --- a/Wii.h +++ b/Wii.h @@ -105,8 +105,15 @@ public: virtual void Reset(); // Use this to reset the service virtual void disconnect(); // Use this void to disconnect any of the controllers + /* + getButtonPress will return true as long as the button is held down + While getButtonClick will only return it once + So you instance if you need to increase a variable once you would use getButtonClick, + but if you need to drive a robot forward you would use getButtonPress + */ bool getButtonPress(Button b); // This will read true as long as the button is held down bool getButtonClick(Button b); // This will only be true when the button is clicked the first time + uint8_t getAnalogHat(AnalogHat a); // Used to read the joystick of the Nunchuck double getPitch() { return pitch; }; // Fusioned angle using a complimentary filter if the Motion Plus is connected diff --git a/examples/Bluetooth/PS3BT/PS3BT.ino b/examples/Bluetooth/PS3BT/PS3BT.ino index 899cc49a..472768bb 100644 --- a/examples/Bluetooth/PS3BT/PS3BT.ino +++ b/examples/Bluetooth/PS3BT/PS3BT.ino @@ -16,7 +16,6 @@ boolean printAngle; void setup() { Serial.begin(115200); - if (Usb.Init() == -1) { Serial.print(F("\r\nOSC did not start")); while(1); //halt @@ -28,168 +27,141 @@ void loop() { if(PS3.PS3Connected || PS3.PS3NavigationConnected) { if(PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { - if(PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117) { - Serial.print(F("LeftHatX: ")); - Serial.print(PS3.getAnalogHat(LeftHatX)); - Serial.print("\t"); - } if(PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117) { - Serial.print(F("LeftHatY: ")); - Serial.print(PS3.getAnalogHat(LeftHatY)); - Serial.print("\t"); - } if(PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117) { - Serial.print(F("RightHatX: ")); - Serial.print(PS3.getAnalogHat(RightHatX)); - Serial.print("\t"); - } if(PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { - Serial.print(F("RightHatY: ")); - Serial.print(PS3.getAnalogHat(RightHatY)); - } - Serial.println(""); + Serial.print(F("\r\nLeftHatX: ")); + Serial.print(PS3.getAnalogHat(LeftHatX)); + Serial.print(F("\tLeftHatY: ")); + Serial.print(PS3.getAnalogHat(LeftHatY)); + Serial.print(F("\tRightHatX: ")); + Serial.print(PS3.getAnalogHat(RightHatX)); + Serial.print(F("\tRightHatY: ")); + Serial.print(PS3.getAnalogHat(RightHatY)); } //Analog button values can be read from almost all buttons if(PS3.getAnalogButton(L2_ANALOG) > 0 || PS3.getAnalogButton(R2_ANALOG) > 0) { - if(PS3.getAnalogButton(L2_ANALOG) > 0) { - Serial.print(F("L2: ")); - Serial.print(PS3.getAnalogButton(L2_ANALOG)); - Serial.print("\t"); - } if(PS3.getAnalogButton(R2_ANALOG) > 0) { - Serial.print(F("R2: ")); - Serial.print(PS3.getAnalogButton(R2_ANALOG)); - } - Serial.println(""); + Serial.print(F("\r\nL2: ")); + Serial.print(PS3.getAnalogButton(L2_ANALOG)); + Serial.print(F("\tR2: ")); + Serial.print(PS3.getAnalogButton(R2_ANALOG)); } + if(PS3.getButtonClick(PS)) { + Serial.print(F("\r\nPS")); + PS3.disconnect(); + } + else { + if(PS3.getButtonClick(TRIANGLE)) + Serial.print(F("\r\nTraingle")); + if(PS3.getButtonClick(CIRCLE)) + Serial.print(F("\r\nCircle")); + if(PS3.getButtonClick(CROSS)) + Serial.print(F("\r\nCross")); + if(PS3.getButtonClick(SQUARE)) + Serial.print(F("\r\nSquare")); - if(PS3.buttonPressed) - { - Serial.print(F("PS3 Controller")); + if(PS3.getButtonClick(UP)) { + Serial.print(F("\r\nUp")); + if(PS3.PS3Connected) { + PS3.setAllOff(); + PS3.setLedOn(LED4); + } + } + if(PS3.getButtonClick(RIGHT)) { + Serial.print(F("\r\nRight")); + if(PS3.PS3Connected) { + PS3.setAllOff(); + PS3.setLedOn(LED1); + } + } + if(PS3.getButtonClick(DOWN)) { + Serial.print(F("\r\nDown")); + if(PS3.PS3Connected) { + PS3.setAllOff(); + PS3.setLedOn(LED2); + } + } + if(PS3.getButtonClick(LEFT)) { + Serial.print(F("\r\nLeft")); + if(PS3.PS3Connected) { + PS3.setAllOff(); + PS3.setLedOn(LED3); + } + } - if(PS3.getButton(PS)) { - Serial.print(F(" - PS")); - PS3.disconnect(); - } else { - if(PS3.getButton(TRIANGLE)) - Serial.print(F(" - Traingle")); - if(PS3.getButton(CIRCLE)) - Serial.print(F(" - Circle")); - if(PS3.getButton(CROSS)) - Serial.print(F(" - Cross")); - if(PS3.getButton(SQUARE)) - Serial.print(F(" - Square")); + if(PS3.getButtonClick(L1)) + Serial.print(F("\r\nL1")); + if(PS3.getButtonClick(L3)) + Serial.print(F("\r\nL3")); + if(PS3.getButtonClick(R1)) + Serial.print(F("\r\nR1")); + if(PS3.getButtonClick(R3)) + Serial.print(F("\r\nR3")); - if(PS3.getButton(UP)) { - Serial.print(F(" - Up")); - if(PS3.PS3Connected) { - PS3.setAllOff(); - PS3.setLedOn(LED4); - } - } if(PS3.getButton(RIGHT)) { - Serial.print(F(" - Right")); - if(PS3.PS3Connected) { - PS3.setAllOff(); - PS3.setLedOn(LED1); - } - } if(PS3.getButton(DOWN)) { - Serial.print(F(" - Down")); - if(PS3.PS3Connected) { - PS3.setAllOff(); - PS3.setLedOn(LED2); - } - } if(PS3.getButton(LEFT)) { - Serial.print(F(" - Left")); - if(PS3.PS3Connected) { - PS3.setAllOff(); - PS3.setLedOn(LED3); - } - } - - if(PS3.getButton(L1)) - Serial.print(F(" - L1")); - //if(PS3.getButton(L2)) - //Serial.print(F(" - L2")); - if(PS3.getButton(L3)) - Serial.print(F(" - L3")); - if(PS3.getButton(R1)) - Serial.print(F(" - R1")); - //if(PS3.getButton(R2)) - //Serial.print(F(" - R2")); - if(PS3.getButton(R3)) - Serial.print(F(" - R3")); - - if(PS3.getButton(SELECT)) { - Serial.print(F(" - Select - ")); - Serial.print(PS3.getStatusString()); - } if(PS3.getButton(START)) { - Serial.print(F(" - Start")); - printAngle = !printAngle; - while(PS3.getButton(START)) - Usb.Task(); - } - Serial.println(""); - } + if(PS3.getButtonClick(SELECT)) { + Serial.print(F("\r\nSelect - ")); + Serial.print(PS3.getStatusString()); + } + if(PS3.getButtonClick(START)) { + Serial.print(F("\r\nStart")); + printAngle = !printAngle; + } } if(printAngle) { - Serial.print(F("Pitch: ")); + Serial.print(F("\r\nPitch: ")); Serial.print(PS3.getAngle(Pitch)); Serial.print(F("\tRoll: ")); - Serial.println(PS3.getAngle(Roll)); + Serial.print(PS3.getAngle(Roll)); } } - else if(PS3.PS3MoveConnected) - { + else if(PS3.PS3MoveConnected) { if(PS3.getAnalogButton(T_ANALOG) > 0) { - Serial.print(F("T: ")); - Serial.println(PS3.getAnalogButton(T_ANALOG)); - } if(PS3.buttonPressed) { - Serial.print(F("PS3 Move Controller")); - - if(PS3.getButton(PS)) { - Serial.print(F(" - PS")); - PS3.disconnect(); - } else { - if(PS3.getButton(SELECT)) { - Serial.print(F(" - Select")); - printTemperature = !printTemperature; - while(PS3.getButton(SELECT)) - Usb.Task(); - } if(PS3.getButton(START)) { - Serial.print(F(" - Start")); - printAngle = !printAngle; - while(PS3.getButton(START)) - Usb.Task(); - } if(PS3.getButton(TRIANGLE)) { - Serial.print(F(" - Triangle")); - PS3.moveSetBulb(Red); - } if(PS3.getButton(CIRCLE)) { - Serial.print(F(" - Circle")); - PS3.moveSetBulb(Green); - } if(PS3.getButton(SQUARE)) { - Serial.print(F(" - Square")); - PS3.moveSetBulb(Blue); - } if(PS3.getButton(CROSS)) { - Serial.print(F(" - Cross")); - PS3.moveSetBulb(Yellow); - } if(PS3.getButton(MOVE)) { - PS3.moveSetBulb(Off); - Serial.print(F(" - Move")); - Serial.print(F(" - ")); - Serial.print(PS3.getStatusString()); - } - //if(PS3.getButton(T)) - //Serial.print(F(" - T")); - - Serial.println(""); + Serial.print(F("\r\nT: ")); + Serial.print(PS3.getAnalogButton(T_ANALOG)); + } + if(PS3.getButtonClick(PS)) { + Serial.print(F("\r\nPS")); + PS3.disconnect(); + } + else { + if(PS3.getButtonClick(SELECT)) { + Serial.print(F("\r\nSelect")); + printTemperature = !printTemperature; + } + if(PS3.getButtonClick(START)) { + Serial.print(F("\r\nStart")); + printAngle = !printAngle; + } + if(PS3.getButtonClick(TRIANGLE)) { + Serial.print(F("\r\nTriangle")); + PS3.moveSetBulb(Red); + } + if(PS3.getButtonClick(CIRCLE)) { + Serial.print(F("\r\nCircle")); + PS3.moveSetBulb(Green); + } + if(PS3.getButtonClick(SQUARE)) { + Serial.print(F("\r\nSquare")); + PS3.moveSetBulb(Blue); + } + if(PS3.getButtonClick(CROSS)) { + Serial.print(F("\r\nCross")); + PS3.moveSetBulb(Yellow); + } + if(PS3.getButtonClick(MOVE)) { + PS3.moveSetBulb(Off); + Serial.print(F("\r\nMove")); + Serial.print(F(" - ")); + Serial.print(PS3.getStatusString()); } } if(printAngle) { - Serial.print(F("Pitch: ")); + Serial.print(F("\r\nPitch: ")); Serial.print(PS3.getAngle(Pitch)); Serial.print(F("\tRoll: ")); - Serial.println(PS3.getAngle(Roll)); + Serial.print(PS3.getAngle(Roll)); } else if(printTemperature) { - Serial.print(F("Temperature: ")); - Serial.println(PS3.getTemperature()); + Serial.print(F("\r\nTemperature: ")); + Serial.print(PS3.getTemperature()); } } } diff --git a/examples/Bluetooth/PS3SPP/PS3SPP.ino b/examples/Bluetooth/PS3SPP/PS3SPP.ino index 10fa0e00..8e12364a 100644 --- a/examples/Bluetooth/PS3SPP/PS3SPP.ino +++ b/examples/Bluetooth/PS3SPP/PS3SPP.ino @@ -3,7 +3,7 @@ For more information visit my blog: http://blog.tkjelectronics.dk/ or send me an e-mail: kristianl@tkjelectronics.com */ - + /* Note: You will need a Arduino Mega 1280/2560 to run this sketch, @@ -22,9 +22,7 @@ PS3BT PS3(&Btd); // This will just create the instance //PS3BT PS3(&Btd,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch boolean firstMessage = true; - -String analogOutput; // We will store the data in these string so we doesn't overflow the dongle -String digitalOutput; +String output; // We will store the data in these string so we doesn't overflow the dongle void setup() { Serial.begin(115200); // This wil lprint the debugging from the libraries @@ -50,118 +48,96 @@ void loop() { else firstMessage = true; - if(PS3.PS3Connected || PS3.PS3NavigationConnected) { - analogOutput = ""; // Reset analog output string + if(PS3.PS3Connected || PS3.PS3NavigationConnected) { + output = ""; // Reset output string if(PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { - if(PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117) { - analogOutput += "LeftHatX: "; - analogOutput += PS3.getAnalogHat(LeftHatX); - analogOutput += "\t"; - } - if(PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117) { - analogOutput += "LeftHatY: "; - analogOutput += PS3.getAnalogHat(LeftHatY); - analogOutput += "\t"; - } - if(PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117) { - analogOutput += "RightHatX: "; - analogOutput += PS3.getAnalogHat(RightHatX); - analogOutput += "\t"; - } - if(PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { - analogOutput += "RightHatY: "; - analogOutput += PS3.getAnalogHat(RightHatY); - analogOutput += "\t"; - } + output += "LeftHatX: "; + output += PS3.getAnalogHat(LeftHatX); + output += "\tLeftHatY: "; + output += PS3.getAnalogHat(LeftHatY); + output += "\tRightHatX: "; + output += PS3.getAnalogHat(RightHatX); + output += "\tRightHatY: "; + output += PS3.getAnalogHat(RightHatY); } //Analog button values can be read from almost all buttons if(PS3.getAnalogButton(L2_ANALOG) || PS3.getAnalogButton(R2_ANALOG)) { - if(analogOutput != "") - analogOutput += "\r\n"; - if(PS3.getAnalogButton(L2_ANALOG)) { - analogOutput += "L2: "; - analogOutput += PS3.getAnalogButton(L2_ANALOG); - analogOutput += "\t"; - } - if(PS3.getAnalogButton(R2_ANALOG)) { - analogOutput += "R2: "; - analogOutput += PS3.getAnalogButton(R2_ANALOG); - analogOutput += "\t"; - } + if(output != "") + output += "\r\n"; + output += "L2: "; + output += PS3.getAnalogButton(L2_ANALOG); + output += "\tR2: "; + output += PS3.getAnalogButton(R2_ANALOG); } - if(analogOutput != "") { - Serial.println(analogOutput); + if(output != "") { + Serial.println(output); if(SerialBT.connected) - SerialBT.println(analogOutput); + SerialBT.println(output); + output = ""; // Reset output string } + if(PS3.getButtonClick(PS)) { + output += " - PS"; + PS3.disconnect(); + } + else { + if(PS3.getButtonClick(TRIANGLE)) + output += " - Traingle"; + if(PS3.getButtonClick(CIRCLE)) + output += " - Circle"; + if(PS3.getButtonClick(CROSS)) + output += " - Cross"; + if(PS3.getButtonClick(SQUARE)) + output += " - Square"; - if(PS3.buttonPressed) { - digitalOutput = "PS3 Controller"; - if(PS3.getButton(PS)) { - digitalOutput += " - PS"; - PS3.disconnect(); + if(PS3.getButtonClick(UP)) { + output += " - Up"; + if(PS3.PS3Connected) { + PS3.setAllOff(); + PS3.setLedOn(LED4); + } + } + if(PS3.getButtonClick(RIGHT)) { + output += " - Right"; + if(PS3.PS3Connected) { + PS3.setAllOff(); + PS3.setLedOn(LED1); + } + } + if(PS3.getButtonClick(DOWN)) { + output += " - Down"; + if(PS3.PS3Connected) { + PS3.setAllOff(); + PS3.setLedOn(LED2); + } + } + if(PS3.getButtonClick(LEFT)) { + output += " - Left"; + if(PS3.PS3Connected) { + PS3.setAllOff(); + PS3.setLedOn(LED3); + } } - else { - if(PS3.getButton(TRIANGLE)) - digitalOutput += " - Traingle"; - if(PS3.getButton(CIRCLE)) - digitalOutput += " - Circle"; - if(PS3.getButton(CROSS)) - digitalOutput += " - Cross"; - if(PS3.getButton(SQUARE)) - digitalOutput += " - Square"; - if(PS3.getButton(UP)) { - digitalOutput += " - UP"; - if(PS3.PS3Connected) { - PS3.setAllOff(); - PS3.setLedOn(LED4); - } - } - if(PS3.getButton(RIGHT)) { - digitalOutput += " - Right"; - if(PS3.PS3Connected) { - PS3.setAllOff(); - PS3.setLedOn(LED1); - } - } - if(PS3.getButton(DOWN)) { - digitalOutput += " - Down"; - if(PS3.PS3Connected) { - PS3.setAllOff(); - PS3.setLedOn(LED2); - } - } - if(PS3.getButton(LEFT)) { - digitalOutput += " - Left"; - if(PS3.PS3Connected) { - PS3.setAllOff(); - PS3.setLedOn(LED3); - } - } + if(PS3.getButtonClick(L1)) + output += " - L1"; + if(PS3.getButtonClick(L3)) + output += " - L3"; + if(PS3.getButtonClick(R1)) + output += " - R1"; + if(PS3.getButtonClick(R3)) + output += " - R3"; - if(PS3.getButton(L1)) - digitalOutput += " - L1"; - //if(PS3.getButton(L2)) - //digitalOutput += " - L2"; - if(PS3.getButton(L3)) - digitalOutput += " - L3"; - if(PS3.getButton(R1)) - digitalOutput += " - R1"; - //if(PS3.getButton(R2)) - //digitalOutput += " - R2"; - if(PS3.getButton(R3)) - digitalOutput += " - R3"; + if(PS3.getButtonClick(SELECT)) + output += " - Select"; + if(PS3.getButtonClick(START)) + output += " - Start"; - if(PS3.getButton(SELECT)) - digitalOutput += " - Select"; - if(PS3.getButton(START)) - digitalOutput += " - Start"; - - Serial.println(digitalOutput); + if(output != "") { + String string = "PS3 Controller" + output; + Serial.println(string); if(SerialBT.connected) - SerialBT.println(digitalOutput); - } - } + SerialBT.println(string); + } + } } } diff --git a/examples/PS3USB/PS3USB.ino b/examples/PS3USB/PS3USB.ino index 0741d220..a516175e 100644 --- a/examples/PS3USB/PS3USB.ino +++ b/examples/PS3USB/PS3USB.ino @@ -15,7 +15,6 @@ uint8_t state = 0; void setup() { Serial.begin(115200); - if (Usb.Init() == -1) { Serial.print(F("\r\nOSC did not start")); while(1); //halt @@ -27,156 +26,124 @@ void loop() { if(PS3.PS3Connected || PS3.PS3NavigationConnected) { if(PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { - if(PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117) { - Serial.print(F("LeftHatX: ")); - Serial.print(PS3.getAnalogHat(LeftHatX)); - Serial.print("\t"); - } - if(PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117) { - Serial.print(F("LeftHatY: ")); - Serial.print(PS3.getAnalogHat(LeftHatY)); - Serial.print("\t"); - } - if(PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117) { - Serial.print(F("RightHatX: ")); - Serial.print(PS3.getAnalogHat(RightHatX)); - Serial.print("\t"); - } - if(PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { - Serial.print(F("RightHatY: ")); - Serial.print(PS3.getAnalogHat(RightHatY)); - } - Serial.println(""); + Serial.print(F("\r\nLeftHatX: ")); + Serial.print(PS3.getAnalogHat(LeftHatX)); + Serial.print(F("\tLeftHatY: ")); + Serial.print(PS3.getAnalogHat(LeftHatY)); + Serial.print(F("\tRightHatX: ")); + Serial.print(PS3.getAnalogHat(RightHatX)); + Serial.print(F("\tRightHatY: ")); + Serial.print(PS3.getAnalogHat(RightHatY)); } - // Analog button values can be read from almost all buttons if(PS3.getAnalogButton(L2_ANALOG) > 0 || PS3.getAnalogButton(R2_ANALOG) > 0) { - if(PS3.getAnalogButton(L2_ANALOG) > 0) { - Serial.print(F("L2: ")); - Serial.print(PS3.getAnalogButton(L2_ANALOG)); - Serial.print("\t"); - } - if(PS3.getAnalogButton(R2_ANALOG) > 0) { - Serial.print(F("R2: ")); - Serial.print(PS3.getAnalogButton(R2_ANALOG)); - } - Serial.println(""); + Serial.print(F("\r\nL2: ")); + Serial.print(PS3.getAnalogButton(L2_ANALOG)); + Serial.print(F("\tR2: ")); + Serial.print(PS3.getAnalogButton(R2_ANALOG)); } + if(PS3.getButtonClick(PS)) + Serial.print(F("\r\nPS")); - if(PS3.buttonPressed) - { - Serial.print(F("PS3 Controller")); + if(PS3.getButtonClick(TRIANGLE)) + Serial.print(F("\r\nTraingle")); + if(PS3.getButtonClick(CIRCLE)) + Serial.print(F("\r\nCircle")); + if(PS3.getButtonClick(CROSS)) + Serial.print(F("\r\nCross")); + if(PS3.getButtonClick(SQUARE)) + Serial.print(F("\r\nSquare")); - if(PS3.getButton(PS)) - Serial.print(F(" - PS")); - - if(PS3.getButton(TRIANGLE)) - Serial.print(F(" - Traingle")); - if(PS3.getButton(CIRCLE)) - Serial.print(F(" - Circle")); - if(PS3.getButton(CROSS)) - Serial.print(F(" - Cross")); - if(PS3.getButton(SQUARE)) - Serial.print(F(" - Square")); + if(PS3.getButtonClick(UP)) { + Serial.print(F("\r\nUp")); + PS3.setAllOff(); + PS3.setLedOn(LED4); + } + if(PS3.getButtonClick(RIGHT)) { + Serial.print(F("\r\nRight")); + PS3.setAllOff(); + PS3.setLedOn(LED1); + } + if(PS3.getButtonClick(DOWN)) { + Serial.print(F("\r\nDown")); + PS3.setAllOff(); + PS3.setLedOn(LED2); + } + if(PS3.getButtonClick(LEFT)) { + Serial.print(F("\r\nLeft")); + PS3.setAllOff(); + PS3.setLedOn(LED3); + } - if(PS3.getButton(UP)) { - Serial.print(F(" - Up")); - PS3.setAllOff(); - PS3.setLedOn(LED4); - } - if(PS3.getButton(RIGHT)) { - Serial.print(F(" - Right")); - PS3.setAllOff(); - PS3.setLedOn(LED1); - } - if(PS3.getButton(DOWN)) { - Serial.print(F(" - Down")); - PS3.setAllOff(); - PS3.setLedOn(LED2); - } - if(PS3.getButton(LEFT)) { - Serial.print(F(" - Left")); - PS3.setAllOff(); - PS3.setLedOn(LED3); - } + if(PS3.getButtonClick(L1)) + Serial.print(F("\r\nL1")); + if(PS3.getButtonClick(L3)) + Serial.print(F("\r\nL3")); + if(PS3.getButtonClick(R1)) + Serial.print(F("\r\nR1")); + if(PS3.getButtonClick(R3)) + Serial.print(F("\r\nR3")); - if(PS3.getButton(L1)) - Serial.print(F(" - L1")); - //if(PS3.getButton(L2)) - //Serial.print(F(" - L2")); - if(PS3.getButton(L3)) - Serial.print(F(" - L3")); - if(PS3.getButton(R1)) - Serial.print(F(" - R1")); - //if(PS3.getButton(R2)) - //Serial.print(F(" - R2")); - if(PS3.getButton(R3)) - Serial.print(F(" - R3")); - - if(PS3.getButton(SELECT)) { - Serial.print(F(" - Select - ")); - Serial.print(PS3.getStatusString()); - } - if(PS3.getButton(START)) { - Serial.print(F(" - Start")); - printAngle = !printAngle; - while(PS3.getButton(START)) - Usb.Task(); - } - Serial.println(""); - } - if(printAngle) { - Serial.print(F("Pitch: ")); - Serial.print(PS3.getAngle(Pitch)); - Serial.print(F("\tRoll: ")); - Serial.println(PS3.getAngle(Roll)); - } + if(PS3.getButtonClick(SELECT)) { + Serial.print(F("\r\nSelect - ")); + Serial.print(PS3.getStatusString()); + } + if(PS3.getButtonClick(START)) { + Serial.print(F("\r\nStart")); + printAngle = !printAngle; + } + } + if(printAngle) { + Serial.print(F("\r\nPitch: ")); + Serial.print(PS3.getAngle(Pitch)); + Serial.print(F("\tRoll: ")); + Serial.print(PS3.getAngle(Roll)); } else if(PS3.PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB switch(state) { - case 0: + case 0: PS3.moveSetRumble(0); PS3.moveSetBulb(Off); state = 1; break; - - case 1: + + case 1: PS3.moveSetRumble(75); PS3.moveSetBulb(Red); state = 2; break; - - case 2: + + case 2: PS3.moveSetRumble(125); PS3.moveSetBulb(Green); state = 3; break; - - case 3: + + case 3: PS3.moveSetRumble(150); PS3.moveSetBulb(Blue); state = 4; break; - - case 4: + + case 4: PS3.moveSetRumble(175); PS3.moveSetBulb(Yellow); state = 5; break; - - case 5: + + case 5: PS3.moveSetRumble(200); PS3.moveSetBulb(Lightblue); state = 6; break; - - case 6: + + case 6: PS3.moveSetRumble(225); PS3.moveSetBulb(Purble); state = 7; break; - - case 7: + + case 7: PS3.moveSetRumble(250); PS3.moveSetBulb(White); state = 0; @@ -185,4 +152,3 @@ void loop() { delay(1000); } } - diff --git a/keywords.txt b/keywords.txt index 3e8f4346..187cef9c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,40 +6,39 @@ # Datatypes (KEYWORD1) #################################################### -PS3BT KEYWORD1 +PS3BT KEYWORD1 PS3USB KEYWORD1 #################################################### # Methods and Functions (KEYWORD2) #################################################### -setBdaddr KEYWORD2 +setBdaddr KEYWORD2 setMoveBdaddr KEYWORD2 -getButton KEYWORD2 +getButtonPress KEYWORD2 +getButtonClick KEYWORD2 getAnalogButton KEYWORD2 getAnalogHat KEYWORD2 -getSensor KEYWORD2 -getAngle KEYWORD2 -getStatus KEYWORD2 +getSensor KEYWORD2 +getAngle KEYWORD2 +get9DOFValues KEYWORD2 +getStatus KEYWORD2 getStatusString KEYWORD2 getTemperature KEYWORD2 -disconnect KEYWORD2 +disconnect KEYWORD2 -setAllOff KEYWORD2 +setAllOff KEYWORD2 setRumbleOff KEYWORD2 -setRumbleOn KEYWORD2 -setLedOff KEYWORD2 -setLedOn KEYWORD2 +setRumbleOn KEYWORD2 +setLedOff KEYWORD2 +setLedOn KEYWORD2 setLedToggle KEYWORD2 -moveSetBulb KEYWORD2 +moveSetBulb KEYWORD2 moveSetRumble KEYWORD2 -PS3Connected KEYWORD2 -PS3MoveConnected KEYWORD2 +PS3Connected KEYWORD2 +PS3MoveConnected KEYWORD2 PS3NavigationConnected KEYWORD2 -buttonChanged KEYWORD2 -buttonPressed KEYWORD2 -buttonReleased KEYWORD2 watingForConnection KEYWORD2 @@ -220,8 +219,6 @@ WII KEYWORD1 wiimoteConnected KEYWORD2 nunchuckConnected KEYWORD2 motionPlusConnected KEYWORD2 -getButtonPress KEYWORD2 -getButtonClick KEYWORD2 setRumbleToggle KEYWORD2 getPitch KEYWORD2 getRoll KEYWORD2