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.

WiiIRCamera.ino 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Example sketch for the Wii libary showing the IR camera functionality. This example
  3. is for the Bluetooth Wii library developed for the USB shield from Circuits@Home
  4. Created by Allan Glover and Kristian Lauszus.
  5. Contact Kristian: http://blog.tkjelectronics.dk/ or send an email at [email protected].
  6. Contact Allan at [email protected]
  7. To test the Wiimote IR camera, you will need access to an IR source. Sunlight will work but is not ideal.
  8. The simpleist solution is to use the Wii sensor bar, i.e. emitter bar, supplied by the Wii system.
  9. Otherwise, wire up a IR LED yourself.
  10. */
  11. #include <Wii.h>
  12. #include <usbhub.h>
  13. // Satisfy the IDE, which needs to see the include statment in the ino too.
  14. #ifdef dobogusinclude
  15. #include <spi4teensy3.h>
  16. #include <SPI.h>
  17. #endif
  18. #ifndef WIICAMERA // Used to check if WIICAMERA is defined
  19. #error "Please set ENABLE_WII_IR_CAMERA to 1 in settings.h"
  20. #endif
  21. USB Usb;
  22. //USBHub Hub1(&Usb); // Some dongles have a hub inside
  23. BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
  24. /* You can create the instance of the class in two ways */
  25. WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once
  26. //WII Wii(&Btd); // After the Wiimote pairs once with the line of code above, you can simply create the instance like so and re upload and then press any button on the Wiimote
  27. bool printAngle;
  28. uint8_t printObjects;
  29. void setup() {
  30. Serial.begin(115200);
  31. #if !defined(__MIPSEL__)
  32. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  33. #endif
  34. if (Usb.Init() == -1) {
  35. Serial.print(F("\r\nOSC did not start"));
  36. while (1); //halt
  37. }
  38. Serial.print(F("\r\nWiimote Bluetooth Library Started"));
  39. }
  40. void loop() {
  41. Usb.Task();
  42. if (Wii.wiimoteConnected) {
  43. if (Wii.getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down
  44. Serial.print(F("\r\nHOME"));
  45. Wii.disconnect();
  46. }
  47. else {
  48. if (Wii.getButtonClick(ONE))
  49. Wii.IRinitialize(); // Run the initialisation sequence
  50. if (Wii.getButtonClick(MINUS) || Wii.getButtonClick(PLUS)) {
  51. if (!Wii.isIRCameraEnabled())
  52. Serial.print(F("\r\nEnable IR camera first"));
  53. else {
  54. if (Wii.getButtonPress(MINUS)) { // getButtonClick will only return true once
  55. if (printObjects > 0)
  56. printObjects--;
  57. }
  58. else {
  59. if (printObjects < 4)
  60. printObjects++;
  61. }
  62. Serial.print(F("\r\nTracking "));
  63. Serial.print(printObjects);
  64. Serial.print(F(" objects"));
  65. }
  66. }
  67. if (Wii.getButtonClick(A)) {
  68. printAngle = !printAngle;
  69. Serial.print(F("\r\nA"));
  70. }
  71. if (Wii.getButtonClick(B)) {
  72. Serial.print(F("\r\nBattery level: "));
  73. Serial.print(Wii.getBatteryLevel()); // You can get the battery level as well
  74. }
  75. }
  76. if (printObjects > 0) {
  77. if (Wii.getIRx1() != 0x3FF || Wii.getIRy1() != 0x3FF || Wii.getIRs1() != 0) { // Only print if the IR camera is actually seeing something
  78. Serial.print(F("\r\nx1: "));
  79. Serial.print(Wii.getIRx1());
  80. Serial.print(F("\ty1: "));
  81. Serial.print(Wii.getIRy1());
  82. Serial.print(F("\ts1:"));
  83. Serial.print(Wii.getIRs1());
  84. }
  85. if (printObjects > 1) {
  86. if (Wii.getIRx2() != 0x3FF || Wii.getIRy2() != 0x3FF || Wii.getIRs2() != 0) {
  87. Serial.print(F("\r\nx2: "));
  88. Serial.print(Wii.getIRx2());
  89. Serial.print(F("\ty2: "));
  90. Serial.print(Wii.getIRy2());
  91. Serial.print(F("\ts2:"));
  92. Serial.print(Wii.getIRs2());
  93. }
  94. if (printObjects > 2) {
  95. if (Wii.getIRx3() != 0x3FF || Wii.getIRy3() != 0x3FF || Wii.getIRs3() != 0) {
  96. Serial.print(F("\r\nx3: "));
  97. Serial.print(Wii.getIRx3());
  98. Serial.print(F("\ty3: "));
  99. Serial.print(Wii.getIRy3());
  100. Serial.print(F("\ts3:"));
  101. Serial.print(Wii.getIRs3());
  102. }
  103. if (printObjects > 3) {
  104. if (Wii.getIRx4() != 0x3FF || Wii.getIRy4() != 0x3FF || Wii.getIRs4() != 0) {
  105. Serial.print(F("\r\nx4: "));
  106. Serial.print(Wii.getIRx4());
  107. Serial.print(F("\ty4: "));
  108. Serial.print(Wii.getIRy4());
  109. Serial.print(F("\ts4:"));
  110. Serial.print(Wii.getIRs4());
  111. }
  112. }
  113. }
  114. }
  115. }
  116. if (printAngle) { // There is no extension bytes available, so the MotionPlus or Nunchuck can't be read
  117. Serial.print(F("\r\nPitch: "));
  118. Serial.print(Wii.getPitch());
  119. Serial.print(F("\tRoll: "));
  120. Serial.print(Wii.getRoll());
  121. }
  122. }
  123. }