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.

PS3Multi.ino 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
  3. This example show how one can use multiple controllers with the library
  4. For more information visit my blog: http://blog.tkjelectronics.dk/ or
  5. send me an e-mail: [email protected]
  6. */
  7. #include <PS3BT.h>
  8. #include <usbhub.h>
  9. // Satisfy the IDE, which needs to see the include statment in the ino too.
  10. #ifdef dobogusinclude
  11. #include <spi4teensy3.h>
  12. #include <SPI.h>
  13. #endif
  14. USB Usb;
  15. //USBHub Hub1(&Usb); // Some dongles have a hub inside
  16. BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
  17. PS3BT *PS3[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
  18. const uint8_t length = sizeof(PS3) / sizeof(PS3[0]); // Get the lenght of the array
  19. bool printAngle[length];
  20. bool oldControllerState[length];
  21. void setup() {
  22. for (uint8_t i = 0; i < length; i++) {
  23. PS3[i] = new PS3BT(&Btd); // Create the instances
  24. PS3[i]->attachOnInit(onInit); // onInit() is called upon a new connection - you can call the function whatever you like
  25. }
  26. Serial.begin(115200);
  27. #if !defined(__MIPSEL__)
  28. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  29. #endif
  30. if (Usb.Init() == -1) {
  31. Serial.print(F("\r\nOSC did not start"));
  32. while (1); //halt
  33. }
  34. Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  35. }
  36. void loop() {
  37. Usb.Task();
  38. for (uint8_t i = 0; i < length; i++) {
  39. if (PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) {
  40. if (PS3[i]->getAnalogHat(LeftHatX) > 137 || PS3[i]->getAnalogHat(LeftHatX) < 117 || PS3[i]->getAnalogHat(LeftHatY) > 137 || PS3[i]->getAnalogHat(LeftHatY) < 117 || PS3[i]->getAnalogHat(RightHatX) > 137 || PS3[i]->getAnalogHat(RightHatX) < 117 || PS3[i]->getAnalogHat(RightHatY) > 137 || PS3[i]->getAnalogHat(RightHatY) < 117) {
  41. Serial.print(F("\r\nLeftHatX: "));
  42. Serial.print(PS3[i]->getAnalogHat(LeftHatX));
  43. Serial.print(F("\tLeftHatY: "));
  44. Serial.print(PS3[i]->getAnalogHat(LeftHatY));
  45. if (PS3[i]->PS3Connected) { // The Navigation controller only have one joystick
  46. Serial.print(F("\tRightHatX: "));
  47. Serial.print(PS3[i]->getAnalogHat(RightHatX));
  48. Serial.print(F("\tRightHatY: "));
  49. Serial.print(PS3[i]->getAnalogHat(RightHatY));
  50. }
  51. }
  52. //Analog button values can be read from almost all buttons
  53. if (PS3[i]->getAnalogButton(L2) || PS3[i]->getAnalogButton(R2)) {
  54. Serial.print(F("\r\nL2: "));
  55. Serial.print(PS3[i]->getAnalogButton(L2));
  56. if (PS3[i]->PS3Connected) {
  57. Serial.print(F("\tR2: "));
  58. Serial.print(PS3[i]->getAnalogButton(R2));
  59. }
  60. }
  61. if (PS3[i]->getButtonClick(PS)) {
  62. Serial.print(F("\r\nPS"));
  63. PS3[i]->disconnect();
  64. oldControllerState[i] = false; // Reset value
  65. }
  66. else {
  67. if (PS3[i]->getButtonClick(TRIANGLE))
  68. Serial.print(F("\r\nTraingle"));
  69. if (PS3[i]->getButtonClick(CIRCLE))
  70. Serial.print(F("\r\nCircle"));
  71. if (PS3[i]->getButtonClick(CROSS))
  72. Serial.print(F("\r\nCross"));
  73. if (PS3[i]->getButtonClick(SQUARE))
  74. Serial.print(F("\r\nSquare"));
  75. if (PS3[i]->getButtonClick(UP)) {
  76. Serial.print(F("\r\nUp"));
  77. if (PS3[i]->PS3Connected) {
  78. PS3[i]->setLedOff();
  79. PS3[i]->setLedOn(LED4);
  80. }
  81. }
  82. if (PS3[i]->getButtonClick(RIGHT)) {
  83. Serial.print(F("\r\nRight"));
  84. if (PS3[i]->PS3Connected) {
  85. PS3[i]->setLedOff();
  86. PS3[i]->setLedOn(LED1);
  87. }
  88. }
  89. if (PS3[i]->getButtonClick(DOWN)) {
  90. Serial.print(F("\r\nDown"));
  91. if (PS3[i]->PS3Connected) {
  92. PS3[i]->setLedOff();
  93. PS3[i]->setLedOn(LED2);
  94. }
  95. }
  96. if (PS3[i]->getButtonClick(LEFT)) {
  97. Serial.print(F("\r\nLeft"));
  98. if (PS3[i]->PS3Connected) {
  99. PS3[i]->setLedOff();
  100. PS3[i]->setLedOn(LED3);
  101. }
  102. }
  103. if (PS3[i]->getButtonClick(L1))
  104. Serial.print(F("\r\nL1"));
  105. if (PS3[i]->getButtonClick(L3))
  106. Serial.print(F("\r\nL3"));
  107. if (PS3[i]->getButtonClick(R1))
  108. Serial.print(F("\r\nR1"));
  109. if (PS3[i]->getButtonClick(R3))
  110. Serial.print(F("\r\nR3"));
  111. if (PS3[i]->getButtonClick(SELECT)) {
  112. Serial.print(F("\r\nSelect - "));
  113. PS3[i]->printStatusString();
  114. }
  115. if (PS3[i]->getButtonClick(START)) {
  116. Serial.print(F("\r\nStart"));
  117. printAngle[i] = !printAngle[i];
  118. }
  119. }
  120. if (printAngle[i]) {
  121. Serial.print(F("\r\nPitch: "));
  122. Serial.print(PS3[i]->getAngle(Pitch));
  123. Serial.print(F("\tRoll: "));
  124. Serial.print(PS3[i]->getAngle(Roll));
  125. }
  126. }
  127. /* I have removed the PS3 Move code as an Uno will run out of RAM if it's included */
  128. //else if(PS3[i]->PS3MoveConnected) {
  129. }
  130. }
  131. void onInit() {
  132. for (uint8_t i = 0; i < length; i++) {
  133. if ((PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) && !oldControllerState[i]) {
  134. oldControllerState[i] = true; // Used to check which is the new controller
  135. PS3[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h"
  136. }
  137. }
  138. }