Wiiuse is a library written in C that connects with several Nintendo Wii remotes. Supports motion sensing, IR tracking, nunchuk, classic controller, and the Guitar Hero 3 controller. Single threaded and nonblocking makes a light weight and clean API.
Licensed under GNU GPLv3 and GNU LGPLv3 (non-commercial).
#include
#include
#ifndef WIN32
#include
#endif
#include "wiiuse.h"
#define MAX_WIIMOTES 4
void
handle_event(struct
wiimote_t* wm) {
printf("\n\n--- EVENT [id %i] ---\n"
, wm->unid);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_A)) printf("A pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_B)) printf("B pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_UP)) printf("UP pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_DOWN)) printf("DOWN pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_LEFT)) printf("LEFT pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_RIGHT)) printf("RIGHT pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_MINUS)) printf("MINUS pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_PLUS)) printf("PLUS pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_ONE)) printf("ONE pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_TWO)) printf("TWO pressed\n"
);
if
(IS_PRESSED(wm, WIIMOTE_BUTTON_HOME)) printf("HOME pressed\n"
);
if
(IS_JUST_PRESSED(wm, WIIMOTE_BUTTON_MINUS))
wiiuse_motion_sensing(wm, 0);
if
(IS_JUST_PRESSED(wm, WIIMOTE_BUTTON_PLUS))
wiiuse_motion_sensing(wm, 1);
if
(IS_JUST_PRESSED(wm, WIIMOTE_BUTTON_B))
wiiuse_toggle_rumble(wm);
if
(IS_JUST_PRESSED(wm, WIIMOTE_BUTTON_UP))
wiiuse_set_ir(wm, 1);
if
(IS_JUST_PRESSED(wm, WIIMOTE_BUTTON_DOWN))
wiiuse_set_ir(wm, 0);
if
(WIIUSE_USING_ACC(wm)) {
printf("wiimote roll = %f [%f]\n"
, wm->orient.roll, wm->orient.a_roll);
printf("wiimote pitch = %f [%f]\n"
, wm->orient.pitch, wm->orient.a_pitch);
printf("wiimote yaw = %f\n"
, wm->orient.yaw);
}
if
(WIIUSE_USING_IR(wm)) {
int
i = 0;
for
(; i < 4; ++i) {
if
(wm->ir.dot[i].visible)
printf("IR source %i: (%u, %u)\n"
, i, wm->ir.dot[i].x, wm->ir.dot[i].y);
}
printf("IR cursor: (%u, %u)\n"
, wm->ir.x, wm->ir.y);
printf("IR z distance: %f\n"
, wm->ir.z);
}
if
(wm->exp.type == EXP_NUNCHUK) {
struct
nunchuk_t* nc = (nunchuk_t*)&wm->exp.nunchuk;
if
(IS_PRESSED(nc, NUNCHUK_BUTTON_C)) printf("Nunchuk: C pressed\n"
);
if
(IS_PRESSED(nc, NUNCHUK_BUTTON_Z)) printf("Nunchuk: Z pressed\n"
);
printf("nunchuk roll = %f\n"
, nc->orient.roll);
printf("nunchuk pitch = %f\n"
, nc->orient.pitch);
printf("nunchuk yaw = %f\n"
, nc->orient.yaw);
printf("nunchuk joystick angle: %f\n"
, nc->js.ang);
printf("nunchuk joystick magnitude: %f\n"
, nc->js.mag);
} else
if
(wm->exp.type == EXP_CLASSIC) {
struct
classic_ctrl_t* cc = (classic_ctrl_t*)&wm->exp.classic;
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_ZL)) printf("Classic: ZL pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_B)) printf("Classic: B pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_Y)) printf("Classic: Y pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_A)) printf("Classic: A pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_X)) printf("Classic: X pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_ZR)) printf("Classic: ZR pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_LEFT)) printf("Classic: LEFT pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_UP)) printf("Classic: UP pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_RIGHT)) printf("Classic: RIGHT pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_DOWN)) printf("Classic: DOWN pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_FULL_L)) printf("Classic: FULL L pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_MINUS)) printf("Classic: MINUS pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_HOME)) printf("Classic: HOME pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_PLUS)) printf("Classic: PLUS pressed\n"
);
if
(IS_PRESSED(cc, CLASSIC_CTRL_BUTTON_FULL_R)) printf("Classic: FULL R pressed\n"
);
printf("classic L button pressed: %f\n"
, cc->l_shoulder);
printf("classic R button pressed: %f\n"
, cc->r_shoulder);
printf("classic left joystick angle: %f\n"
, cc->ljs.ang);
printf("classic left joystick magnitude: %f\n"
, cc->ljs.mag);
printf("classic right joystick angle: %f\n"
, cc->rjs.ang);
printf("classic right joystick magnitude: %f\n"
, cc->rjs.mag);
} else
if
(wm->exp.type == EXP_GUITAR_HERO_3) {
struct
guitar_hero_3_t* gh3 = (guitar_hero_3_t*)&wm->exp.gh3;
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_STRUM_UP)) printf("Guitar: Strum Up pressed\n"
);
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_STRUM_DOWN)) printf("Guitar: Strum Down pressed\n"
);
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_YELLOW)) printf("Guitar: Yellow pressed\n"
);
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_GREEN)) printf("Guitar: Green pressed\n"
);
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_BLUE)) printf("Guitar: Blue pressed\n"
);
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_RED)) printf("Guitar: Red pressed\n"
);
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_ORANGE)) printf("Guitar: Orange pressed\n"
);
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_PLUS)) printf("Guitar: Plus pressed\n"
);
if
(IS_PRESSED(gh3, GUITAR_HERO_3_BUTTON_MINUS)) printf("Guitar: Minus pressed\n"
);
printf("Guitar whammy bar: %f\n"
, gh3->whammy_bar);
printf("Guitar joystick angle: %f\n"
, gh3->js.ang);
printf("Guitar joystick magnitude: %f\n"
, gh3->js.mag);
}
}
void
handle_read(struct
wiimote_t* wm, byte* data, unsigned
short
len) {
int
i = 0;
printf("\n\n--- DATA READ [wiimote id %i] ---\n"
, wm->unid);
printf("finished read of size %i\n"
, len);
for
(; i < len; ++i) {
if
(!(i%16))
printf("\n"
);
printf("%x "
, data[i]);
}
printf("\n\n"
);
}
void
handle_ctrl_status(struct
wiimote_t* wm) {
printf("\n\n--- CONTROLLER STATUS [wiimote id %i] ---\n"
, wm->unid);
printf("attachment: %i\n"
, wm->exp.type);
printf("speaker: %i\n"
, WIIUSE_USING_SPEAKER(wm));
printf("ir: %i\n"
, WIIUSE_USING_IR(wm));
printf("leds: %i %i %i %i\n"
, WIIUSE_IS_LED_SET(wm, 1), WIIUSE_IS_LED_SET(wm, 2), WIIUSE_IS_LED_SET(wm, 3), WIIUSE_IS_LED_SET(wm, 4));
printf("battery: %f %%\n"
, wm->battery_level);
}
void
handle_disconnect(wiimote* wm) {
printf("\n\n--- DISCONNECTED [wiimote id %i] ---\n"
, wm->unid);
}
void
test(struct
wiimote_t* wm, byte* data, unsigned
short
len) {
printf("test: %i [%x %x %x %x]\n"
, len, data[0], data[1], data[2], data[3]);
}
int
main(int
argc, char
** argv) {
wiimote** wiimotes;
int
found, connected;
wiimotes = wiiuse_init(MAX_WIIMOTES);
found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5);
if
(!found) {
printf ("No wiimotes found."
);
return
0;
}
connected = wiiuse_connect(wiimotes, MAX_WIIMOTES);
if
(connected)
printf("Connected to %i wiimotes (of %i found).\n"
, connected, found);
else
{
printf("Failed to connect to any wiimote.\n"
);
return
0;
}
wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1);
wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2);
wiiuse_set_leds(wiimotes[2], WIIMOTE_LED_3);
wiiuse_set_leds(wiimotes[3], WIIMOTE_LED_4);
wiiuse_rumble(wiimotes[0], 1);
wiiuse_rumble(wiimotes[1], 1);
#ifndef WIN32
usleep(200000);
#else
Sleep(200);
#endif
wiiuse_rumble(wiimotes[0], 0);
wiiuse_rumble(wiimotes[1], 0);
while
(1) {
if
(wiiuse_poll(wiimotes, MAX_WIIMOTES)) {
int
i = 0;
for
(; i < MAX_WIIMOTES; ++i) {
switch
(wiimotes[i]->event) {
case
WIIUSE_EVENT:
handle_event(wiimotes[i]);
break
;
case
WIIUSE_STATUS:
handle_ctrl_status(wiimotes[i]);
break
;
case
WIIUSE_DISCONNECT:
case
WIIUSE_UNEXPECTED_DISCONNECT:
handle_disconnect(wiimotes[i]);
break
;
case
WIIUSE_READ_DATA:
break
;
case
WIIUSE_NUNCHUK_INSERTED:
printf("Nunchuk inserted.\n"
);
break
;
case
WIIUSE_CLASSIC_CTRL_INSERTED:
printf("Classic controller inserted.\n"
);
break
;
case
WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
handle_ctrl_status(wiimotes[i]);
printf("Guitar Hero 3 controller inserted.\n"
);
break
;
case
WIIUSE_NUNCHUK_REMOVED:
case
WIIUSE_CLASSIC_CTRL_REMOVED:
case
WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
handle_ctrl_status(wiimotes[i]);
printf("An expansion was removed.\n"
);
break
;
default
:
break
;
}
}
}
}
wiiuse_cleanup(wiimotes, MAX_WIIMOTES);
return
0;
}