mirror of
https://github.com/felis/USB_Host_Shield_2.0.git
synced 2024-03-22 11:31:26 +01:00
The library now also support multiple Wiimotes
This commit is contained in:
parent
c4563ec7c8
commit
ac4fc92ea7
5 changed files with 125 additions and 14 deletions
6
BTD.cpp
6
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,14 +733,10 @@ 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; i<BTD_NUMSERVICES; i++)
|
||||
if (btService[i])
|
||||
btService[i]->ACLData(l2capinbuf);
|
||||
}
|
||||
}
|
||||
#ifdef EXTRADEBUG
|
||||
else if (rcode != hrNAK) {
|
||||
Notify(PSTR("\r\nACL data in error: "));
|
||||
|
|
2
BTD.h
2
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;
|
||||
|
|
11
Wii.cpp
11
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
|
||||
|
|
1
Wii.h
1
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;
|
||||
|
|
113
examples/Bluetooth/WiiMulti/WiiMulti.ino
Normal file
113
examples/Bluetooth/WiiMulti/WiiMulti.ino
Normal file
|
@ -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 <Wii.h>
|
||||
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;i<length;i++) {
|
||||
if(!Wii[i]) continue; // Skip if it hasn't been defined
|
||||
if(Wii[i]->wiimoteConnected) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue