USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino

124 lines
4.3 KiB
Arduino
Raw Normal View History

/*
Example sketch for the Wiimote Bluetooth library - developed by Kristian Lauszus
This example show how one can use multiple controllers with the library
2013-11-15 19:05:25 +01:00
For more information visit my blog: http://blog.tkjelectronics.dk/ or
send me an e-mail: kristianl@tkjelectronics.com
*/
#include <Wii.h>
2013-08-13 21:31:52 +02:00
#include <usbhub.h>
USB Usb;
2013-08-13 21:31:52 +02:00
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!
2013-11-15 19:05:25 +01:00
const uint8_t length = sizeof(Wii) / sizeof(Wii[0]); // Get the lenght of the array
boolean printAngle[length];
boolean oldControllerState[length];
void setup() {
2013-11-15 19:05:25 +01:00
for (uint8_t i = 0; i < length; i++) {
2013-06-12 05:11:43 +02:00
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
}
2013-11-15 19:05:25 +01:00
Serial.begin(115200);
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
2013-11-15 19:05:25 +01:00
while (1); //halt
}
Serial.print(F("\r\nWiimote Bluetooth Library Started"));
}
void loop() {
Usb.Task();
2013-11-15 19:05:25 +01:00
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"));
2013-01-23 22:47:47 +01:00
Wii[i]->disconnect();
oldControllerState[i] = false; // Reset value
2013-11-15 19:05:25 +01:00
}
else {
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(LEFT)) {
Wii[i]->setLedOff();
Wii[i]->setLedOn(LED1);
Serial.print(F("\r\nLeft"));
}
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(RIGHT)) {
Wii[i]->setLedOff();
Wii[i]->setLedOn(LED3);
Serial.print(F("\r\nRight"));
}
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(DOWN)) {
Wii[i]->setLedOff();
Wii[i]->setLedOn(LED4);
Serial.print(F("\r\nDown"));
2013-11-15 19:05:25 +01:00
}
if (Wii[i]->getButtonClick(UP)) {
Wii[i]->setLedOff();
2013-11-15 19:05:25 +01:00
Wii[i]->setLedOn(LED2);
Serial.print(F("\r\nUp"));
}
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(PLUS))
Serial.print(F("\r\nPlus"));
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(MINUS))
Serial.print(F("\r\nMinus"));
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(ONE))
Serial.print(F("\r\nOne"));
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(TWO))
Serial.print(F("\r\nTwo"));
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(A)) {
printAngle[i] = !printAngle[i];
Serial.print(F("\r\nA"));
2013-11-15 19:05:25 +01:00
}
if (Wii[i]->getButtonClick(B)) {
Wii[i]->setRumbleToggle();
Serial.print(F("\r\nB"));
}
}
2013-11-15 19:05:25 +01:00
if (printAngle[i]) {
Serial.print(F("\r\nPitch: "));
Serial.print(Wii[i]->getPitch());
Serial.print(F("\tRoll: "));
Serial.print(Wii[i]->getRoll());
2013-11-15 19:05:25 +01:00
if (Wii[i]->motionPlusConnected) {
Serial.print(F("\tYaw: "));
Serial.print(Wii[i]->getYaw());
2013-11-15 19:05:25 +01:00
}
if (Wii[i]->nunchuckConnected) {
Serial.print(F("\tNunchuck Pitch: "));
2013-10-21 18:41:47 +02:00
Serial.print(Wii[i]->getNunchuckPitch());
Serial.print(F("\tNunchuck Roll: "));
2013-10-21 18:41:47 +02:00
Serial.print(Wii[i]->getNunchuckRoll());
}
}
}
2013-11-15 19:05:25 +01:00
if (Wii[i]->nunchuckConnected) {
if (Wii[i]->getButtonClick(Z))
Serial.print(F("\r\nZ"));
2013-11-15 19:05:25 +01:00
if (Wii[i]->getButtonClick(C))
Serial.print(F("\r\nC"));
2013-11-15 19:05:25 +01:00
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() {
2013-11-15 19:05:25 +01:00
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((LED)i); // Cast directly to LED enum - see: "controllerEnums.h"
}
}
}