Advertisement
Guest User

Untitled

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