Wii: Simplify analog stick reading

This commit is contained in:
Vicki Pfau 2018-08-05 20:59:23 -07:00
parent 8f7a0e6858
commit d181778883
1 changed files with 24 additions and 36 deletions

View File

@ -998,77 +998,65 @@ int32_t _readGyroZ(struct mRotationSource* source) {
} }
static s8 WPAD_StickX(u8 chan, u8 right) { static s8 WPAD_StickX(u8 chan, u8 right) {
float mag = 0.0; struct expansion_t exp;
float ang = 0.0; WPAD_Expansion(chan, &exp);
WPADData *data = WPAD_Data(chan); struct joystick_t* js = NULL;
switch (data->exp.type) { switch (exp.type) {
case WPAD_EXP_NUNCHUK: case WPAD_EXP_NUNCHUK:
case WPAD_EXP_GUITARHERO3: case WPAD_EXP_GUITARHERO3:
if (right == 0) { if (right == 0) {
mag = data->exp.nunchuk.js.mag; js = &exp.nunchuk.js;
ang = data->exp.nunchuk.js.ang;
} }
break; break;
case WPAD_EXP_CLASSIC: case WPAD_EXP_CLASSIC:
if (right == 0) { if (right == 0) {
mag = data->exp.classic.ljs.mag; js = &exp.classic.ljs;
ang = data->exp.classic.ljs.ang;
} else { } else {
mag = data->exp.classic.rjs.mag; js = &exp.classic.rjs;
ang = data->exp.classic.rjs.ang;
} }
break; break;
default: default:
break; break;
} }
/* calculate X value (angle need to be converted into radian) */ if (!js) {
if (mag > 1.0) { return 0;
mag = 1.0;
} else if (mag < -1.0) {
mag = -1.0;
} }
double val = mag * sinf(M_PI * ang / 180.0f); int centered = (int) js->pos.x - (int) js->center.x;
int range = js->max.x - js->min.x;
return (s8)(val * 128.0f); return (centered * 0xFF) / range;
} }
static s8 WPAD_StickY(u8 chan, u8 right) { static s8 WPAD_StickY(u8 chan, u8 right) {
float mag = 0.0; struct expansion_t exp;
float ang = 0.0; WPAD_Expansion(chan, &exp);
WPADData *data = WPAD_Data(chan); struct joystick_t* js = NULL;
switch (data->exp.type) { switch (exp.type) {
case WPAD_EXP_NUNCHUK: case WPAD_EXP_NUNCHUK:
case WPAD_EXP_GUITARHERO3: case WPAD_EXP_GUITARHERO3:
if (right == 0) { if (right == 0) {
mag = data->exp.nunchuk.js.mag; js = &exp.nunchuk.js;
ang = data->exp.nunchuk.js.ang;
} }
break; break;
case WPAD_EXP_CLASSIC: case WPAD_EXP_CLASSIC:
if (right == 0) { if (right == 0) {
mag = data->exp.classic.ljs.mag; js = &exp.classic.ljs;
ang = data->exp.classic.ljs.ang;
} else { } else {
mag = data->exp.classic.rjs.mag; js = &exp.classic.rjs;
ang = data->exp.classic.rjs.ang;
} }
break; break;
default: default:
break; break;
} }
/* calculate X value (angle need to be converted into radian) */ if (!js) {
if (mag > 1.0) { return 0;
mag = 1.0;
} else if (mag < -1.0) {
mag = -1.0;
} }
double val = mag * cosf(M_PI * ang / 180.0f); int centered = (int) js->pos.y - (int) js->center.y;
int range = js->max.y - js->min.y;
return (s8)(val * 128.0f); return (centered * 0xFF) / range;
} }
void _retraceCallback(u32 count) { void _retraceCallback(u32 count) {