Blame |
Last modification |
View Log
| RSS feed
/* This file is part of MoteCtrl
http://www.mikrokopter.de/ucwiki/en/MoteCtrl
*/
#include <stdio.h> //I/O functions
#include <windows.h> //like stdlib.h
//#include "wiiuse.h" //the wiiuse lib to communicate with the wiimote
#include "wiimote.h"
/*Nunchuck struct, main.c don't needs this, only wiimote.c needs this
in the handle_event function */
struct nunchuk_t
* nc
;
int init_wiimote
(void) {
int tmp
;
/*Initialize / create one Wiimote handle:
wiiuse_init is creating the corresponding wiimote struct and returns
a pointer to the created typedefed struct */
mote
= wiiuse_init
(MAX_WIIMOTES
);
/*Now connect to the wiimote, that should be in discovery mode (all 4 leds
should be blinking, after you activated the Bluetooth HID service
--> means: "I'm in discovery mode, searching for a Wii").
wiiuse_find will scan and connect the number of wiimotes passed.
The information about the connected wiimote will be stored in the
passed wiimote struct. */
printf("\nAutodetecting your Bluetooth Stack (like Blue Soleil for instance)\n"
"Scanning for available Wiimotes...\n"
"(make sure BT HID service is running and the 4 LEDs of the Wiimote are blinking)\n\n");
tmp
= wiiuse_find
(mote
, MAX_WIIMOTES
, 8); //8 sec timeout
if(tmp
== 0) {
printf("***ERR: Couldn't find Bluetooth stack / No Wiimote found\n"
"Bluetooth stack supported by wiiuse? HID service running?\n"
"Wiimote in discovery mode (all 4 LED's blinking)?\n\n");
return EXIT_FAILURE
;
}
else { printf("\n***Wiimote found, trying to connect...\n\n"); }
//for backup (since wiiuse_find already connected) call wiiuse_connect:
tmp
= wiiuse_connect
(mote
, MAX_WIIMOTES
);
if(tmp
== 0) {
printf("***ERR:Couldn't establish connection with found Wiimote\n\n");
return EXIT_FAILURE
;
}
else { printf("***Connection to Wiimote established!\n\n"); }
//Wiimote is connected, everything could be done now
//Do a bit of fun:
wiiuse_rumble
(mote
[0], 1); //switch rumble on
//blinking with leds ;)
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_1
);
Sleep
(100);
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_NONE
);
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_2
);
Sleep
(100);
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_NONE
);
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_3
);
Sleep
(100);
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_NONE
);
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_4
);
Sleep
(100);
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_NONE
);
wiiuse_rumble
(mote
[0], 0); //switch rumble off
/*if there happens no LED change event (or some event) after rumble-turn-off,
the rumble-turn-off has no effect (seems to be a bug in the wiiuse API) */
wiiuse_set_leds
(mote
[0], WIIMOTE_LED_1
);
Sleep
(1000); //wait a sek to let the rumble vibe off
//do not press any buttons in that time!
//Avtivate the motion sensing feature of the wiimote:
wiiuse_motion_sensing
(mote
[0], 1);
return EXIT_SUCCESS
;
}
int check_nunchuck
(void) {
if(mote
[0]->exp.
type){
return 1;
}
else { return 0; }
}
//write Disconnect Wrapper ?
//wiiuse_disconnect(mote[0]);
/*This Subroutine is called, when the wiimote sends a generic event, like
a pushed button or the tilt changing by at least 0.5 degrees:
(it passes the wiimote data to the ExternalControl struct) */
void handle_event
(struct wiimote_t
* wm
) {
/***** Button and tilt-sensor assignments for the WIIMOTE: *****/
/*GAS: (better use the nunchuck buttons to control the gas, since the
wiimotes tilt sensor is very sensitive and when pressing buttons on
the wiimote you atomatically change the state of the tilt)*/
if (IS_JUST_PRESSED
(wm
, WIIMOTE_BUTTON_HOME
)) { //reset gas value to 0
ExternControl.
Gas = 0; //(be careful with that!)
printf("\n***************Resetting GAS to ZERO!***************\n\n");
}
if (IS_JUST_PRESSED
(wm
, WIIMOTE_BUTTON_UP
)) {
if (ExternControl.
Gas < 240) { //overflow protection, max gas limit
ExternControl.
Gas++; //increment gas value
}
}
if (IS_JUST_PRESSED
(wm
, WIIMOTE_BUTTON_DOWN
)) {
if (ExternControl.
Gas > 0) { //underflow protection (0-- = 255!!!GAS)
ExternControl.
Gas--; //decrement gas value
}
}
//NICK & ROLL:
if( (wm
->orient.
roll > 127.0) || //overrun protections
(wm
->orient.
roll < -127.0) ||
(wm
->orient.
pitch > 127.0) ||
(wm
->orient.
pitch < -127.0))
{ return; }
else {
/*Associate the degree-Values of the wiimotes tilt sensor to the
corresponding ExternalControl struct values: */
ExternControl.
Nick = (signed char) wm
->orient.
pitch;
ExternControl.
Roll = -( (signed char) wm
->orient.
roll );
}
/***** Button and joystick assignments for the NUNCHUCK extension: *****/
nc
= (nunchuk_t
*)&mote
[0]->exp.
nunchuk;
//GAS:
if (IS_JUST_PRESSED
(nc
, NUNCHUK_BUTTON_C
)) {
if (ExternControl.
Gas < 240) { //overflow protection, max gas limit
ExternControl.
Gas++; //increment gas value
}
}
if (IS_JUST_PRESSED
(nc
, NUNCHUK_BUTTON_Z
)) {
if (ExternControl.
Gas > 0) { //underflow protection (0-- = 255!!!GAS)
ExternControl.
Gas--; //decrement gas value
}
}
//GIER:
if( (nc
->js.
ang > 0.0) && (nc
->js.
ang < 180) ) {
/* Nunchuck joystick is pressed to the right half, means
gier with the MikroKopter to the right: */
ExternControl.
Gier = (signed char) (nc
->js.
mag * 100);
}
if( (nc
->js.
ang > 180) ) {
/* Nunchuck joystick is pressed to the left half, means
gier with the MikroKopter to the left: */
ExternControl.
Gier = -(signed char) (nc
->js.
mag * 100);
}
//DEBUG Output: Print ExternControl status
printf("Gas: %3d Gier: %4d Nick: %4d Roll: %4d \n",
ExternControl.
Gas, ExternControl.
Gier, ExternControl.
Nick, ExternControl.
Roll);
}
/*
TODO: bei connectionabbruch (ext.config=0),
ctrl+q / ctrl+x -> abbruch (disconnect, ext.config=0)
*/