]> git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS4BT/PS4BT.ino
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / usb_hid / USB_Host_Shield_2.0 / examples / Bluetooth / PS4BT / PS4BT.ino
1 /*
2 Example sketch for the PS4 Bluetooth 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 <PS4BT.h>
8 #include <usbhub.h>
9
10 // Satisfy the IDE, which needs to see the include statment in the ino too.
11 #ifdef dobogusinclude
12 #include <spi4teensy3.h>
13 #include <SPI.h>
14 #endif
15
16 USB Usb;
17 //USBHub Hub1(&Usb); // Some dongles have a hub inside
18 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
19
20 /* You can create the instance of the PS4BT class in two ways */
21 // This will start an inquiry and then pair with the PS4 controller - you only have to do this once
22 // You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in paring mode
23 PS4BT PS4(&Btd, PAIR);
24
25 // After that you can simply create the instance like so and then press the PS button on the device
26 //PS4BT PS4(&Btd);
27
28 bool printAngle, printTouch;
29 uint8_t oldL2Value, oldR2Value;
30
31 void setup() {
32 Serial.begin(115200);
33 #if !defined(__MIPSEL__)
34 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
35 #endif
36 if (Usb.Init() == -1) {
37 Serial.print(F("\r\nOSC did not start"));
38 while (1); // Halt
39 }
40 Serial.print(F("\r\nPS4 Bluetooth Library Started"));
41 }
42 void loop() {
43 Usb.Task();
44
45 if (PS4.connected()) {
46 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) {
47 Serial.print(F("\r\nLeftHatX: "));
48 Serial.print(PS4.getAnalogHat(LeftHatX));
49 Serial.print(F("\tLeftHatY: "));
50 Serial.print(PS4.getAnalogHat(LeftHatY));
51 Serial.print(F("\tRightHatX: "));
52 Serial.print(PS4.getAnalogHat(RightHatX));
53 Serial.print(F("\tRightHatY: "));
54 Serial.print(PS4.getAnalogHat(RightHatY));
55 }
56
57 if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller
58 Serial.print(F("\r\nL2: "));
59 Serial.print(PS4.getAnalogButton(L2));
60 Serial.print(F("\tR2: "));
61 Serial.print(PS4.getAnalogButton(R2));
62 }
63 if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different
64 PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
65 oldL2Value = PS4.getAnalogButton(L2);
66 oldR2Value = PS4.getAnalogButton(R2);
67
68 if (PS4.getButtonClick(PS)) {
69 Serial.print(F("\r\nPS"));
70 PS4.disconnect();
71 }
72 else {
73 if (PS4.getButtonClick(TRIANGLE)) {
74 Serial.print(F("\r\nTraingle"));
75 PS4.setRumbleOn(RumbleLow);
76 }
77 if (PS4.getButtonClick(CIRCLE)) {
78 Serial.print(F("\r\nCircle"));
79 PS4.setRumbleOn(RumbleHigh);
80 }
81 if (PS4.getButtonClick(CROSS)) {
82 Serial.print(F("\r\nCross"));
83 PS4.setLedFlash(10, 10); // Set it to blink rapidly
84 }
85 if (PS4.getButtonClick(SQUARE)) {
86 Serial.print(F("\r\nSquare"));
87 PS4.setLedFlash(0, 0); // Turn off blinking
88 }
89
90 if (PS4.getButtonClick(UP)) {
91 Serial.print(F("\r\nUp"));
92 PS4.setLed(Red);
93 } if (PS4.getButtonClick(RIGHT)) {
94 Serial.print(F("\r\nRight"));
95 PS4.setLed(Blue);
96 } if (PS4.getButtonClick(DOWN)) {
97 Serial.print(F("\r\nDown"));
98 PS4.setLed(Yellow);
99 } if (PS4.getButtonClick(LEFT)) {
100 Serial.print(F("\r\nLeft"));
101 PS4.setLed(Green);
102 }
103
104 if (PS4.getButtonClick(L1))
105 Serial.print(F("\r\nL1"));
106 if (PS4.getButtonClick(L3))
107 Serial.print(F("\r\nL3"));
108 if (PS4.getButtonClick(R1))
109 Serial.print(F("\r\nR1"));
110 if (PS4.getButtonClick(R3))
111 Serial.print(F("\r\nR3"));
112
113 if (PS4.getButtonClick(SHARE))
114 Serial.print(F("\r\nShare"));
115 if (PS4.getButtonClick(OPTIONS)) {
116 Serial.print(F("\r\nOptions"));
117 printAngle = !printAngle;
118 }
119 if (PS4.getButtonClick(TOUCHPAD)) {
120 Serial.print(F("\r\nTouchpad"));
121 printTouch = !printTouch;
122 }
123
124 if (printAngle) { // Print angle calculated using the accelerometer only
125 Serial.print(F("\r\nPitch: "));
126 Serial.print(PS4.getAngle(Pitch));
127 Serial.print(F("\tRoll: "));
128 Serial.print(PS4.getAngle(Roll));
129 }
130
131 if (printTouch) { // Print the x, y coordinates of the touchpad
132 if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
133 Serial.print(F("\r\n"));
134 for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
135 if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
136 Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
137 Serial.print(PS4.getX(i));
138 Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": "));
139 Serial.print(PS4.getY(i));
140 Serial.print(F("\t"));
141 }
142 }
143 }
144 }
145 }
146 }
Imprint / Impressum