You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

PS4BT.ino 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Example sketch for the PS4 Bluetooth library - developed by Kristian Lauszus
  3. For more information visit my blog: http://blog.tkjelectronics.dk/ or
  4. send me an e-mail: [email protected]
  5. */
  6. #include <PS4BT.h>
  7. #include <usbhub.h>
  8. // Satisfy the IDE, which needs to see the include statment in the ino too.
  9. #ifdef dobogusinclude
  10. #include <spi4teensy3.h>
  11. #include <SPI.h>
  12. #endif
  13. USB Usb;
  14. //USBHub Hub1(&Usb); // Some dongles have a hub inside
  15. BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
  16. /* You can create the instance of the PS4BT class in two ways */
  17. // This will start an inquiry and then pair with the PS4 controller - you only have to do this once
  18. // 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
  19. PS4BT PS4(&Btd, PAIR);
  20. // After that you can simply create the instance like so and then press the PS button on the device
  21. //PS4BT PS4(&Btd);
  22. bool printAngle, printTouch;
  23. uint8_t oldL2Value, oldR2Value;
  24. void setup() {
  25. Serial.begin(115200);
  26. #if !defined(__MIPSEL__)
  27. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  28. #endif
  29. if (Usb.Init() == -1) {
  30. Serial.print(F("\r\nOSC did not start"));
  31. while (1); // Halt
  32. }
  33. Serial.print(F("\r\nPS4 Bluetooth Library Started"));
  34. }
  35. void loop() {
  36. Usb.Task();
  37. if (PS4.connected()) {
  38. 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) {
  39. Serial.print(F("\r\nLeftHatX: "));
  40. Serial.print(PS4.getAnalogHat(LeftHatX));
  41. Serial.print(F("\tLeftHatY: "));
  42. Serial.print(PS4.getAnalogHat(LeftHatY));
  43. Serial.print(F("\tRightHatX: "));
  44. Serial.print(PS4.getAnalogHat(RightHatX));
  45. Serial.print(F("\tRightHatY: "));
  46. Serial.print(PS4.getAnalogHat(RightHatY));
  47. }
  48. if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller
  49. Serial.print(F("\r\nL2: "));
  50. Serial.print(PS4.getAnalogButton(L2));
  51. Serial.print(F("\tR2: "));
  52. Serial.print(PS4.getAnalogButton(R2));
  53. }
  54. if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different
  55. PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
  56. oldL2Value = PS4.getAnalogButton(L2);
  57. oldR2Value = PS4.getAnalogButton(R2);
  58. if (PS4.getButtonClick(PS)) {
  59. Serial.print(F("\r\nPS"));
  60. PS4.disconnect();
  61. }
  62. else {
  63. if (PS4.getButtonClick(TRIANGLE)) {
  64. Serial.print(F("\r\nTraingle"));
  65. PS4.setRumbleOn(RumbleLow);
  66. }
  67. if (PS4.getButtonClick(CIRCLE)) {
  68. Serial.print(F("\r\nCircle"));
  69. PS4.setRumbleOn(RumbleHigh);
  70. }
  71. if (PS4.getButtonClick(CROSS)) {
  72. Serial.print(F("\r\nCross"));
  73. PS4.setLedFlash(10, 10); // Set it to blink rapidly
  74. }
  75. if (PS4.getButtonClick(SQUARE)) {
  76. Serial.print(F("\r\nSquare"));
  77. PS4.setLedFlash(0, 0); // Turn off blinking
  78. }
  79. if (PS4.getButtonClick(UP)) {
  80. Serial.print(F("\r\nUp"));
  81. PS4.setLed(Red);
  82. } if (PS4.getButtonClick(RIGHT)) {
  83. Serial.print(F("\r\nRight"));
  84. PS4.setLed(Blue);
  85. } if (PS4.getButtonClick(DOWN)) {
  86. Serial.print(F("\r\nDown"));
  87. PS4.setLed(Yellow);
  88. } if (PS4.getButtonClick(LEFT)) {
  89. Serial.print(F("\r\nLeft"));
  90. PS4.setLed(Green);
  91. }
  92. if (PS4.getButtonClick(L1))
  93. Serial.print(F("\r\nL1"));
  94. if (PS4.getButtonClick(L3))
  95. Serial.print(F("\r\nL3"));
  96. if (PS4.getButtonClick(R1))
  97. Serial.print(F("\r\nR1"));
  98. if (PS4.getButtonClick(R3))
  99. Serial.print(F("\r\nR3"));
  100. if (PS4.getButtonClick(SHARE))
  101. Serial.print(F("\r\nShare"));
  102. if (PS4.getButtonClick(OPTIONS)) {
  103. Serial.print(F("\r\nOptions"));
  104. printAngle = !printAngle;
  105. }
  106. if (PS4.getButtonClick(TOUCHPAD)) {
  107. Serial.print(F("\r\nTouchpad"));
  108. printTouch = !printTouch;
  109. }
  110. if (printAngle) { // Print angle calculated using the accelerometer only
  111. Serial.print(F("\r\nPitch: "));
  112. Serial.print(PS4.getAngle(Pitch));
  113. Serial.print(F("\tRoll: "));
  114. Serial.print(PS4.getAngle(Roll));
  115. }
  116. if (printTouch) { // Print the x, y coordinates of the touchpad
  117. if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
  118. Serial.print(F("\r\n"));
  119. for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
  120. if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
  121. Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
  122. Serial.print(PS4.getX(i));
  123. Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": "));
  124. Serial.print(PS4.getY(i));
  125. Serial.print(F("\t"));
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }