Format PS3 examples

This commit is contained in:
Kristian Lauszus 2013-11-12 19:45:06 +01:00
parent 16c4272358
commit b50919b077
2 changed files with 173 additions and 193 deletions

View file

@ -1,6 +1,6 @@
/* /*
Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
For more information visit my blog: http://blog.tkjelectronics.dk/ or For more information visit my blog: http://blog.tkjelectronics.dk/ or
send me an e-mail: kristianl@tkjelectronics.com send me an e-mail: kristianl@tkjelectronics.com
*/ */
@ -22,152 +22,152 @@ void setup() {
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection 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) { if (Usb.Init() == -1) {
Serial.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(PS3.PS3Connected || PS3.PS3NavigationConnected) { 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 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) {
Serial.print(F("\r\nLeftHatX: ")); Serial.print(F("\r\nLeftHatX: "));
Serial.print(PS3.getAnalogHat(LeftHatX)); Serial.print(PS3.getAnalogHat(LeftHatX));
Serial.print(F("\tLeftHatY: ")); Serial.print(F("\tLeftHatY: "));
Serial.print(PS3.getAnalogHat(LeftHatY)); Serial.print(PS3.getAnalogHat(LeftHatY));
if(!PS3.PS3NavigationConnected) { if (!PS3.PS3NavigationConnected) {
Serial.print(F("\tRightHatX: ")); Serial.print(F("\tRightHatX: "));
Serial.print(PS3.getAnalogHat(RightHatX)); Serial.print(PS3.getAnalogHat(RightHatX));
Serial.print(F("\tRightHatY: ")); Serial.print(F("\tRightHatY: "));
Serial.print(PS3.getAnalogHat(RightHatY)); Serial.print(PS3.getAnalogHat(RightHatY));
} }
} }
//Analog button values can be read from almost all buttons // Analog button values can be read from almost all buttons
if(PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) { if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
Serial.print(F("\r\nL2: ")); Serial.print(F("\r\nL2: "));
Serial.print(PS3.getAnalogButton(L2)); Serial.print(PS3.getAnalogButton(L2));
if(!PS3.PS3NavigationConnected) { if (!PS3.PS3NavigationConnected) {
Serial.print(F("\tR2: ")); Serial.print(F("\tR2: "));
Serial.print(PS3.getAnalogButton(R2)); Serial.print(PS3.getAnalogButton(R2));
} }
} }
if(PS3.getButtonClick(PS)) { if (PS3.getButtonClick(PS)) {
Serial.print(F("\r\nPS")); Serial.print(F("\r\nPS"));
PS3.disconnect(); PS3.disconnect();
} }
else { else {
if(PS3.getButtonClick(TRIANGLE)) if (PS3.getButtonClick(TRIANGLE))
Serial.print(F("\r\nTraingle")); Serial.print(F("\r\nTraingle"));
if(PS3.getButtonClick(CIRCLE)) if (PS3.getButtonClick(CIRCLE))
Serial.print(F("\r\nCircle")); Serial.print(F("\r\nCircle"));
if(PS3.getButtonClick(CROSS)) if (PS3.getButtonClick(CROSS))
Serial.print(F("\r\nCross")); Serial.print(F("\r\nCross"));
if(PS3.getButtonClick(SQUARE)) if (PS3.getButtonClick(SQUARE))
Serial.print(F("\r\nSquare")); Serial.print(F("\r\nSquare"));
if(PS3.getButtonClick(UP)) { if (PS3.getButtonClick(UP)) {
Serial.print(F("\r\nUp")); Serial.print(F("\r\nUp"));
if(PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED4); PS3.setLedOn(LED4);
} }
} }
if(PS3.getButtonClick(RIGHT)) { if (PS3.getButtonClick(RIGHT)) {
Serial.print(F("\r\nRight")); Serial.print(F("\r\nRight"));
if(PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED1); PS3.setLedOn(LED1);
}
}
if(PS3.getButtonClick(DOWN)) {
Serial.print(F("\r\nDown"));
if(PS3.PS3Connected) {
PS3.setAllOff();
PS3.setLedOn(LED2);
} }
} }
if(PS3.getButtonClick(LEFT)) { if (PS3.getButtonClick(DOWN)) {
Serial.print(F("\r\nLeft")); Serial.print(F("\r\nDown"));
if(PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED3); PS3.setLedOn(LED2);
} }
} }
if (PS3.getButtonClick(LEFT)) {
Serial.print(F("\r\nLeft"));
if (PS3.PS3Connected) {
PS3.setAllOff();
PS3.setLedOn(LED3);
}
}
if(PS3.getButtonClick(L1)) if (PS3.getButtonClick(L1))
Serial.print(F("\r\nL1")); Serial.print(F("\r\nL1"));
if(PS3.getButtonClick(L3)) if (PS3.getButtonClick(L3))
Serial.print(F("\r\nL3")); Serial.print(F("\r\nL3"));
if(PS3.getButtonClick(R1)) if (PS3.getButtonClick(R1))
Serial.print(F("\r\nR1")); Serial.print(F("\r\nR1"));
if(PS3.getButtonClick(R3)) if (PS3.getButtonClick(R3))
Serial.print(F("\r\nR3")); Serial.print(F("\r\nR3"));
if(PS3.getButtonClick(SELECT)) { if (PS3.getButtonClick(SELECT)) {
Serial.print(F("\r\nSelect - ")); Serial.print(F("\r\nSelect - "));
Serial.print(PS3.getStatusString()); Serial.print(PS3.getStatusString());
} }
if(PS3.getButtonClick(START)) { if (PS3.getButtonClick(START)) {
Serial.print(F("\r\nStart")); Serial.print(F("\r\nStart"));
printAngle = !printAngle; printAngle = !printAngle;
} }
} }
if(printAngle) { if (printAngle) {
Serial.print(F("\r\nPitch: ")); Serial.print(F("\r\nPitch: "));
Serial.print(PS3.getAngle(Pitch)); Serial.print(PS3.getAngle(Pitch));
Serial.print(F("\tRoll: ")); Serial.print(F("\tRoll: "));
Serial.print(PS3.getAngle(Roll)); Serial.print(PS3.getAngle(Roll));
} }
} }
else if(PS3.PS3MoveConnected) { else if (PS3.PS3MoveConnected) {
if(PS3.getAnalogButton(T)) { if (PS3.getAnalogButton(T)) {
Serial.print(F("\r\nT: ")); Serial.print(F("\r\nT: "));
Serial.print(PS3.getAnalogButton(T)); Serial.print(PS3.getAnalogButton(T));
} }
if(PS3.getButtonClick(PS)) { if (PS3.getButtonClick(PS)) {
Serial.print(F("\r\nPS")); Serial.print(F("\r\nPS"));
PS3.disconnect(); PS3.disconnect();
} }
else { else {
if(PS3.getButtonClick(SELECT)) { if (PS3.getButtonClick(SELECT)) {
Serial.print(F("\r\nSelect")); Serial.print(F("\r\nSelect"));
printTemperature = !printTemperature; printTemperature = !printTemperature;
} }
if(PS3.getButtonClick(START)) { if (PS3.getButtonClick(START)) {
Serial.print(F("\r\nStart")); Serial.print(F("\r\nStart"));
printAngle = !printAngle; printAngle = !printAngle;
} }
if(PS3.getButtonClick(TRIANGLE)) { if (PS3.getButtonClick(TRIANGLE)) {
Serial.print(F("\r\nTriangle")); Serial.print(F("\r\nTriangle"));
PS3.moveSetBulb(Red); PS3.moveSetBulb(Red);
} }
if(PS3.getButtonClick(CIRCLE)) { if (PS3.getButtonClick(CIRCLE)) {
Serial.print(F("\r\nCircle")); Serial.print(F("\r\nCircle"));
PS3.moveSetBulb(Green); PS3.moveSetBulb(Green);
} }
if(PS3.getButtonClick(SQUARE)) { if (PS3.getButtonClick(SQUARE)) {
Serial.print(F("\r\nSquare")); Serial.print(F("\r\nSquare"));
PS3.moveSetBulb(Blue); PS3.moveSetBulb(Blue);
} }
if(PS3.getButtonClick(CROSS)) { if (PS3.getButtonClick(CROSS)) {
Serial.print(F("\r\nCross")); Serial.print(F("\r\nCross"));
PS3.moveSetBulb(Yellow); PS3.moveSetBulb(Yellow);
} }
if(PS3.getButtonClick(MOVE)) { if (PS3.getButtonClick(MOVE)) {
PS3.moveSetBulb(Off); PS3.moveSetBulb(Off);
Serial.print(F("\r\nMove")); Serial.print(F("\r\nMove"));
Serial.print(F(" - ")); Serial.print(F(" - "));
Serial.print(PS3.getStatusString()); Serial.print(PS3.getStatusString());
} }
} }
if(printAngle) { if (printAngle) {
Serial.print(F("\r\nPitch: ")); Serial.print(F("\r\nPitch: "));
Serial.print(PS3.getAngle(Pitch)); Serial.print(PS3.getAngle(Pitch));
Serial.print(F("\tRoll: ")); Serial.print(F("\tRoll: "));
Serial.print(PS3.getAngle(Roll)); Serial.print(PS3.getAngle(Roll));
} }
else if(printTemperature) { else if (printTemperature) {
Serial.print(F("\r\nTemperature: ")); Serial.print(F("\r\nTemperature: "));
Serial.print(PS3.getTemperature()); Serial.print(PS3.getTemperature());
} }

View file

@ -1,6 +1,6 @@
/* /*
Example sketch for the PS3 USB library - developed by Kristian Lauszus Example sketch for the PS3 USB library - developed by Kristian Lauszus
For more information visit my blog: http://blog.tkjelectronics.dk/ or For more information visit my blog: http://blog.tkjelectronics.dk/ or
send me an e-mail: kristianl@tkjelectronics.com send me an e-mail: kristianl@tkjelectronics.com
*/ */
@ -19,142 +19,122 @@ void setup() {
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection 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) { if (Usb.Init() == -1) {
Serial.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 USB Library Started")); Serial.print(F("\r\nPS3 USB Library Started"));
} }
void loop() { void loop() {
Usb.Task(); Usb.Task();
if(PS3.PS3Connected || PS3.PS3NavigationConnected) { 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 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) {
Serial.print(F("\r\nLeftHatX: ")); Serial.print(F("\r\nLeftHatX: "));
Serial.print(PS3.getAnalogHat(LeftHatX)); Serial.print(PS3.getAnalogHat(LeftHatX));
Serial.print(F("\tLeftHatY: ")); Serial.print(F("\tLeftHatY: "));
Serial.print(PS3.getAnalogHat(LeftHatY)); Serial.print(PS3.getAnalogHat(LeftHatY));
if(!PS3.PS3NavigationConnected) { if (!PS3.PS3NavigationConnected) {
Serial.print(F("\tRightHatX: ")); Serial.print(F("\tRightHatX: "));
Serial.print(PS3.getAnalogHat(RightHatX)); Serial.print(PS3.getAnalogHat(RightHatX));
Serial.print(F("\tRightHatY: ")); Serial.print(F("\tRightHatY: "));
Serial.print(PS3.getAnalogHat(RightHatY)); Serial.print(PS3.getAnalogHat(RightHatY));
} }
} }
// Analog button values can be read from almost all buttons // Analog button values can be read from almost all buttons
if(PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) { if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
Serial.print(F("\r\nL2: ")); Serial.print(F("\r\nL2: "));
Serial.print(PS3.getAnalogButton(L2)); Serial.print(PS3.getAnalogButton(L2));
if(!PS3.PS3NavigationConnected) { if (!PS3.PS3NavigationConnected) {
Serial.print(F("\tR2: ")); Serial.print(F("\tR2: "));
Serial.print(PS3.getAnalogButton(R2)); Serial.print(PS3.getAnalogButton(R2));
} }
} }
if(PS3.getButtonClick(PS)) if (PS3.getButtonClick(PS))
Serial.print(F("\r\nPS")); Serial.print(F("\r\nPS"));
if(PS3.getButtonClick(TRIANGLE)) if (PS3.getButtonClick(TRIANGLE))
Serial.print(F("\r\nTraingle")); Serial.print(F("\r\nTraingle"));
if(PS3.getButtonClick(CIRCLE)) if (PS3.getButtonClick(CIRCLE))
Serial.print(F("\r\nCircle")); Serial.print(F("\r\nCircle"));
if(PS3.getButtonClick(CROSS)) if (PS3.getButtonClick(CROSS))
Serial.print(F("\r\nCross")); Serial.print(F("\r\nCross"));
if(PS3.getButtonClick(SQUARE)) if (PS3.getButtonClick(SQUARE))
Serial.print(F("\r\nSquare")); Serial.print(F("\r\nSquare"));
if(PS3.getButtonClick(UP)) { if (PS3.getButtonClick(UP)) {
Serial.print(F("\r\nUp")); Serial.print(F("\r\nUp"));
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED4); PS3.setLedOn(LED4);
} }
if(PS3.getButtonClick(RIGHT)) { if (PS3.getButtonClick(RIGHT)) {
Serial.print(F("\r\nRight")); Serial.print(F("\r\nRight"));
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED1); PS3.setLedOn(LED1);
} }
if(PS3.getButtonClick(DOWN)) { if (PS3.getButtonClick(DOWN)) {
Serial.print(F("\r\nDown")); Serial.print(F("\r\nDown"));
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED2); PS3.setLedOn(LED2);
} }
if(PS3.getButtonClick(LEFT)) { if (PS3.getButtonClick(LEFT)) {
Serial.print(F("\r\nLeft")); Serial.print(F("\r\nLeft"));
PS3.setAllOff(); PS3.setAllOff();
PS3.setLedOn(LED3); PS3.setLedOn(LED3);
} }
if(PS3.getButtonClick(L1)) if (PS3.getButtonClick(L1))
Serial.print(F("\r\nL1")); Serial.print(F("\r\nL1"));
if(PS3.getButtonClick(L3)) if (PS3.getButtonClick(L3))
Serial.print(F("\r\nL3")); Serial.print(F("\r\nL3"));
if(PS3.getButtonClick(R1)) if (PS3.getButtonClick(R1))
Serial.print(F("\r\nR1")); Serial.print(F("\r\nR1"));
if(PS3.getButtonClick(R3)) if (PS3.getButtonClick(R3))
Serial.print(F("\r\nR3")); Serial.print(F("\r\nR3"));
if(PS3.getButtonClick(SELECT)) { if (PS3.getButtonClick(SELECT)) {
Serial.print(F("\r\nSelect - ")); Serial.print(F("\r\nSelect - "));
Serial.print(PS3.getStatusString()); 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:
PS3.moveSetRumble(0);
PS3.moveSetBulb(Off);
state = 1;
break;
case 1:
PS3.moveSetRumble(75);
PS3.moveSetBulb(Red);
state = 2;
break;
case 2:
PS3.moveSetRumble(125);
PS3.moveSetBulb(Green);
state = 3;
break;
case 3:
PS3.moveSetRumble(150);
PS3.moveSetBulb(Blue);
state = 4;
break;
case 4:
PS3.moveSetRumble(175);
PS3.moveSetBulb(Yellow);
state = 5;
break;
case 5:
PS3.moveSetRumble(200);
PS3.moveSetBulb(Lightblue);
state = 6;
break;
case 6:
PS3.moveSetRumble(225);
PS3.moveSetBulb(Purble);
state = 7;
break;
case 7:
PS3.moveSetRumble(250);
PS3.moveSetBulb(White);
state = 0;
break;
} }
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
if (state == 0) {
PS3.moveSetRumble(0);
PS3.moveSetBulb(Off);
} else if (state == 1) {
PS3.moveSetRumble(75);
PS3.moveSetBulb(Red);
} else if (state == 2) {
PS3.moveSetRumble(125);
PS3.moveSetBulb(Green);
} else if (state == 3) {
PS3.moveSetRumble(150);
PS3.moveSetBulb(Blue);
} else if (state == 4) {
PS3.moveSetRumble(175);
PS3.moveSetBulb(Yellow);
} else if (state == 5) {
PS3.moveSetRumble(200);
PS3.moveSetBulb(Lightblue);
} else if (state == 6) {
PS3.moveSetRumble(225);
PS3.moveSetBulb(Purble);
} else if (state == 7) {
PS3.moveSetRumble(250);
PS3.moveSetBulb(White);
}
state++;
if (state > 7)
state = 0;
delay(1000); delay(1000);
} }
} }