simplified callbacks and filling of WiimoteEvent objects.

git-svn-id: http://wiiusej.googlecode.com/svn/trunk@15 ae48ae66-6a45-0410-b38e-211266189506
This commit is contained in:
guilhem.duche
2008-02-06 13:26:23 +00:00
parent 717ac34e0d
commit 2432d8f74d

View File

@@ -15,6 +15,7 @@
#define WIIMOTE_ID_1 1 #define WIIMOTE_ID_1 1
#define WIIMOTE_ID_2 2 #define WIIMOTE_ID_2 2
#define WIIMOTE_STATE_RUMBLE 0x08 #define WIIMOTE_STATE_RUMBLE 0x08
#define WIIMOTE_STATE_CONNECTED 0x04
#define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s)) #define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s))
#define WIIMOTE_IS_FLAG_SET(wm, s) ((wm->flags & (s)) == (s)) #define WIIMOTE_IS_FLAG_SET(wm, s) ((wm->flags & (s)) == (s))
@@ -24,7 +25,8 @@ static void handle_event(struct wiimote_t* wm);
static void handle_ctrl_status(struct wiimote_t* wm, int attachment, static void handle_ctrl_status(struct wiimote_t* wm, int attachment,
int speaker, int ir, int led[4], float battery_level); int speaker, int ir, int led[4], float battery_level);
static void handle_disconnect(wiimote* wm); static void handle_disconnect(wiimote* wm);
static void copy_common_status(struct wiimote_t* wm);
static void copy_common_status(struct wiimote_t* wm);/* function with common code for callbacks */
/********************* VARIABLES DECLARATIONS *****************************/ /********************* VARIABLES DECLARATIONS *****************************/
@@ -322,36 +324,13 @@ static void handle_event(struct wiimote_t* wm) {
/* fill java class */ /* fill java class */
copy_common_status(wm); copy_common_status(wm);
/* Take care of buttons pressed */ /* Set all buttons */
if (wm->btns) { mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setAllButtons", "(SSS)V");
/* if a button is just pressed, report it */ if (mid == 0) {
mid = (*globalEnv)->GetMethodID(globalEnv, cls, return;
"setButtonsJustPressed", "(S)V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wm->btns);
}
if (wm->btns_released) {
/* if a button is just released, report it */
mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"setButtonsJustReleased", "(S)V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid,
wm->btns_released);
}
if (wm->btns_held) {
/* if a button is held, report it */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setButtonsHeld",
"(S)V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid,
wm->btns_held);
} }
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wm->btns,
wm->btns_released, wm->btns_held);
/* /*
* If IR tracking is enabled then print the coordinates * If IR tracking is enabled then print the coordinates
@@ -381,22 +360,15 @@ static void handle_event(struct wiimote_t* wm) {
/* Motion Sensing */ /* Motion Sensing */
if (WIIUSE_USING_ACC(wm)) { if (WIIUSE_USING_ACC(wm)) {
/* set orientation */ /* set orientation and gravity force */
cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim);
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setOrientation", mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"(FFF)V"); "setOrientationAndGforce", "(FFFFFF)V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid,
wm->orient.roll, wm->orient.pitch, wm->orient.yaw);
/* set gravity force*/
cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim);
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setGforce", "(FFF)V");
if (mid == 0) { if (mid == 0) {
return; return;
} }
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid,
wm->orient.roll, wm->orient.pitch, wm->orient.yaw,
wm->gforce.x, wm->gforce.y, wm->gforce.z); wm->gforce.x, wm->gforce.y, wm->gforce.z);
} }
} }
@@ -430,11 +402,6 @@ static void handle_ctrl_status(struct wiimote_t* wm, int attachment,
copy_common_status(wm); copy_common_status(wm);
/* LEDS */ /* LEDS */
cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim);
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setLeds", "(S)V");
if (mid == 0) {
return;
}
if (led[0]) if (led[0])
leds += 1; leds += 1;
if (led[1]) if (led[1])
@@ -443,53 +410,15 @@ static void handle_ctrl_status(struct wiimote_t* wm, int attachment,
leds += 4; leds += 4;
if (led[3]) if (led[3])
leds += 8; leds += 8;
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, leds);
/* Battery */ /* set values for battery, leds, speaker and attachment*/
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setBatteryLevel", "(F)V"); mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"setBatteryLedsSpeakerAttachment", "(FSZZ)V");
if (mid == 0) { if (mid == 0) {
return; return;
} }
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid,
battery_level); battery_level, leds, speaker, attachment);
/* Speaker */
if (speaker) {
/* set Speaker Enabled */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setSpeakerEnabled",
"()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
} else {
/* set Speaker Disabled */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setSpeakerDisabled",
"()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
}
/* Attachment */
if (attachment) {
/* set there is an attachment */
mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"setThereIsAnAttachment", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
} else {
/* set there is no attachment */
mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"setThereIsNoAttachment", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
}
} }
@@ -502,36 +431,16 @@ static void handle_ctrl_status(struct wiimote_t* wm, int attachment,
* if the connection is interrupted. * if the connection is interrupted.
*/ */
static void handle_disconnect(wiimote* wm) { static void handle_disconnect(wiimote* wm) {
//printf("\n\n--- DISCONNECTED [wiimote id %i] ---\n", wm->unid);
/* Variables Declarations */
jclass cls;
jmethodID mid;
/* call java method handling disconnection */ /* call java method handling disconnection */
/* Set wiimote id */ copy_common_status(wm);
cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim);
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setWiimoteId", "(I)V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wm->unid);
/* set the wiimote disconnected */
cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim);
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setDisconnected", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
} }
/** /**
* Fills status variables. * Fills status variables. This method fills some status variables always filled in a WiiMoteEvent object.
* This function is used in handle_event and handle_ctrl_status. * This function is called in every callback function.
*/ */
static void copy_common_status(struct wiimote_t* wm) { static void copy_common_status(struct wiimote_t* wm) {
@@ -539,121 +448,17 @@ static void copy_common_status(struct wiimote_t* wm) {
jmethodID mid; jmethodID mid;
jclass cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); jclass cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim);
/* Set wiimote id */ /* set statuses */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setWiimoteId", "(I)V"); mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setPermanentStatus",
if (mid == 0) { "(IZZZZFZZ)V");
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wm->unid);
/* set the wiimote connected */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setConnected", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
/* IR state */
if (WIIUSE_USING_IR(wm)) {
/* set IRActive */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setIrActive", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
} else {
/* set IRInactive */
mid
= (*globalEnv)->GetMethodID(globalEnv, cls,
"setIrInactive", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
}
/* Rumble status */
if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) {
/* set rumble active */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setRumbleActive",
"()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
} else {
/* set rumble inactive */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setRumbleInactive",
"()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
}
/* Motion sensing status */
if (WIIUSE_USING_ACC(wm)) {
/* Set motion sensing active*/
mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"setMotionSensingActive", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
} else {/* motion sensing not activated */
/* Set motion sensing inactive*/
mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"setMotionSensingInactive", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
}
/* orientation threshold value */
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setOrientationThreshold",
"(F)V");
if (mid == 0) { if (mid == 0) {
return; return;
} }
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid,
wm->orient_threshold); 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));
/* continuous status */
if (WIIMOTE_IS_FLAG_SET(wm,WIIUSE_CONTINUOUS)) {
/* Set WIIUSE_CONTINUOUS active*/
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setContinuousActive",
"()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
} else {
/* Set WIIUSE_CONTINUOUS inactive*/
mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"setContinuousInactive", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
}
/* smoothing status */
if (WIIMOTE_IS_FLAG_SET(wm,WIIUSE_SMOOTHING)) {
/* Set WIIUSE_SMOOTHING active*/
mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setSmoothingActive",
"()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
} else {
/* Set WIIUSE_SMOOTHING inactive*/
mid = (*globalEnv)->GetMethodID(globalEnv, cls,
"setSmoothingInactive", "()V");
if (mid == 0) {
return;
}
(*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid);
}
} }