From 1d29060e08ae3ac36575a35a92c291a79a736be0 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 18 Jan 2013 12:16:06 +0100 Subject: [PATCH] (Wiiuse) Merge patches from rpavlik/wiiuse (GX) Remove Wii Classic analog stick menu moving -too twitchy to bother with --- frontend/menu/rmenu_gx.c | 8 +++---- gx/gx_input.c | 46 +++++++++++----------------------------- wii/wiiuse/classic.c | 35 ++++++++++++++++-------------- wii/wiiuse/dynamics.c | 38 ++++++++++++++++----------------- 4 files changed, 54 insertions(+), 73 deletions(-) diff --git a/frontend/menu/rmenu_gx.c b/frontend/menu/rmenu_gx.c index b2fa934d93..b06d33f360 100644 --- a/frontend/menu/rmenu_gx.c +++ b/frontend/menu/rmenu_gx.c @@ -23,10 +23,10 @@ uint16_t menu_framebuf[400 * 240]; static const struct retro_keybind _rmenu_nav_binds[] = { #ifdef HW_RVL - { 0, 0, NULL, 0, GX_GC_UP | GX_GC_LSTICK_UP | GX_GC_RSTICK_UP | GX_CLASSIC_UP | GX_CLASSIC_LSTICK_UP | GX_CLASSIC_RSTICK_UP | GX_WIIMOTE_UP | GX_NUNCHUK_UP, 0 }, - { 0, 0, NULL, 0, GX_GC_DOWN | GX_GC_LSTICK_DOWN | GX_GC_RSTICK_DOWN | GX_CLASSIC_DOWN | GX_CLASSIC_LSTICK_DOWN | GX_CLASSIC_RSTICK_DOWN | GX_WIIMOTE_DOWN | GX_NUNCHUK_DOWN, 0 }, - { 0, 0, NULL, 0, GX_GC_LEFT | GX_GC_LSTICK_LEFT | GX_GC_RSTICK_LEFT | GX_CLASSIC_LEFT | GX_CLASSIC_LSTICK_LEFT | GX_CLASSIC_RSTICK_LEFT | GX_WIIMOTE_LEFT | GX_NUNCHUK_LEFT, 0 }, - { 0, 0, NULL, 0, GX_GC_RIGHT | GX_GC_LSTICK_RIGHT | GX_GC_RSTICK_RIGHT | GX_CLASSIC_RIGHT | GX_CLASSIC_LSTICK_RIGHT | GX_CLASSIC_RSTICK_RIGHT | GX_WIIMOTE_RIGHT | GX_NUNCHUK_RIGHT, 0 }, + { 0, 0, NULL, 0, GX_GC_UP | GX_GC_LSTICK_UP | GX_GC_RSTICK_UP | GX_CLASSIC_UP | GX_WIIMOTE_UP | GX_NUNCHUK_UP, 0 }, + { 0, 0, NULL, 0, GX_GC_DOWN | GX_GC_LSTICK_DOWN | GX_GC_RSTICK_DOWN | GX_CLASSIC_DOWN | GX_WIIMOTE_DOWN | GX_NUNCHUK_DOWN, 0 }, + { 0, 0, NULL, 0, GX_GC_LEFT | GX_GC_LSTICK_LEFT | GX_GC_RSTICK_LEFT | GX_CLASSIC_LEFT | GX_WIIMOTE_LEFT | GX_NUNCHUK_LEFT, 0 }, + { 0, 0, NULL, 0, GX_GC_RIGHT | GX_GC_LSTICK_RIGHT | GX_GC_RSTICK_RIGHT | GX_CLASSIC_RIGHT | GX_WIIMOTE_RIGHT | GX_NUNCHUK_RIGHT, 0 }, { 0, 0, NULL, 0, GX_GC_A | GX_CLASSIC_A | GX_WIIMOTE_A | GX_WIIMOTE_2, 0 }, { 0, 0, NULL, 0, GX_GC_B | GX_CLASSIC_B | GX_WIIMOTE_B | GX_WIIMOTE_1, 0 }, { 0, 0, NULL, 0, GX_GC_START | GX_CLASSIC_PLUS | GX_WIIMOTE_PLUS, 0 }, diff --git a/gx/gx_input.c b/gx/gx_input.c index 82508830ce..befe36e962 100644 --- a/gx/gx_input.c +++ b/gx/gx_input.c @@ -332,41 +332,19 @@ static void gx_input_poll(void *data) && (down & WPAD_CLASSIC_BUTTON_ZR)) *state_cur |= GX_QUIT_KEY; - //TODO: Hack, analog stick twitchiness needs to be properly fixed - if(g_extern.lifecycle_mode_state & (1ULL << MODE_MENU_DRAW)) - { - s8 x = gx_stick_x(exp->classic.ljs); - s8 y = gx_stick_y(exp->classic.ljs); + u8 ls_x = exp->classic.ljs.pos.x; + u8 ls_y = exp->classic.ljs.pos.y; + u8 rs_x = exp->classic.rjs.pos.x; + u8 rs_y = exp->classic.rjs.pos.y; - if (abs(x) > JOYSTICK_THRESHOLD) - *state_cur |= x > 0 ? GX_CLASSIC_LSTICK_RIGHT : GX_CLASSIC_LSTICK_LEFT; - if (abs(y) > JOYSTICK_THRESHOLD) - *state_cur |= y > 0 ? GX_CLASSIC_LSTICK_UP : GX_CLASSIC_LSTICK_DOWN; - - x = gx_stick_x(exp->classic.rjs); - y = gx_stick_y(exp->classic.rjs); - - if (abs(x) > JOYSTICK_THRESHOLD) - *state_cur |= x > 0 ? GX_CLASSIC_RSTICK_RIGHT : GX_CLASSIC_RSTICK_LEFT; - if (abs(y) > JOYSTICK_THRESHOLD) - *state_cur |= y > 0 ? GX_CLASSIC_RSTICK_UP : GX_CLASSIC_RSTICK_DOWN; - } - else - { - u8 ls_x = exp->classic.ljs.pos.x; - u8 ls_y = exp->classic.ljs.pos.y; - u8 rs_x = exp->classic.rjs.pos.x; - u8 rs_y = exp->classic.rjs.pos.y; - - *state_cur |= (ls_x > 40) ? GX_CLASSIC_LSTICK_RIGHT : 0; - *state_cur |= (ls_x < 25) ? GX_CLASSIC_LSTICK_LEFT : 0; - *state_cur |= (ls_y > 45) ? GX_CLASSIC_LSTICK_UP : 0; - *state_cur |= (ls_y < 20) ? GX_CLASSIC_LSTICK_DOWN : 0; - *state_cur |= (rs_x > 40) ? GX_CLASSIC_RSTICK_RIGHT : 0; - *state_cur |= (rs_x < 25) ? GX_CLASSIC_RSTICK_LEFT: 0; - *state_cur |= (rs_y > 45) ? GX_CLASSIC_RSTICK_UP : 0; - *state_cur |= (rs_y < 20) ? GX_CLASSIC_RSTICK_DOWN : 0; - } + *state_cur |= (ls_x > 40) ? GX_CLASSIC_LSTICK_RIGHT : 0; + *state_cur |= (ls_x < 25) ? GX_CLASSIC_LSTICK_LEFT : 0; + *state_cur |= (ls_y > 45) ? GX_CLASSIC_LSTICK_UP : 0; + *state_cur |= (ls_y < 20) ? GX_CLASSIC_LSTICK_DOWN : 0; + *state_cur |= (rs_x > 40) ? GX_CLASSIC_RSTICK_RIGHT : 0; + *state_cur |= (rs_x < 25) ? GX_CLASSIC_RSTICK_LEFT: 0; + *state_cur |= (rs_y > 45) ? GX_CLASSIC_RSTICK_UP : 0; + *state_cur |= (rs_y < 20) ? GX_CLASSIC_RSTICK_DOWN : 0; } else if (type == WPAD_EXP_NUNCHUK) { diff --git a/wii/wiiuse/classic.c b/wii/wiiuse/classic.c index 095860a003..629c1277de 100644 --- a/wii/wiiuse/classic.c +++ b/wii/wiiuse/classic.c @@ -61,9 +61,12 @@ static void classic_ctrl_pressed_buttons(struct classic_ctrl_t* cc, short now); * * @return Returns 1 if handshake was successful, 0 if not. */ + +#define HANDSHAKE_BYTES_USED 12 + int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, ubyte* data, uword len) { //int i; - int offset = 0; + //int offset = 0; cc->btns = 0; cc->btns_held = 0; @@ -74,7 +77,7 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, ubyt for (i = 0; i < len; ++i) data[i] = (data[i] ^ 0x17) + 0x17; */ - if (data[offset] == 0xFF) { + if (data[0] == 0xFF || len < HANDSHAKE_BYTES_USED) { /* * Sometimes the data returned here is not correct. * This might happen because the wiimote is lagging @@ -85,29 +88,29 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, ubyt * but since the next 16 bytes are the same, just use * those. */ - if (data[offset + 16] == 0xFF) { + if (len < 17 || len < HANDSHAKE_BYTES_USED + 16 || data[16] == 0xFF) { /* get the calibration data again */ //WIIUSE_DEBUG("Classic controller handshake appears invalid, trying again."); wiiuse_read_data(wm, data, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN, wiiuse_handshake_expansion); } else - offset += 16; + data += 16; } /* joystick stuff */ - cc->ljs.max.x = 64; - cc->ljs.min.x = data[1 + offset] / 4; - cc->ljs.center.x = 32; - cc->ljs.max.y = 64; - cc->ljs.min.y = data[4 + offset] / 4; - cc->ljs.center.y = 32; + cc->ljs.max.x = data[0] / 4; + cc->ljs.min.x = data[1] / 4; + cc->ljs.center.x = data[2] / 4; + cc->ljs.max.y = data[3] / 4; + cc->ljs.min.y = data[4] / 4; + cc->ljs.center.y = data[5] / 4; - cc->rjs.max.x = 32; - cc->rjs.min.x = data[7 + offset] / 8; - cc->rjs.center.x = 16; - cc->rjs.max.y = 32; - cc->rjs.min.y = data[10 + offset] / 8; - cc->rjs.center.y = 16; + cc->rjs.max.x = data[6] / 8; + cc->rjs.min.x = data[7] / 8; + cc->rjs.center.x = data[8] / 8; + cc->rjs.max.y = data[9] / 8; + cc->rjs.min.y = data[10] / 8; + cc->rjs.center.y = data[11] / 8; /* handshake done */ wm->event = WIIUSE_CLASSIC_CTRL_INSERTED; diff --git a/wii/wiiuse/dynamics.c b/wii/wiiuse/dynamics.c index 805e3ba5e6..f8f1b81676 100644 --- a/wii/wiiuse/dynamics.c +++ b/wii/wiiuse/dynamics.c @@ -139,6 +139,20 @@ void calculate_gforce(struct accel_t* ac, struct vec3w_t* accel, struct gforce_t gforce->z = ((float)accel->z - (float)ac->cal_zero.z) / zg; } +static float applyCalibration(float inval,float minval, float maxval,float centerval) +{ + float ret; + /* We don't use the exact ranges but the ranges +1 in case we get bad calibration + * data - avoid div0 */ + + if (inval == centerval) + ret = 0; + else if (inval < centerval) + ret = (inval - centerval) / (centerval - minval + 1); + else + ret = (inval - centerval) / (maxval - centerval + 1); + return ret; +} /** * @brief Calculate the angle and magnitude of a joystick. @@ -166,27 +180,13 @@ void calc_joystick_state(struct joystick_t* js, float x, float y) { * The range is therefore -1 to 1, 0 being the exact center rather than * the middle of min and max. */ - if (x == js->center.x) - rx = 0; - else if (x >= js->center.x) - rx = ((float)(x - js->center.x) / (float)(js->max.x - js->center.x)); - else - rx = ((float)(x - js->min.x) / (float)(js->center.x - js->min.x)) - 1.0f; - - if (y == js->center.y) - ry = 0; - else if (y >= js->center.y) - ry = ((float)(y - js->center.y) / (float)(js->max.y - js->center.y)); - else - ry = ((float)(y - js->min.y) / (float)(js->center.y - js->min.y)) - 1.0f; + rx = applyCalibration(x, js->min.x, js->max.x, js->center.x); + ry = applyCalibration(y, js->min.y, js->max.y, js->center.y); /* calculate the joystick angle and magnitude */ - ang = RAD_TO_DEGREE(atanf(ry / rx)); - ang -= 90.0f; - if (rx < 0.0f) - ang -= 180.0f; - js->ang = absf(ang); - js->mag = (float) sqrt((rx * rx) + (ry * ry)); + ang = RAD_TO_DEGREE(atan2f(ry, rx)); + js->mag = sqrtf((rx * rx) + (ry * ry)); + js->ang = ang + 180.0f; }