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.

PS4USB.ino 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Example sketch for the PS4 USB 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 <PS4USB.h>
  7. // Satisfy the IDE, which needs to see the include statment in the ino too.
  8. #ifdef dobogusinclude
  9. #include <spi4teensy3.h>
  10. #include <SPI.h>
  11. #endif
  12. USB Usb;
  13. PS4USB PS4(&Usb);
  14. bool printAngle, printTouch;
  15. uint8_t oldL2Value, oldR2Value;
  16. void setup() {
  17. Serial.begin(115200);
  18. #if !defined(__MIPSEL__)
  19. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  20. #endif
  21. if (Usb.Init() == -1) {
  22. Serial.print(F("\r\nOSC did not start"));
  23. while (1); // Halt
  24. }
  25. Serial.print(F("\r\nPS4 USB Library Started"));
  26. }
  27. void loop() {
  28. Usb.Task();
  29. if (PS4.connected()) {
  30. 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) {
  31. Serial.print(F("\r\nLeftHatX: "));
  32. Serial.print(PS4.getAnalogHat(LeftHatX));
  33. Serial.print(F("\tLeftHatY: "));
  34. Serial.print(PS4.getAnalogHat(LeftHatY));
  35. Serial.print(F("\tRightHatX: "));
  36. Serial.print(PS4.getAnalogHat(RightHatX));
  37. Serial.print(F("\tRightHatY: "));
  38. Serial.print(PS4.getAnalogHat(RightHatY));
  39. }
  40. if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller
  41. Serial.print(F("\r\nL2: "));
  42. Serial.print(PS4.getAnalogButton(L2));
  43. Serial.print(F("\tR2: "));
  44. Serial.print(PS4.getAnalogButton(R2));
  45. }
  46. if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different
  47. PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
  48. oldL2Value = PS4.getAnalogButton(L2);
  49. oldR2Value = PS4.getAnalogButton(R2);
  50. if (PS4.getButtonClick(PS))
  51. Serial.print(F("\r\nPS"));
  52. if (PS4.getButtonClick(TRIANGLE)) {
  53. Serial.print(F("\r\nTraingle"));
  54. PS4.setRumbleOn(RumbleLow);
  55. }
  56. if (PS4.getButtonClick(CIRCLE)) {
  57. Serial.print(F("\r\nCircle"));
  58. PS4.setRumbleOn(RumbleHigh);
  59. }
  60. if (PS4.getButtonClick(CROSS)) {
  61. Serial.print(F("\r\nCross"));
  62. PS4.setLedFlash(10, 10); // Set it to blink rapidly
  63. }
  64. if (PS4.getButtonClick(SQUARE)) {
  65. Serial.print(F("\r\nSquare"));
  66. PS4.setLedFlash(0, 0); // Turn off blinking
  67. }
  68. if (PS4.getButtonClick(UP)) {
  69. Serial.print(F("\r\nUp"));
  70. PS4.setLed(Red);
  71. } if (PS4.getButtonClick(RIGHT)) {
  72. Serial.print(F("\r\nRight"));
  73. PS4.setLed(Blue);
  74. } if (PS4.getButtonClick(DOWN)) {
  75. Serial.print(F("\r\nDown"));
  76. PS4.setLed(Yellow);
  77. } if (PS4.getButtonClick(LEFT)) {
  78. Serial.print(F("\r\nLeft"));
  79. PS4.setLed(Green);
  80. }
  81. if (PS4.getButtonClick(L1))
  82. Serial.print(F("\r\nL1"));
  83. if (PS4.getButtonClick(L3))
  84. Serial.print(F("\r\nL3"));
  85. if (PS4.getButtonClick(R1))
  86. Serial.print(F("\r\nR1"));
  87. if (PS4.getButtonClick(R3))
  88. Serial.print(F("\r\nR3"));
  89. if (PS4.getButtonClick(SHARE))
  90. Serial.print(F("\r\nShare"));
  91. if (PS4.getButtonClick(OPTIONS)) {
  92. Serial.print(F("\r\nOptions"));
  93. printAngle = !printAngle;
  94. }
  95. if (PS4.getButtonClick(TOUCHPAD)) {
  96. Serial.print(F("\r\nTouchpad"));
  97. printTouch = !printTouch;
  98. }
  99. if (printAngle) { // Print angle calculated using the accelerometer only
  100. Serial.print(F("\r\nPitch: "));
  101. Serial.print(PS4.getAngle(Pitch));
  102. Serial.print(F("\tRoll: "));
  103. Serial.print(PS4.getAngle(Roll));
  104. }
  105. if (printTouch) { // Print the x, y coordinates of the touchpad
  106. if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
  107. Serial.print(F("\r\n"));
  108. for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
  109. if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
  110. Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
  111. Serial.print(PS4.getX(i));
  112. Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": "));
  113. Serial.print(PS4.getY(i));
  114. Serial.print(F("\t"));
  115. }
  116. }
  117. }
  118. }
  119. }