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