]> git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3SPP/PS3SPP.ino
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / usb_hid / USB_Host_Shield_2.0 / examples / Bluetooth / PS3SPP / PS3SPP.ino
1 /*
2 Example sketch for the 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 This example show how one can combine all the difference Bluetooth services in one single code.
7 Note:
8 You will need a Arduino Mega 1280/2560 to run this sketch,
9 as a normal Arduino (Uno, Duemilanove etc.) doesn't have enough SRAM and FLASH
10 */
11
12 #include <PS3BT.h>
13 #include <SPP.h>
14 #include <usbhub.h>
15
16 // Satisfy the IDE, which needs to see the include statment in the ino too.
17 #ifdef dobogusinclude
18 #include <spi4teensy3.h>
19 #include <SPI.h>
20 #endif
21
22 USB Usb;
23 //USBHub Hub1(&Usb); // Some dongles have a hub inside
24
25 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
26
27 /* You can create the instances of the bluetooth services in two ways */
28 SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "0000"
29 //SPP SerialBTBT(&Btd,"Lauszus's Arduino","0000"); // You can also set the name and pin like so
30 PS3BT PS3(&Btd); // This will just create the instance
31 //PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
32
33 bool firstMessage = true;
34 String output = ""; // We will store the data in this string
35
36 void setup() {
37 Serial.begin(115200); // This wil lprint the debugging from the libraries
38 #if !defined(__MIPSEL__)
39 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
40 #endif
41 if (Usb.Init() == -1) {
42 Serial.print(F("\r\nOSC did not start"));
43 while (1); //halt
44 }
45 Serial.print(F("\r\nBluetooth Library Started"));
46 output.reserve(200); // Reserve 200 bytes for the output string
47 }
48 void loop() {
49 Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well
50
51 if (SerialBT.connected) {
52 if (firstMessage) {
53 firstMessage = false;
54 SerialBT.println(F("Hello from Arduino")); // Send welcome message
55 }
56 if (Serial.available())
57 SerialBT.write(Serial.read());
58 if (SerialBT.available())
59 Serial.write(SerialBT.read());
60 }
61 else
62 firstMessage = true;
63
64 if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
65 output = ""; // Reset output string
66 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) {
67 output += "LeftHatX: ";
68 output += PS3.getAnalogHat(LeftHatX);
69 output += "\tLeftHatY: ";
70 output += PS3.getAnalogHat(LeftHatY);
71 if (PS3.PS3Connected) { // The Navigation controller only have one joystick
72 output += "\tRightHatX: ";
73 output += PS3.getAnalogHat(RightHatX);
74 output += "\tRightHatY: ";
75 output += PS3.getAnalogHat(RightHatY);
76 }
77 }
78 //Analog button values can be read from almost all buttons
79 if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
80 if (output != "")
81 output += "\r\n";
82 output += "L2: ";
83 output += PS3.getAnalogButton(L2);
84 if (PS3.PS3Connected) {
85 output += "\tR2: ";
86 output += PS3.getAnalogButton(R2);
87 }
88 }
89 if (output != "") {
90 Serial.println(output);
91 if (SerialBT.connected)
92 SerialBT.println(output);
93 output = ""; // Reset output string
94 }
95 if (PS3.getButtonClick(PS)) {
96 output += " - PS";
97 PS3.disconnect();
98 }
99 else {
100 if (PS3.getButtonClick(TRIANGLE))
101 output += " - Traingle";
102 if (PS3.getButtonClick(CIRCLE))
103 output += " - Circle";
104 if (PS3.getButtonClick(CROSS))
105 output += " - Cross";
106 if (PS3.getButtonClick(SQUARE))
107 output += " - Square";
108
109 if (PS3.getButtonClick(UP)) {
110 output += " - Up";
111 if (PS3.PS3Connected) {
112 PS3.setLedOff();
113 PS3.setLedOn(LED4);
114 }
115 }
116 if (PS3.getButtonClick(RIGHT)) {
117 output += " - Right";
118 if (PS3.PS3Connected) {
119 PS3.setLedOff();
120 PS3.setLedOn(LED1);
121 }
122 }
123 if (PS3.getButtonClick(DOWN)) {
124 output += " - Down";
125 if (PS3.PS3Connected) {
126 PS3.setLedOff();
127 PS3.setLedOn(LED2);
128 }
129 }
130 if (PS3.getButtonClick(LEFT)) {
131 output += " - Left";
132 if (PS3.PS3Connected) {
133 PS3.setLedOff();
134 PS3.setLedOn(LED3);
135 }
136 }
137
138 if (PS3.getButtonClick(L1))
139 output += " - L1";
140 if (PS3.getButtonClick(L3))
141 output += " - L3";
142 if (PS3.getButtonClick(R1))
143 output += " - R1";
144 if (PS3.getButtonClick(R3))
145 output += " - R3";
146
147 if (PS3.getButtonClick(SELECT)) {
148 output += " - Select";
149 }
150 if (PS3.getButtonClick(START))
151 output += " - Start";
152
153 if (output != "") {
154 String string = "PS3 Controller" + output;
155 Serial.println(string);
156 if (SerialBT.connected)
157 SerialBT.println(string);
158 }
159 }
160 delay(10);
161 }
162 }
Imprint / Impressum