]> git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / usb_hid / USB_Host_Shield_2.0 / examples / Bluetooth / WiiMulti / WiiMulti.ino
1 /*
2 Example sketch for the Wiimote 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 <Wii.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 WII *Wii[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(Wii) / sizeof(Wii[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 Wii[i] = new WII(&Btd); // You will have to pair each controller with the dongle before you can define the instances like so, just add PAIR as the second argument
29 Wii[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\nWiimote Bluetooth Library Started"));
41 }
42 void loop() {
43 Usb.Task();
44
45 for (uint8_t i = 0; i < length; i++) {
46 if (Wii[i]->wiimoteConnected) {
47 if (Wii[i]->getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down
48 Serial.print(F("\r\nHOME"));
49 Wii[i]->disconnect();
50 oldControllerState[i] = false; // Reset value
51 }
52 else {
53 if (Wii[i]->getButtonClick(LEFT)) {
54 Wii[i]->setLedOff();
55 Wii[i]->setLedOn(LED1);
56 Serial.print(F("\r\nLeft"));
57 }
58 if (Wii[i]->getButtonClick(RIGHT)) {
59 Wii[i]->setLedOff();
60 Wii[i]->setLedOn(LED3);
61 Serial.print(F("\r\nRight"));
62 }
63 if (Wii[i]->getButtonClick(DOWN)) {
64 Wii[i]->setLedOff();
65 Wii[i]->setLedOn(LED4);
66 Serial.print(F("\r\nDown"));
67 }
68 if (Wii[i]->getButtonClick(UP)) {
69 Wii[i]->setLedOff();
70 Wii[i]->setLedOn(LED2);
71 Serial.print(F("\r\nUp"));
72 }
73
74 if (Wii[i]->getButtonClick(PLUS))
75 Serial.print(F("\r\nPlus"));
76 if (Wii[i]->getButtonClick(MINUS))
77 Serial.print(F("\r\nMinus"));
78
79 if (Wii[i]->getButtonClick(ONE))
80 Serial.print(F("\r\nOne"));
81 if (Wii[i]->getButtonClick(TWO))
82 Serial.print(F("\r\nTwo"));
83
84 if (Wii[i]->getButtonClick(A)) {
85 printAngle[i] = !printAngle[i];
86 Serial.print(F("\r\nA"));
87 }
88 if (Wii[i]->getButtonClick(B)) {
89 Wii[i]->setRumbleToggle();
90 Serial.print(F("\r\nB"));
91 }
92 }
93 if (printAngle[i]) {
94 Serial.print(F("\r\nPitch: "));
95 Serial.print(Wii[i]->getPitch());
96 Serial.print(F("\tRoll: "));
97 Serial.print(Wii[i]->getRoll());
98 if (Wii[i]->motionPlusConnected) {
99 Serial.print(F("\tYaw: "));
100 Serial.print(Wii[i]->getYaw());
101 }
102 if (Wii[i]->nunchuckConnected) {
103 Serial.print(F("\tNunchuck Pitch: "));
104 Serial.print(Wii[i]->getNunchuckPitch());
105 Serial.print(F("\tNunchuck Roll: "));
106 Serial.print(Wii[i]->getNunchuckRoll());
107 }
108 }
109 }
110 if (Wii[i]->nunchuckConnected) {
111 if (Wii[i]->getButtonClick(Z))
112 Serial.print(F("\r\nZ"));
113 if (Wii[i]->getButtonClick(C))
114 Serial.print(F("\r\nC"));
115 if (Wii[i]->getAnalogHat(HatX) > 137 || Wii[i]->getAnalogHat(HatX) < 117 || Wii[i]->getAnalogHat(HatY) > 137 || Wii[i]->getAnalogHat(HatY) < 117) {
116 Serial.print(F("\r\nHatX: "));
117 Serial.print(Wii[i]->getAnalogHat(HatX));
118 Serial.print(F("\tHatY: "));
119 Serial.print(Wii[i]->getAnalogHat(HatY));
120 }
121 }
122 }
123 }
124
125 void onInit() {
126 for (uint8_t i = 0; i < length; i++) {
127 if (Wii[i]->wiimoteConnected && !oldControllerState[i]) {
128 oldControllerState[i] = true; // Used to check which is the new controller
129 Wii[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h"
130 }
131 }
132 }
Imprint / Impressum