diff --git a/examples/PS3SPP/PS3SPP.ino b/examples/PS3SPP/PS3SPP.ino index ca896d2d..b089c6a7 100644 --- a/examples/PS3SPP/PS3SPP.ino +++ b/examples/PS3SPP/PS3SPP.ino @@ -3,32 +3,36 @@ For more information visit my blog: http://blog.tkjelectronics.dk/ or send me an e-mail: kristianl@tkjelectronics.com */ + -#include "PS3BT.h" -#include "SPP.h" +#include +#include USB Usb; -BTD Btd(&Usb); -/* You can create the instances of the class in two ways */ +BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so + +/* You can create the instances of the bluetooth services in two ways */ SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234" -//SPP SerialBTBT(&Btd, "Lauszus' Arduino","0000"); // You can also set the name and pin like so +//SPP SerialBTBT(&Btd,"Lauszus's Arduino","0000"); // You can also set the name and pin like so 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 printTemperature; -boolean printAngle; boolean firstMessage = true; +String analogOutput; // We will store the data in these string so we doesn't overflow the dongle +String digitalOutput; + void setup() { Serial.begin(115200); // This wil lprint the debugging from the libraries if (Usb.Init() == -1) { - SerialBT.print(F("\r\nOSC did not start")); + Serial.print(F("\r\nOSC did not start")); while(1); //halt } Serial.print(F("\r\nPS3 Bluetooth Library Started")); } void loop() { Usb.Task(); - if(SerialBT.connected) { + + if(SerialBT.connected) { if(firstMessage) { firstMessage = false; SerialBT.println(F("Hello from Arduino")); // Send welcome message @@ -40,78 +44,91 @@ void loop() { } else firstMessage = true; - - if((PS3.PS3Connected || PS3.PS3NavigationConnected) && SerialBT.connected) { + + if(PS3.PS3Connected || PS3.PS3NavigationConnected) { + analogOutput = ""; // Reset analog 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) { - SerialBT.print(F("LeftHatX: ")); - SerialBT.print(PS3.getAnalogHat(LeftHatX)); - SerialBT.print("\t"); - } if(PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117) { - SerialBT.print(F("LeftHatY: ")); - SerialBT.print(PS3.getAnalogHat(LeftHatY)); - SerialBT.print("\t"); - } if(PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117) { - SerialBT.print(F("RightHatX: ")); - SerialBT.print(PS3.getAnalogHat(RightHatX)); - SerialBT.print("\t"); - } if(PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { - SerialBT.print(F("RightHatY: ")); - SerialBT.print(PS3.getAnalogHat(RightHatY)); + analogOutput += "LeftHatX: "; + analogOutput += PS3.getAnalogHat(LeftHatX); + analogOutput += "\t"; } - SerialBT.println(""); + 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"; + } } - //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) { - SerialBT.print(F("L2: ")); - SerialBT.print(PS3.getAnalogButton(L2_ANALOG)); - SerialBT.print("\t"); - } if(PS3.getAnalogButton(R2_ANALOG) > 0) { - SerialBT.print(F("R2: ")); - SerialBT.print(PS3.getAnalogButton(R2_ANALOG)); + 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"; } - SerialBT.println(""); + } + if(analogOutput != "") { + Serial.println(analogOutput); + if(SerialBT.connected) + SerialBT.println(analogOutput); } - if(PS3.buttonPressed) - { - SerialBT.print(F("PS3 Controller")); - + if(PS3.buttonPressed) { + digitalOutput = "PS3 Controller"; if(PS3.getButton(PS)) { - SerialBT.print(F(" - PS")); + digitalOutput += " - PS"; PS3.disconnect(); - } else { + } + else { if(PS3.getButton(TRIANGLE)) - SerialBT.print(F(" - Traingle")); + digitalOutput += " - Traingle"; if(PS3.getButton(CIRCLE)) - SerialBT.print(F(" - Circle")); + digitalOutput += " - Circle"; if(PS3.getButton(CROSS)) - SerialBT.print(F(" - Cross")); + digitalOutput += " - Cross"; if(PS3.getButton(SQUARE)) - SerialBT.print(F(" - Square")); + digitalOutput += " - Square"; if(PS3.getButton(UP)) { - SerialBT.print(F(" - Up")); + digitalOutput += " - UP"; if(PS3.PS3Connected) { PS3.setAllOff(); PS3.setLedOn(LED4); } - } if(PS3.getButton(RIGHT)) { - SerialBT.print(F(" - Right")); + } + if(PS3.getButton(RIGHT)) { + digitalOutput += " - Right"; if(PS3.PS3Connected) { PS3.setAllOff(); PS3.setLedOn(LED1); } - } if(PS3.getButton(DOWN)) { - SerialBT.print(F(" - Down")); + } + if(PS3.getButton(DOWN)) { + digitalOutput += " - Down"; if(PS3.PS3Connected) { PS3.setAllOff(); PS3.setLedOn(LED2); } - } if(PS3.getButton(LEFT)) { - SerialBT.print(F(" - Left")); + } + if(PS3.getButton(LEFT)) { + digitalOutput += " - Left"; if(PS3.PS3Connected) { PS3.setAllOff(); PS3.setLedOn(LED3); @@ -119,93 +136,27 @@ void loop() { } if(PS3.getButton(L1)) - SerialBT.print(F(" - L1")); - //if(PS3.getButton(L2)) - //SerialBT.print(F(" - L2")); + digitalOutput += " - L1"; + if(PS3.getButton(L2)) + digitalOutput += " - L2"; if(PS3.getButton(L3)) - SerialBT.print(F(" - L3")); + digitalOutput += " - L3"; if(PS3.getButton(R1)) - SerialBT.print(F(" - R1")); - //if(PS3.getButton(R2)) - //SerialBT.print(F(" - R2")); + digitalOutput += " - R1"; + if(PS3.getButton(R2)) + digitalOutput += " - R2"; if(PS3.getButton(R3)) - SerialBT.print(F(" - R3")); + digitalOutput += " - R3"; - if(PS3.getButton(SELECT)) { - SerialBT.print(F(" - Select - ")); -// SerialBT.print(PS3.getStatusString()); - } if(PS3.getButton(START)) { - SerialBT.print(F(" - Start")); - printAngle = !printAngle; - while(PS3.getButton(START)) - Usb.Task(); - } - SerialBT.println(""); + if(PS3.getButton(SELECT)) + digitalOutput += " - Select"; + if(PS3.getButton(START)) + digitalOutput += " - Start"; + + Serial.println(digitalOutput); + if(SerialBT.connected) + SerialBT.println(digitalOutput); } } - if(printAngle) { - SerialBT.print(F("Pitch: ")); - SerialBT.print(PS3.getAngle(Pitch)); - SerialBT.print(F("\tRoll: ")); - SerialBT.println(PS3.getAngle(Roll)); - } } - else if(PS3.PS3MoveConnected && SerialBT.connected) - { - if(PS3.getAnalogButton(T_ANALOG) > 0) { - SerialBT.print(F("T: ")); - SerialBT.println(PS3.getAnalogButton(T_ANALOG)); - } if(PS3.buttonPressed) { - SerialBT.print(F("PS3 Move Controller")); - - if(PS3.getButton(PS)) { - SerialBT.print(F(" - PS")); - PS3.disconnect(); - } else { - if(PS3.getButton(SELECT)) { - SerialBT.print(F(" - Select")); - printTemperature = !printTemperature; - while(PS3.getButton(SELECT)) - Usb.Task(); - } if(PS3.getButton(START)) { - SerialBT.print(F(" - Start")); - printAngle = !printAngle; - while(PS3.getButton(START)) - Usb.Task(); - } if(PS3.getButton(TRIANGLE)) { - SerialBT.print(F(" - Triangle")); - PS3.moveSetBulb(Red); - } if(PS3.getButton(CIRCLE)) { - SerialBT.print(F(" - Circle")); - PS3.moveSetBulb(Green); - } if(PS3.getButton(SQUARE)) { - SerialBT.print(F(" - Square")); - PS3.moveSetBulb(Blue); - } if(PS3.getButton(CROSS)) { - SerialBT.print(F(" - Cross")); - PS3.moveSetBulb(Yellow); - } if(PS3.getButton(MOVE)) { - PS3.moveSetBulb(Off); - SerialBT.print(F(" - Move")); - SerialBT.print(F(" - ")); -// SerialBT.print(PS3.getStatusString()); - } - //if(PS3.getButton(T)) - //SerialBT.print(F(" - T")); - - SerialBT.println(""); - } - } - if(printAngle) { - SerialBT.print(F("Pitch: ")); - SerialBT.print(PS3.getAngle(Pitch)); - SerialBT.print(F("\tRoll: ")); - SerialBT.println(PS3.getAngle(Roll)); - } - else if(printTemperature) { - SerialBT.print(F("Temperature: ")); -// SerialBT.println(PS3.getTemperature()); - } - } - delay(5); }