diff --git a/BTD.cpp b/BTD.cpp index b0f59a02..43e0bcad 100755 --- a/BTD.cpp +++ b/BTD.cpp @@ -39,8 +39,6 @@ bPollEnable(false) // Don't start polling before dongle is connected if (pUsb) // register in USB subsystem pUsb->RegisterDeviceClass(this); //set devConfig[] entry - - wiiServiceID = -1; } uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) { @@ -735,13 +733,9 @@ void BTD::ACL_event_task() { uint16_t MAX_BUFFER_SIZE = BULK_MAXPKTSIZE; uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_DATAIN_PIPE ].epAddr, &MAX_BUFFER_SIZE, l2capinbuf); // input on endpoint 2 if(!rcode) { // Check for errors - if(connectToWii) // Only send the data to the Wii service - btService[wiiServiceID]->ACLData(l2capinbuf); - else { - for (uint8_t i=0; iACLData(l2capinbuf); - } + for (uint8_t i=0; iACLData(l2capinbuf); } #ifdef EXTRADEBUG else if (rcode != hrNAK) { diff --git a/BTD.h b/BTD.h index 6efa3a5a..2cae3b7a 100755 --- a/BTD.h +++ b/BTD.h @@ -181,8 +181,6 @@ public: uint8_t remote_name[30]; // First 30 chars of last remote name uint8_t hci_version; - int8_t wiiServiceID; // Stores the service ID of the Wii service - bool connectToWii; // Used to only send the ACL data to the wiimote bool incomingWii; bool pairWithWii; diff --git a/Wii.cpp b/Wii.cpp index 564e1701..a629821a 100755 --- a/Wii.cpp +++ b/Wii.cpp @@ -28,7 +28,7 @@ WII::WII(BTD *p, bool pair): pBtd(p) // pointer to USB class instance - mandatory { if (pBtd) - pBtd->wiiServiceID = pBtd->registerServiceClass(this); // Register it as a Bluetooth service + pBtd->registerServiceClass(this); // Register it as a Bluetooth service pBtd->pairWithWii = pair; @@ -48,6 +48,7 @@ void WII::Reset() { motionPlusConnected = false; activateNunchuck = false; motionValuesReset = false; + activeConnection = false; l2cap_event_flag = 0; // Reset flags l2cap_state = L2CAP_WAIT; } @@ -61,7 +62,9 @@ void WII::disconnect() { // Use this void to disconnect any of the controllers } void WII::ACLData(uint8_t* l2capinbuf) { - if (((l2capinbuf[0] | (l2capinbuf[1] << 8)) == (hci_handle | 0x2000)) || pBtd->incomingWii) { // acl_handle_ok or it's a new connection + if (((l2capinbuf[0] | (l2capinbuf[1] << 8)) == (hci_handle | 0x2000)) || (pBtd->incomingWii && !wiimoteConnected && !activeConnection)) { // acl_handle_ok or it's a new connection + pBtd->incomingWii = false; + activeConnection = true; if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) { #ifdef DEBUG @@ -577,7 +580,9 @@ void WII::L2CAP_task() { void WII::Run() { switch (l2cap_state) { case L2CAP_WAIT: - if(pBtd->connectToWii) { + if(pBtd->connectToWii && !pBtd->l2capConnectionClaimed && !wiimoteConnected && !activeConnection) { + pBtd->l2capConnectionClaimed = true; + activeConnection = true; #ifdef DEBUG Notify(PSTR("\r\nSend HID Control Connection Request")); #endif diff --git a/Wii.h b/Wii.h index 81b64c7a..87d8a028 100755 --- a/Wii.h +++ b/Wii.h @@ -207,6 +207,7 @@ private: /* Variables filled from HCI event management */ uint16_t hci_handle; + bool activeConnection; // Used to indicate if it's already has established a connection /* variables used by high level L2CAP task */ uint8_t l2cap_state; diff --git a/examples/Bluetooth/WiiMulti/WiiMulti.ino b/examples/Bluetooth/WiiMulti/WiiMulti.ino new file mode 100644 index 00000000..ab970158 --- /dev/null +++ b/examples/Bluetooth/WiiMulti/WiiMulti.ino @@ -0,0 +1,113 @@ +/* + Example sketch for the Wiimote Bluetooth library - developed by Kristian Lauszus + This example show how one can use multiple controllers with the library + For more information visit my blog: http://blog.tkjelectronics.dk/ or + send me an e-mail: kristianl@tkjelectronics.com + */ + +#include +USB Usb; +BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so +//WII Wii(&Btd,PAIR); // You will have to pair each controller with the dongle before you can define the instances like below +WII Wii_1(&Btd); +WII Wii_2(&Btd); +//WII Wii_3(&Btd); // You can create as many instances as you like, but it will take up a lot of RAM!! + +WII* Wii[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like +const uint8_t length = sizeof(Wii)/sizeof(Wii[0]); // Get the lenght of the array +bool printAngle[length]; + +void setup() { + Wii[0] = &Wii_1; + Wii[1] = &Wii_2; + //Wii[2] = &Wii_3; // You only need to uncomment this if you wanted to use another controller + + Serial.begin(115200); + if (Usb.Init() == -1) { + Serial.print(F("\r\nOSC did not start")); + while(1); //halt + } + Serial.print(F("\r\nWiimote Bluetooth Library Started")); +} +void loop() { + Usb.Task(); + + for(uint8_t i=0;iwiimoteConnected) { + if(Wii[i]->getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down + Serial.print(F("\r\nHOME")); + Wii[i]->disconnect(); // Disconnect the Wiimote - it will establish the connection again since the Wiimote automatically reconnects + } + else { + if(Wii[i]->getButtonClick(LEFT)) { + Wii[i]->setAllOff(); + Wii[i]->setLedOn(LED1); + Serial.print(F("\r\nLeft")); + } + if(Wii[i]->getButtonClick(RIGHT)) { + Wii[i]->setAllOff(); + Wii[i]->setLedOn(LED3); + Serial.print(F("\r\nRight")); + } + if(Wii[i]->getButtonClick(DOWN)) { + Wii[i]->setAllOff(); + Wii[i]->setLedOn(LED4); + Serial.print(F("\r\nDown")); + } + if(Wii[i]->getButtonClick(UP)) { + Wii[i]->setAllOff(); + Wii[i]->setLedOn(LED2); + Serial.print(F("\r\nUp")); + } + + if(Wii[i]->getButtonClick(PLUS)) + Serial.print(F("\r\nPlus")); + if(Wii[i]->getButtonClick(MINUS)) + Serial.print(F("\r\nMinus")); + + if(Wii[i]->getButtonClick(ONE)) + Serial.print(F("\r\nOne")); + if(Wii[i]->getButtonClick(TWO)) + Serial.print(F("\r\nTwo")); + + if(Wii[i]->getButtonClick(A)) { + printAngle[i] = !printAngle[i]; + Serial.print(F("\r\nA")); + } + if(Wii[i]->getButtonClick(B)) { + Wii[i]->setRumbleToggle(); + Serial.print(F("\r\nB")); + } + } + if(printAngle[i]) { + Serial.print(F("\r\nPitch: ")); + Serial.print(Wii[i]->getPitch()); + Serial.print(F("\tRoll: ")); + Serial.print(Wii[i]->getRoll()); + if(Wii[i]->motionPlusConnected) { + Serial.print(F("\tYaw: ")); + Serial.print(Wii[i]->getYaw()); + } + if(Wii[i]->nunchuckConnected) { + Serial.print(F("\tNunchuck Pitch: ")); + Serial.print(Wii[i]->nunchuckPitch); + Serial.print(F("\tNunchuck Roll: ")); + Serial.print(Wii[i]->nunchuckRoll); + } + } + } + if(Wii[i]->nunchuckConnected) { + if(Wii[i]->getButtonClick(Z)) + Serial.print(F("\r\nZ")); + if(Wii[i]->getButtonClick(C)) + Serial.print(F("\r\nC")); + if(Wii[i]->getAnalogHat(HatX) > 137 || Wii[i]->getAnalogHat(HatX) < 117 || Wii[i]->getAnalogHat(HatY) > 137 || Wii[i]->getAnalogHat(HatY) < 117) { + Serial.print(F("\r\nHatX: ")); + Serial.print(Wii[i]->getAnalogHat(HatX)); + Serial.print(F("\tHatY: ")); + Serial.print(Wii[i]->getAnalogHat(HatY)); + } + } + } +}