]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/Wii/Wii.ino
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / Bluetooth / Wii / Wii.ino
1 /*
2  Example sketch for the Wiimote Bluetooth library - developed by Kristian Lauszus
3  For more information visit my blog: http://blog.tkjelectronics.dk/ or
4  send me an e-mail:  kristianl@tkjelectronics.com
5  */
6
7 #include <Wii.h>
8 #include <usbhub.h>
9
10 // Satisfy the IDE, which needs to see the include statment in the ino too.
11 #ifdef dobogusinclude
12 #include <spi4teensy3.h>
13 #include <SPI.h>
14 #endif
15
16 USB Usb;
17 //USBHub Hub1(&Usb); // Some dongles have a hub inside
18
19 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
20 /* You can create the instance of the class in two ways */
21 WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once
22 //WII Wii(&Btd); // After that you can simply create the instance like so and then press any button on the Wiimote
23
24 bool printAngle;
25
26 void setup() {
27   Serial.begin(115200);
28 #if !defined(__MIPSEL__)
29   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
30 #endif
31   if (Usb.Init() == -1) {
32     Serial.print(F("\r\nOSC did not start"));
33     while (1); //halt
34   }
35   Serial.print(F("\r\nWiimote Bluetooth Library Started"));
36 }
37 void loop() {
38   Usb.Task();
39   if (Wii.wiimoteConnected) {
40     if (Wii.getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down
41       Serial.print(F("\r\nHOME"));
42       Wii.disconnect();
43     }
44     else {
45       if (Wii.getButtonClick(LEFT)) {
46         Wii.setLedOff();
47         Wii.setLedOn(LED1);
48         Serial.print(F("\r\nLeft"));
49       }
50       if (Wii.getButtonClick(RIGHT)) {
51         Wii.setLedOff();
52         Wii.setLedOn(LED3);
53         Serial.print(F("\r\nRight"));
54       }
55       if (Wii.getButtonClick(DOWN)) {
56         Wii.setLedOff();
57         Wii.setLedOn(LED4);
58         Serial.print(F("\r\nDown"));
59       }
60       if (Wii.getButtonClick(UP)) {
61         Wii.setLedOff();
62         Wii.setLedOn(LED2);
63         Serial.print(F("\r\nUp"));
64       }
65
66       if (Wii.getButtonClick(PLUS))
67         Serial.print(F("\r\nPlus"));
68       if (Wii.getButtonClick(MINUS))
69         Serial.print(F("\r\nMinus"));
70
71       if (Wii.getButtonClick(ONE))
72         Serial.print(F("\r\nOne"));
73       if (Wii.getButtonClick(TWO))
74         Serial.print(F("\r\nTwo"));
75
76       if (Wii.getButtonClick(A)) {
77         printAngle = !printAngle;
78         Serial.print(F("\r\nA"));
79       }
80       if (Wii.getButtonClick(B)) {
81         Wii.setRumbleToggle();
82         Serial.print(F("\r\nB"));
83       }
84     }
85 #if 0 // Set this to 1 in order to see the angle of the controllers
86     if (printAngle) {
87       Serial.print(F("\r\nPitch: "));
88       Serial.print(Wii.getPitch());
89       Serial.print(F("\tRoll: "));
90       Serial.print(Wii.getRoll());
91       if (Wii.motionPlusConnected) {
92         Serial.print(F("\tYaw: "));
93         Serial.print(Wii.getYaw());
94       }
95       if (Wii.nunchuckConnected) {
96         Serial.print(F("\tNunchuck Pitch: "));
97         Serial.print(Wii.getNunchuckPitch());
98         Serial.print(F("\tNunchuck Roll: "));
99         Serial.print(Wii.getNunchuckRoll());
100       }
101     }
102 #endif
103   }
104 #if 0 // Set this to 1 if you are using a Nunchuck controller
105   if (Wii.nunchuckConnected) {
106     if (Wii.getButtonClick(Z))
107       Serial.print(F("\r\nZ"));
108     if (Wii.getButtonClick(C))
109       Serial.print(F("\r\nC"));
110     if (Wii.getAnalogHat(HatX) > 137 ||  Wii.getAnalogHat(HatX) < 117 || Wii.getAnalogHat(HatY) > 137 || Wii.getAnalogHat(HatY) < 117) {
111       Serial.print(F("\r\nHatX: "));
112       Serial.print(Wii.getAnalogHat(HatX));
113       Serial.print(F("\tHatY: "));
114       Serial.print(Wii.getAnalogHat(HatY));
115     }
116   }
117 #endif
118 }