From 541cbb5156568adbf49628d85f3acf907008cd47 Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Wed, 14 Sep 2011 08:51:23 -0500 Subject: [PATCH 01/16] Merged stuff from fWiine, Wiiuse master and local Motion+ modifs, not working yet Dos2unix and unexpanded, then selectively committed by rpavlik --- src/events.c | 140 ++++++++++++++++++++++++++++++++-- src/io.c | 3 + src/motion_plus.c | 173 ++++++++++++++++++++++++++++++++++++++++-- src/nunchuk.c | 5 +- src/nunchuk.h | 2 + src/wiiuse.h | 33 +++++++- src/wiiuse_internal.h | 7 +- 7 files changed, 338 insertions(+), 25 deletions(-) diff --git a/src/events.c b/src/events.c index 8a895ad..6fe9621 100644 --- a/src/events.c +++ b/src/events.c @@ -60,6 +60,7 @@ static void idle_cycle(struct wiimote_t* wm); static void clear_dirty_reads(struct wiimote_t* wm); static void propagate_event(struct wiimote_t* wm, byte event, byte* msg); static void event_data_read(struct wiimote_t* wm, byte* msg); +static void event_data_write(struct wiimote_t *wm, byte *msg); static void event_status(struct wiimote_t* wm, byte* msg); static void handle_expansion(struct wiimote_t* wm, byte* msg); @@ -396,7 +397,7 @@ static void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { } case WM_RPT_WRITE: { - /* write feedback - safe to skip */ + event_data_write(wm,msg); break; } default: @@ -546,6 +547,52 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) { } +static void event_data_write(struct wiimote_t *wm, byte *msg) +{ + + struct data_req_t* req = wm->data_req; + + wiiuse_pressed_buttons(wm,msg); + + /* if we don't have a request out then we didn't ask for this packet */ + if (!req) { + WIIUSE_WARNING("Transmitting data packet when no request was made."); + return; + } + if(!(req->state==REQ_SENT)) { + WIIUSE_WARNING("Transmission is not necessary"); + /* delete this request */ + wm->data_req = req->next; + free(req); + return; + } + + + req->state = REQ_DONE; + + if(req->cb) { + /* this was a callback, so invoke it now */ + req->cb(wm,NULL,0); + /* delete this request */ + wm->data_req = req->next; + free(req); + } else { + /* + * This should generate an event. + * We need to leave the event in the array so the client + * can access it still. We'll flag is as being 'REQ_DONE' + * and give the client one cycle to use it. Next event + * we will remove it from the list. + */ + wm->event = WIIUSE_WRITE_DATA; + + } + /* if another request exists send it to the wiimote */ + if (wm->data_req) { + wiiuse_send_next_pending_write_request(wm); + } +} + /** * @brief Read the controller status. * @@ -559,6 +606,7 @@ static void event_status(struct wiimote_t* wm, byte* msg) { int attachment = 0; int ir = 0; int exp_changed = 0; + struct data_req_t* req = wm->data_req; /* * An event occured. @@ -613,13 +661,33 @@ static void event_status(struct wiimote_t* wm, byte* msg) { * We need to send a WIIMOTE_CMD_REPORT_TYPE packet to * reenable other incoming reports. */ - if (exp_changed && WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { - /* - * Since the expansion status changed IR needs to - * be reset for the new IR report mode. - */ - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); - wiiuse_set_ir(wm, 1); + if (exp_changed) + { + if (exp_changed && WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) + { + /* + * Since the expansion status changed IR needs to + * be reset for the new IR report mode. + */ + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); + wiiuse_set_ir(wm, 1); + } else { + wiiuse_set_report_type(wm); + return; + } + + /* handling new Tx for changed exp */ + if(!req) return; + if(!(req->state==REQ_SENT)) return; + + wm->data_req = req->next; + + req->state = REQ_DONE; + /* if(req->cb!=NULL) req->cb(wm,msg,6); */ + + free(req); + wiiuse_send_next_pending_write_request(wm); + } else wiiuse_set_report_type(wm); } @@ -822,6 +890,33 @@ static void save_state(struct wiimote_t* wm) { wm->lstate.exp_wb_rbl = wm->exp.wb.rbl; break; + case EXP_MOTION_PLUS: + case EXP_MOTION_PLUS_CLASSIC: + case EXP_MOTION_PLUS_NUNCHUK: + { + wm->lstate.drx = wm->exp.mp.raw_gyro.p; + wm->lstate.dry = wm->exp.mp.raw_gyro.r; + wm->lstate.drz = wm->exp.mp.raw_gyro.y; + + if(wm->exp.type == EXP_MOTION_PLUS_CLASSIC) + { + wm->lstate.exp_ljs_ang = wm->exp.classic.ljs.ang; + wm->lstate.exp_ljs_mag = wm->exp.classic.ljs.mag; + wm->lstate.exp_rjs_ang = wm->exp.classic.rjs.ang; + wm->lstate.exp_rjs_mag = wm->exp.classic.rjs.mag; + wm->lstate.exp_r_shoulder = wm->exp.classic.r_shoulder; + wm->lstate.exp_l_shoulder = wm->exp.classic.l_shoulder; + wm->lstate.exp_btns = wm->exp.classic.btns; + } else { + wm->lstate.exp_ljs_ang = wm->exp.nunchuk.js.ang; + wm->lstate.exp_ljs_mag = wm->exp.nunchuk.js.mag; + wm->lstate.exp_btns = wm->exp.nunchuk.btns; + wm->lstate.exp_accel = wm->exp.nunchuk.accel; + } + + break; + } + case EXP_NONE: break; } @@ -925,6 +1020,35 @@ static int state_changed(struct wiimote_t* wm) { STATE_CHANGED(wm->lstate.exp_wb_rbl,wm->exp.wb.bl); break; } + + case EXP_MOTION_PLUS: + case EXP_MOTION_PLUS_CLASSIC: + case EXP_MOTION_PLUS_NUNCHUK: + { + STATE_CHANGED(wm->lstate.drx, wm->exp.mp.raw_gyro.p); + STATE_CHANGED(wm->lstate.dry, wm->exp.mp.raw_gyro.r); + STATE_CHANGED(wm->lstate.drz, wm->exp.mp.raw_gyro.y); + + if(wm->exp.type == EXP_MOTION_PLUS_CLASSIC) + { + STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.classic.ljs.ang); + STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.classic.ljs.mag); + STATE_CHANGED(wm->lstate.exp_rjs_ang, wm->exp.classic.rjs.ang); + STATE_CHANGED(wm->lstate.exp_rjs_mag, wm->exp.classic.rjs.mag); + STATE_CHANGED(wm->lstate.exp_r_shoulder, wm->exp.classic.r_shoulder); + STATE_CHANGED(wm->lstate.exp_l_shoulder, wm->exp.classic.l_shoulder); + STATE_CHANGED(wm->lstate.exp_btns, wm->exp.classic.btns); + } else { + STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.nunchuk.js.ang); + STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.nunchuk.js.mag); + STATE_CHANGED(wm->lstate.exp_btns, wm->exp.nunchuk.btns); + + CROSS_THRESH(wm->lstate.exp_orient, wm->exp.nunchuk.orient, wm->exp.nunchuk.orient_threshold); + CROSS_THRESH_XYZ(wm->lstate.exp_accel, wm->exp.nunchuk.accel, wm->exp.nunchuk.accel_threshold); + } + + break; + } case EXP_NONE: { break; diff --git a/src/io.c b/src/io.c index 44d2c36..d3a956c 100644 --- a/src/io.c +++ b/src/io.c @@ -105,6 +105,9 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { wiiuse_set_ir(wm, 1); } + wm->event = WIIUSE_CONNECT; + wiiuse_status(wm); + break; } default: diff --git a/src/motion_plus.c b/src/motion_plus.c index 46b8b50..3371f97 100644 --- a/src/motion_plus.c +++ b/src/motion_plus.c @@ -32,15 +32,21 @@ #include "events.h" /* for disable_expansion */ #include "ir.h" /* for wiiuse_set_ir_mode */ +#include "nunchuk.h" +#include "dynamics.h" #include /* for memset */ +#include /* for fabs */ + +static void wiiuse_calibrate_motion_plus(struct motion_plus_t *mp); +static void calculate_gyro_rates(struct motion_plus_t* mp); void wiiuse_motion_plus_check(struct wiimote_t *wm,byte *data,unsigned short len) { uint32_t val; if(data == NULL) { - wiiuse_read_data_cb(wm, wiiuse_motion_plus_check, wm->motion_plus_id, WM_EXP_ID, 6); + wiiuse_read_data_cb(wm, wiiuse_motion_plus_check, wm->motion_plus_id, WM_EXP_ID, 6); } else { @@ -48,13 +54,50 @@ void wiiuse_motion_plus_check(struct wiimote_t *wm,byte *data,unsigned short len WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_FAILED); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); val = from_big_endian_uint32_t(data + 2); - if(val == EXP_ID_CODE_MOTION_PLUS) + if(val == EXP_ID_CODE_MOTION_PLUS || + val == EXP_ID_CODE_MOTION_PLUS_NUNCHUK || + val == EXP_ID_CODE_MOTION_PLUS_CLASSIC) { /* handshake done */ wm->event = WIIUSE_MOTION_PLUS_ACTIVATED; - wm->exp.type = EXP_MOTION_PLUS; + + switch(val) + { + case EXP_ID_CODE_MOTION_PLUS: + wm->exp.type = EXP_MOTION_PLUS; + break; + + case EXP_ID_CODE_MOTION_PLUS_NUNCHUK: + wm->exp.type = EXP_MOTION_PLUS_NUNCHUK; + break; + + case EXP_ID_CODE_MOTION_PLUS_CLASSIC: + wm->exp.type = EXP_MOTION_PLUS_CLASSIC; + break; + + default: + /* huh? */ + WIIUSE_WARNING("Unknown ID returned in Motion+ handshake %d\n", val); + wm->exp.type = EXP_MOTION_PLUS; + break; + } + WIIUSE_DEBUG("Motion plus connected"); WIIMOTE_ENABLE_STATE(wm,WIIMOTE_STATE_EXP); + + /* Init gyroscopes */ + wm->exp.mp.cal_gyro.r = 0; + wm->exp.mp.cal_gyro.p = 0; + wm->exp.mp.cal_gyro.y = 0; + wm->exp.mp.orient.roll = 0.0; + wm->exp.mp.orient.pitch = 0.0; + wm->exp.mp.orient.yaw = 0.0; + wm->exp.mp.raw_gyro_threshold = 10; + + wm->exp.mp.nc = &(wm->exp.nunchuk); + wm->exp.mp.classic = &(wm->exp.classic); + wm->exp.nunchuk.flags = &wm->flags; + wiiuse_set_ir_mode(wm); } } @@ -104,10 +147,124 @@ void motion_plus_disconnected(struct motion_plus_t* mp) void motion_plus_event(struct motion_plus_t* mp, byte* msg) { - mp->rx = ((msg[5] & 0xFC) << 6) | msg[2]; /* Pitch */ - mp->ry = ((msg[4] & 0xFC) << 6) | msg[1]; /* Roll */ - mp->rz = ((msg[3] & 0xFC) << 6) | msg[0]; /* Yaw */ + /* + * Pass-through modes interleave data from the gyro + * with the expansion data. This extracts the tag + * determining which is which + */ + int isMPFrame = (1 << 1) & msg[5]; + mp->ext = msg[4] & 0x1; /* extension attached to pass-through port? */ - mp->ext = msg[4] & 0x1; - mp->status = (msg[3] & 0x3) | ((msg[4] & 0x2) << 1); /* roll, yaw, pitch */ + if (mp->ext == 0 || isMPFrame) /* reading gyro frame */ + { + /* Check if the gyroscope is in fast or slow mode (0 if rotating fast, 1 if slow or still) */ + mp->acc_mode = ((msg[4] & 0x2) << 1) | ((msg[3] & 0x1) << 1) | ((msg[3] & 0x2) >> 1); + + mp->raw_gyro.r = ((msg[4] & 0xFC) << 6) | msg[1]; + mp->raw_gyro.p = ((msg[5] & 0xFC) << 6) | msg[2]; + mp->raw_gyro.y = ((msg[3] & 0xFC) << 6) | msg[0]; + + /* First calibration */ + if ((mp->raw_gyro.r > 5000) && (mp->raw_gyro.p > 5000) && (mp->raw_gyro.y > 5000) && + !(mp->cal_gyro.r) + && !(mp->cal_gyro.p) + && !(mp->cal_gyro.y)) + { + wiiuse_calibrate_motion_plus(mp); + } + + /* Calculate angular rates in deg/sec and performs some simple filtering */ + calculate_gyro_rates(mp); + } + + else + { /* expansion frame */ + /* FIXME: Handle pass-through modes */ +/* if (mp->mode == WIIUSE_MP_NUNCHUK) */ + { + /* ok, this is nunchuck, re-encode it as regular nunchuck packet */ + + /* get button states */ + nunchuk_pressed_buttons(mp->nc, (msg[5] >> 2)); + + /* calculate joystick state */ + calc_joystick_state(&(mp->nc->js), msg[0], msg[1]); + + /* calculate orientation */ + mp->nc->accel.x = msg[2]; + mp->nc->accel.y = msg[3]; + mp->nc->accel.z = (msg[4] & 0xFE) | ((msg[5] >> 5) & 0x04); + + calculate_orientation(&(mp->nc->accel_calib), + &(mp->nc->accel), + &(mp->nc->orient), + NUNCHUK_IS_FLAG_SET(mp->nc, WIIUSE_SMOOTHING)); + + calculate_gforce(&(mp->nc->accel_calib), + &(mp->nc->accel), + &(mp->nc->gforce)); + + } +/* else if (mp->mode == WIIUSE_MP_CLASSIC) */ +/* { */ +/* WIIUSE_ERROR("Classic controller pass-through is not implemented!\n"); */ +/* } */ + } +} + +/** + * @brief Calibrate the Motion Plus gyroscopes. + * + * @param mp Pointer to a motion_plus_t structure. + * + * This should be called only after receiving the first values + * from the Motion Plus. + */ +void wiiuse_calibrate_motion_plus(struct motion_plus_t *mp) +{ + mp->cal_gyro.r = mp->raw_gyro.r; + mp->cal_gyro.p = mp->raw_gyro.p; + mp->cal_gyro.y = mp->raw_gyro.y; + mp->orient.roll = 0.0; + mp->orient.pitch = 0.0; + mp->orient.yaw = 0.0; +} + +static void calculate_gyro_rates(struct motion_plus_t* mp) +{ + short int tmp_r, tmp_p, tmp_y; + float tmp_roll, tmp_pitch, tmp_yaw; + + /* We consider calibration data */ + tmp_r = mp->raw_gyro.r - mp->cal_gyro.r; + tmp_p = mp->raw_gyro.p - mp->cal_gyro.p; + tmp_y = mp->raw_gyro.y - mp->cal_gyro.y; + + /* We convert to degree/sec according to fast/slow mode */ + if (mp->acc_mode & 0x04) + tmp_roll = tmp_r / 20.0; + else + tmp_roll = tmp_r / 4.0; + + if (mp->acc_mode & 0x02) + tmp_pitch = tmp_p / 20.0; + else + tmp_pitch = tmp_p / 4.0; + + if (mp->acc_mode & 0x01) + tmp_yaw = tmp_y / 20.0; + else + tmp_yaw = tmp_y / 4.0; + + /* Simple filtering */ + if (fabs(tmp_roll) < 0.5) + tmp_roll = 0.0; + if (fabs(tmp_pitch) < 0.5) + tmp_pitch = 0.0; + if (fabs(tmp_yaw) < 0.5) + tmp_yaw = 0.0; + + mp->angle_rate_gyro.r = tmp_roll; + mp->angle_rate_gyro.p = tmp_pitch; + mp->angle_rate_gyro.y = tmp_yaw; } diff --git a/src/nunchuk.c b/src/nunchuk.c index 1e49ccb..6e9811d 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -39,9 +39,6 @@ #include /* for malloc */ #include /* for memset */ - -static void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now); - /** * @brief Handle the handshake data from the nunchuk. * @@ -164,7 +161,7 @@ void nunchuk_event(struct nunchuk_t* nc, byte* msg) { * @param nc Pointer to a nunchuk_t structure. * @param msg The message byte specified in the event packet. */ -static void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now) { +void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now) { /* message is inverted (0 is active, 1 is inactive) */ now = ~now & NUNCHUK_BUTTON_ALL; diff --git a/src/nunchuk.h b/src/nunchuk.h index 4be7da8..951749d 100644 --- a/src/nunchuk.h +++ b/src/nunchuk.h @@ -48,6 +48,8 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un void nunchuk_disconnected(struct nunchuk_t* nc); void nunchuk_event(struct nunchuk_t* nc, byte* msg); + +void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now); /** @} */ #ifdef __cplusplus diff --git a/src/wiiuse.h b/src/wiiuse.h index fcf1336..6279a07 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -198,6 +198,8 @@ #define EXP_GUITAR_HERO_3 3 #define EXP_WII_BOARD 4 #define EXP_MOTION_PLUS 5 +#define EXP_MOTION_PLUS_NUNCHUK 6 /* Motion+ in nunchuk pass-through mode */ +#define EXP_MOTION_PLUS_CLASSIC 7 /* Motion+ in classic ctr. pass-through mode */ /** @} */ /** @brief IR correction types */ @@ -322,6 +324,21 @@ struct read_req_t { struct read_req_t* next; /**< next read request in the queue */ }; +/** + * @struct ang3s_t + * @brief RPY short angles. + */ +typedef struct ang3s_t { + short r, p, y; +} ang3s_t; + +/** + * @struct ang3f_t + * @brief RPY float angles. + */ +typedef struct ang3f_t { + float r, p, y; +} ang3f_t; /** * @brief Unsigned x,y byte vector. @@ -517,9 +534,17 @@ typedef struct guitar_hero_3_t { */ typedef struct motion_plus_t { - short rx, ry, rz; - unsigned char status; unsigned char ext; + + struct ang3s_t raw_gyro; /**< current raw gyroscope data */ + struct ang3s_t cal_gyro; /**< calibration raw gyroscope data */ + struct ang3f_t angle_rate_gyro; /**< current gyro angle rate */ + struct orient_t orient; /**< current orientation on each axis using Motion Plus gyroscopes */ + byte acc_mode; /**< Fast/slow rotation mode for roll, pitch and yaw (0 if rotating fast, 1 if slow or still) */ + int raw_gyro_threshold; /**< threshold for gyroscopes to generate an event */ + + struct nunchuk_t *nc; /* pointers to nunchuk & classic in pass-through-mode */ + struct classic_ctrl_t *classic; } motion_plus_t; /** @@ -568,12 +593,13 @@ typedef struct wii_board_t { typedef struct expansion_t { int type; /**< type of expansion attached */ + struct motion_plus_t mp; + union { struct nunchuk_t nunchuk; struct classic_ctrl_t classic; struct guitar_hero_3_t gh3; struct wii_board_t wb; - struct motion_plus_t mp; }; } expansion_t; @@ -637,6 +663,7 @@ typedef enum WIIUSE_EVENT_TYPE { WIIUSE_DISCONNECT, WIIUSE_UNEXPECTED_DISCONNECT, WIIUSE_READ_DATA, + WIIUSE_WRITE_DATA, WIIUSE_NUNCHUK_INSERTED, WIIUSE_NUNCHUK_REMOVED, WIIUSE_CLASSIC_CTRL_INSERTED, diff --git a/src/wiiuse_internal.h b/src/wiiuse_internal.h index b742fae..af342aa 100644 --- a/src/wiiuse_internal.h +++ b/src/wiiuse_internal.h @@ -171,6 +171,8 @@ #define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD #define EXP_ID_CODE_GUITAR 0x9A1EFDFB #define EXP_ID_CODE_MOTION_PLUS 0xa4200405 +#define EXP_ID_CODE_MOTION_PLUS_NUNCHUK 0xA4200505 /** Motion Plus ID in Nunchuck passthrough mode */ +#define EXP_ID_CODE_MOTION_PLUS_CLASSIC 0xA4200705 /** Motion Plus ID in Classic control. passthrough */ #define EXP_HANDSHAKE_LEN 224 @@ -184,8 +186,6 @@ #define WIIMOTE_STATE_DEV_FOUND 0x0001 #define WIIMOTE_STATE_HANDSHAKE 0x0002 /* actual connection exists but no handshake yet */ #define WIIMOTE_STATE_HANDSHAKE_COMPLETE 0x0004 /* actual connection exists but no handshake yet */ -#define WIIMOTE_STATE_EXP_HANDSHAKE 0x00020 /* actual connection exists but no handshake yet */ -#define WIIMOTE_STATE_EXP_FAILED 0x00040 /* actual connection exists but no handshake yet */ #define WIIMOTE_STATE_CONNECTED 0x0008 #define WIIMOTE_STATE_RUMBLE 0x0010 #define WIIMOTE_STATE_ACC 0x0020 @@ -197,6 +197,9 @@ #define WIIMOTE_STATE_IR_SENS_LVL3 0x0800 #define WIIMOTE_STATE_IR_SENS_LVL4 0x1000 #define WIIMOTE_STATE_IR_SENS_LVL5 0x2000 +#define WIIMOTE_STATE_EXP_HANDSHAKE 0x40000 /* actual M+ connection exists but no handshake yet */ +#define WIIMOTE_STATE_EXP_FAILED 0x80000 /* actual M+ connection exists but handshake failed */ + #define WIIMOTE_INIT_STATES (WIIMOTE_STATE_IR_SENS_LVL3) From 9b5ef23276e91a47d5c3cacc81c8184d88499e94 Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Wed, 14 Sep 2011 08:59:48 -0500 Subject: [PATCH 02/16] More fWiine merging Conflicts: src/events.c src/motion_plus.c src/wiiuse.h --- src/events.c | 1 + src/io.c | 7 +++++-- src/motion_plus.c | 44 ++++++++++++++++++++++++------------------- src/motion_plus.h | 5 ++++- src/wiiuse.h | 1 + src/wiiuse_internal.h | 7 +++++-- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/events.c b/src/events.c index 6fe9621..116075e 100644 --- a/src/events.c +++ b/src/events.c @@ -44,6 +44,7 @@ #include "nunchuk.h" /* for nunchuk_disconnected, etc */ #include "wiiboard.h" /* for wii_board_disconnected, etc */ #include "io.h" /* for wiiuse_io_read on Windows, etc */ +#include "motion_plus.h" #ifndef WIIUSE_WIN32 #include /* for timeval */ diff --git a/src/io.c b/src/io.c index d3a956c..0fbb7ea 100644 --- a/src/io.c +++ b/src/io.c @@ -68,6 +68,7 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { break; } + case 1: { struct read_req_t* req = wm->read_req; @@ -90,10 +91,10 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { accel->cal_zero.x, accel->cal_zero.y, accel->cal_zero.z, accel->cal_g.x, accel->cal_g.y, accel->cal_g.z); + wiiuse_set_motion_plus(wm, 0); - /* request the status of the wiimote to see if there is an expansion */ + /* request the status of the wiimote to check for any expansion */ wiiuse_status(wm); - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE); wm->handshake_state++; @@ -106,10 +107,12 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { } wm->event = WIIUSE_CONNECT; + wm->expansion_state = 0; wiiuse_status(wm); break; } + default: { break; diff --git a/src/motion_plus.c b/src/motion_plus.c index 3371f97..d79208c 100644 --- a/src/motion_plus.c +++ b/src/motion_plus.c @@ -41,19 +41,21 @@ static void wiiuse_calibrate_motion_plus(struct motion_plus_t *mp); static void calculate_gyro_rates(struct motion_plus_t* mp); -void wiiuse_motion_plus_check(struct wiimote_t *wm,byte *data,unsigned short len) + +void wiiuse_motion_plus_handshake(struct wiimote_t *wm,byte *data,unsigned short len) { uint32_t val; if(data == NULL) { - wiiuse_read_data_cb(wm, wiiuse_motion_plus_check, wm->motion_plus_id, WM_EXP_ID, 6); + wiiuse_read_data_cb(wm, wiiuse_motion_plus_handshake, wm->motion_plus_id, WM_EXP_ID, 6); } else { - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_FAILED); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); + val = from_big_endian_uint32_t(data + 2); + if(val == EXP_ID_CODE_MOTION_PLUS || val == EXP_ID_CODE_MOTION_PLUS_NUNCHUK || val == EXP_ID_CODE_MOTION_PLUS_CLASSIC) @@ -83,7 +85,6 @@ void wiiuse_motion_plus_check(struct wiimote_t *wm,byte *data,unsigned short len } WIIUSE_DEBUG("Motion plus connected"); - WIIMOTE_ENABLE_STATE(wm,WIIMOTE_STATE_EXP); /* Init gyroscopes */ wm->exp.mp.cal_gyro.r = 0; @@ -98,6 +99,9 @@ void wiiuse_motion_plus_check(struct wiimote_t *wm,byte *data,unsigned short len wm->exp.mp.classic = &(wm->exp.classic); wm->exp.nunchuk.flags = &wm->flags; + wm->exp.mp.ext = 0; + wm->exp.mp.ext_initialized = 0; + wiiuse_set_ir_mode(wm); } } @@ -105,7 +109,6 @@ void wiiuse_motion_plus_check(struct wiimote_t *wm,byte *data,unsigned short len static void wiiuse_set_motion_plus_clear2(struct wiimote_t *wm,byte *data,unsigned short len) { - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_FAILED); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); wiiuse_set_ir_mode(wm); @@ -122,13 +125,10 @@ void wiiuse_set_motion_plus(struct wiimote_t *wm, int status) { byte val; - if(WIIMOTE_IS_SET(wm,WIIMOTE_STATE_EXP_HANDSHAKE)) - return; - - WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); - if(status) + if(status && !WIIMOTE_IS_SET(wm,WIIMOTE_STATE_EXP_HANDSHAKE)) { - val = 0x04; + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); + val = (status == 1) ? 0x04 : 0x05; wiiuse_write_data_cb(wm, WM_EXP_MOTION_PLUS_ENABLE, &val, 1, wiiuse_motion_plus_check); } else @@ -145,7 +145,7 @@ void motion_plus_disconnected(struct motion_plus_t* mp) memset(mp, 0, sizeof(struct motion_plus_t)); } -void motion_plus_event(struct motion_plus_t* mp, byte* msg) +void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) { /* * Pass-through modes interleave data from the gyro @@ -178,9 +178,9 @@ void motion_plus_event(struct motion_plus_t* mp, byte* msg) } else - { /* expansion frame */ - /* FIXME: Handle pass-through modes */ -/* if (mp->mode == WIIUSE_MP_NUNCHUK) */ + { + /* expansion frame */ + if (exp_type == EXP_MOTION_PLUS_NUNCHUK) { /* ok, this is nunchuck, re-encode it as regular nunchuck packet */ @@ -205,10 +205,16 @@ void motion_plus_event(struct motion_plus_t* mp, byte* msg) &(mp->nc->gforce)); } -/* else if (mp->mode == WIIUSE_MP_CLASSIC) */ -/* { */ -/* WIIUSE_ERROR("Classic controller pass-through is not implemented!\n"); */ -/* } */ + + else if (exp_type == EXP_MOTION_PLUS_CLASSIC) + { + WIIUSE_ERROR("Classic controller pass-through is not implemented!\n"); + } + + else + { + WIIUSE_ERROR("Unsupported mode passed to motion_plus_event() !\n"); + } } } diff --git a/src/motion_plus.h b/src/motion_plus.h index 9b05cce..88cdba7 100644 --- a/src/motion_plus.h +++ b/src/motion_plus.h @@ -45,7 +45,10 @@ extern "C" { /** @{ */ void motion_plus_disconnected(struct motion_plus_t* mp); -void motion_plus_event(struct motion_plus_t* mp, byte* msg); +void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg); + +void wiiuse_motion_plus_handshake(struct wiimote_t *wm, byte *data,unsigned short len); + /** @} */ #ifdef __cplusplus diff --git a/src/wiiuse.h b/src/wiiuse.h index 6279a07..b1a003d 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -534,6 +534,7 @@ typedef struct guitar_hero_3_t { */ typedef struct motion_plus_t { + int ext_initialized; /* is the pass-through device initialized? */ unsigned char ext; struct ang3s_t raw_gyro; /**< current raw gyroscope data */ diff --git a/src/wiiuse_internal.h b/src/wiiuse_internal.h index af342aa..ef05501 100644 --- a/src/wiiuse_internal.h +++ b/src/wiiuse_internal.h @@ -135,6 +135,7 @@ #define WM_EXP_MEM_ENABLE2 0x04A400FB #define WM_EXP_MEM_CALIBR 0x04A40020 #define WM_EXP_MOTION_PLUS_ENABLE 0x04A600FE +#define WM_EXP_MOTION_PLUS_INIT 0x04A600F0 #define WM_REG_IR 0x04B00030 #define WM_REG_IR_BLOCK1 0x04B00000 #define WM_REG_IR_BLOCK2 0x04B0001A @@ -197,8 +198,10 @@ #define WIIMOTE_STATE_IR_SENS_LVL3 0x0800 #define WIIMOTE_STATE_IR_SENS_LVL4 0x1000 #define WIIMOTE_STATE_IR_SENS_LVL5 0x2000 -#define WIIMOTE_STATE_EXP_HANDSHAKE 0x40000 /* actual M+ connection exists but no handshake yet */ -#define WIIMOTE_STATE_EXP_FAILED 0x80000 /* actual M+ connection exists but handshake failed */ +#define WIIMOTE_STATE_EXP_HANDSHAKE 0x10000 /* actual M+ connection exists but no handshake yet */ +#define WIIMOTE_STATE_EXP_EXTERN 0x20000 /* actual M+ connection exists but handshake failed */ +#define WIIMOTE_STATE_EXP_FAILED 0x40000 /* actual M+ connection exists but handshake failed */ + #define WIIMOTE_INIT_STATES (WIIMOTE_STATE_IR_SENS_LVL3) From 80e8fa37acc1661052c8d59cf0ff9cd694899bd0 Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Wed, 14 Sep 2011 09:04:58 -0500 Subject: [PATCH 03/16] cleanup Conflicts: src/events.c src/io.c src/wiiuse.c src/wiiuse.h --- src/events.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/io.c | 4 +-- src/wiiuse.c | 2 ++ src/wiiuse.h | 3 +- 4 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/events.c b/src/events.c index 116075e..5e67358 100644 --- a/src/events.c +++ b/src/events.c @@ -657,6 +657,71 @@ static void event_status(struct wiimote_t* wm, byte* msg) { } #endif + +#if 0 + switch(wm->expansion_state) + { + case 0: // regular expansion detection + { + if(attachment && wm->exp.type != EXP_NONE) + { + wm->expansion_dattempts = 0; + wm->expansion_state++; + } + + else + { + // give it another chance still + if(wm->expansion_dattempts < 10) + { + wm->expansion_dattempts++; + wiiuse_status(wm); + return; + } + + // likely no attachment, give up and try M+ + else { + wm->expansion_dattempts = 0; + wiiuse_set_motion_plus(wm, WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP) ? 2 : 1); + wm->expansion_state++; + } + } + + break; + } + + case 1: // try to init Motion+ + { + if(attachment && wm->exp.type != EXP_NONE) + { + wm->expansion_dattempts = 0; + wm->expansion_state++; + } + + else + { + // give it another chance still + if(wm->expansion_dattempts < 10) + { + wm->expansion_dattempts++; + wiiuse_status(wm); + return; + } + + else { + // give up and move on + wm->expansion_state++; + } + } + break; + } + + default: + break; + } + +#endif + /* * From now on the remote will only send status packets. * We need to send a WIIMOTE_CMD_REPORT_TYPE packet to @@ -690,7 +755,9 @@ static void event_status(struct wiimote_t* wm, byte* msg) { wiiuse_send_next_pending_write_request(wm); } else + { wiiuse_set_report_type(wm); + } } @@ -714,6 +781,11 @@ static void handle_expansion(struct wiimote_t* wm, byte* msg) { case EXP_WII_BOARD: wii_board_event(&wm->exp.wb, msg); break; + case EXP_MOTION_PLUS: + case EXP_MOTION_PLUS_CLASSIC: + case EXP_MOTION_PLUS_NUNCHUK: + motion_plus_event(&wm->exp.mp, wm->exp.type, msg); + break; default: break; } @@ -789,6 +861,15 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED; break; } + + case EXP_ID_CODE_MOTION_PLUS: + case EXP_ID_CODE_MOTION_PLUS_CLASSIC: + case EXP_ID_CODE_MOTION_PLUS_NUNCHUK: + //wiiuse_motion_plus_handshake(wm, data, len); + wm->event = WIIUSE_MOTION_PLUS_ACTIVATED; + break; + + default: { WIIUSE_WARNING("Unknown expansion type. Code: 0x%x", id); @@ -833,6 +914,12 @@ void disable_expansion(struct wiimote_t* wm) { wii_board_disconnected(&wm->exp.wb); wm->event = WIIUSE_WII_BOARD_CTRL_REMOVED; break; + case EXP_MOTION_PLUS: + case EXP_MOTION_PLUS_CLASSIC: + case EXP_MOTION_PLUS_NUNCHUK: + motion_plus_disconnected(&wm->exp.mp); + wm->event = WIIUSE_MOTION_PLUS_REMOVED; + break; default: break; } diff --git a/src/io.c b/src/io.c index 0fbb7ea..5bd9e33 100644 --- a/src/io.c +++ b/src/io.c @@ -91,7 +91,8 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { accel->cal_zero.x, accel->cal_zero.y, accel->cal_zero.z, accel->cal_g.x, accel->cal_g.y, accel->cal_g.z); - wiiuse_set_motion_plus(wm, 0); + /*wiiuse_set_motion_plus(wm, 0);*/ + wiiuse_status(wm); /* request the status of the wiimote to check for any expansion */ wiiuse_status(wm); @@ -107,7 +108,6 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { } wm->event = WIIUSE_CONNECT; - wm->expansion_state = 0; wiiuse_status(wm); break; diff --git a/src/wiiuse.c b/src/wiiuse.c index 07dbce8..f1189a6 100644 --- a/src/wiiuse.c +++ b/src/wiiuse.c @@ -147,6 +147,8 @@ struct wiimote_t** wiiuse_init(int wiimotes) { wm[i]->event = WIIUSE_NONE; wm[i]->exp.type = EXP_NONE; + wm[i]->expansion_state = 0; + wm[i]->expansion_dattempts = 0; wiiuse_set_aspect_ratio(wm[i], WIIUSE_ASPECT_4_3); wiiuse_set_ir_position(wm[i], WIIUSE_IR_ABOVE); diff --git a/src/wiiuse.h b/src/wiiuse.h index b1a003d..87de021 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -714,7 +714,8 @@ typedef struct wiimote_t { WCONST int flags; /**< options flag */ WCONST byte handshake_state; /**< the state of the connection handshake */ - WCONST unsigned char expansion_state; /**< the state of the expansion handshake */ + WCONST byte expansion_state; /**< the state of the expansion handshake */ + WCONST byte expansion_dattempts; /**< how many times did we wait already */ WCONST struct data_req_t* data_req; /**< list of data read requests */ WCONST struct read_req_t* read_req; /**< list of data read requests */ From 9ae7f37396428ac21249bad6469414dfb5840169 Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Wed, 14 Sep 2011 09:12:33 -0500 Subject: [PATCH 04/16] Added expansion detection from fWiine Added gyro rate calculations & calibrations from WiiC Conflicts: src/events.c src/io.c src/motion_plus.c src/wiiuse_internal.h --- src/events.c | 135 +++++++++++++++++++++++++++++++----------- src/io.c | 28 ++++++++- src/motion_plus.c | 17 ++++-- src/wiiuse_internal.h | 16 +++-- 4 files changed, 149 insertions(+), 47 deletions(-) diff --git a/src/events.c b/src/events.c index 5e67358..f4de7db 100644 --- a/src/events.c +++ b/src/events.c @@ -609,6 +609,10 @@ static void event_status(struct wiimote_t* wm, byte* msg) { int exp_changed = 0; struct data_req_t* req = wm->data_req; + /* initial handshake is not finished yet, ignore this */ + if(WIIMOTE_IS_SET(wm, WIIMOTE_STATE_HANDSHAKE)) + return; + /* * An event occured. * This event can be overwritten by a more specific @@ -661,7 +665,7 @@ static void event_status(struct wiimote_t* wm, byte* msg) { #if 0 switch(wm->expansion_state) { - case 0: // regular expansion detection + case 0: /* regular expansion detection */ { if(attachment && wm->exp.type != EXP_NONE) { @@ -671,7 +675,7 @@ static void event_status(struct wiimote_t* wm, byte* msg) { else { - // give it another chance still + /* give it another chance still */ if(wm->expansion_dattempts < 10) { wm->expansion_dattempts++; @@ -679,7 +683,7 @@ static void event_status(struct wiimote_t* wm, byte* msg) { return; } - // likely no attachment, give up and try M+ + /* likely no attachment, give up and try M+ */ else { wm->expansion_dattempts = 0; wiiuse_set_motion_plus(wm, WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP) ? 2 : 1); @@ -690,7 +694,7 @@ static void event_status(struct wiimote_t* wm, byte* msg) { break; } - case 1: // try to init Motion+ + case 1: /* try to init Motion+ */ { if(attachment && wm->exp.type != EXP_NONE) { @@ -700,7 +704,7 @@ static void event_status(struct wiimote_t* wm, byte* msg) { else { - // give it another chance still + /* give it another chance still */ if(wm->expansion_dattempts < 10) { wm->expansion_dattempts++; @@ -709,7 +713,7 @@ static void event_status(struct wiimote_t* wm, byte* msg) { } else { - // give up and move on + /* give up and move on */ wm->expansion_state++; } } @@ -727,37 +731,27 @@ static void event_status(struct wiimote_t* wm, byte* msg) { * We need to send a WIIMOTE_CMD_REPORT_TYPE packet to * reenable other incoming reports. */ - if (exp_changed) - { if (exp_changed && WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) - { - /* - * Since the expansion status changed IR needs to - * be reset for the new IR report mode. - */ - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); - wiiuse_set_ir(wm, 1); - } else { - wiiuse_set_report_type(wm); - return; - } - - /* handling new Tx for changed exp */ - if(!req) return; - if(!(req->state==REQ_SENT)) return; - - wm->data_req = req->next; - - req->state = REQ_DONE; - /* if(req->cb!=NULL) req->cb(wm,msg,6); */ - - free(req); - wiiuse_send_next_pending_write_request(wm); - - } else { + /* + * Since the expansion status changed IR needs to + * be reset for the new IR report mode. + */ + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); + wiiuse_set_ir(wm, 1); + } else { wiiuse_set_report_type(wm); + return; } + + /* handling new Tx for changed exp */ + if(!req) return; + if(!(req->state==REQ_SENT)) return; + wm->data_req = req->next; + req->state = REQ_DONE; + /* if(req->cb!=NULL) req->cb(wm,msg,6); */ + free(req); + wiiuse_send_next_pending_write_request(wm); } @@ -807,7 +801,79 @@ static void handle_expansion(struct wiimote_t* wm, byte* msg) { */ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { int id; + byte val = 0; + byte buf = 0x00; + byte* handshake_buf; +#ifndef OLD_EXP_HANDSHAKE + switch(wm->expansion_state) { + /* These two initialization writes disable the encryption */ + case 0: + wm->expansion_state = 1; + /* increase the timeout until the handshake completes */ +#ifdef WIIUSE_WIN32 + WIIUSE_DEBUG("write 0x55 - Setting timeout to expansion %i ms.", wm->exp_timeout); + wm->timeout = wm->exp_timeout; +#endif + buf = 0x55; + wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &buf, 1, handshake_expansion); + break; + case 1: + wm->expansion_state = 2; + /* increase the timeout until the handshake completes */ +#ifdef WIIUSE_WIN32 + WIIUSE_DEBUG("write 0x00 - Setting timeout to expansion %i ms.", wm->exp_timeout); + wm->timeout = wm->exp_timeout; +#endif + val = 0x00; + wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE2, &buf, 1, handshake_expansion); + break; + case 2: + wm->expansion_state = 3; + /* get the calibration data */ + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) + disable_expansion(wm); + handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); + wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); + + /* tell the wiimote to send expansion data */ + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP); + break; + case 3: + if(!data || !len) return; + id = from_big_endian_uint32_t(data + 220); + switch(id) { + case EXP_ID_CODE_NUNCHUK: + if (nunchuk_handshake(wm, &wm->exp.nunchuk, data, len)) + wm->event = WIIUSE_NUNCHUK_INSERTED; + break; + case EXP_ID_CODE_CLASSIC_CONTROLLER: + if (classic_ctrl_handshake(wm, &wm->exp.classic, data, len)) + wm->event = WIIUSE_CLASSIC_CTRL_INSERTED; + break; + case EXP_ID_CODE_GUITAR: + if (guitar_hero_3_handshake(wm, &wm->exp.gh3, data, len)) + wm->event = WIIUSE_GUITAR_HERO_3_CTRL_INSERTED; + break; + case EXP_ID_CODE_MOTION_PLUS: + case EXP_ID_CODE_MOTION_PLUS_CLASSIC: + case EXP_ID_CODE_MOTION_PLUS_NUNCHUK: + /* wiiuse_motion_plus_handshake(wm, data, len); */ + wm->event = WIIUSE_MOTION_PLUS_ACTIVATED; + break; + default: + WIIUSE_WARNING("Unknown expansion type. Code: 0x%x", id); + break; + } + free(data); + WIIMOTE_DISABLE_STATE(wm,WIIMOTE_STATE_EXP_HANDSHAKE); + WIIMOTE_ENABLE_STATE(wm,WIIMOTE_STATE_EXP); + wiiuse_set_ir_mode(wm); + wiiuse_status(wm); + break; + } + +#else if (!data) { byte* handshake_buf; byte buf = 0x00; @@ -865,7 +931,7 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { case EXP_ID_CODE_MOTION_PLUS: case EXP_ID_CODE_MOTION_PLUS_CLASSIC: case EXP_ID_CODE_MOTION_PLUS_NUNCHUK: - //wiiuse_motion_plus_handshake(wm, data, len); + /* wiiuse_motion_plus_handshake(wm, data, len); */ wm->event = WIIUSE_MOTION_PLUS_ACTIVATED; break; @@ -878,6 +944,7 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { } free(data); +#endif } diff --git a/src/io.c b/src/io.c index 5bd9e33..6cde801 100644 --- a/src/io.c +++ b/src/io.c @@ -35,6 +35,23 @@ #include /* for free, malloc */ +static void wiiuse_disable_motion_plus2(struct wiimote_t *wm,byte *data,unsigned short len) +{ + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_FAILED); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); + wiiuse_set_ir_mode(wm); + + wm->handshake_state++; + wiiuse_handshake(wm, NULL, 0); + +} + +static void wiiuse_disable_motion_plus1(struct wiimote_t *wm,byte *data,unsigned short len) +{ + byte val = 0x00; + wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &val, 1, wiiuse_disable_motion_plus2); +} + /** * @brief Get initialization data from the wiimote. * @@ -91,11 +108,16 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { accel->cal_zero.x, accel->cal_zero.y, accel->cal_zero.z, accel->cal_g.x, accel->cal_g.y, accel->cal_g.z); - /*wiiuse_set_motion_plus(wm, 0);*/ - wiiuse_status(wm); + /* M+ off */ + byte val = 0x55; + wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &val, 1, wiiuse_disable_motion_plus1); + break; + } + + case 2: + { /* request the status of the wiimote to check for any expansion */ - wiiuse_status(wm); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE); wm->handshake_state++; diff --git a/src/motion_plus.c b/src/motion_plus.c index d79208c..37eaeb2 100644 --- a/src/motion_plus.c +++ b/src/motion_plus.c @@ -53,6 +53,7 @@ void wiiuse_motion_plus_handshake(struct wiimote_t *wm,byte *data,unsigned short { WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_FAILED); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP); /* tell wiimote to include exp. data in reports */ val = from_big_endian_uint32_t(data + 2); @@ -103,6 +104,7 @@ void wiiuse_motion_plus_handshake(struct wiimote_t *wm,byte *data,unsigned short wm->exp.mp.ext_initialized = 0; wiiuse_set_ir_mode(wm); + wiiuse_set_report_type(wm); } } } @@ -129,7 +131,7 @@ void wiiuse_set_motion_plus(struct wiimote_t *wm, int status) { WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); val = (status == 1) ? 0x04 : 0x05; - wiiuse_write_data_cb(wm, WM_EXP_MOTION_PLUS_ENABLE, &val, 1, wiiuse_motion_plus_check); + wiiuse_write_data_cb(wm, WM_EXP_MOTION_PLUS_ENABLE, &val, 1, wiiuse_motion_plus_handshake); } else { @@ -165,10 +167,15 @@ void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) mp->raw_gyro.y = ((msg[3] & 0xFC) << 6) | msg[0]; /* First calibration */ - if ((mp->raw_gyro.r > 5000) && (mp->raw_gyro.p > 5000) && (mp->raw_gyro.y > 5000) && - !(mp->cal_gyro.r) - && !(mp->cal_gyro.p) - && !(mp->cal_gyro.y)) + if ((mp->raw_gyro.r > 5000) && + (mp->raw_gyro.p > 5000) && + (mp->raw_gyro.y > 5000) && + (mp->raw_gyro.r < 0x3fff) && + (mp->raw_gyro.p < 0x3fff) && + (mp->raw_gyro.y < 0x3fff) && + !(mp->cal_gyro.r) && + !(mp->cal_gyro.p) && + !(mp->cal_gyro.y)) { wiiuse_calibrate_motion_plus(mp); } diff --git a/src/wiiuse_internal.h b/src/wiiuse_internal.h index ef05501..4f6de90 100644 --- a/src/wiiuse_internal.h +++ b/src/wiiuse_internal.h @@ -167,11 +167,17 @@ */ /* encrypted expansion id codes (located at 0x04A400FC) */ -#define EXP_ID_CODE_NUNCHUK 0x9A1EFEFE -#define EXP_ID_CODE_WII_BOARD 0xA4200402 -#define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD -#define EXP_ID_CODE_GUITAR 0x9A1EFDFB -#define EXP_ID_CODE_MOTION_PLUS 0xa4200405 +/* #define EXP_ID_CODE_NUNCHUK 0x9A1EFEFE */ +/* #define EXP_ID_CODE_WII_BOARD 0xA4200402 */ +/* #define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD */ +/* #define EXP_ID_CODE_GUITAR 0x9A1EFDFB */ + +/* decrypted expansion id codes (located at 0x04A400FC) */ +#define EXP_ID_CODE_NUNCHUK 0xA4200000 +#define EXP_ID_CODE_WII_BOARD 0xA4200402 +#define EXP_ID_CODE_CLASSIC_CONTROLLER 0xA4200101 +#define EXP_ID_CODE_GUITAR 0xA4200103 +#define EXP_ID_CODE_MOTION_PLUS 0xa4200405 #define EXP_ID_CODE_MOTION_PLUS_NUNCHUK 0xA4200505 /** Motion Plus ID in Nunchuck passthrough mode */ #define EXP_ID_CODE_MOTION_PLUS_CLASSIC 0xA4200705 /** Motion Plus ID in Classic control. passthrough */ From 44dcb16c584ffb1f387c3a161a6a7c1b82dbd6cf Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Mon, 12 Sep 2011 12:42:43 +0200 Subject: [PATCH 05/16] Windows compilation fixes --- src/io.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/io.c b/src/io.c index 6cde801..e3078e1 100644 --- a/src/io.c +++ b/src/io.c @@ -31,6 +31,7 @@ * @brief Handles device I/O (non-OS specific). */ #include "io.h" +#include "ir.h" #include /* for free, malloc */ @@ -90,6 +91,7 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { { struct read_req_t* req = wm->read_req; struct accel_t* accel = &wm->accel_calib; + byte val; /* received read data */ accel->cal_zero.x = req->buf[0]; @@ -109,7 +111,7 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { accel->cal_g.x, accel->cal_g.y, accel->cal_g.z); /* M+ off */ - byte val = 0x55; + val = 0x55; wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &val, 1, wiiuse_disable_motion_plus1); break; From ea793369f60c3a7f377d8b2e7ad8580f15d7a890 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 14 Sep 2011 09:25:00 -0500 Subject: [PATCH 06/16] Fix mingw build --- src/wiiuse_internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wiiuse_internal.h b/src/wiiuse_internal.h index 4f6de90..69a32e7 100644 --- a/src/wiiuse_internal.h +++ b/src/wiiuse_internal.h @@ -275,7 +275,7 @@ void wiiuse_send_next_pending_read_request(struct wiimote_t* wm); void wiiuse_send_next_pending_write_request(struct wiimote_t* wm); int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len); int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int offset, uint16_t len); -int wiiuse_write_data_cb(struct wiimote_t *wm, uint addr, unsigned char *data, unsigned char len, wiiuse_write_cb write_cb); +int wiiuse_write_data_cb(struct wiimote_t *wm, unsigned int addr, byte* data, byte len, wiiuse_write_cb write_cb); #ifdef WIIUSE_DOXYGEN_PARSING From 61f1e7e18d03acd890444f1bf5a575f6242a14ad Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 14 Sep 2011 10:30:06 -0500 Subject: [PATCH 07/16] add comments from include-what-you-use --- src/events.c | 2 +- src/motion_plus.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/events.c b/src/events.c index f4de7db..a5cdf31 100644 --- a/src/events.c +++ b/src/events.c @@ -43,8 +43,8 @@ #include "ir.h" /* for calculate_basic_ir, etc */ #include "nunchuk.h" /* for nunchuk_disconnected, etc */ #include "wiiboard.h" /* for wii_board_disconnected, etc */ +#include "motion_plus.h" /* for motion_plus_disconnected, etc */ #include "io.h" /* for wiiuse_io_read on Windows, etc */ -#include "motion_plus.h" #ifndef WIIUSE_WIN32 #include /* for timeval */ diff --git a/src/motion_plus.c b/src/motion_plus.c index 37eaeb2..25b3ca7 100644 --- a/src/motion_plus.c +++ b/src/motion_plus.c @@ -32,8 +32,8 @@ #include "events.h" /* for disable_expansion */ #include "ir.h" /* for wiiuse_set_ir_mode */ -#include "nunchuk.h" -#include "dynamics.h" +#include "nunchuk.h" /* for nunchuk_pressed_buttons */ +#include "dynamics.h" /* for calc_joystick_state, etc */ #include /* for memset */ #include /* for fabs */ From a1883ee160b026846c69d605c169f8473770db3d Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 14 Sep 2011 12:15:25 -0500 Subject: [PATCH 08/16] Add Jan Ciger to changelog and readme --- CHANGELOG.mkd | 9 +++++---- README.mkd | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.mkd b/CHANGELOG.mkd index 42cd795..8ea27bf 100644 --- a/CHANGELOG.mkd +++ b/CHANGELOG.mkd @@ -18,13 +18,14 @@ Added: Added to more easily support a ctypes python binding, generated by and included with RPythonic http://code.google.com/p/rpythonic/ Thanks to hartsantler for this feature. - + - Ability to build as a static library - must define WIIUSE_STATIC in client app as well as choosing the appropriate option when building WiiUse. The default is still a shared library (dll/so). Merged from - paulburton and added to build system. -- Initial MotionPlus support, merged from fwiine - thanks to admiral0 for - the pull request. + paulburton and added to build system. +- Initial MotionPlus support. Thanks to admiral0 for the initial pull + request merging from fwiine, and to Jan Ciger Reviatech SAS for building + on it with fwiine and WiiC code as well as additional code. Fixed: diff --git a/README.mkd b/README.mkd index 042ceba..5a50fd0 100644 --- a/README.mkd +++ b/README.mkd @@ -52,9 +52,10 @@ Additional Contributors: - Paul Burton - Karl Semich - Johannes Zarl -- hartsantler -- admiral0 and fwiine project -- Jeff Baker/Inv3rsion, LLC. +- hartsantler +- admiral0 and fwiine project +- Jeff Baker/Inv3rsion, LLC. +- Jan Ciger - Reviatech SAS License From e1fcc8266c8f3fdb3995bf5cb28d79129b922645 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 14 Sep 2011 13:02:30 -0500 Subject: [PATCH 09/16] more IWYU include comments --- src/io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/io.c b/src/io.c index e3078e1..168ff9f 100644 --- a/src/io.c +++ b/src/io.c @@ -31,7 +31,7 @@ * @brief Handles device I/O (non-OS specific). */ #include "io.h" -#include "ir.h" +#include "ir.h" /* for wiiuse_set_ir_mode */ #include /* for free, malloc */ From f7b198e0e68d0738ddfca58d245819a1b995cc83 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 14 Sep 2011 13:02:41 -0500 Subject: [PATCH 10/16] Remove old encrypted expansion codes - no longer needed --- src/wiiuse_internal.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/wiiuse_internal.h b/src/wiiuse_internal.h index 69a32e7..f244b8b 100644 --- a/src/wiiuse_internal.h +++ b/src/wiiuse_internal.h @@ -166,12 +166,6 @@ * Expansion stuff */ -/* encrypted expansion id codes (located at 0x04A400FC) */ -/* #define EXP_ID_CODE_NUNCHUK 0x9A1EFEFE */ -/* #define EXP_ID_CODE_WII_BOARD 0xA4200402 */ -/* #define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD */ -/* #define EXP_ID_CODE_GUITAR 0x9A1EFDFB */ - /* decrypted expansion id codes (located at 0x04A400FC) */ #define EXP_ID_CODE_NUNCHUK 0xA4200000 #define EXP_ID_CODE_WII_BOARD 0xA4200402 From 60570ea8d379e31bbbb2c566549fbfcc7584c35e Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 14 Sep 2011 13:03:36 -0500 Subject: [PATCH 11/16] update changelog to mention windows improvement --- CHANGELOG.mkd | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.mkd b/CHANGELOG.mkd index 8ea27bf..426e21a 100644 --- a/CHANGELOG.mkd +++ b/CHANGELOG.mkd @@ -34,6 +34,9 @@ Fixed: - Builds properly now on mingw (both cross-compile and native). +- Improved reliability on Windows when running an app twice without dis-connecting + the Wiimote (came as a part of the MotionPlus support.) + Changed: - Improved header includes using feedback from (include-what-you-use)[iwyu] From 446731114ca31580857479b4a7c74aacf9f6901e Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 14 Sep 2011 14:51:50 -0500 Subject: [PATCH 12/16] remove #if 0 code. --- src/events.c | 65 ---------------------------------------------------- 1 file changed, 65 deletions(-) diff --git a/src/events.c b/src/events.c index a5cdf31..9e287df 100644 --- a/src/events.c +++ b/src/events.c @@ -661,71 +661,6 @@ static void event_status(struct wiimote_t* wm, byte* msg) { } #endif - -#if 0 - switch(wm->expansion_state) - { - case 0: /* regular expansion detection */ - { - if(attachment && wm->exp.type != EXP_NONE) - { - wm->expansion_dattempts = 0; - wm->expansion_state++; - } - - else - { - /* give it another chance still */ - if(wm->expansion_dattempts < 10) - { - wm->expansion_dattempts++; - wiiuse_status(wm); - return; - } - - /* likely no attachment, give up and try M+ */ - else { - wm->expansion_dattempts = 0; - wiiuse_set_motion_plus(wm, WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP) ? 2 : 1); - wm->expansion_state++; - } - } - - break; - } - - case 1: /* try to init Motion+ */ - { - if(attachment && wm->exp.type != EXP_NONE) - { - wm->expansion_dattempts = 0; - wm->expansion_state++; - } - - else - { - /* give it another chance still */ - if(wm->expansion_dattempts < 10) - { - wm->expansion_dattempts++; - wiiuse_status(wm); - return; - } - - else { - /* give up and move on */ - wm->expansion_state++; - } - } - break; - } - - default: - break; - } - -#endif - /* * From now on the remote will only send status packets. * We need to send a WIIMOTE_CMD_REPORT_TYPE packet to From 3c9cfdf656506a6bb80618bcc71b92203b46ec16 Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Thu, 15 Sep 2011 01:29:43 +0200 Subject: [PATCH 13/16] Renamed variables to something more descriptive, removed unused code --- src/events.c | 86 ++++------------------------------------------- src/motion_plus.c | 48 +++++++++++++------------- src/wiiuse.h | 14 ++++---- 3 files changed, 37 insertions(+), 111 deletions(-) diff --git a/src/events.c b/src/events.c index 9e287df..61b1605 100644 --- a/src/events.c +++ b/src/events.c @@ -740,7 +740,6 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { byte buf = 0x00; byte* handshake_buf; -#ifndef OLD_EXP_HANDSHAKE switch(wm->expansion_state) { /* These two initialization writes disable the encryption */ case 0: @@ -807,79 +806,6 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { wiiuse_status(wm); break; } - -#else - if (!data) { - byte* handshake_buf; - byte buf = 0x00; - - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) - disable_expansion(wm); - - /* increase the timeout until the handshake completes */ - #ifdef WIIUSE_WIN32 - WIIUSE_DEBUG("Setting timeout to expansion %i ms.", wm->exp_timeout); - wm->timeout = wm->exp_timeout; - #endif - - wiiuse_write_data(wm, WM_EXP_MEM_ENABLE, &buf, 1); - - /* get the calibration data */ - handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); - wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); - - /* tell the wiimote to send expansion data */ - WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP); - - return; - } - - id = from_big_endian_uint32_t(data + 220); - - /* call the corresponding handshake function for this expansion */ - switch (id) { - case EXP_ID_CODE_NUNCHUK: - { - if (nunchuk_handshake(wm, &wm->exp.nunchuk, data, len)) - wm->event = WIIUSE_NUNCHUK_INSERTED; - break; - } - case EXP_ID_CODE_CLASSIC_CONTROLLER: - { - if (classic_ctrl_handshake(wm, &wm->exp.classic, data, len)) - wm->event = WIIUSE_CLASSIC_CTRL_INSERTED; - break; - } - case EXP_ID_CODE_GUITAR: - { - if (guitar_hero_3_handshake(wm, &wm->exp.gh3, data, len)) - wm->event = WIIUSE_GUITAR_HERO_3_CTRL_INSERTED; - break; - } - case EXP_ID_CODE_WII_BOARD: - { - if (wii_board_handshake(wm, &wm->exp.wb, data, len)) - wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED; - break; - } - - case EXP_ID_CODE_MOTION_PLUS: - case EXP_ID_CODE_MOTION_PLUS_CLASSIC: - case EXP_ID_CODE_MOTION_PLUS_NUNCHUK: - /* wiiuse_motion_plus_handshake(wm, data, len); */ - wm->event = WIIUSE_MOTION_PLUS_ACTIVATED; - break; - - - default: - { - WIIUSE_WARNING("Unknown expansion type. Code: 0x%x", id); - break; - } - } - - free(data); -#endif } @@ -984,9 +910,9 @@ static void save_state(struct wiimote_t* wm) { case EXP_MOTION_PLUS_CLASSIC: case EXP_MOTION_PLUS_NUNCHUK: { - wm->lstate.drx = wm->exp.mp.raw_gyro.p; - wm->lstate.dry = wm->exp.mp.raw_gyro.r; - wm->lstate.drz = wm->exp.mp.raw_gyro.y; + wm->lstate.drx = wm->exp.mp.raw_gyro.pitch; + wm->lstate.dry = wm->exp.mp.raw_gyro.roll; + wm->lstate.drz = wm->exp.mp.raw_gyro.yaw; if(wm->exp.type == EXP_MOTION_PLUS_CLASSIC) { @@ -1115,9 +1041,9 @@ static int state_changed(struct wiimote_t* wm) { case EXP_MOTION_PLUS_CLASSIC: case EXP_MOTION_PLUS_NUNCHUK: { - STATE_CHANGED(wm->lstate.drx, wm->exp.mp.raw_gyro.p); - STATE_CHANGED(wm->lstate.dry, wm->exp.mp.raw_gyro.r); - STATE_CHANGED(wm->lstate.drz, wm->exp.mp.raw_gyro.y); + STATE_CHANGED(wm->lstate.drx, wm->exp.mp.raw_gyro.pitch); + STATE_CHANGED(wm->lstate.dry, wm->exp.mp.raw_gyro.roll); + STATE_CHANGED(wm->lstate.drz, wm->exp.mp.raw_gyro.yaw); if(wm->exp.type == EXP_MOTION_PLUS_CLASSIC) { diff --git a/src/motion_plus.c b/src/motion_plus.c index 25b3ca7..d06ef75 100644 --- a/src/motion_plus.c +++ b/src/motion_plus.c @@ -88,9 +88,9 @@ void wiiuse_motion_plus_handshake(struct wiimote_t *wm,byte *data,unsigned short WIIUSE_DEBUG("Motion plus connected"); /* Init gyroscopes */ - wm->exp.mp.cal_gyro.r = 0; - wm->exp.mp.cal_gyro.p = 0; - wm->exp.mp.cal_gyro.y = 0; + wm->exp.mp.cal_gyro.roll = 0; + wm->exp.mp.cal_gyro.pitch = 0; + wm->exp.mp.cal_gyro.yaw = 0; wm->exp.mp.orient.roll = 0.0; wm->exp.mp.orient.pitch = 0.0; wm->exp.mp.orient.yaw = 0.0; @@ -162,20 +162,20 @@ void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) /* Check if the gyroscope is in fast or slow mode (0 if rotating fast, 1 if slow or still) */ mp->acc_mode = ((msg[4] & 0x2) << 1) | ((msg[3] & 0x1) << 1) | ((msg[3] & 0x2) >> 1); - mp->raw_gyro.r = ((msg[4] & 0xFC) << 6) | msg[1]; - mp->raw_gyro.p = ((msg[5] & 0xFC) << 6) | msg[2]; - mp->raw_gyro.y = ((msg[3] & 0xFC) << 6) | msg[0]; + mp->raw_gyro.roll = ((msg[4] & 0xFC) << 6) | msg[1]; + mp->raw_gyro.pitch = ((msg[5] & 0xFC) << 6) | msg[2]; + mp->raw_gyro.yaw = ((msg[3] & 0xFC) << 6) | msg[0]; /* First calibration */ - if ((mp->raw_gyro.r > 5000) && - (mp->raw_gyro.p > 5000) && - (mp->raw_gyro.y > 5000) && - (mp->raw_gyro.r < 0x3fff) && - (mp->raw_gyro.p < 0x3fff) && - (mp->raw_gyro.y < 0x3fff) && - !(mp->cal_gyro.r) && - !(mp->cal_gyro.p) && - !(mp->cal_gyro.y)) + if ((mp->raw_gyro.roll > 5000) && + (mp->raw_gyro.pitch > 5000) && + (mp->raw_gyro.yaw > 5000) && + (mp->raw_gyro.roll < 0x3fff) && + (mp->raw_gyro.pitch < 0x3fff) && + (mp->raw_gyro.yaw < 0x3fff) && + !(mp->cal_gyro.roll) && + !(mp->cal_gyro.pitch) && + !(mp->cal_gyro.yaw)) { wiiuse_calibrate_motion_plus(mp); } @@ -235,9 +235,9 @@ void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) */ void wiiuse_calibrate_motion_plus(struct motion_plus_t *mp) { - mp->cal_gyro.r = mp->raw_gyro.r; - mp->cal_gyro.p = mp->raw_gyro.p; - mp->cal_gyro.y = mp->raw_gyro.y; + mp->cal_gyro.roll = mp->raw_gyro.roll; + mp->cal_gyro.pitch = mp->raw_gyro.pitch; + mp->cal_gyro.yaw = mp->raw_gyro.yaw; mp->orient.roll = 0.0; mp->orient.pitch = 0.0; mp->orient.yaw = 0.0; @@ -249,9 +249,9 @@ static void calculate_gyro_rates(struct motion_plus_t* mp) float tmp_roll, tmp_pitch, tmp_yaw; /* We consider calibration data */ - tmp_r = mp->raw_gyro.r - mp->cal_gyro.r; - tmp_p = mp->raw_gyro.p - mp->cal_gyro.p; - tmp_y = mp->raw_gyro.y - mp->cal_gyro.y; + tmp_r = mp->raw_gyro.roll - mp->cal_gyro.roll; + tmp_p = mp->raw_gyro.pitch - mp->cal_gyro.pitch; + tmp_y = mp->raw_gyro.yaw - mp->cal_gyro.yaw; /* We convert to degree/sec according to fast/slow mode */ if (mp->acc_mode & 0x04) @@ -277,7 +277,7 @@ static void calculate_gyro_rates(struct motion_plus_t* mp) if (fabs(tmp_yaw) < 0.5) tmp_yaw = 0.0; - mp->angle_rate_gyro.r = tmp_roll; - mp->angle_rate_gyro.p = tmp_pitch; - mp->angle_rate_gyro.y = tmp_yaw; + mp->angle_rate_gyro.roll = tmp_roll; + mp->angle_rate_gyro.pitch = tmp_pitch; + mp->angle_rate_gyro.yaw = tmp_yaw; } diff --git a/src/wiiuse.h b/src/wiiuse.h index 87de021..93c1f75 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -326,18 +326,18 @@ struct read_req_t { /** * @struct ang3s_t - * @brief RPY short angles. + * @brief Roll/Pitch/Yaw short angles. */ typedef struct ang3s_t { - short r, p, y; + int16_t roll, pitch, yaw; } ang3s_t; /** * @struct ang3f_t - * @brief RPY float angles. + * @brief Roll/Pitch/Yaw float angles. */ typedef struct ang3f_t { - float r, p, y; + float roll, pitch, yaw; } ang3f_t; /** @@ -534,8 +534,8 @@ typedef struct guitar_hero_3_t { */ typedef struct motion_plus_t { - int ext_initialized; /* is the pass-through device initialized? */ - unsigned char ext; + int ext_initialized; /**< is the pass-through device initialized? */ + byte ext; /**< is there a device on the pass-through port? */ struct ang3s_t raw_gyro; /**< current raw gyroscope data */ struct ang3s_t cal_gyro; /**< calibration raw gyroscope data */ @@ -544,7 +544,7 @@ typedef struct motion_plus_t byte acc_mode; /**< Fast/slow rotation mode for roll, pitch and yaw (0 if rotating fast, 1 if slow or still) */ int raw_gyro_threshold; /**< threshold for gyroscopes to generate an event */ - struct nunchuk_t *nc; /* pointers to nunchuk & classic in pass-through-mode */ + struct nunchuk_t *nc; /**< pointers to nunchuk & classic in pass-through-mode */ struct classic_ctrl_t *classic; } motion_plus_t; From 1c5bb6cb333116d79dcfb40e961e87aaca92ae77 Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Thu, 15 Sep 2011 01:32:09 +0200 Subject: [PATCH 14/16] Removed unused variables --- src/motion_plus.c | 1 - src/wiiuse.c | 1 - src/wiiuse.h | 2 -- 3 files changed, 4 deletions(-) diff --git a/src/motion_plus.c b/src/motion_plus.c index d06ef75..ab52ffb 100644 --- a/src/motion_plus.c +++ b/src/motion_plus.c @@ -101,7 +101,6 @@ void wiiuse_motion_plus_handshake(struct wiimote_t *wm,byte *data,unsigned short wm->exp.nunchuk.flags = &wm->flags; wm->exp.mp.ext = 0; - wm->exp.mp.ext_initialized = 0; wiiuse_set_ir_mode(wm); wiiuse_set_report_type(wm); diff --git a/src/wiiuse.c b/src/wiiuse.c index f1189a6..0711cad 100644 --- a/src/wiiuse.c +++ b/src/wiiuse.c @@ -148,7 +148,6 @@ struct wiimote_t** wiiuse_init(int wiimotes) { wm[i]->exp.type = EXP_NONE; wm[i]->expansion_state = 0; - wm[i]->expansion_dattempts = 0; wiiuse_set_aspect_ratio(wm[i], WIIUSE_ASPECT_4_3); wiiuse_set_ir_position(wm[i], WIIUSE_IR_ABOVE); diff --git a/src/wiiuse.h b/src/wiiuse.h index 93c1f75..ce11f88 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -534,7 +534,6 @@ typedef struct guitar_hero_3_t { */ typedef struct motion_plus_t { - int ext_initialized; /**< is the pass-through device initialized? */ byte ext; /**< is there a device on the pass-through port? */ struct ang3s_t raw_gyro; /**< current raw gyroscope data */ @@ -715,7 +714,6 @@ typedef struct wiimote_t { WCONST byte handshake_state; /**< the state of the connection handshake */ WCONST byte expansion_state; /**< the state of the expansion handshake */ - WCONST byte expansion_dattempts; /**< how many times did we wait already */ WCONST struct data_req_t* data_req; /**< list of data read requests */ WCONST struct read_req_t* read_req; /**< list of data read requests */ From 5be24736b088b85d50e4b42d8602ed73cbea4f5b Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Thu, 15 Sep 2011 02:30:09 +0200 Subject: [PATCH 15/16] Added section on Motion+ in known issues --- README.mkd | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.mkd b/README.mkd index 5a50fd0..767c693 100644 --- a/README.mkd +++ b/README.mkd @@ -172,6 +172,12 @@ Known Issues On Windows using more than one wiimote (usually more than two wiimotes) may cause significant latency. +If you are going to use Motion+, make sure to call wiiuse_poll or wiiuse_update +in a loop for some 10-15 seconds before enabling it. Ideally you should be checking +the status of any expansion (nunchuk) you may have connected as well. +Otherwise the extra expansion may not initialize correctly - the initialization +and calibration takes some time. + Acknowledgements by Michael Laforest ------------------------------------ From 97768bf63c65646ce13676db5a5eacd2cb4e0f0e Mon Sep 17 00:00:00 2001 From: Jan Ciger Date: Thu, 15 Sep 2011 02:31:49 +0200 Subject: [PATCH 16/16] Added documentation --- src/motion_plus.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/motion_plus.c b/src/motion_plus.c index ab52ffb..27d0d0e 100644 --- a/src/motion_plus.c +++ b/src/motion_plus.c @@ -122,6 +122,13 @@ static void wiiuse_set_motion_plus_clear1(struct wiimote_t *wm,byte *data,unsign wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &val, 1, wiiuse_set_motion_plus_clear2); } +/** + * @brief Enable/disable Motion+ expansion + * + * @param wm Pointer to the wiimote with Motion+ + * @param status 0 - off, 1 - on, standalone, 2 - nunchuk pass-through + * + */ void wiiuse_set_motion_plus(struct wiimote_t *wm, int status) { byte val;