Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
178 Alpin 1
/*      This file is part of MoteCtrl
2
        http://www.mikrokopter.de/ucwiki/en/MoteCtrl
3
*/
4
 
5
#include <stdio.h>              //I/O functions
6
#include <windows.h>    //like stdlib.h
7
//#include "wiiuse.h"   //the wiiuse lib to communicate with the wiimote
8
 
9
#include "wiimote.h"
10
 
11
/*Nunchuck struct, main.c don't needs this, only wiimote.c needs this
12
in the handle_event function */
13
struct nunchuk_t* nc;
14
 
15
 
16
int init_wiimote (void) {
17
 
18
 
19
        int tmp;
20
 
21
        /*Initialize / create one Wiimote handle:
22
        wiiuse_init is creating the corresponding wiimote struct and returns
23
        a pointer to the created typedefed struct */
24
        mote = wiiuse_init(MAX_WIIMOTES);
25
 
26
        /*Now connect to the wiimote, that should be in discovery mode (all 4 leds
27
        should be blinking, after you activated the Bluetooth HID service
28
        --> means: "I'm in discovery mode, searching for a Wii").
29
        wiiuse_find will scan and connect the number of wiimotes passed.
30
        The information about the connected wiimote will be stored in the
31
        passed wiimote struct. */
32
        printf("\nAutodetecting your Bluetooth Stack (like Blue Soleil for instance)\n"
33
                "Scanning for available Wiimotes...\n"
34
                "(make sure BT HID service is running and the 4 LEDs of the Wiimote are blinking)\n\n");
35
 
36
        tmp = wiiuse_find(mote, MAX_WIIMOTES, 8);       //8 sec timeout
37
 
38
        if(tmp == 0) {
39
                printf("***ERR: Couldn't find Bluetooth stack / No Wiimote found\n"
40
                        "Bluetooth stack supported by wiiuse? HID service running?\n"
41
                        "Wiimote in discovery mode (all 4 LED's blinking)?\n\n");
42
                return EXIT_FAILURE;
43
        }
44
        else { printf("\n***Wiimote found, trying to connect...\n\n"); }
45
 
46
        //for backup (since wiiuse_find already connected) call wiiuse_connect:
47
        tmp = wiiuse_connect(mote, MAX_WIIMOTES);
48
 
49
        if(tmp == 0) {
50
                printf("***ERR:Couldn't establish connection with found Wiimote\n\n");
51
                return EXIT_FAILURE;
52
        }
53
        else { printf("***Connection to Wiimote established!\n\n"); }
54
 
55
 
56
        //Wiimote is connected, everything could be done now
57
 
58
        //Do a bit of fun:
59
 
60
        wiiuse_rumble(mote[0], 1);              //switch rumble on
61
 
62
        //blinking with leds ;)
63
        wiiuse_set_leds(mote[0], WIIMOTE_LED_1);
64
        Sleep(100);
65
        wiiuse_set_leds(mote[0], WIIMOTE_LED_NONE);
66
        wiiuse_set_leds(mote[0], WIIMOTE_LED_2);
67
        Sleep(100);
68
        wiiuse_set_leds(mote[0], WIIMOTE_LED_NONE);
69
        wiiuse_set_leds(mote[0], WIIMOTE_LED_3);
70
        Sleep(100);
71
        wiiuse_set_leds(mote[0], WIIMOTE_LED_NONE);
72
        wiiuse_set_leds(mote[0], WIIMOTE_LED_4);
73
        Sleep(100);
74
        wiiuse_set_leds(mote[0], WIIMOTE_LED_NONE);
75
 
76
        wiiuse_rumble(mote[0], 0);              //switch rumble off
77
 
78
 
79
        /*if there happens no LED change event (or some event) after rumble-turn-off,
80
        the rumble-turn-off has no effect (seems to be a bug in the wiiuse API) */
81
        wiiuse_set_leds(mote[0], WIIMOTE_LED_1);       
82
 
83
        Sleep(1000);    //wait a sek to let the rumble vibe off
84
                                        //do not press any buttons in that time!
85
 
86
        //Avtivate the motion sensing feature of the wiimote:
87
        wiiuse_motion_sensing(mote[0], 1);
88
 
89
 
90
        return EXIT_SUCCESS;
91
}
92
 
