diff options
author | Jack Humbert <jack.humb@gmail.com> | 2015-10-27 14:52:18 -0400 |
---|---|---|
committer | Jack Humbert <jack.humb@gmail.com> | 2015-10-27 14:52:18 -0400 |
commit | d311fd8d1a0af3b767d26b520d399bf0db22f058 (patch) | |
tree | b9de5704143ac205b4e26222f9d7551e9b161924 /tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino | |
parent | a766918d5c48204375f4c207b30bbbf1389df14f (diff) | |
parent | 6f3141965e88c4ee0ebf7c18a243e4c2d9c5021b (diff) |
Merge pull request #34 from jackhumbert/tmk-master
merging from tmk, new_project script and template
Diffstat (limited to 'tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino')
-rw-r--r-- | tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino new file mode 100644 index 0000000000..07c6f13d2b --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino @@ -0,0 +1,132 @@ +/* + 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> +#include <usbhub.h> + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +//USBHub Hub1(&Usb); // Some dongles have a hub inside + +BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so +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! +const uint8_t length = sizeof(Wii) / sizeof(Wii[0]); // Get the lenght of the array +bool printAngle[length]; +bool oldControllerState[length]; + +void setup() { + for (uint8_t i = 0; i < length; i++) { + 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 + Wii[i]->attachOnInit(onInit); // onInit() is called upon a new connection - you can call the function whatever you like + } + + Serial.begin(115200); +#if !defined(__MIPSEL__) + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection +#endif + 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]->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(); + oldControllerState[i] = false; // Reset value + } + else { + if (Wii[i]->getButtonClick(LEFT)) { + Wii[i]->setLedOff(); + Wii[i]->setLedOn(LED1); + Serial.print(F("\r\nLeft")); + } + if (Wii[i]->getButtonClick(RIGHT)) { + Wii[i]->setLedOff(); + Wii[i]->setLedOn(LED3); + Serial.print(F("\r\nRight")); + } + if (Wii[i]->getButtonClick(DOWN)) { + Wii[i]->setLedOff(); + Wii[i]->setLedOn(LED4); + Serial.print(F("\r\nDown")); + } + if (Wii[i]->getButtonClick(UP)) { + Wii[i]->setLedOff(); + 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]->getNunchuckPitch()); + Serial.print(F("\tNunchuck Roll: ")); + Serial.print(Wii[i]->getNunchuckRoll()); + } + } + } + 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)); + } + } + } +} + +void onInit() { + for (uint8_t i = 0; i < length; i++) { + if (Wii[i]->wiimoteConnected && !oldControllerState[i]) { + oldControllerState[i] = true; // Used to check which is the new controller + Wii[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h" + } + } +} |