Exemple #1
0
int wiiuse_set_report_type(struct wiimote_t *wm,cmd_blk_cb cb)
{
	ubyte buf[2];
	int motion,ir,exp;

	if(!wm || !WIIMOTE_IS_CONNECTED(wm)) return 0;

	buf[0] = (WIIMOTE_IS_FLAG_SET(wm, WIIUSE_CONTINUOUS) ? 0x04 : 0x00);	/* set to 0x04 for continuous reporting */
	buf[1] = 0x00;

	motion = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_ACC) || WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR);
	exp = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP);
	ir = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR);

	if (motion && ir && exp)	buf[1] = WM_RPT_BTN_ACC_IR_EXP;
	else if (motion && exp)		buf[1] = WM_RPT_BTN_ACC_EXP;
	else if (motion && ir)		buf[1] = WM_RPT_BTN_ACC_IR;
	else if (ir && exp)			buf[1] = WM_RPT_BTN_IR_EXP;
	else if (ir)				buf[1] = WM_RPT_BTN_ACC_IR;
	else if (exp)				buf[1] = WM_RPT_BTN_EXP;
	else if (motion)			buf[1] = WM_RPT_BTN_ACC;
	else						buf[1] = WM_RPT_BTN;

	WIIUSE_DEBUG("Setting report type: 0x%x", buf[1]);

	wiiuse_sendcmd(wm,WM_CMD_REPORT_TYPE,buf,2,cb);
	return buf[1];
}
Exemple #2
0
/**
 *	@brief	Set the report type based on the current wiimote state.
 *
 *	@param wm		Pointer to a wiimote_t structure.
 *
 *	@return The report type sent.
 *
 *	The wiimote reports formatted packets depending on the
 *	report type that was last requested.  This function will
 *	update the type of report that should be sent based on
 *	the current state of the device.
 */
int wiiuse_set_report_type(struct wiimote_t* wm) {

    byte buf[2];
	int motion, exp, ir, balance_board;

	if (!wm || !WIIMOTE_IS_CONNECTED(wm)) {
		return 0;
	}

	buf[0] = (WIIMOTE_IS_FLAG_SET(wm, WIIUSE_CONTINUOUS) ? 0x04 : 0x00);	/* set to 0x04 for continuous reporting */
	buf[1] = 0x00;

	/* if rumble is enabled, make sure we keep it */
	if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) {
		buf[0] |= 0x01;
	}

	motion = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_ACC);
	exp = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP);
	ir = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR);
	balance_board = exp && (wm->exp.type == EXP_WII_BOARD);

	if (motion && ir && exp)	{
		buf[1] = WM_RPT_BTN_ACC_IR_EXP;
	} else if (motion && exp) {
		buf[1] = WM_RPT_BTN_ACC_EXP;
	} else if (motion && ir) {
		buf[1] = WM_RPT_BTN_ACC_IR;
	} else if (ir && exp) {
		buf[1] = WM_RPT_BTN_IR_EXP;
	} else if (ir) {
		buf[1] = WM_RPT_BTN_ACC_IR;
	} else if(exp && balance_board) {
        if(wm->exp.wb.use_alternate_report)
	        buf[1] = WM_RPT_BTN_EXP_8;
        else
            buf[1] = WM_RPT_BTN_EXP;
	} else if (exp) {
		buf[1] = WM_RPT_BTN_EXP;
	} else if (motion) {
		buf[1] = WM_RPT_BTN_ACC;
	} else {
		buf[1] = WM_RPT_BTN;
	}

	WIIUSE_DEBUG("Setting report type: 0x%x", buf[1]);

	exp = wiiuse_send(wm, WM_CMD_REPORT_TYPE, buf, 2);
	if (exp <= 0) {
		return exp;
	}

	return buf[1];
}
/**
 * Fills status variables. This method fills some status variables always filled in a WiiMoteEvent object.
 * This function is called in every callback function.
 */
