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.

PS3USB.ino 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Example sketch for the PS3 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 <PS3USB.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. /* You can create the instance of the class in two ways */
  14. PS3USB PS3(&Usb); // This will just create the instance
  15. //PS3USB PS3(&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
  16. bool printAngle;
  17. uint8_t state = 0;
  18. void setup() {
  19. Serial.begin(115200);
  20. #if !defined(__MIPSEL__)
  21. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  22. #endif
  23. if (Usb.Init() == -1) {
  24. Serial.print(F("\r\nOSC did not start"));
  25. while (1); //halt
  26. }
  27. Serial.print(F("\r\nPS3 USB Library Started"));
  28. }
  29. void loop() {
  30. Usb.Task();
  31. if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
  32. 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) {
  33. Serial.print(F("\r\nLeftHatX: "));
  34. Serial.print(PS3.getAnalogHat(LeftHatX));
  35. Serial.print(F("\tLeftHatY: "));
  36. Serial.print(PS3.getAnalogHat(LeftHatY));
  37. if (PS3.PS3Connected) { // The Navigation controller only have one joystick
  38. Serial.print(F("\tRightHatX: "));
  39. Serial.print(PS3.getAnalogHat(RightHatX));
  40. Serial.print(F("\tRightHatY: "));
  41. Serial.print(PS3.getAnalogHat(RightHatY));
  42. }
  43. }
  44. // Analog button values can be read from almost all buttons
  45. if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
  46. Serial.print(F("\r\nL2: "));
  47. Serial.print(PS3.getAnalogButton(L2));
  48. if (!PS3.PS3NavigationConnected) {
  49. Serial.print(F("\tR2: "));
  50. Serial.print(PS3.getAnalogButton(R2));
  51. }
  52. }
  53. if (PS3.getButtonClick(PS))
  54. Serial.print(F("\r\nPS"));
  55. if (PS3.getButtonClick(TRIANGLE))
  56. Serial.print(F("\r\nTraingle"));
  57. if (PS3.getButtonClick(CIRCLE))
  58. Serial.print(F("\r\nCircle"));
  59. if (PS3.getButtonClick(CROSS))
  60. Serial.print(F("\r\nCross"));
  61. if (PS3.getButtonClick(SQUARE))
  62. Serial.print(F("\r\nSquare"));
  63. if (PS3.getButtonClick(UP)) {
  64. Serial.print(F("\r\nUp"));
  65. PS3.setLedOff();
  66. PS3.setLedOn(LED4);
  67. }
  68. if (PS3.getButtonClick(RIGHT)) {
  69. Serial.print(F("\r\nRight"));
  70. PS3.setLedOff();
  71. PS3.setLedOn(LED1);
  72. }
  73. if (PS3.getButtonClick(DOWN)) {
  74. Serial.print(F("\r\nDown"));
  75. PS3.setLedOff();
  76. PS3.setLedOn(LED2);
  77. }
  78. if (PS3.getButtonClick(LEFT)) {
  79. Serial.print(F("\r\nLeft"));
  80. PS3.setLedOff();
  81. PS3.setLedOn(LED3);
  82. }
  83. if (PS3.getButtonClick(L1))
  84. Serial.print(F("\r\nL1"));
  85. if (PS3.getButtonClick(L3))
  86. Serial.print(F("\r\nL3"));
  87. if (PS3.getButtonClick(R1))
  88. Serial.print(F("\r\nR1"));
  89. if (PS3.getButtonClick(R3))
  90. Serial.print(F("\r\nR3"));
  91. if (PS3.getButtonClick(SELECT)) {
  92. Serial.print(F("\r\nSelect - "));
  93. PS3.printStatusString();
  94. }
  95. if (PS3.getButtonClick(START)) {
  96. Serial.print(F("\r\nStart"));
  97. printAngle = !printAngle;
  98. }
  99. if (printAngle) {
  100. Serial.print(F("\r\nPitch: "));
  101. Serial.print(PS3.getAngle(Pitch));
  102. Serial.print(F("\tRoll: "));
  103. Serial.print(PS3.getAngle(Roll));
  104. }
  105. }
  106. 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
  107. if (state == 0) {
  108. PS3.moveSetRumble(0);
  109. PS3.moveSetBulb(Off);
  110. } else if (state == 1) {
  111. PS3.moveSetRumble(75);
  112. PS3.moveSetBulb(Red);
  113. } else if (state == 2) {
  114. PS3.moveSetRumble(125);
  115. PS3.moveSetBulb(Green);
  116. } else if (state == 3) {
  117. PS3.moveSetRumble(150);
  118. PS3.moveSetBulb(Blue);
  119. } else if (state == 4) {
  120. PS3.moveSetRumble(175);
  121. PS3.moveSetBulb(Yellow);
  122. } else if (state == 5) {
  123. PS3.moveSetRumble(200);
  124. PS3.moveSetBulb(Lightblue);
  125. } else if (state == 6) {
  126. PS3.moveSetRumble(225);
  127. PS3.moveSetBulb(Purble);
  128. } else if (state == 7) {
  129. PS3.moveSetRumble(250);
  130. PS3.moveSetBulb(White);
  131. }
  132. state++;
  133. if (state > 7)
  134. state = 0;
  135. delay(1000);
  136. }
  137. }