2012-03-04 02:38:50 +01:00
/*
Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
For more information visit my blog : http : //blog.tkjelectronics.dk/ or
send me an e - mail : kristianl @ tkjelectronics . com
2012-03-26 16:25:24 +02:00
*/
2012-03-04 02:38:50 +01:00
2012-03-02 08:34:29 +01:00
# include <PS3BT.h>
USB Usb ;
2012-04-12 23:15:18 +02:00
/* You can create the instance of the class in two ways */
PS3BT BT ( & Usb ) ; // This will just create the instance
//PS3BT BT(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
2012-03-02 08:34:29 +01:00
boolean printTemperature ;
2012-04-12 23:15:18 +02:00
boolean printAngle ;
2012-03-02 08:34:29 +01:00
void setup ( )
{
Serial . begin ( 115200 ) ;
2012-03-26 16:25:24 +02:00
if ( Usb . Init ( ) = = - 1 ) {
Serial . print ( F ( " \r \n OSC did not start " ) ) ;
2012-03-02 08:34:29 +01:00
while ( 1 ) ; //halt
}
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " \r \n PS3 Bluetooth Library Started " ) ) ;
2012-03-02 08:34:29 +01:00
}
void loop ( )
{
Usb . Task ( ) ;
2012-03-26 16:25:24 +02:00
if ( BT . PS3BTConnected | | BT . PS3NavigationBTConnected ) {
if ( BT . getAnalogHat ( LeftHatX ) > 137 | | BT . getAnalogHat ( LeftHatX ) < 117 | | BT . getAnalogHat ( LeftHatY ) > 137 | | BT . getAnalogHat ( LeftHatY ) < 117 | | BT . getAnalogHat ( RightHatX ) > 137 | | BT . getAnalogHat ( RightHatX ) < 117 | | BT . getAnalogHat ( RightHatY ) > 137 | | BT . getAnalogHat ( RightHatY ) < 117 ) {
if ( BT . getAnalogHat ( LeftHatX ) > 137 | | BT . getAnalogHat ( LeftHatX ) < 117 ) {
Serial . print ( F ( " LeftHatX: " ) ) ;
2012-04-12 23:15:18 +02:00
Serial . print ( BT . getAnalogHat ( LeftHatX ) ) ;
2012-03-26 16:25:24 +02:00
Serial . print ( " \t " ) ;
} if ( BT . getAnalogHat ( LeftHatY ) > 137 | | BT . getAnalogHat ( LeftHatY ) < 117 ) {
Serial . print ( F ( " LeftHatY: " ) ) ;
2012-04-12 23:15:18 +02:00
Serial . print ( BT . getAnalogHat ( LeftHatY ) ) ;
2012-03-26 16:25:24 +02:00
Serial . print ( " \t " ) ;
} if ( BT . getAnalogHat ( RightHatX ) > 137 | | BT . getAnalogHat ( RightHatX ) < 117 ) {
Serial . print ( F ( " RightHatX: " ) ) ;
2012-04-12 23:15:18 +02:00
Serial . print ( BT . getAnalogHat ( RightHatX ) ) ;
2012-03-26 16:25:24 +02:00
Serial . print ( " \t " ) ;
} if ( BT . getAnalogHat ( RightHatY ) > 137 | | BT . getAnalogHat ( RightHatY ) < 117 ) {
Serial . print ( F ( " RightHatY: " ) ) ;
2012-04-12 23:15:18 +02:00
Serial . print ( BT . getAnalogHat ( RightHatY ) ) ;
2012-03-26 16:25:24 +02:00
}
Serial . println ( " " ) ;
2012-03-02 08:34:29 +01:00
}
//Analog button values can be read from almost all buttons
2012-03-26 16:25:24 +02:00
if ( BT . getAnalogButton ( L2_ANALOG ) > 0 | | BT . getAnalogButton ( R2_ANALOG ) > 0 ) {
if ( BT . getAnalogButton ( L2_ANALOG ) > 0 ) {
Serial . print ( F ( " L2: " ) ) ;
2012-04-12 23:15:18 +02:00
Serial . print ( BT . getAnalogButton ( L2_ANALOG ) ) ;
2012-03-26 16:25:24 +02:00
Serial . print ( " \t " ) ;
} if ( BT . getAnalogButton ( R2_ANALOG ) > 0 ) {
Serial . print ( F ( " R2: " ) ) ;
2012-04-12 23:15:18 +02:00
Serial . print ( BT . getAnalogButton ( R2_ANALOG ) ) ;
2012-03-26 16:25:24 +02:00
}
Serial . println ( " " ) ;
2012-03-02 08:34:29 +01:00
}
2012-04-11 02:18:35 +02:00
if ( BT . buttonPressed )
2012-03-02 08:34:29 +01:00
{
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " PS3 Controller " ) ) ;
if ( BT . getButton ( PS ) ) {
Serial . print ( F ( " - PS " ) ) ;
2012-03-02 08:34:29 +01:00
BT . disconnect ( ) ;
2012-03-26 16:25:24 +02:00
} else {
2012-03-02 08:34:29 +01:00
if ( BT . getButton ( TRIANGLE ) )
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - Traingle " ) ) ;
2012-03-02 08:34:29 +01:00
if ( BT . getButton ( CIRCLE ) )
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - Circle " ) ) ;
2012-03-02 08:34:29 +01:00
if ( BT . getButton ( CROSS ) )
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - Cross " ) ) ;
2012-03-02 08:34:29 +01:00
if ( BT . getButton ( SQUARE ) )
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - Square " ) ) ;
2012-03-02 08:34:29 +01:00
2012-03-26 16:25:24 +02:00
if ( BT . getButton ( UP ) ) {
Serial . print ( F ( " - Up " ) ) ;
2012-03-02 08:34:29 +01:00
BT . setAllOff ( ) ;
BT . setLedOn ( LED4 ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( RIGHT ) ) {
Serial . print ( F ( " - Right " ) ) ;
2012-03-02 08:34:29 +01:00
BT . setAllOff ( ) ;
BT . setLedOn ( LED1 ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( DOWN ) ) {
Serial . print ( F ( " - Down " ) ) ;
2012-03-02 08:34:29 +01:00
BT . setAllOff ( ) ;
BT . setLedOn ( LED2 ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( LEFT ) ) {
Serial . print ( F ( " - Left " ) ) ;
2012-03-02 08:34:29 +01:00
BT . setAllOff ( ) ;
BT . setLedOn ( LED3 ) ;
}
if ( BT . getButton ( L1 ) )
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - L1 " ) ) ;
2012-03-02 08:34:29 +01:00
//if(BT.getButton(L2))
2012-04-11 02:18:35 +02:00
//Serial.print(F(" - L2"));
2012-03-02 08:34:29 +01:00
if ( BT . getButton ( L3 ) )
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - L3 " ) ) ;
2012-03-02 08:34:29 +01:00
if ( BT . getButton ( R1 ) )
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - R1 " ) ) ;
2012-03-02 08:34:29 +01:00
//if(BT.getButton(R2))
2012-03-26 16:25:24 +02:00
//Serial.print(F(" - R2"));
2012-03-02 08:34:29 +01:00
if ( BT . getButton ( R3 ) )
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - R3 " ) ) ;
2012-03-02 08:34:29 +01:00
2012-03-26 16:25:24 +02:00
if ( BT . getButton ( SELECT ) ) {
Serial . print ( F ( " - Select - " ) ) ;
2012-03-02 08:34:29 +01:00
Serial . print ( BT . getStatusString ( ) ) ;
2012-04-12 23:15:18 +02:00
} if ( BT . getButton ( START ) ) {
Serial . print ( F ( " - Start " ) ) ;
printAngle = ! printAngle ;
while ( BT . getButton ( START ) )
Usb . Task ( ) ;
}
2012-03-26 16:25:24 +02:00
Serial . println ( " " ) ;
}
2012-04-12 23:15:18 +02:00
}
if ( printAngle ) {
Serial . print ( F ( " Pitch: " ) ) ;
2012-04-24 01:35:43 +02:00
Serial . print ( BT . getAngle ( Pitch ) ) ;
2012-04-12 23:15:18 +02:00
Serial . print ( F ( " \t Roll: " ) ) ;
2012-04-24 01:35:43 +02:00
Serial . println ( BT . getAngle ( Roll ) ) ;
2012-04-12 23:15:18 +02:00
}
2012-03-02 08:34:29 +01:00
}
else if ( BT . PS3MoveBTConnected )
{
2012-03-26 16:25:24 +02:00
if ( BT . getAnalogButton ( T_MOVE_ANALOG ) > 0 ) {
Serial . print ( F ( " T: " ) ) ;
2012-04-12 23:15:18 +02:00
Serial . println ( BT . getAnalogButton ( T_MOVE_ANALOG ) ) ;
2012-04-11 02:18:35 +02:00
} if ( BT . buttonPressed ) {
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " PS3 Move Controller " ) ) ;
if ( BT . getButton ( PS_MOVE ) ) {
Serial . print ( F ( " - PS " ) ) ;
2012-03-02 08:34:29 +01:00
BT . disconnect ( ) ;
2012-03-26 16:25:24 +02:00
} else {
if ( BT . getButton ( SELECT_MOVE ) ) {
Serial . print ( F ( " - Select " ) ) ;
2012-04-24 22:49:34 +02:00
printTemperature = ! printTemperature ;
while ( BT . getButton ( SELECT_MOVE ) )
Usb . Task ( ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( START_MOVE ) ) {
Serial . print ( F ( " - Start " ) ) ;
2012-04-24 22:49:34 +02:00
printAngle = ! printAngle ;
while ( BT . getButton ( START_MOVE ) )
Usb . Task ( ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( TRIANGLE_MOVE ) ) {
Serial . print ( F ( " - Triangle " ) ) ;
2012-03-02 08:34:29 +01:00
BT . moveSetBulb ( Red ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( CIRCLE_MOVE ) ) {
Serial . print ( F ( " - Circle " ) ) ;
2012-03-02 08:34:29 +01:00
BT . moveSetBulb ( Green ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( SQUARE_MOVE ) ) {
Serial . print ( F ( " - Square " ) ) ;
2012-03-02 08:34:29 +01:00
BT . moveSetBulb ( Blue ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( CROSS_MOVE ) ) {
Serial . print ( F ( " - Cross " ) ) ;
2012-03-02 08:34:29 +01:00
BT . moveSetBulb ( Yellow ) ;
2012-03-26 16:25:24 +02:00
} if ( BT . getButton ( MOVE_MOVE ) ) {
2012-03-02 08:34:29 +01:00
BT . moveSetBulb ( Off ) ;
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " - Move " ) ) ;
Serial . print ( F ( " - " ) ) ;
2012-03-02 08:34:29 +01:00
Serial . print ( BT . getStatusString ( ) ) ;
}
2012-03-26 16:25:24 +02:00
//if(BT.getButton(T))
//Serial.print(F(" - T"));
Serial . println ( " " ) ;
2012-03-02 08:34:29 +01:00
}
}
2012-04-24 22:49:34 +02:00
if ( printAngle ) {
Serial . print ( F ( " Pitch: " ) ) ;
Serial . print ( BT . getAngle ( Pitch ) ) ;
Serial . print ( F ( " \t Roll: " ) ) ;
Serial . println ( BT . getAngle ( Roll ) ) ;
}
else if ( printTemperature ) {
2012-03-02 08:34:29 +01:00
String templow ;
String temphigh ;
2012-04-12 23:15:18 +02:00
String input = String ( BT . getSensor ( tempMove ) ) ;
2012-03-02 08:34:29 +01:00
2012-03-26 16:25:24 +02:00
if ( input . length ( ) > 3 ) {
2012-03-02 08:34:29 +01:00
temphigh = input . substring ( 0 , 2 ) ;
templow = input . substring ( 2 ) ;
2012-03-26 16:25:24 +02:00
} else {
2012-03-02 08:34:29 +01:00
temphigh = input . substring ( 0 , 1 ) ;
templow = input . substring ( 1 ) ;
}
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " Temperature: " ) ) ;
2012-03-02 08:34:29 +01:00
Serial . print ( temphigh ) ;
2012-03-26 16:25:24 +02:00
Serial . print ( F ( " . " ) ) ;
Serial . println ( templow ) ;
2012-03-02 08:34:29 +01:00
}
}
2012-04-11 02:18:35 +02:00
}