/** * This file is part of WiiuseJ. * * WiiuseJ is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * WiiuseJ is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with WiiuseJ. If not, see . */ #ifndef WIN32 #include #else #endif #include "wiiusej_WiiUseApi.h" #include "wiiuse.h" /* * These are some identifiers for wiimotes * * See below in main() for what they are used for. */ #define WIIMOTE_STATE_RUMBLE 0x08 #define WIIMOTE_STATE_CONNECTED 0x04 #define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s)) #define WIIMOTE_IS_FLAG_SET(wm, s) ((wm->flags & (s)) == (s)) /********************* VARIABLES DECLARATIONS *****************************/ /* * Make a temp array of wiimote ids. * Here I only anticipate connecting up to * two wiimotes. Each wiimote connected * will get one of these ids. */ static wiimote** wiimotes; static int nbMaxWiiMotes=0; /****************** GENERAL FUNCTIONS DEFINITIONS *************************/ /** * Try to connect to 2 wiimotes. * Make them rumble to show they are connected. * @param nbConnects number of connections maximum. * @param rumble * make the connected wiimotes rumble. * @return 0 if there is an error otherwise it returns * the number of wiimotes connected.. */ JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections (JNIEnv *env, jobject obj, jint nbConnects, jboolean rumble) { /* variables declarations */ int found, connected, i; short leds; nbMaxWiiMotes = nbConnects; /* initialize wiimotes array with the maximum number of wiimotes */ wiimotes = wiiuse_init(nbMaxWiiMotes); /* * Find wiimote devices * Now we need to find some wiimotes. * Give the function the wiimote array we created, and tell it there * are 2 wiimotes we are interested in. * Set the timeout to be 5 seconds. * This will return the number of actual wiimotes that are in discovery mode. */ found = wiiuse_find(wiimotes, nbMaxWiiMotes, 5); if (!found) return 0; /* * Connect to the wiimotes * Now that we found some wiimotes, connect to them. * Give the function the wiimote array and the number of wiimote devices we found. * This will return the number of established connections to the found wiimotes. */ connected = wiiuse_connect(wiimotes, nbMaxWiiMotes); if (!connected) return 0; //no problems during connection show that wiimotes are connected /* * Now set the LEDs and rumble for a second so it's easy * to tell which wiimotes are connected (just like the wii does). */ for (i=0;iGetObjectClass(env, gath); jmethodID mid; if (wiiuse_poll(wiimotes, nbMaxWiiMotes)) { /* * This happens if something happened on any wiimote. * So go through each one and check if anything happened. */ for (i=0; i < nbMaxWiiMotes; ++i) { switch (wiimotes[i]->event) { case WIIUSE_EVENT: /* a generic event occured */ mid = (*env)->GetMethodID(env, cls, "prepareWiiMoteEvent", "(ISSS)V"); if (mid == 0) { return; } (*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid, wiimotes[i]->btns, wiimotes[i]->btns_released, wiimotes[i]->btns_held); /* * If IR tracking is enabled then print the coordinates * on the virtual screen that the wiimote is pointing to. * * Also make sure that we see at least 1 dot. */ if (WIIUSE_USING_IR(wiimotes[i])) { int a = 0; mid = (*env)->GetMethodID(env, cls, "prepareIRevent", "(IIIIIIIIISS)V"); if (mid == 0) { return; } (*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->ir.x, wiimotes[i]->ir.y, wiimotes[i]->ir.z, wiimotes[i]->ir.ax, wiimotes[i]->ir.ay, wiimotes[i]->ir.vres[0], wiimotes[i]->ir.vres[1], wiimotes[i]->ir.offset[0], wiimotes[i]->ir.offset[1], wiimotes[i]->ir.pos, wiimotes[i]->ir.aspect); mid = (*env)->GetMethodID(env, cls, "addIRPointToPreparedWiiMoteEvent", "(IISSS)V"); if (mid == 0) { return; } /* go through each of the 4 possible IR sources */ for (; a < 4; a++) { /* check if the source is visible */ if (wiimotes[i]->ir.dot[a].visible) { (*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->ir.dot[a].x, wiimotes[i]->ir.dot[a].y, wiimotes[i]->ir.dot[a].rx, wiimotes[i]->ir.dot[a].ry, wiimotes[i]->ir.dot[a].size); } } } /* Motion Sensing */ if (WIIUSE_USING_ACC(wiimotes[i])) { /* set orientation and gravity force */ mid = (*env)->GetMethodID(env, cls, "addMotionSensingValues", "(FIZFFFFFFFSSS)V"); if (mid == 0) { return; } (*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->orient_threshold, wiimotes[i]->accel_threshold, WIIMOTE_IS_FLAG_SET(wiimotes[i],WIIUSE_SMOOTHING), wiimotes[i]->accel_calib.st_alpha, wiimotes[i]->orient.roll, wiimotes[i]->orient.pitch, wiimotes[i]->orient.yaw, wiimotes[i]->gforce.x, wiimotes[i]->gforce.y, wiimotes[i]->gforce.z, wiimotes[i]->accel.x, wiimotes[i]->accel.y, wiimotes[i]->accel.z); } /* add generic event to java object used to gather events in c environment */ mid = (*env)->GetMethodID(env, cls, "addWiimoteEvent", "()V"); if (mid == 0) { return; } (*env)->CallVoidMethod(env, gath, mid); break; case WIIUSE_STATUS: /* a status event occured */ mid = (*env)->GetMethodID(env, cls, "addStatusEvent", "(IZFSZIZZZZ)V"); if (mid == 0) { return; } /* LEDS */ if (WIIUSE_IS_LED_SET(wiimotes[i], 1)) leds += 1; if (WIIUSE_IS_LED_SET(wiimotes[i], 2)) leds += 2; if (WIIUSE_IS_LED_SET(wiimotes[i], 3)) leds += 4; if (WIIUSE_IS_LED_SET(wiimotes[i], 4)) leds += 8; (*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid, WIIMOTE_IS_SET(wiimotes[i], WIIMOTE_STATE_CONNECTED), wiimotes[i]->battery_level, leds, WIIUSE_USING_SPEAKER(wiimotes[i]), wiimotes[i]->exp.type,WIIMOTE_IS_SET(wiimotes[i], WIIMOTE_STATE_RUMBLE), WIIMOTE_IS_FLAG_SET(wiimotes[i],WIIUSE_CONTINUOUS), WIIUSE_USING_IR(wiimotes[i]),WIIUSE_USING_ACC(wiimotes[i])); break; case WIIUSE_DISCONNECT: /* the wiimote disconnected */ mid = (*env)->GetMethodID(env, cls, "addDisconnectionEvent", "(I)V"); if (mid == 0) { return; } (*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid); break; default: break; } } } }