Save all strings in string buffer to prevent the dongle from overflowing

This commit is contained in:
Kristian Lauszus 2012-08-08 19:23:03 +02:00
parent 5ce7cc2b55
commit 961a5005dc

View file

@ -4,31 +4,35 @@
send me an e-mail: kristianl@tkjelectronics.com send me an e-mail: kristianl@tkjelectronics.com
*/ */
#include "PS3BT.h"
#include "SPP.h" #include <PS3BT.h>
#include <SPP.h>
USB Usb; USB Usb;
BTD Btd(&Usb); BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instances of the class in two ways */
/* 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 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); // 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 //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; boolean firstMessage = true;
String analogOutput; // We will store the data in these string so we doesn't overflow the dongle
String digitalOutput;
void setup() { void setup() {
Serial.begin(115200); // This wil lprint the debugging from the libraries Serial.begin(115200); // This wil lprint the debugging from the libraries
if (Usb.Init() == -1) { if (Usb.Init() == -1) {
SerialBT.print(F("\r\nOSC did not start")); Serial.print(F("\r\nOSC did not start"));
while(1); //halt while(1); //halt
} }
Serial.print(F("\r\nPS3 Bluetooth Library Started")); Serial.print(F("\r\nPS3 Bluetooth Library Started"));
} }
void loop() { void loop() {
Usb.Task(); Usb.Task();
if(SerialBT.connected) {
if(SerialBT.connected) {
if(firstMessage) { if(firstMessage) {
firstMessage = false; firstMessage = false;
SerialBT.println(F("Hello from Arduino")); // Send welcome message SerialBT.println(F("Hello from Arduino")); // Send welcome message
@ -41,77 +45,90 @@ void loop() {
else else
firstMessage = true; 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 || 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) { if(PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117) {
SerialBT.print(F("LeftHatX: ")); analogOutput += "LeftHatX: ";
SerialBT.print(PS3.getAnalogHat(LeftHatX)); analogOutput += PS3.getAnalogHat(LeftHatX);
SerialBT.print("\t"); analogOutput += "\t";
} if(PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117) { }
SerialBT.print(F("LeftHatY: ")); if(PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117) {
SerialBT.print(PS3.getAnalogHat(LeftHatY)); analogOutput += "LeftHatY: ";
SerialBT.print("\t"); analogOutput += PS3.getAnalogHat(LeftHatY);
} if(PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117) { analogOutput += "\t";
SerialBT.print(F("RightHatX: ")); }
SerialBT.print(PS3.getAnalogHat(RightHatX)); if(PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117) {
SerialBT.print("\t"); analogOutput += "RightHatX: ";
} if(PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { analogOutput += PS3.getAnalogHat(RightHatX);
SerialBT.print(F("RightHatY: ")); analogOutput += "\t";
SerialBT.print(PS3.getAnalogHat(RightHatY)); }
if(PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) {
analogOutput += "RightHatY: ";
analogOutput += PS3.getAnalogHat(RightHatY);
analogOutput += "\t";
} }
SerialBT.println("");
} }
//Analog button values can be read from almost all buttons //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) || PS3.getAnalogButton(R2_ANALOG)) {
if(PS3.getAnalogButton(L2_ANALOG) > 0) { if(analogOutput != "")
SerialBT.print(F("L2: ")); analogOutput += "\r\n";
SerialBT.print(PS3.getAnalogButton(L2_ANALOG)); if(PS3.getAnalogButton(L2_ANALOG)) {
SerialBT.print("\t"); analogOutput += "L2: ";
} if(PS3.getAnalogButton(R2_ANALOG) > 0) { analogOutput += PS3.getAnalogButton(L2_ANALOG);
SerialBT.print(F("R2: ")); analogOutput += "\t";
SerialBT.print(PS3.getAnalogButton(R2_ANALOG));
} }
SerialBT.println(""); if(PS3.getAnalogButton(R2_ANALOG)) {
analogOutput += "R2: ";
analogOutput += PS3.getAnalogButton(R2_ANALOG);
analogOutput += "\t";
}
}
if(analogOutput != "") {
Serial.println(analogOutput);
if(SerialBT.connected)
SerialBT.println(analogOutput);
} }
if(PS3.buttonPressed) if(PS3.buttonPressed) {
{ digitalOutput = "PS3 Controller";
SerialBT.print(F("PS3 Controller"));
if(PS3.getButton(PS)) { if(PS3.getButton(PS)) {
SerialBT.print(F(" - PS")); digitalOutput += " - PS";
PS3.disconnect(); PS3.disconnect();
} else { }
else {
if(PS3.getButton(TRIANGLE)) if(PS3.getButton(TRIANGLE))
SerialBT.print(F(" - Traingle")); digitalOutput += " - Traingle";
if(PS3.getButton(CIRCLE)) if(PS3.getButton(CIRCLE))
SerialBT.print(F(" - Circle")); digitalOutput += " - Circle";
if(PS3.getButton(CROSS)) if(PS3.getButton(CROSS))
SerialBT.print(F(" - Cross")); digitalOutput += " - Cross";
if(PS3.getButton(SQUARE)) if(PS3.getButton(SQUARE))
SerialBT.print(F(" - Square")); digitalOutput += " - Square";
if(PS3.getButton(UP)) { if(PS3.getButton(UP)) {
SerialBT.print(F(" - Up")); digitalOutput += " - UP";
if(PS3.PS3Connected) { if(PS3.PS3Connected) {
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED4); PS3.setLedOn(LED4);
} }
} if(PS3.getButton(RIGHT)) { }
SerialBT.print(F(" - Right")); if(PS3.getButton(RIGHT)) {
digitalOutput += " - Right";
if(PS3.PS3Connected) { if(PS3.PS3Connected) {
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED1); PS3.setLedOn(LED1);
} }
} if(PS3.getButton(DOWN)) { }
SerialBT.print(F(" - Down")); if(PS3.getButton(DOWN)) {
digitalOutput += " - Down";
if(PS3.PS3Connected) { if(PS3.PS3Connected) {
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED2); PS3.setLedOn(LED2);
} }
} if(PS3.getButton(LEFT)) { }
SerialBT.print(F(" - Left")); if(PS3.getButton(LEFT)) {
digitalOutput += " - Left";
if(PS3.PS3Connected) { if(PS3.PS3Connected) {
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED3); PS3.setLedOn(LED3);
@ -119,93 +136,27 @@ void loop() {
} }
if(PS3.getButton(L1)) if(PS3.getButton(L1))
SerialBT.print(F(" - L1")); digitalOutput += " - L1";
//if(PS3.getButton(L2)) if(PS3.getButton(L2))
//SerialBT.print(F(" - L2")); digitalOutput += " - L2";
if(PS3.getButton(L3)) if(PS3.getButton(L3))
SerialBT.print(F(" - L3")); digitalOutput += " - L3";
if(PS3.getButton(R1)) if(PS3.getButton(R1))
SerialBT.print(F(" - R1")); digitalOutput += " - R1";
//if(PS3.getButton(R2)) if(PS3.getButton(R2))
//SerialBT.print(F(" - R2")); digitalOutput += " - R2";
if(PS3.getButton(R3)) if(PS3.getButton(R3))
SerialBT.print(F(" - R3")); digitalOutput += " - R3";
if(PS3.getButton(SELECT)) { if(PS3.getButton(SELECT))
SerialBT.print(F(" - Select - ")); digitalOutput += " - Select";
// SerialBT.print(PS3.getStatusString()); if(PS3.getButton(START))
} if(PS3.getButton(START)) { digitalOutput += " - Start";
SerialBT.print(F(" - Start"));
printAngle = !printAngle; Serial.println(digitalOutput);
while(PS3.getButton(START)) if(SerialBT.connected)
Usb.Task(); SerialBT.println(digitalOutput);
}
SerialBT.println("");
} }
} }
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);
} }