2014-01-10 17:44:51 +01:00
/*
Example sketch for the PS4 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
*/
# include <PS4BT.h>
# include <usbhub.h>
// Satisfy IDE, which only needs to see the include statment in the ino.
# ifdef dobogusinclude
# include <spi4teensy3.h>
# endif
USB Usb ;
//USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd ( & Usb ) ; // You have to create the Bluetooth Dongle instance like so
2014-01-18 22:36:01 +01:00
/* You can create the instance of the PS4BT class in two ways */
2014-01-10 17:44:51 +01:00
// This will start an inquiry and then pair with the PS4 controller - you only have to do this once
2014-01-12 16:31:47 +01:00
// You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in paring mode
2014-01-18 22:36:01 +01:00
PS4BT PS4 ( & Btd , PAIR ) ;
2014-01-10 17:44:51 +01:00
// After that you can simply create the instance like so and then press the PS button on the device
2014-01-18 22:36:01 +01:00
//PS4BT PS4(&Btd);
2014-01-10 17:44:51 +01:00
2014-01-22 05:13:49 +01:00
boolean printAngle , printTouch ;
2014-01-10 17:44:51 +01:00
void setup ( ) {
Serial . begin ( 115200 ) ;
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 ) {
Serial . print ( F ( " \r \n OSC did not start " ) ) ;
while ( 1 ) ; // Halt
}
Serial . print ( F ( " \r \n PS4 Bluetooth Library Started " ) ) ;
}
void loop ( ) {
Usb . Task ( ) ;
if ( PS4 . connected ( ) ) {
if ( PS4 . getAnalogHat ( LeftHatX ) > 137 | | PS4 . getAnalogHat ( LeftHatX ) < 117 | | PS4 . getAnalogHat ( LeftHatY ) > 137 | | PS4 . getAnalogHat ( LeftHatY ) < 117 | | PS4 . getAnalogHat ( RightHatX ) > 137 | | PS4 . getAnalogHat ( RightHatX ) < 117 | | PS4 . getAnalogHat ( RightHatY ) > 137 | | PS4 . getAnalogHat ( RightHatY ) < 117 ) {
Serial . print ( F ( " \r \n LeftHatX: " ) ) ;
Serial . print ( PS4 . getAnalogHat ( LeftHatX ) ) ;
Serial . print ( F ( " \t LeftHatY: " ) ) ;
Serial . print ( PS4 . getAnalogHat ( LeftHatY ) ) ;
Serial . print ( F ( " \t RightHatX: " ) ) ;
Serial . print ( PS4 . getAnalogHat ( RightHatX ) ) ;
Serial . print ( F ( " \t RightHatY: " ) ) ;
Serial . print ( PS4 . getAnalogHat ( RightHatY ) ) ;
}
if ( PS4 . getAnalogButton ( L2 ) | | PS4 . getAnalogButton ( R2 ) ) { // These are the only analog buttons on the PS4 controller
Serial . print ( F ( " \r \n L2: " ) ) ;
Serial . print ( PS4 . getAnalogButton ( L2 ) ) ;
Serial . print ( F ( " \t R2: " ) ) ;
Serial . print ( PS4 . getAnalogButton ( R2 ) ) ;
}
if ( PS4 . getButtonClick ( PS ) ) {
Serial . print ( F ( " \r \n PS " ) ) ;
PS4 . disconnect ( ) ;
}
else {
if ( PS4 . getButtonClick ( TRIANGLE ) )
Serial . print ( F ( " \r \n Traingle " ) ) ;
if ( PS4 . getButtonClick ( CIRCLE ) )
Serial . print ( F ( " \r \n Circle " ) ) ;
if ( PS4 . getButtonClick ( CROSS ) )
Serial . print ( F ( " \r \n Cross " ) ) ;
if ( PS4 . getButtonClick ( SQUARE ) )
Serial . print ( F ( " \r \n Square " ) ) ;
if ( PS4 . getButtonClick ( UP ) )
Serial . print ( F ( " \r \n Up " ) ) ;
if ( PS4 . getButtonClick ( RIGHT ) )
Serial . print ( F ( " \r \n Right " ) ) ;
if ( PS4 . getButtonClick ( DOWN ) )
Serial . print ( F ( " \r \n Down " ) ) ;
if ( PS4 . getButtonClick ( LEFT ) )
Serial . print ( F ( " \r \n Left " ) ) ;
if ( PS4 . getButtonClick ( L1 ) )
Serial . print ( F ( " \r \n L1 " ) ) ;
if ( PS4 . getButtonClick ( L3 ) )
Serial . print ( F ( " \r \n L3 " ) ) ;
if ( PS4 . getButtonClick ( R1 ) )
Serial . print ( F ( " \r \n R1 " ) ) ;
if ( PS4 . getButtonClick ( R3 ) )
Serial . print ( F ( " \r \n R3 " ) ) ;
if ( PS4 . getButtonClick ( SHARE ) )
Serial . print ( F ( " \r \n Share " ) ) ;
2014-01-22 05:13:49 +01:00
if ( PS4 . getButtonClick ( OPTIONS ) ) {
2014-01-10 17:44:51 +01:00
Serial . print ( F ( " \r \n Options " ) ) ;
2014-01-22 05:13:49 +01:00
printAngle = ! printAngle ;
}
if ( PS4 . getButtonClick ( TOUCHPAD ) ) {
2014-01-12 16:33:19 +01:00
Serial . print ( F ( " \r \n Touchpad " ) ) ;
2014-01-22 05:13:49 +01:00
printTouch = ! printTouch ;
}
if ( printAngle ) { // Print angle calculated using the accelerometer only
Serial . print ( F ( " \r \n Pitch: " ) ) ;
Serial . print ( PS4 . getAngle ( Pitch ) ) ;
Serial . print ( F ( " \t Roll: " ) ) ;
Serial . print ( PS4 . getAngle ( Roll ) ) ;
}
if ( printTouch ) { // Print the x, y coordinates of the touchpad
if ( PS4 . isTouching ( 0 ) | | PS4 . isTouching ( 1 ) ) // Print newline and carriage return if any of the fingers are touching the touchpad
Serial . print ( F ( " \r \n " ) ) ;
for ( uint8_t i = 0 ; i < 2 ; i + + ) { // The touchpad track two fingers
if ( PS4 . isTouching ( i ) ) { // Print the position of the finger if it is touching the touchpad
Serial . print ( F ( " X " ) ) ; Serial . print ( i + 1 ) ; Serial . print ( F ( " : " ) ) ;
Serial . print ( PS4 . getX ( i ) ) ;
Serial . print ( F ( " \t Y " ) ) ; Serial . print ( i + 1 ) ; Serial . print ( F ( " : " ) ) ;
Serial . print ( PS4 . getY ( i ) ) ;
Serial . print ( F ( " \t " ) ) ;
}
}
}
2014-01-10 17:44:51 +01:00
}
}
}