static void copy_common_status(struct wiimote_t* wm) {

	/* Variables Declarations */
	jmethodID mid;
	jclass cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim);

	/* set statuses */
	mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setPermanentStatus",
			"(IZZZZFZZ)V");
	if (mid == 0) {
		return;
	}
	(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid,
							wm->unid, WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED),
							WIIUSE_USING_IR(wm), WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE),
							WIIUSE_USING_ACC(wm), wm->orient_threshold,
							WIIMOTE_IS_FLAG_SET(wm,WIIUSE_CONTINUOUS),
							WIIMOTE_IS_FLAG_SET(wm,WIIUSE_SMOOTHING));

}
/**
 * Get status and values from the wiimotes and send it through callbacks.
 * @param wim the wiimote object to fill with the datas.
 */
JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_specialPoll
(JNIEnv *env, jobject obj, jobject gath) {

	/* Variables Declarations */
	int i;
	short leds = 0;
	jclass cls = (*env)->GetObjectClass(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;
					WIIUSE_GET_IR_SENSITIVITY_CORRECTED(wiimotes[i], &a);

					mid = (*env)->GetMethodID(env, cls, "prepareIRevent",
							"(IIFIIIIIISSSF)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,
							a , wiimotes[i]->ir.distance);

					mid = (*env)->GetMethodID(env, cls, "addIRPointToPreparedWiiMoteEvent",
							"(IISSS)V");
					if (mid == 0) {
						return;
					}
					/* go through each of the 4 possible IR sources */
					for (a=0; 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", "(FIZFFFFFFFFFSSS)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]->orient.a_roll, wiimotes[i]->orient.a_pitch,
							wiimotes[i]->gforce.x, wiimotes[i]->gforce.y, wiimotes[i]->gforce.z,
							wiimotes[i]->accel.x, wiimotes[i]->accel.y, wiimotes[i]->accel.z);
				}

				/* Expansions support support*/
				if (WIIUSE_USING_EXP(wiimotes[i])) {
					/* Nunchuk support */
					if (wiimotes[i]->exp.type == EXP_NUNCHUK) {
						/* put nunchuk values in wiimote generic event */
						mid = (*env)->GetMethodID(env, cls,
								"addNunchunkEventToPreparedWiimoteEvent", "(SSSFIZFFFFFFFFFSSSFFSSSSSS)V");
						if (mid == 0) {
							return;
						}
						struct nunchuk_t* nc = (nunchuk_t*)&wiimotes[i]->exp.nunchuk;

						(*env)->CallVoidMethod(env, gath, mid,
								/* buttons */
								nc->btns,nc->btns_released,nc->btns_held,
								/* motion sensing */
								nc->orient_threshold,nc->accel_threshold,
								WIIMOTE_IS_FLAG_SET(wiimotes[i],WIIUSE_SMOOTHING),nc->accel_calib.st_alpha,
								nc->orient.roll, nc->orient.pitch, nc->orient.yaw,
								nc->orient.a_roll, nc->orient.a_pitch,
								nc->gforce.x, nc->gforce.y, nc->gforce.z,
								nc->accel.x, nc->accel.y, nc->accel.z,
								/* joystick */
								nc->js.ang,nc->js.mag,
								nc->js.max.x,nc->js.max.y,
								nc->js.min.x,nc->js.min.y,
								nc->js.center.x,nc->js.center.y);
					} else if (wiimotes[i]->exp.type == EXP_GUITAR_HERO_3) {
						/* put guitar hero values in wiimote generic event */
						mid = (*env)->GetMethodID(env, cls,
								"addGuitarHeroEventToPreparedWiimoteEvent", "(SSSFFFSSSSSS)V");
						if (mid == 0) {
							return;
						}
						struct guitar_hero_3_t* gh = (guitar_hero_3_t*)&wiimotes[i]->exp.gh3;

						(*env)->CallVoidMethod(env, gath, mid,
								/* buttons */
								gh->btns,gh->btns_released,gh->btns_held,
								/* whammy bar */
								gh->whammy_bar,
								/* joystick */
								gh->js.ang,gh->js.mag,
								gh->js.max.x,gh->js.max.y,
								gh->js.min.x,gh->js.min.y,
								gh->js.center.x,gh->js.center.y);
					}if (wiimotes[i]->exp.type == EXP_CLASSIC) {
						/* put classic controller values in wiimote generic event */
						mid = (*env)->GetMethodID(env, cls,
								"addClassicControllerEventToPreparedWiimoteEvent", "(SSSFFFFSSSSSSFFSSSSSS)V");
						if (mid == 0) {
							return;
						}
						struct classic_ctrl_t* cl = (classic_ctrl_t*)&wiimotes[i]->exp.classic;

						(*env)->CallVoidMethod(env, gath, mid,
								/* buttons */
								cl->btns,cl->btns_released,cl->btns_held,
								/* shoulder buttons */
								cl->r_shoulder,cl->l_shoulder,
								/* joystick left*/
								cl->ljs.ang,cl->ljs.mag,
								cl->ljs.max.x,cl->ljs.max.y,
								cl->ljs.min.x,cl->ljs.min.y,
								cl->ljs.center.x,cl->ljs.center.y,
								/* joystick right */
								cl->rjs.ang,cl->rjs.mag,
								cl->rjs.max.x,cl->rjs.max.y,
								cl->rjs.min.x,cl->rjs.min.y,
								cl->rjs.center.x,cl->rjs.center.y);
					}
				}

				/* 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_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;

				case WIIUSE_UNEXPECTED_DISCONNECT:
				/* the wimote disconnected */
				mid = (*env)->GetMethodID(env, cls, "addDisconnectionEvent", "(I)V");
				if (mid == 0) {
					return;
				}
				(*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid);
				break;

				case WIIUSE_NUNCHUK_INSERTED:
				/* the nunchuk was just connected */
				mid = (*env)->GetMethodID(env, cls, "addNunchukInsertedEvent", "(I)V");
				if (mid == 0) {
					return;
				}
				(*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid);
				break;

				case WIIUSE_NUNCHUK_REMOVED:
				/* the nunchuk disconnected */
				mid = (*env)->GetMethodID(env, cls, "addNunchukRemovedEvent", "(I)V");
				if (mid == 0) {
					return;
				}
				(*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid);
				break;

				case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
				/* the guitar hero was just connected */
				mid = (*env)->GetMethodID(env, cls, "addGuitarHeroInsertedEvent", "(I)V");
				if (mid == 0) {
					return;
				}
				(*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid);
				break;

				case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
				/* the guitar hero disconnected */
				mid = (*env)->GetMethodID(env, cls, "addGuitarHeroRemovedEvent", "(I)V");
				if (mid == 0) {
					return;
				}
				(*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid);
				break;

				case WIIUSE_CLASSIC_CTRL_INSERTED:
				/* the classic controller was just connected */
				mid = (*env)->GetMethodID(env, cls, "addClassicControllerInsertedEvent", "(I)V");
				if (mid == 0) {
					return;
				}
				(*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid);
				break;

				case WIIUSE_CLASSIC_CTRL_REMOVED:
				/* the classic controller disconnected */
				mid = (*env)->GetMethodID(env, cls, "addClassicControllerRemovedEvent", "(I)V");
				if (mid == 0) {
					return;
				}
				(*env)->CallVoidMethod(env, gath, mid, wiimotes[i]->unid);
				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;

				default:
				break;
			}
		}
	}
}
/**
 * Get status and values from the wiimotes and send it through callbacks.
 * @param wim the wiimote object to fill with the datas.
 */
JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_specialPoll
(JNIEnv *env, jobject obj, jobject gath) {

	/* Variables Declarations */
	int i;
	short leds = 0;
	jclass cls = (*env)->GetObjectClass(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;
			}
		}
	}
}