You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

WiiMulti.ino 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: [email protected]
  6. */
  7. #include <Wii.h>
  8. #include <usbhub.h>
  9. // Satisfy the IDE, which needs to see the include statment in the ino too.
  10. #ifdef dobogusinclude
  11. #include <spi4teensy3.h>
  12. #include <SPI.h>
  13. #endif
  14. USB Usb;
  15. //USBHub Hub1(&Usb); // Some dongles have a hub inside
  16. BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
  17. 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!
  18. const uint8_t length = sizeof(Wii) / sizeof(Wii[0]); // Get the lenght of the array
  19. bool printAngle[length];
  20. bool oldControllerState[length];
  21. void setup() {
  22. for (uint8_t i = 0; i < length; i++) {
  23. 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
  24. Wii[i]->attachOnInit(onInit); // onInit() is called upon a new connection - you can call the function whatever you like
  25. }
  26. Serial.begin(115200);
  27. #if !defined(__MIPSEL__)
  28. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  29. #endif
  30. if (Usb.Init() == -1) {
  31. Serial.print(F("\r\nOSC did not start"));
  32. while (1); //halt
  33. }
  34. Serial.print(F("\r\nWiimote Bluetooth Library Started"));
  35. }
  36. void loop() {
  37. Usb.Task();
  38. for (uint8_t i = 0; i < length; i++) {
  39. if (Wii[i]->wiimoteConnected) {
  40. if (Wii[i]->getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down
  41. Serial.print(F("\r\nHOME"));
  42. Wii[i]->disconnect();
  43. oldControllerState[i] = false; // Reset value
  44. }
  45. else {
  46. if (Wii[i]->getButtonClick(LEFT)) {
  47. Wii[i]->setLedOff();
  48. Wii[i]->setLedOn(LED1);
  49. Serial.print(F("\r\nLeft"));
  50. }
  51. if (Wii[i]->getButtonClick(RIGHT)) {
  52. Wii[i]->setLedOff();
  53. Wii[i]->setLedOn(LED3);
  54. Serial.print(F("\r\nRight"));
  55. }
  56. if (Wii[i]->getButtonClick(DOWN)) {
  57. Wii[i]->setLedOff();
  58. Wii[i]->setLedOn(LED4);
  59. Serial.print(F("\r\nDown"));
  60. }
  61. if (Wii[i]->getButtonClick(UP)) {
  62. Wii[i]->setLedOff();
  63. Wii[i]->setLedOn(LED2);
  64. Serial.print(F("\r\nUp"));
  65. }
  66. if (Wii[i]->getButtonClick(PLUS))
  67. Serial.print(F("\r\nPlus"));
  68. if (Wii[i]->getButtonClick(MINUS))
  69. Serial.print(F("\r\nMinus"));
  70. if (Wii[i]->getButtonClick(ONE))
  71. Serial.print(F("\r\nOne"));
  72. if (Wii[i]->getButtonClick(TWO))
  73. Serial.print(F("\r\nTwo"));
  74. if (Wii[i]->getButtonClick(A)) {
  75. printAngle[i] = !printAngle[i];
  76. Serial.print(F("\r\nA"));
  77. }
  78. if (Wii[i]->getButtonClick(B)) {
  79. Wii[i]->setRumbleToggle();
  80. Serial.print(F("\r\nB"));
  81. }
  82. }
  83. if (printAngle[i]) {
  84. Serial.print(F("\r\nPitch: "));
  85. Serial.print(Wii[i]->getPitch());
  86. Serial.print(F("\tRoll: "));
  87. Serial.print(Wii[i]->getRoll());
  88. if (Wii[i]->motionPlusConnected) {
  89. Serial.print(F("\tYaw: "));
  90. Serial.print(Wii[i]->getYaw());
  91. }
  92. if (Wii[i]->nunchuckConnected) {
  93. Serial.print(F("\tNunchuck Pitch: "));
  94. Serial.print(Wii[i]->getNunchuckPitch());
  95. Serial.print(F("\tNunchuck Roll: "));
  96. Serial.print(Wii[i]->getNunchuckRoll());
  97. }
  98. }
  99. }
  100. if (Wii[i]->nunchuckConnected) {
  101. if (Wii[i]->getButtonClick(Z))
  102. Serial.print(F("\r\nZ"));
  103. if (Wii[i]->getButtonClick(C))
  104. Serial.print(F("\r\nC"));
  105. if (Wii[i]->getAnalogHat(HatX) > 137 || Wii[i]->getAnalogHat(HatX) < 117 || Wii[i]->getAnalogHat(HatY) > 137 || Wii[i]->getAnalogHat(HatY) < 117) {
  106. Serial.print(F("\r\nHatX: "));
  107. Serial.print(Wii[i]->getAnalogHat(HatX));
  108. Serial.print(F("\tHatY: "));
  109. Serial.print(Wii[i]->getAnalogHat(HatY));
  110. }
  111. }
  112. }
  113. }
  114. void onInit() {
  115. for (uint8_t i = 0; i < length; i++) {
  116. if (Wii[i]->wiimoteConnected && !oldControllerState[i]) {
  117. oldControllerState[i] = true; // Used to check which is the new controller
  118. Wii[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h"
  119. }
  120. }
  121. }