93
 
94
int check_nunchuck(void) {
95
        if(mote[0]->exp.type){
96
                return 1;
97
        }
98
        else { return 0; }
99
}
100
 
101
 
102
//write Disconnect Wrapper ?
103
        //wiiuse_disconnect(mote[0]);
104
 
105
 
106
/*This Subroutine is called, when the wiimote sends a generic event, like
107
 a pushed button or the tilt changing by at least 0.5 degrees:
108
(it passes the wiimote data to the ExternalControl struct) */
109
void handle_event(struct wiimote_t* wm) {
110
 
111
        /***** Button and tilt-sensor assignments for the WIIMOTE: *****/
112
 
113
        /*GAS: (better use the nunchuck buttons to control the gas, since the
114
        wiimotes tilt sensor is very sensitive and when pressing buttons on
115
        the wiimote you atomatically change the state of the tilt)*/
116
 
117
        if (IS_JUST_PRESSED(wm, WIIMOTE_BUTTON_HOME)) { //reset gas value to 0
118
                ExternControl.Gas = 0;                                          //(be careful with that!)
119
                printf("\n***************Resetting GAS to ZERO!***************\n\n");
120
        }
121
 
122
        if (IS_JUST_PRESSED(wm, WIIMOTE_BUTTON_UP)) {
123
                if (ExternControl.Gas < 240) {  //overflow protection, max gas limit
124
                ExternControl.Gas++;    //increment gas value
125
                }
126
        }
127
 
128
        if (IS_JUST_PRESSED(wm, WIIMOTE_BUTTON_DOWN)) {
129
                if (ExternControl.Gas > 0) {    //underflow protection (0-- = 255!!!GAS)
130
                ExternControl.Gas--;    //decrement gas value
131
                }
132
        }
133
 
134
        //NICK & ROLL:
135
        if( (wm->orient.roll > 127.0) ||        //overrun protections
136
                (wm->orient.roll < -127.0) ||
137
                (wm->orient.pitch > 127.0) ||
138
                (wm->orient.pitch < -127.0))
139
                { return; }
140
        else {
141
                /*Associate the degree-Values of the wiimotes tilt sensor to the
142
                corresponding ExternalControl struct values: */
143
                ExternControl.Nick = (signed char) wm->orient.pitch;
144
                ExternControl.Roll = -( (signed char) wm->orient.roll );
145
        }
146
 
147
        /***** Button and joystick assignments for the NUNCHUCK extension: *****/
148
 
149
        nc = (nunchuk_t*)&mote[0]->exp.nunchuk;
150
 
151
        //GAS: 
152
        if (IS_JUST_PRESSED(nc, NUNCHUK_BUTTON_C)) {
153
                if (ExternControl.Gas < 240) {  //overflow protection, max gas limit
154
                ExternControl.Gas++;    //increment gas value
155
                }
156
        }
157
 
158
        if (IS_JUST_PRESSED(nc, NUNCHUK_BUTTON_Z)) {
159
                if (ExternControl.Gas > 0) {    //underflow protection (0-- = 255!!!GAS)
160
                ExternControl.Gas--;    //decrement gas value
161
                }
162
        }
163
 
164
        //GIER: 
165
        if( (nc->js.ang > 0.0) && (nc->js.ang < 180) ) {
166
                /* Nunchuck joystick is pressed to the right half, means
167
                gier with the MikroKopter to the right: */
168
                ExternControl.Gier = (signed char) (nc->js.mag * 100);
169
        }
170
 
171
        if( (nc->js.ang > 180) ) {
172
                /* Nunchuck joystick is pressed to the left half, means
173
                gier with the MikroKopter to the left: */
174
                ExternControl.Gier = -(signed char) (nc->js.mag * 100);
175
        }
176
 
177
 
178
        //DEBUG Output: Print ExternControl status 
179
        printf("Gas: %3d    Gier: %4d    Nick: %4d    Roll: %4d \n",
180
                ExternControl.Gas, ExternControl.Gier, ExternControl.Nick, ExternControl.Roll);
181
 
182
}
183
/*
184
TODO: bei connectionabbruch (ext.config=0),
185
ctrl+q / ctrl+x -> abbruch (disconnect, ext.config=0)
186
*/