]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PS4USB/PS4USB.ino
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / PS4USB / PS4USB.ino
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:  kristianl@tkjelectronics.com
5  */
6
7 #include <PS4USB.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 PS4USB PS4(&Usb);
17
18 bool printAngle, printTouch;
19 uint8_t oldL2Value, oldR2Value;
20
21 void setup() {
22   Serial.begin(115200);
23 #if !defined(__MIPSEL__)
24   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
25 #endif
26   if (Usb.Init() == -1) {
27     Serial.print(F("\r\nOSC did not start"));
28     while (1); // Halt
29   }
30   Serial.print(F("\r\nPS4 USB Library Started"));
31 }
32
33 void loop() {
34   Usb.Task();
35
36   if (PS4.connected()) {
37     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) {
38       Serial.print(F("\r\nLeftHatX: "));
39       Serial.print(PS4.getAnalogHat(LeftHatX));
40       Serial.print(F("\tLeftHatY: "));
41       Serial.print(PS4.getAnalogHat(LeftHatY));
42       Serial.print(F("\tRightHatX: "));
43       Serial.print(PS4.getAnalogHat(RightHatX));
44       Serial.print(F("\tRightHatY: "));
45       Serial.print(PS4.getAnalogHat(RightHatY));
46     }
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
59     if (PS4.getButtonClick(PS))
60       Serial.print(F("\r\nPS"));
61     if (PS4.getButtonClick(TRIANGLE)) {
62       Serial.print(F("\r\nTraingle"));
63       PS4.setRumbleOn(RumbleLow);
64     }
65     if (PS4.getButtonClick(CIRCLE)) {
66       Serial.print(F("\r\nCircle"));
67       PS4.setRumbleOn(RumbleHigh);
68     }
69     if (PS4.getButtonClick(CROSS)) {
70       Serial.print(F("\r\nCross"));
71       PS4.setLedFlash(10, 10); // Set it to blink rapidly
72     }
73     if (PS4.getButtonClick(SQUARE)) {
74       Serial.print(F("\r\nSquare"));
75       PS4.setLedFlash(0, 0); // Turn off blinking
76     }
77
78     if (PS4.getButtonClick(UP)) {
79       Serial.print(F("\r\nUp"));
80       PS4.setLed(Red);
81     } if (PS4.getButtonClick(RIGHT)) {
82       Serial.print(F("\r\nRight"));
83       PS4.setLed(Blue);
84     } if (PS4.getButtonClick(DOWN)) {
85       Serial.print(F("\r\nDown"));
86       PS4.setLed(Yellow);
87     } if (PS4.getButtonClick(LEFT)) {
88       Serial.print(F("\r\nLeft"));
89       PS4.setLed(Green);
90     }
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
101     if (PS4.getButtonClick(SHARE))
102       Serial.print(F("\r\nShare"));
103     if (PS4.getButtonClick(OPTIONS)) {
104       Serial.print(F("\r\nOptions"));
105       printAngle = !printAngle;
106     }
107     if (PS4.getButtonClick(TOUCHPAD)) {
108       Serial.print(F("\r\nTouchpad"));
109       printTouch = !printTouch;
110     }
111
112     if (printAngle) { // Print angle calculated using the accelerometer only
113       Serial.print(F("\r\nPitch: "));
114       Serial.print(PS4.getAngle(Pitch));
115       Serial.print(F("\tRoll: "));
116       Serial.print(PS4.getAngle(Roll));
117     }
118
119     if (printTouch) { // Print the x, y coordinates of the touchpad
120       if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
121         Serial.print(F("\r\n"));
122       for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
123         if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
124           Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
125           Serial.print(PS4.getX(i));
126           Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": "));
127           Serial.print(PS4.getY(i));
128           Serial.print(F("\t"));
129         }
130       }
131     }
132   }
133 }