]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3Multi/PS3Multi.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 / Bluetooth / PS3Multi / PS3Multi.ino
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:  kristianl@tkjelectronics.com
6  */
7
8 #include <PS3BT.h>
9 #include <usbhub.h>
10
11 // Satisfy the IDE, which needs to see the include statment in the ino too.
12 #ifdef dobogusinclude
13 #include <spi4teensy3.h>
14 #include <SPI.h>
15 #endif
16
17 USB Usb;
18 //USBHub Hub1(&Usb); // Some dongles have a hub inside
19
20 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
21 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!
22 const uint8_t length = sizeof(PS3) / sizeof(PS3[0]); // Get the lenght of the array
23 bool printAngle[length];
24 bool oldControllerState[length];
25
26 void setup() {
27   for (uint8_t i = 0; i < length; i++) {
28     PS3[i] = new PS3BT(&Btd); // Create the instances
29     PS3[i]->attachOnInit(onInit); // onInit() is called upon a new connection - you can call the function whatever you like
30   }
31
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\nPS3 Bluetooth Library Started"));
41 }
42 void loop() {
43   Usb.Task();
44
45   for (uint8_t i = 0; i < length; i++) {
46     if (PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) {
47       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) {
48         Serial.print(F("\r\nLeftHatX: "));
49         Serial.print(PS3[i]->getAnalogHat(LeftHatX));
50         Serial.print(F("\tLeftHatY: "));
51         Serial.print(PS3[i]->getAnalogHat(LeftHatY));
52         if (PS3[i]->PS3Connected) { // The Navigation controller only have one joystick
53           Serial.print(F("\tRightHatX: "));
54           Serial.print(PS3[i]->getAnalogHat(RightHatX));
55           Serial.print(F("\tRightHatY: "));
56           Serial.print(PS3[i]->getAnalogHat(RightHatY));
57         }
58       }
59       //Analog button values can be read from almost all buttons
60       if (PS3[i]->getAnalogButton(L2) || PS3[i]->getAnalogButton(R2)) {
61         Serial.print(F("\r\nL2: "));
62         Serial.print(PS3[i]->getAnalogButton(L2));
63         if (PS3[i]->PS3Connected) {
64           Serial.print(F("\tR2: "));
65           Serial.print(PS3[i]->getAnalogButton(R2));
66         }
67       }
68       if (PS3[i]->getButtonClick(PS)) {
69         Serial.print(F("\r\nPS"));
70         PS3[i]->disconnect();
71         oldControllerState[i] = false; // Reset value
72       }
73       else {
74         if (PS3[i]->getButtonClick(TRIANGLE))
75           Serial.print(F("\r\nTraingle"));
76         if (PS3[i]->getButtonClick(CIRCLE))
77           Serial.print(F("\r\nCircle"));
78         if (PS3[i]->getButtonClick(CROSS))
79           Serial.print(F("\r\nCross"));
80         if (PS3[i]->getButtonClick(SQUARE))
81           Serial.print(F("\r\nSquare"));
82
83         if (PS3[i]->getButtonClick(UP)) {
84           Serial.print(F("\r\nUp"));
85           if (PS3[i]->PS3Connected) {
86             PS3[i]->setLedOff();
87             PS3[i]->setLedOn(LED4);
88           }
89         }
90         if (PS3[i]->getButtonClick(RIGHT)) {
91           Serial.print(F("\r\nRight"));
92           if (PS3[i]->PS3Connected) {
93             PS3[i]->setLedOff();
94             PS3[i]->setLedOn(LED1);
95           }
96         }
97         if (PS3[i]->getButtonClick(DOWN)) {
98           Serial.print(F("\r\nDown"));
99           if (PS3[i]->PS3Connected) {
100             PS3[i]->setLedOff();
101             PS3[i]->setLedOn(LED2);
102           }
103         }
104         if (PS3[i]->getButtonClick(LEFT)) {
105           Serial.print(F("\r\nLeft"));
106           if (PS3[i]->PS3Connected) {
107             PS3[i]->setLedOff();
108             PS3[i]->setLedOn(LED3);
109           }
110         }
111
112         if (PS3[i]->getButtonClick(L1))
113           Serial.print(F("\r\nL1"));
114         if (PS3[i]->getButtonClick(L3))
115           Serial.print(F("\r\nL3"));
116         if (PS3[i]->getButtonClick(R1))
117           Serial.print(F("\r\nR1"));
118         if (PS3[i]->getButtonClick(R3))
119           Serial.print(F("\r\nR3"));
120
121         if (PS3[i]->getButtonClick(SELECT)) {
122           Serial.print(F("\r\nSelect - "));
123           PS3[i]->printStatusString();
124         }
125         if (PS3[i]->getButtonClick(START)) {
126           Serial.print(F("\r\nStart"));
127           printAngle[i] = !printAngle[i];
128         }
129       }
130       if (printAngle[i]) {
131         Serial.print(F("\r\nPitch: "));
132         Serial.print(PS3[i]->getAngle(Pitch));
133         Serial.print(F("\tRoll: "));
134         Serial.print(PS3[i]->getAngle(Roll));
135       }
136     }
137     /* I have removed the PS3 Move code as an Uno will run out of RAM if it's included */
138     //else if(PS3[i]->PS3MoveConnected) {
139   }
140 }
141
142 void onInit() {
143   for (uint8_t i = 0; i < length; i++) {
144     if ((PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) && !oldControllerState[i]) {
145       oldControllerState[i] = true; // Used to check which is the new controller
146       PS3[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h"
147     }
148   }
149 }