From 0e3baaa6ab178c7cf30fb75e0a01cd9629102381 Mon Sep 17 00:00:00 2001 From: death2droid Date: Sun, 20 Sep 2009 13:31:32 +0000 Subject: [PATCH] Wiiuse:Backport some changes from the libogc version of wiiuse. Still got a lot to back port :(. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4307 8ced0084-cf51-0410-be5f-012b33b47a6e --- Externals/WiiUseSrc/Src/dynamics.c | 48 ++++++++ Externals/WiiUseSrc/Src/dynamics.h | 1 + Externals/WiiUseSrc/Src/events.c | 3 +- Externals/WiiUseSrc/Src/guitar_hero_3.c | 37 +++++++ Externals/WiiUseSrc/Src/guitar_hero_3.h | 20 ++-- Externals/WiiUseSrc/Src/ir.c | 6 +- Externals/WiiUseSrc/Src/nunchuk.c | 20 ++++ Externals/WiiUseSrc/Src/wiiboard.c | 127 +++++----------------- Externals/WiiUseSrc/Src/wiiuse.h | 15 +++ Externals/WiiUseSrc/Src/wiiuse_internal.h | 3 +- 10 files changed, 167 insertions(+), 113 deletions(-) diff --git a/Externals/WiiUseSrc/Src/dynamics.c b/Externals/WiiUseSrc/Src/dynamics.c index b6baa72828..4b7bcc29b6 100644 --- a/Externals/WiiUseSrc/Src/dynamics.c +++ b/Externals/WiiUseSrc/Src/dynamics.c @@ -226,3 +226,51 @@ void apply_smoothing(struct accel_t* ac, struct orient_t* orient, int type) { } } } + + +void calc_balanceboard_state(struct wii_board_t *wb) +{ + /* + Interpolate values + Calculations borrowed from wiili.org - No names to mention sadly :( http://www.wiili.org/index.php/Wii_Balance_Board_PC_Drivers + */ + + if(wb->rtrctr[1]) + { + wb->tr = 17.0f*(float)(wb->rtr-wb->ctr[0])/(float)(wb->ctr[1]-wb->ctr[0]); + } + else + { + wb->tr = 17.0f*(float)(wb->rtr-wb->ctr[1])/(float)(wb->ctr[2]-wb->ctr[1]) + 17.0f; + } + + if(wb->rtlctl[1]) + { + wb->tl = 17.0f*(float)(wb->rtl-wb->ctl[0])/(float)(wb->ctl[1]-wb->ctl[0]); + } + else + { + wb->tl = 17.0f*(float)(wb->rtl-wb->ctl[1])/(float)(wb->ctl[2]-wb->ctl[1]) + 17.0f; + } + + if(wb->rbrcbr[1]) + { + wb->br = 17.0f*(float)(wb->rbr-wb->cbr[0])/(float)(wb->cbr[1]-wb->cbr[0]); + } + else + { + wb->br = 17.0f*(float)(wb->rbr-wb->cbr[1])/(float)(wb->cbr[2]-wb->cbr[1]) + 17.0f; + } + + if(wb->rblcbl[1]) + { + wb->bl = 17.0f*(float)(wb->rbl-wb->cbl[0])/(float)(wb->cbl[1]-wb->cbl[0]); + } + else + { + wb->bl = 17.0f*(float)(wb->rbl-wb->cbl[1])/(float)(wb->cbl[2]-wb->cbl[1]) + 17.0f; + } + + wb->x = (wb->tr+wb->br) - (wb->tl+wb->bl)/2.0f; + wb->y = (wb->bl+wb->br) - (wb->tl+wb->tr)/2.0f; +} \ No newline at end of file diff --git a/Externals/WiiUseSrc/Src/dynamics.h b/Externals/WiiUseSrc/Src/dynamics.h index 2a8f96500b..66ea8a4a5d 100644 --- a/Externals/WiiUseSrc/Src/dynamics.h +++ b/Externals/WiiUseSrc/Src/dynamics.h @@ -48,6 +48,7 @@ void calculate_orientation(struct accel_t* ac, struct vec3b_t* accel, struct ori void calculate_gforce(struct accel_t* ac, struct vec3b_t* accel, struct gforce_t* gforce); void calc_joystick_state(struct joystick_t* js, float x, float y); void apply_smoothing(struct accel_t* ac, struct orient_t* orient, int type); +void calc_balanceboard_state(struct wii_board_t *wb); #ifdef __cplusplus } diff --git a/Externals/WiiUseSrc/Src/events.c b/Externals/WiiUseSrc/Src/events.c index e1fc099625..a86f0fd3cd 100644 --- a/Externals/WiiUseSrc/Src/events.c +++ b/Externals/WiiUseSrc/Src/events.c @@ -644,7 +644,7 @@ void disable_expansion(struct wiimote_t* wm) { guitar_hero_3_disconnected(&wm->exp.gh3); wm->event = WIIUSE_GUITAR_HERO_3_CTRL_REMOVED; break; - case EXP_WII_BOARD://Fix me + case EXP_WII_BOARD: wii_board_disconnected(&wm->exp.wb); wm->event = WIIUSE_WII_BOARD_CTRL_REMOVED; break; @@ -654,6 +654,7 @@ void disable_expansion(struct wiimote_t* wm) { WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP); wm->exp.type = EXP_NONE; + } diff --git a/Externals/WiiUseSrc/Src/guitar_hero_3.c b/Externals/WiiUseSrc/Src/guitar_hero_3.c index 8b2f6b0ce5..51ff40a365 100644 --- a/Externals/WiiUseSrc/Src/guitar_hero_3.c +++ b/Externals/WiiUseSrc/Src/guitar_hero_3.c @@ -69,7 +69,10 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b gh3->btns = 0; gh3->btns_held = 0; gh3->btns_released = 0; + gh3->wb_raw = 0; gh3->whammy_bar = 0.0f; + gh3->tb_raw = 0; + gh3->touch_bar = -1; /* decrypt data */ for (i = 0; i < len; ++i) @@ -107,6 +110,7 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b gh3->js.center.y = GUITAR_HERO_3_JS_CENTER_Y; /* handshake done */ + wm->event = WIIUSE_GUITAR_HERO_3_CTRL_INSERTED; wm->exp.type = EXP_GUITAR_HERO_3; #ifdef WIN32 @@ -143,11 +147,40 @@ void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg) { guitar_hero_3_pressed_buttons(gh3, BIG_ENDIAN_SHORT(*(short*)(msg + 4))); + gh3->js.pos.x = (msg[0] & GUITAR_HERO_3_JS_MASK); + gh3->js.pos.y = (msg[1] & GUITAR_HERO_3_JS_MASK); + gh3->tb_raw = (msg[2] & GUITAR_HERO_3_TOUCH_MASK); + gh3->wb_raw = (msg[3] & GUITAR_HERO_3_WHAMMY_MASK); + + /* touch bar */ + gh3->touch_bar = 0; + if (gh3->tb_raw > 0x1B) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_ORANGE; + else if (gh3->tb_raw > 0x18) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_ORANGE | GUITAR_HERO_3_TOUCH_BLUE; + else if (gh3->tb_raw > 0x15) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_BLUE; + else if (gh3->tb_raw > 0x13) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_BLUE | GUITAR_HERO_3_TOUCH_YELLOW; + else if (gh3->tb_raw > 0x10) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_YELLOW; + else if (gh3->tb_raw > 0x0D) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_AVAILABLE; + else if (gh3->tb_raw > 0x0B) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_YELLOW | GUITAR_HERO_3_TOUCH_RED; + else if (gh3->tb_raw > 0x08) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_RED; + else if (gh3->tb_raw > 0x05) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_RED | GUITAR_HERO_3_TOUCH_GREEN; + else if (gh3->tb_raw > 0x02) + gh3->touch_bar = GUITAR_HERO_3_TOUCH_GREEN; + /* whammy bar */ gh3->whammy_bar = (msg[3] - GUITAR_HERO_3_WHAMMY_BAR_MIN) / (float)(GUITAR_HERO_3_WHAMMY_BAR_MAX - GUITAR_HERO_3_WHAMMY_BAR_MIN); /* joy stick */ calc_joystick_state(&gh3->js, msg[0], msg[1]); + } @@ -158,9 +191,13 @@ void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg) { * @param msg The message byte specified in the event packet. */ static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now) { + /* message is inverted (0 is active, 1 is inactive) */ now = ~now & GUITAR_HERO_3_BUTTON_ALL; + /* preserve old btns pressed */ + gh3->btns_last = gh3->btns; + /* pressed now & were pressed, then held */ gh3->btns_held = (now & gh3->btns); diff --git a/Externals/WiiUseSrc/Src/guitar_hero_3.h b/Externals/WiiUseSrc/Src/guitar_hero_3.h index 024d603177..bb58c60d8c 100644 --- a/Externals/WiiUseSrc/Src/guitar_hero_3.h +++ b/Externals/WiiUseSrc/Src/guitar_hero_3.h @@ -36,14 +36,18 @@ #include "wiiuse_internal.h" -#define GUITAR_HERO_3_JS_MIN_X 0xC5 -#define GUITAR_HERO_3_JS_MAX_X 0xFC -#define GUITAR_HERO_3_JS_CENTER_X 0xE0 -#define GUITAR_HERO_3_JS_MIN_Y 0xC5 -#define GUITAR_HERO_3_JS_MAX_Y 0xFA -#define GUITAR_HERO_3_JS_CENTER_Y 0xE0 -#define GUITAR_HERO_3_WHAMMY_BAR_MIN 0xEF -#define GUITAR_HERO_3_WHAMMY_BAR_MAX 0xFA +#define GUITAR_HERO_3_JS_MASK 0x3F +#define GUITAR_HERO_3_TOUCH_MASK 0x1F +#define GUITAR_HERO_3_WHAMMY_MASK 0x1F + +#define GUITAR_HERO_3_JS_MIN_X 0x05 +#define GUITAR_HERO_3_JS_MAX_X 0x3C +#define GUITAR_HERO_3_JS_CENTER_X 0x20 +#define GUITAR_HERO_3_JS_MIN_Y 0x05 +#define GUITAR_HERO_3_JS_MAX_Y 0x3A +#define GUITAR_HERO_3_JS_CENTER_Y 0x20 +#define GUITAR_HERO_3_WHAMMY_BAR_MIN 0x0F +#define GUITAR_HERO_3_WHAMMY_BAR_MAX 0x1A #ifdef __cplusplus extern "C" { diff --git a/Externals/WiiUseSrc/Src/ir.c b/Externals/WiiUseSrc/Src/ir.c index 7a9bb68289..32bbe0e166 100644 --- a/Externals/WiiUseSrc/Src/ir.c +++ b/Externals/WiiUseSrc/Src/ir.c @@ -49,7 +49,7 @@ static void get_ir_dot_avg(struct ir_dot_t* dot, int* x, int* y); static void reorder_ir_dots(struct ir_dot_t* dot); static float ir_distance(struct ir_dot_t* dot); static int ir_correct_for_bounds(int* x, int* y, enum aspect_t aspect, int offset_x, int offset_y); -static void ir_convert_to_vres(int* x, int* y, enum aspect_t aspect, int vx, int vy); +static void ir_convert_to_vres(int* x, int* y, enum aspect_t aspect, unsigned int vx, unsigned int vy); /** @@ -626,7 +626,7 @@ static void reorder_ir_dots(struct ir_dot_t* dot) { i = 0; for (; !dot[i].visible || dot[i].order; ++i) - if (i > 4) + if (i == 4) return; for (j = 0; j < 4; ++j) { @@ -714,7 +714,7 @@ static int ir_correct_for_bounds(int* x, int* y, enum aspect_t aspect, int offse /** * @brief Interpolate the point to the user defined virtual screen resolution. */ -static void ir_convert_to_vres(int* x, int* y, enum aspect_t aspect, int vx, int vy) { +static void ir_convert_to_vres(int* x, int* y, enum aspect_t aspect, unsigned int vx, unsigned int vy) { int xs, ys; if (aspect == WIIUSE_ASPECT_16_9) { diff --git a/Externals/WiiUseSrc/Src/nunchuk.c b/Externals/WiiUseSrc/Src/nunchuk.c index 40a7b7759a..f1d371a5ad 100644 --- a/Externals/WiiUseSrc/Src/nunchuk.c +++ b/Externals/WiiUseSrc/Src/nunchuk.c @@ -111,6 +111,16 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un /* handshake done */ wm->exp.type = EXP_NUNCHUK; + /* if min and max are reported as 0, initialize them to usable values based on center, and fine tune in nunchuck_event() */ + if (nc->js.center.x) { + if (nc->js.min.x == 0) nc->js.min.y = nc->js.center.y - 80; + if (nc->js.max.x == 0) nc->js.max.x = nc->js.center.y + 80; + } + if (nc->js.center.y) { + if (nc->js.min.y == 0) nc->js.min.x = nc->js.center.x - 80; + if (nc->js.max.y == 0) nc->js.max.x = nc->js.center.x + 80; + } + #ifdef WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; #endif @@ -149,6 +159,16 @@ void nunchuk_event(struct nunchuk_t* nc, byte* msg) { /* calculate joystick state */ calc_joystick_state(&nc->js, msg[0], msg[1]); + /* if min and max are reported as 0, initialize them to usable values based on center, and fine tune in nunchuck_event() */ + if (nc->js.center.x) { + if (nc->js.min.x == 0) nc->js.min.y = nc->js.center.y - 80; + if (nc->js.max.x == 0) nc->js.max.x = nc->js.center.y + 80; + } + if (nc->js.center.y) { + if (nc->js.min.y == 0) nc->js.min.x = nc->js.center.x - 80; + if (nc->js.max.y == 0) nc->js.max.x = nc->js.center.x + 80; + } + /* calculate orientation */ nc->accel.x = msg[2]; nc->accel.y = msg[3]; diff --git a/Externals/WiiUseSrc/Src/wiiboard.c b/Externals/WiiUseSrc/Src/wiiboard.c index 38a540b1a8..4f462a88a6 100644 --- a/Externals/WiiUseSrc/Src/wiiboard.c +++ b/Externals/WiiUseSrc/Src/wiiboard.c @@ -28,7 +28,7 @@ /** * @file - * @brief Wii Fit Balance Board device. + * @brief Wiiboard expansion device. */ #include @@ -45,16 +45,6 @@ #include "events.h" #include "wiiboard.h" -static short big_to_lil(unsigned short num) -{ - short ret = num; - char *bret = (char*)&ret; - char tmp = bret[1]; - bret[1] = bret[0]; - bret[0] = tmp; - return ret; -} - /** * @brief Handle the handshake data from the guitar. * @@ -66,51 +56,37 @@ static short big_to_lil(unsigned short num) */ int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data, unsigned short len) { - int i; - short* handshake_short; - /* decrypt data */ - printf("DECRYPTED DATA WIIBOARD\n"); - for (i = 0; i < len; i++) - { - if(i%16==0) - { - if(i!=0) - printf("\n"); - printf("%X: ",0x4a40000+32+i); + int offset = 0; + if (data[offset]==0xff) { + if (data[offset+16]==0xff) { + WIIUSE_DEBUG("Wii Balance Board handshake appears invalid, trying again."); + wiiuse_read_data(wm, data, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); + return 0; } - printf("%02X ", data[i]); + offset += 16; } - printf("\n"); + wb->ctr[0] = (data[offset+4]<<8)|data[offset+5]; + wb->cbr[0] = (data[offset+6]<<8)|data[offset+7]; + wb->ctl[0] = (data[offset+8]<<8)|data[offset+9]; + wb->cbl[0] = (data[offset+10]<<8)|data[offset+11]; - handshake_short = (short*)data; - - wb->ctr[0] = big_to_lil(handshake_short[2]); - wb->cbr[0] = big_to_lil(handshake_short[3]); - wb->ctl[0] = big_to_lil(handshake_short[4]); - wb->cbl[0] = big_to_lil(handshake_short[5]); - - wb->ctr[1] = big_to_lil(handshake_short[6]); - wb->cbr[1] = big_to_lil(handshake_short[7]); - wb->ctl[1] = big_to_lil(handshake_short[8]); - wb->cbl[1] = big_to_lil(handshake_short[9]); - - wb->ctr[2] = big_to_lil(handshake_short[10]); - wb->cbr[2] = big_to_lil(handshake_short[11]); - wb->ctl[2] = big_to_lil(handshake_short[12]); - wb->cbl[2] = big_to_lil(handshake_short[13]); + wb->ctr[1] = (data[offset+12]<<8)|data[offset+13]; + wb->cbr[1] = (data[offset+14]<<8)|data[offset+15]; + wb->ctl[1] = (data[offset+16]<<8)|data[offset+17]; + wb->cbl[1] = (data[offset+18]<<8)|data[offset+19]; + wb->ctr[2] = (data[offset+20]<<8)|data[offset+21]; + wb->cbr[2] = (data[offset+22]<<8)|data[offset+23]; + wb->ctl[2] = (data[offset+24]<<8)|data[offset+25]; + wb->cbl[2] = (data[offset+26]<<8)|data[offset+27]; /* handshake done */ + wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED; wm->exp.type = EXP_WII_BOARD; - - #ifdef WIN32 - wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; - #endif - - return 1; + return 1; } @@ -130,58 +106,9 @@ void wii_board_disconnected(struct wii_board_t* wb) { * @param msg The message specified in the event packet. */ void wii_board_event(struct wii_board_t* wb, byte* msg) { - short *shmsg = (short*)(msg); - wb->rtr = big_to_lil(shmsg[0]); - if(wb->rtr<0) wb->rtr = 0; - wb->rbr = big_to_lil(shmsg[1]); - if(wb->rbr<0) wb->rbr = 0; - wb->rtl = big_to_lil(shmsg[2]); - if(wb->rtl<0) wb->rtl = 0; - wb->rbl = big_to_lil(shmsg[3]); - if(wb->rbl<0) wb->rbl = 0; - - /* - Interpolate values - Calculations borrowed from wiili.org - No names to mention sadly :( http://www.wiili.org/index.php/Wii_Balance_Board_PC_Drivers page however! - */ - - if(wb->rtrctr[1]) - { - wb->tr = 68*(wb->rtr-wb->ctr[0])/(wb->ctr[1]-wb->ctr[0]); - } - else if(wb->rtr >= wb->ctr[1]) - { - wb->tr = 68*(wb->rtr-wb->ctr[1])/(wb->ctr[2]-wb->ctr[1]) + 68; - } - - if(wb->rtlctl[1]) - { - wb->tl = 68*(wb->rtl-wb->ctl[0])/(wb->ctl[1]-wb->ctl[0]); - } - else if(wb->rtl >= wb->ctl[1]) - { - wb->tl = 68*(wb->rtl-wb->ctl[1])/(wb->ctl[2]-wb->ctl[1]) + 68; - } - - if(wb->rbrcbr[1]) - { - wb->br = 68*(wb->rbr-wb->cbr[0])/(wb->cbr[1]-wb->cbr[0]); - } - else if(wb->rbr >= wb->cbr[1]) - { - wb->br = 68*(wb->rbr-wb->cbr[1])/(wb->cbr[2]-wb->cbr[1]) + 68; - } - - if(wb->rblcbl[1]) - { - wb->bl = 68*(wb->rbl-wb->cbl[0])/(wb->cbl[1]-wb->cbl[0]); - } - else if(wb->rbl >= wb->cbl[1]) - { - wb->bl = 68*(wb->rbl-wb->cbl[1])/(wb->cbl[2]-wb->cbl[1]) + 68; - } -} - -void wiiuse_set_wii_board_calib(struct wiimote_t *wm) -{ + wb->rtr = (msg[0]<<8)|msg[1]; + wb->rbr = (msg[2]<<8)|msg[3]; + wb->rtl = (msg[4]<<8)|msg[5]; + wb->rbl = (msg[6]<<8)|msg[7]; + calc_balanceboard_state(wb); } diff --git a/Externals/WiiUseSrc/Src/wiiuse.h b/Externals/WiiUseSrc/Src/wiiuse.h index 04b04fe958..c064479685 100644 --- a/Externals/WiiUseSrc/Src/wiiuse.h +++ b/Externals/WiiUseSrc/Src/wiiuse.h @@ -116,6 +116,13 @@ #define GUITAR_HERO_3_BUTTON_STRUM_DOWN 0x4000 #define GUITAR_HERO_3_BUTTON_ALL 0xFEFF +/* guitar hero world tour touch bar codes */ +#define GUITAR_HERO_3_TOUCH_AVAILABLE 0x1000 +#define GUITAR_HERO_3_TOUCH_GREEN 0x1001 +#define GUITAR_HERO_3_TOUCH_RED 0x1002 +#define GUITAR_HERO_3_TOUCH_YELLOW 0x1004 +#define GUITAR_HERO_3_TOUCH_BLUE 0x1008 +#define GUITAR_HERO_3_TOUCH_ORANGE 0x1010 /* wiimote option flags */ #define WIIUSE_SMOOTHING 0x01 @@ -393,6 +400,7 @@ typedef struct joystick_t { struct vec2b_t max; /**< maximum joystick values */ struct vec2b_t min; /**< minimum joystick values */ struct vec2b_t center; /**< center joystick values */ + struct vec2b_t pos; /**< raw position values */ float ang; /**< angle the joystick is being held */ float mag; /**< magnitude of the joystick (range 0-1) */ @@ -445,11 +453,16 @@ typedef struct classic_ctrl_t { */ typedef struct guitar_hero_3_t { short btns; /**< what buttons have just been pressed */ + short btns_last; /**< what buttons have just been pressed */ short btns_held; /**< what buttons are being held down */ short btns_released; /**< what buttons were just released this */ + byte wb_raw; float whammy_bar; /**< whammy bar (range 0-1) */ + byte tb_raw; + int touch_bar; /**< touch bar */ + struct joystick_t js; /**< joystick calibration */ } guitar_hero_3_t; @@ -473,6 +486,8 @@ typedef struct wii_board_t { short ctr[3]; short cbl[3]; short cbr[3]; /* /Calibration */ + float x; + float y; } wii_board_t; /** diff --git a/Externals/WiiUseSrc/Src/wiiuse_internal.h b/Externals/WiiUseSrc/Src/wiiuse_internal.h index 3277f6de63..3f4c4ff9be 100644 --- a/Externals/WiiUseSrc/Src/wiiuse_internal.h +++ b/Externals/WiiUseSrc/Src/wiiuse_internal.h @@ -129,6 +129,7 @@ #define WM_IR_TYPE_BASIC 0x01 #define WM_IR_TYPE_EXTENDED 0x03 +#define WM_IR_TYPE_FULL 0x05 /* controller status flags for the first message byte */ /* bit 1 is unknown */ @@ -153,7 +154,7 @@ /* encrypted expansion id codes (located at 0x04A400FC) */ #define EXP_ID_CODE_NUNCHUK 0x9A1EFEFE -#define EXP_ID_CODE_WII_BOARD 0xa4200402 +#define EXP_ID_CODE_WII_BOARD 0xa4200402 //Find the encrypted epansion id code #define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD #define EXP_ID_CODE_GUITAR 0x9A1EFDFB