diff --git a/src/classic.c b/src/classic.c index a87d1ef..eea8ffc 100644 --- a/src/classic.c +++ b/src/classic.c @@ -77,8 +77,9 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); return 0; - } else + } else { data += 16; + } } @@ -100,9 +101,9 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte /* handshake done */ wm->exp.type = EXP_CLASSIC; - #ifdef WIIUSE_WIN32 +#ifdef WIIUSE_WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; - #endif +#endif return 1; } diff --git a/src/classic.h b/src/classic.h index 7fe7b30..e54337a 100644 --- a/src/classic.h +++ b/src/classic.h @@ -40,14 +40,14 @@ extern "C" { #endif -/** @defgroup internal_classic Internal: Classic Controller */ -/** @{ */ -int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len); + /** @defgroup internal_classic Internal: Classic Controller */ + /** @{ */ + int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len); -void classic_ctrl_disconnected(struct classic_ctrl_t* cc); + void classic_ctrl_disconnected(struct classic_ctrl_t* cc); -void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg); -/** @} */ + void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg); + /** @} */ #ifdef __cplusplus } diff --git a/src/dynamics.c b/src/dynamics.c index d6ffbf7..76ac202 100644 --- a/src/dynamics.c +++ b/src/dynamics.c @@ -77,12 +77,21 @@ void calculate_orientation(struct accel_t* ac, struct vec3b_t* accel, struct ori z = ((float)accel->z - (float)ac->cal_zero.z) / zg; /* make sure x,y,z are between -1 and 1 for the tan functions */ - if (x < -1.0f) x = -1.0f; - else if (x > 1.0f) x = 1.0f; - if (y < -1.0f) y = -1.0f; - else if (y > 1.0f) y = 1.0f; - if (z < -1.0f) z = -1.0f; - else if (z > 1.0f) z = 1.0f; + if (x < -1.0f) { + x = -1.0f; + } else if (x > 1.0f) { + x = 1.0f; + } + if (y < -1.0f) { + y = -1.0f; + } else if (y > 1.0f) { + y = 1.0f; + } + if (z < -1.0f) { + z = -1.0f; + } else if (z > 1.0f) { + z = 1.0f; + } /* if it is over 1g then it is probably accelerating and not reliable */ if (abs(accel->x - ac->cal_zero.x) <= ac->cal_g.x) { @@ -180,39 +189,39 @@ 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) { switch (type) { - case SMOOTH_ROLL: - { - /* it's possible last iteration was nan or inf, so set it to 0 if that happened */ - if (isnan(ac->st_roll) || isinf(ac->st_roll)) - ac->st_roll = 0.0f; + case SMOOTH_ROLL: { + /* it's possible last iteration was nan or inf, so set it to 0 if that happened */ + if (isnan(ac->st_roll) || isinf(ac->st_roll)) { + ac->st_roll = 0.0f; + } - /* - * If the sign changes (which will happen if going from -180 to 180) - * or from (-1 to 1) then don't smooth, just use the new angle. - */ - if (((ac->st_roll < 0) && (orient->roll > 0)) || ((ac->st_roll > 0) && (orient->roll < 0))) { - ac->st_roll = orient->roll; - } else { - orient->roll = ac->st_roll + (ac->st_alpha * (orient->a_roll - ac->st_roll)); - ac->st_roll = orient->roll; + /* + * If the sign changes (which will happen if going from -180 to 180) + * or from (-1 to 1) then don't smooth, just use the new angle. + */ + if (((ac->st_roll < 0) && (orient->roll > 0)) || ((ac->st_roll > 0) && (orient->roll < 0))) { + ac->st_roll = orient->roll; + } else { + orient->roll = ac->st_roll + (ac->st_alpha * (orient->a_roll - ac->st_roll)); + ac->st_roll = orient->roll; + } + + return; } - return; - } + case SMOOTH_PITCH: { + if (isnan(ac->st_pitch) || isinf(ac->st_pitch)) { + ac->st_pitch = 0.0f; + } - case SMOOTH_PITCH: - { - if (isnan(ac->st_pitch) || isinf(ac->st_pitch)) - ac->st_pitch = 0.0f; + if (((ac->st_pitch < 0) && (orient->pitch > 0)) || ((ac->st_pitch > 0) && (orient->pitch < 0))) { + ac->st_pitch = orient->pitch; + } else { + orient->pitch = ac->st_pitch + (ac->st_alpha * (orient->a_pitch - ac->st_pitch)); + ac->st_pitch = orient->pitch; + } - if (((ac->st_pitch < 0) && (orient->pitch > 0)) || ((ac->st_pitch > 0) && (orient->pitch < 0))) { - ac->st_pitch = orient->pitch; - } else { - orient->pitch = ac->st_pitch + (ac->st_alpha * (orient->a_pitch - ac->st_pitch)); - ac->st_pitch = orient->pitch; + return; } - - return; - } } } diff --git a/src/dynamics.h b/src/dynamics.h index 9722b4d..5ef14e0 100644 --- a/src/dynamics.h +++ b/src/dynamics.h @@ -44,14 +44,14 @@ extern "C" { #endif -/** @defgroup internal_dynamics Internal: Dynamics Functions */ -/** @{ */ + /** @defgroup internal_dynamics Internal: Dynamics Functions */ + /** @{ */ -void calculate_orientation(struct accel_t* ac, struct vec3b_t* accel, struct orient_t* orient, int smooth); -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 calculate_orientation(struct accel_t* ac, struct vec3b_t* accel, struct orient_t* orient, int smooth); + 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); + /** @} */ #ifdef __cplusplus } diff --git a/src/events.c b/src/events.c index ac398f5..cc7b2c1 100644 --- a/src/events.c +++ b/src/events.c @@ -99,7 +99,7 @@ int wiiuse_update(struct wiimote_t** wiimotes, int nwiimotes, wiiuse_update_cb c s.event = wiimotes[i]->event; s.state = wiimotes[i]->state; s.expansion = wiimotes[i]->exp; - callback( &s ); + callback(&s); evnt++; break; } @@ -184,108 +184,98 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { save_state(wm); switch (event) { - case WM_RPT_BTN: - { - /* button */ - wiiuse_pressed_buttons(wm, msg); - break; - } - case WM_RPT_BTN_ACC: - { - /* button - motion */ - wiiuse_pressed_buttons(wm, msg); + case WM_RPT_BTN: { + /* button */ + wiiuse_pressed_buttons(wm, msg); + break; + } + case WM_RPT_BTN_ACC: { + /* button - motion */ + wiiuse_pressed_buttons(wm, msg); - handle_wm_accel(wm, msg); + handle_wm_accel(wm, msg); - break; - } - case WM_RPT_READ: - { - /* data read */ - event_data_read(wm, msg); + break; + } + case WM_RPT_READ: { + /* data read */ + event_data_read(wm, msg); - /* yeah buttons may be pressed, but this wasn't an "event" */ - return; - } - case WM_RPT_CTRL_STATUS: - { - /* controller status */ - event_status(wm, msg); + /* yeah buttons may be pressed, but this wasn't an "event" */ + return; + } + case WM_RPT_CTRL_STATUS: { + /* controller status */ + event_status(wm, msg); - /* don't execute the event callback */ - return; - } - case WM_RPT_BTN_EXP: - { - /* button - expansion */ - wiiuse_pressed_buttons(wm, msg); - handle_expansion(wm, msg+2); + /* don't execute the event callback */ + return; + } + case WM_RPT_BTN_EXP: { + /* button - expansion */ + wiiuse_pressed_buttons(wm, msg); + handle_expansion(wm, msg + 2); - break; - } - case WM_RPT_BTN_ACC_EXP: - { - /* button - motion - expansion */ - wiiuse_pressed_buttons(wm, msg); + break; + } + case WM_RPT_BTN_ACC_EXP: { + /* button - motion - expansion */ + wiiuse_pressed_buttons(wm, msg); - handle_wm_accel(wm, msg); + handle_wm_accel(wm, msg); - handle_expansion(wm, msg+5); + handle_expansion(wm, msg + 5); - break; - } - case WM_RPT_BTN_ACC_IR: - { - /* button - motion - ir */ - wiiuse_pressed_buttons(wm, msg); + break; + } + case WM_RPT_BTN_ACC_IR: { + /* button - motion - ir */ + wiiuse_pressed_buttons(wm, msg); - handle_wm_accel(wm, msg); + handle_wm_accel(wm, msg); - /* ir */ - calculate_extended_ir(wm, msg+5); + /* ir */ + calculate_extended_ir(wm, msg + 5); - break; - } - case WM_RPT_BTN_IR_EXP: - { - /* button - ir - expansion */ - wiiuse_pressed_buttons(wm, msg); - handle_expansion(wm, msg+12); + break; + } + case WM_RPT_BTN_IR_EXP: { + /* button - ir - expansion */ + wiiuse_pressed_buttons(wm, msg); + handle_expansion(wm, msg + 12); - /* ir */ - calculate_basic_ir(wm, msg+2); + /* ir */ + calculate_basic_ir(wm, msg + 2); - break; - } - case WM_RPT_BTN_ACC_IR_EXP: - { - /* button - motion - ir - expansion */ - wiiuse_pressed_buttons(wm, msg); + break; + } + case WM_RPT_BTN_ACC_IR_EXP: { + /* button - motion - ir - expansion */ + wiiuse_pressed_buttons(wm, msg); - handle_wm_accel(wm, msg); + handle_wm_accel(wm, msg); - handle_expansion(wm, msg+15); + handle_expansion(wm, msg + 15); - /* ir */ - calculate_basic_ir(wm, msg+5); + /* ir */ + calculate_basic_ir(wm, msg + 5); - break; - } - case WM_RPT_WRITE: - { - event_data_write(wm,msg); - break; - } - default: - { - WIIUSE_WARNING("Unknown event, can not handle it [Code 0x%x].", event); - return; - } + break; + } + case WM_RPT_WRITE: { + event_data_write(wm, msg); + break; + } + default: { + WIIUSE_WARNING("Unknown event, can not handle it [Code 0x%x].", event); + return; + } } /* was there an event? */ - if (state_changed(wm)) + if (state_changed(wm)) { wm->event = WIIUSE_EVENT; + } } @@ -334,8 +324,9 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) { wiiuse_pressed_buttons(wm, msg); /* find the next non-dirty request */ - while (req && req->dirty) + while (req && req->dirty) { req = req->next; + } /* if we don't have a request out then we didn't ask for this packet */ if (!req) { @@ -345,12 +336,13 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) { err = msg[2] & 0x0F; - if (err == 0x08) + if (err == 0x08) { WIIUSE_WARNING("Unable to read data - address does not exist."); - else if (err == 0x07) + } else if (err == 0x07) { WIIUSE_WARNING("Unable to read data - address is for write-only registers."); - else if (err) + } else if (err) { WIIUSE_WARNING("Unable to read data - unknown error code %x.", err); + } if (err) { /* this request errored out, so skip it and go to the next one */ @@ -360,8 +352,9 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) { free(req); /* if another request exists send it to the wiimote */ - if (wm->read_req) + if (wm->read_req) { wiiuse_send_next_pending_read_request(wm); + } return; } @@ -373,7 +366,9 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) { req->wait -= len; if (req->wait >= req->size) /* this should never happen */ + { req->wait = 0; + } WIIUSE_DEBUG("Received read packet:"); WIIUSE_DEBUG(" Packet read offset: %i bytes", offset); @@ -385,15 +380,16 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) { /* reconstruct this part of the data */ memcpy((req->buf + offset - req->addr), (msg + 5), len); - #ifdef WITH_WIIUSE_DEBUG +#ifdef WITH_WIIUSE_DEBUG { int i = 0; printf("Read: "); - for (; i < req->size - req->wait; ++i) + for (; i < req->size - req->wait; ++i) { printf("%x ", req->buf[i]); + } printf("\n"); } - #endif +#endif /* if all data has been received, execute the read event callback or generate event */ if (!req->wait) { @@ -417,25 +413,25 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) { } /* if another request exists send it to the wiimote */ - if (wm->read_req) + if (wm->read_req) { wiiuse_send_next_pending_read_request(wm); + } } } -static void event_data_write(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); + 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)) { + if (!(req->state == REQ_SENT)) { WIIUSE_WARNING("Transmission is not necessary"); /* delete this request */ wm->data_req = req->next; @@ -446,23 +442,23 @@ static void event_data_write(struct wiimote_t *wm, byte *msg) req->state = REQ_DONE; - if(req->cb) { + if (req->cb) { /* this was a callback, so invoke it now */ - req->cb(wm,NULL,0); + 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; + /* + * 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); @@ -485,8 +481,9 @@ static void event_status(struct wiimote_t* wm, byte* msg) { struct data_req_t* req = wm->data_req; /* initial handshake is not finished yet, ignore this */ - if(WIIMOTE_IS_SET(wm, WIIMOTE_STATE_HANDSHAKE)) + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_HANDSHAKE)) { return; + } /* * An event occurred. @@ -498,29 +495,39 @@ static void event_status(struct wiimote_t* wm, byte* msg) { wiiuse_pressed_buttons(wm, msg); /* find what LEDs are lit */ - if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_1) led[0] = 1; - if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_2) led[1] = 1; - if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_3) led[2] = 1; - if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_4) led[3] = 1; + if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_1) { + led[0] = 1; + } + if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_2) { + led[1] = 1; + } + if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_3) { + led[2] = 1; + } + if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_4) { + led[3] = 1; + } /* probe for Motion+ */ - if(!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_MPLUS_PRESENT)) + if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_MPLUS_PRESENT)) { wiiuse_probe_motion_plus(wm); + } /* is an attachment connected to the expansion port? */ - if ((msg[2] & WM_CTRL_STATUS_BYTE1_ATTACHMENT) == WM_CTRL_STATUS_BYTE1_ATTACHMENT) - { + if ((msg[2] & WM_CTRL_STATUS_BYTE1_ATTACHMENT) == WM_CTRL_STATUS_BYTE1_ATTACHMENT) { WIIUSE_DEBUG("Attachment detected!"); attachment = 1; } /* is the speaker enabled? */ - if ((msg[2] & WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED) == WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED) + if ((msg[2] & WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED) == WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED) { WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_SPEAKER); + } /* is IR sensing enabled? */ - if ((msg[2] & WM_CTRL_STATUS_BYTE1_IR_ENABLED) == WM_CTRL_STATUS_BYTE1_IR_ENABLED) + if ((msg[2] & WM_CTRL_STATUS_BYTE1_IR_ENABLED) == WM_CTRL_STATUS_BYTE1_IR_ENABLED) { ir = 1; + } /* find the battery level and normalize between 0 and 1 */ wm->battery_level = (msg[5] / (float)WM_MAX_BATTERY_CODE); @@ -536,20 +543,19 @@ static void event_status(struct wiimote_t* wm, byte* msg) { exp_changed = 1; } - #ifdef WIIUSE_WIN32 +#ifdef WIIUSE_WIN32 if (!attachment) { WIIUSE_DEBUG("Setting timeout to normal %i ms.", wm->normal_timeout); wm->timeout = wm->normal_timeout; } - #endif +#endif /* * From now on the remote will only send status packets. * 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)) - { + 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. @@ -562,8 +568,12 @@ static void event_status(struct wiimote_t* wm, byte* msg) { } /* handling new Tx for changed exp */ - if(!req) return; - if(!(req->state==REQ_SENT)) return; + 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); */ @@ -623,8 +633,8 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { int gotIt = 0; WIIUSE_DEBUG("handshake_expansion with state %d", wm->expansion_state); - switch(wm->expansion_state) { - /* These two initialization writes disable the encryption */ + switch (wm->expansion_state) { + /* These two initialization writes disable the encryption */ case 0: wm->expansion_state = 1; /* increase the timeout until the handshake completes */ @@ -648,22 +658,23 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { case 2: wm->expansion_state = 3; /* get the calibration data */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) { disable_expansion(wm); + } handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); /* tell the wiimote to send expansion data */ WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP); wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); break; case 3: - if(!data || !len) { + if (!data || !len) { WIIUSE_DEBUG("no handshake data received from expansion"); disable_expansion(wm); return; } wm->expansion_state = 0; id = from_big_endian_uint32_t(data + 220); - switch(id) { + switch (id) { case EXP_ID_CODE_NUNCHUK: if (nunchuk_handshake(wm, &wm->exp.nunchuk, data, len)) { wm->event = WIIUSE_NUNCHUK_INSERTED; @@ -693,12 +704,12 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { gotIt = 1; break; - case EXP_ID_CODE_WII_BOARD: - if(wii_board_handshake(wm, &wm->exp.wb, data, len)) { + case EXP_ID_CODE_WII_BOARD: + if (wii_board_handshake(wm, &wm->exp.wb, data, len)) { wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED; gotIt = 1; } - break; + break; default: WIIUSE_WARNING("Unknown expansion type. Code: 0x%x", id); @@ -706,8 +717,8 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { } free(data); if (gotIt) { - WIIMOTE_DISABLE_STATE(wm,WIIMOTE_STATE_EXP_HANDSHAKE); - WIIMOTE_ENABLE_STATE(wm,WIIMOTE_STATE_EXP); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP); } else { WIIUSE_WARNING("Could not handshake with expansion id: 0x%x", id); } @@ -731,8 +742,9 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { */ void disable_expansion(struct wiimote_t* wm) { WIIUSE_DEBUG("Disabling expansion"); - if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) + if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) { return; + } /* tell the associated module the expansion was removed */ switch (wm->exp.type) { @@ -819,31 +831,29 @@ static void save_state(struct wiimote_t* wm) { case EXP_MOTION_PLUS: case EXP_MOTION_PLUS_CLASSIC: - case EXP_MOTION_PLUS_NUNCHUK: - { - 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; + case EXP_MOTION_PLUS_NUNCHUK: { + 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) - { - 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; + 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; } - break; - } - case EXP_NONE: break; } @@ -856,9 +866,9 @@ static void save_state(struct wiimote_t* wm) { * @return 1 if a significant change occurred, 0 if not. */ static int state_changed(struct wiimote_t* wm) { - #define STATE_CHANGED(a, b) if (a != b) return 1 +#define STATE_CHANGED(a, b) if (a != b) return 1 - #define CROSS_THRESH(last, now, thresh) \ +#define CROSS_THRESH(last, now, thresh) \ do { \ if (WIIMOTE_IS_FLAG_SET(wm, WIIUSE_ORIENT_THRESH)) { \ if ((diff_f(last.roll, now.roll) >= thresh) || \ @@ -875,7 +885,7 @@ static int state_changed(struct wiimote_t* wm) { } \ } while (0) - #define CROSS_THRESH_XYZ(last, now, thresh) \ +#define CROSS_THRESH_XYZ(last, now, thresh) \ do { \ if (WIIMOTE_IS_FLAG_SET(wm, WIIUSE_ORIENT_THRESH)) { \ if ((diff_f(last.x, now.x) >= thresh) || \ @@ -910,54 +920,16 @@ static int state_changed(struct wiimote_t* wm) { /* expansion */ switch (wm->exp.type) { - case EXP_NUNCHUK: - { - 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); + case EXP_NUNCHUK: { + 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_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); - break; - } - case EXP_GUITAR_HERO_3: - { - STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.gh3.js.ang); - STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.gh3.js.mag); - STATE_CHANGED(wm->lstate.exp_r_shoulder, wm->exp.gh3.whammy_bar); - STATE_CHANGED(wm->lstate.exp_btns, wm->exp.gh3.btns); - break; - } - case EXP_WII_BOARD: - { - STATE_CHANGED(wm->lstate.exp_wb_rtr,wm->exp.wb.tr); - STATE_CHANGED(wm->lstate.exp_wb_rtl,wm->exp.wb.tl); - STATE_CHANGED(wm->lstate.exp_wb_rbr,wm->exp.wb.br); - 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.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) - { + 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_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); @@ -965,21 +937,52 @@ static int state_changed(struct wiimote_t* wm) { 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_GUITAR_HERO_3: { + STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.gh3.js.ang); + STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.gh3.js.mag); + STATE_CHANGED(wm->lstate.exp_r_shoulder, wm->exp.gh3.whammy_bar); + STATE_CHANGED(wm->lstate.exp_btns, wm->exp.gh3.btns); + break; + } + case EXP_WII_BOARD: { + STATE_CHANGED(wm->lstate.exp_wb_rtr, wm->exp.wb.tr); + STATE_CHANGED(wm->lstate.exp_wb_rtl, wm->exp.wb.tl); + STATE_CHANGED(wm->lstate.exp_wb_rbr, wm->exp.wb.br); + STATE_CHANGED(wm->lstate.exp_wb_rbl, wm->exp.wb.bl); + break; } - break; - } - case EXP_NONE: - { - 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.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) { + 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; + } } STATE_CHANGED(wm->lstate.btns, wm->btns); diff --git a/src/guitar_hero_3.c b/src/guitar_hero_3.c index 296cb43..5bdc58a 100644 --- a/src/guitar_hero_3.c +++ b/src/guitar_hero_3.c @@ -86,8 +86,9 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); return 0; - } else + } else { data += 16; + } } /* joystick stuff */ @@ -101,9 +102,9 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b /* handshake done */ wm->exp.type = EXP_GUITAR_HERO_3; - #ifdef WIIUSE_WIN32 +#ifdef WIIUSE_WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; - #endif +#endif return 1; } diff --git a/src/guitar_hero_3.h b/src/guitar_hero_3.h index e9d3e95..b8ffa07 100644 --- a/src/guitar_hero_3.h +++ b/src/guitar_hero_3.h @@ -50,14 +50,14 @@ extern "C" { #endif -/** @defgroup internal_gh3 Internal: Guitar Hero 3 controller */ -/** @{ */ -int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, byte* data, unsigned short len); + /** @defgroup internal_gh3 Internal: Guitar Hero 3 controller */ + /** @{ */ + int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, byte* data, unsigned short len); -void guitar_hero_3_disconnected(struct guitar_hero_3_t* gh3); + void guitar_hero_3_disconnected(struct guitar_hero_3_t* gh3); -void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg); -/** @} */ + void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg); + /** @} */ #ifdef __cplusplus } diff --git a/src/io.c b/src/io.c index 82e9af7..e94064f 100644 --- a/src/io.c +++ b/src/io.c @@ -62,7 +62,7 @@ * This function is declared in wiiuse.h */ int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout) { - return wiiuse_os_find(wm, max_wiimotes, timeout); + return wiiuse_os_find(wm, max_wiimotes, timeout); } /** @@ -87,7 +87,7 @@ int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout) { * This function is declared in wiiuse.h */ int wiiuse_connect(struct wiimote_t** wm, int wiimotes) { - return wiiuse_os_connect(wm, wiimotes); + return wiiuse_os_connect(wm, wiimotes); } /** @@ -106,10 +106,10 @@ int wiiuse_connect(struct wiimote_t** wm, int wiimotes) { * This function is declared in wiiuse.h */ void wiiuse_disconnect(struct wiimote_t* wm) { - wiiuse_os_disconnect(wm); + wiiuse_os_disconnect(wm); } - /** +/** * @brief Wait until specified report arrives and return it * * @param wm Pointer to a wiimote_t structure. @@ -120,12 +120,10 @@ void wiiuse_disconnect(struct wiimote_t* wm) { * report from the Wiimote. * */ -void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength) -{ - for(;;) - { - if(wiiuse_os_read(wm, buffer, bufferLength) > 0) { - if(buffer[0] == report) { +void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength) { + for (;;) { + if (wiiuse_os_read(wm, buffer, bufferLength) > 0) { + if (buffer[0] == report) { break; } else { WIIUSE_WARNING("(id %i) dropping report 0x%x, waiting for 0x%x", wm->unid, buffer[0], report); @@ -147,8 +145,7 @@ void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int buff * amount of data from the Wiimote. * */ -void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data) -{ +void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data) { byte pkt[6]; byte buf[MAX_PAYLOAD]; unsigned n_full_reports; @@ -164,10 +161,10 @@ void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, uns /* read from registers or memory */ pkt[0] = (memory != 0) ? 0x00 : 0x04; - + /* length in big endian */ to_big_endian_uint16_t(pkt + 4, size); - + /* send */ wiiuse_send(wm, WM_CMD_READ_DATA, pkt, sizeof(pkt)); @@ -176,16 +173,14 @@ void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, uns last_report = size % 16; output = data; - for(i = 0; i < n_full_reports; ++i) - { + for (i = 0; i < n_full_reports; ++i) { wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD); memmove(output, buf + 6, 16); output += 16; } /* read the last incomplete packet */ - if(last_report) - { + if (last_report) { wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD); memmove(output, buf + 6, last_report); } @@ -207,8 +202,7 @@ void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, uns #ifdef WIIUSE_SYNC_HANDSHAKE -void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) -{ +void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { /* send request to wiimote for accelerometer calibration */ byte buf[MAX_PAYLOAD]; @@ -254,118 +248,113 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); /* now enable IR if it was set before the handshake completed */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) - { - WIIUSE_DEBUG("Handshake finished, enabling IR."); - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); - wiiuse_set_ir(wm, 1); - } + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { + WIIUSE_DEBUG("Handshake finished, enabling IR."); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); + wiiuse_set_ir(wm, 1); + } - WIIUSE_DEBUG("Asking for status ...\n"); - wm->event = WIIUSE_CONNECT; - wiiuse_status(wm); + WIIUSE_DEBUG("Asking for status ...\n"); + wm->event = WIIUSE_CONNECT; + wiiuse_status(wm); } } #else -static void wiiuse_disable_motion_plus1(struct wiimote_t *wm,byte *data,unsigned short len); -static void wiiuse_disable_motion_plus2(struct wiimote_t *wm,byte *data,unsigned short len); +static void wiiuse_disable_motion_plus1(struct wiimote_t *wm, byte *data, unsigned short len); +static void wiiuse_disable_motion_plus2(struct wiimote_t *wm, byte *data, unsigned short len); void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) { - if (!wm) return; + if (!wm) { + return; + } switch (wm->handshake_state) { - case 0: - { - byte* buf; + case 0: { + byte* buf; - /* continous reporting off, report to buttons only */ - WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); - wiiuse_set_leds(wm, WIIMOTE_LED_NONE); + /* continous reporting off, report to buttons only */ + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); + wiiuse_set_leds(wm, WIIMOTE_LED_NONE); - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_ACC); - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_RUMBLE); - WIIMOTE_DISABLE_FLAG(wm, WIIUSE_CONTINUOUS); - - wiiuse_set_report_type(wm); - - /* send request to wiimote for accelerometer calibration */ - buf = (byte*)malloc(sizeof(byte) * 8); - wiiuse_read_data_cb(wm, wiiuse_handshake, buf, WM_MEM_OFFSET_CALIBRATION, 7); - wm->handshake_state++; - - wiiuse_set_leds(wm, WIIMOTE_LED_NONE); - - break; - } - - case 1: - { - 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]; - accel->cal_zero.y = req->buf[1]; - accel->cal_zero.z = req->buf[2]; - - accel->cal_g.x = req->buf[4] - accel->cal_zero.x; - accel->cal_g.y = req->buf[5] - accel->cal_zero.y; - accel->cal_g.z = req->buf[6] - accel->cal_zero.z; - - /* done with the buffer */ - free(req->buf); - - /* handshake is done */ - WIIUSE_DEBUG("Handshake finished. Calibration: Idle: X=%x Y=%x Z=%x\t+1g: X=%x Y=%x Z=%x", - accel->cal_zero.x, accel->cal_zero.y, accel->cal_zero.z, - accel->cal_g.x, accel->cal_g.y, accel->cal_g.z); - - /* M+ off */ - 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 */ - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); - WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE); - wm->handshake_state++; - - /* now enable IR if it was set before the handshake completed */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { - WIIUSE_DEBUG("Handshake finished, enabling IR."); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_ACC); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); - wiiuse_set_ir(wm, 1); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_RUMBLE); + WIIMOTE_DISABLE_FLAG(wm, WIIUSE_CONTINUOUS); + + wiiuse_set_report_type(wm); + + /* send request to wiimote for accelerometer calibration */ + buf = (byte*)malloc(sizeof(byte) * 8); + wiiuse_read_data_cb(wm, wiiuse_handshake, buf, WM_MEM_OFFSET_CALIBRATION, 7); + wm->handshake_state++; + + wiiuse_set_leds(wm, WIIMOTE_LED_NONE); + + break; } - wm->event = WIIUSE_CONNECT; - wiiuse_status(wm); + case 1: { + struct read_req_t* req = wm->read_req; + struct accel_t* accel = &wm->accel_calib; + byte val; - break; - } + /* received read data */ + accel->cal_zero.x = req->buf[0]; + accel->cal_zero.y = req->buf[1]; + accel->cal_zero.z = req->buf[2]; - default: - { - break; - } + accel->cal_g.x = req->buf[4] - accel->cal_zero.x; + accel->cal_g.y = req->buf[5] - accel->cal_zero.y; + accel->cal_g.z = req->buf[6] - accel->cal_zero.z; + + /* done with the buffer */ + free(req->buf); + + /* handshake is done */ + WIIUSE_DEBUG("Handshake finished. Calibration: Idle: X=%x Y=%x Z=%x\t+1g: X=%x Y=%x Z=%x", + accel->cal_zero.x, accel->cal_zero.y, accel->cal_zero.z, + accel->cal_g.x, accel->cal_g.y, accel->cal_g.z); + + /* M+ off */ + 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 */ + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE); + wm->handshake_state++; + + /* now enable IR if it was set before the handshake completed */ + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { + WIIUSE_DEBUG("Handshake finished, enabling IR."); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); + wiiuse_set_ir(wm, 1); + } + + wm->event = WIIUSE_CONNECT; + wiiuse_status(wm); + + break; + } + + default: { + break; + } } } -static void wiiuse_disable_motion_plus1(struct wiimote_t *wm,byte *data,unsigned short len) -{ +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); } -static void wiiuse_disable_motion_plus2(struct wiimote_t *wm,byte *data,unsigned short len) -{ +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); diff --git a/src/io.h b/src/io.h index 9dd1da8..0149cb0 100644 --- a/src/io.h +++ b/src/io.h @@ -40,13 +40,13 @@ extern "C" { #endif -/** @defgroup internal_io Internal: Device I/O */ -/** @{ */ -void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len); + /** @defgroup internal_io Internal: Device I/O */ + /** @{ */ + void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len); -void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength); -void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data); -/** @} */ + void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength); + void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data); + /** @} */ #ifdef __cplusplus } diff --git a/src/ir.c b/src/ir.c index f7e833a..25137fb 100644 --- a/src/ir.c +++ b/src/ir.c @@ -57,16 +57,22 @@ static const byte WM_IR_BLOCK2_LEVEL4[] = "\x35\x03"; static const byte WM_IR_BLOCK1_LEVEL5[] = "\x07\x00\x00\x71\x01\x00\x72\x00\x20"; static const byte WM_IR_BLOCK2_LEVEL5[] = "\x1f\x03"; -void wiiuse_set_ir_mode(struct wiimote_t *wm) -{ +void wiiuse_set_ir_mode(struct wiimote_t *wm) { byte buf = 0x00; - if(!wm) return; - if(!WIIMOTE_IS_SET(wm,WIIMOTE_STATE_IR)) return; + if (!wm) { + return; + } + if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { + return; + } - if(WIIMOTE_IS_SET(wm,WIIMOTE_STATE_EXP)) buf = WM_IR_TYPE_BASIC; - else buf = WM_IR_TYPE_EXTENDED; - wiiuse_write_data(wm,WM_REG_IR_MODENUM, &buf, 1); + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) { + buf = WM_IR_TYPE_BASIC; + } else { + buf = WM_IR_TYPE_EXTENDED; + } + wiiuse_write_data(wm, WM_REG_IR_MODENUM, &buf, 1); } /** * @brief Set if the wiimote should track IR targets. @@ -80,8 +86,9 @@ void wiiuse_set_ir(struct wiimote_t* wm, int status) { const byte* block2 = NULL; int ir_level; - if (!wm) + if (!wm) { return; + } /* * Wait for the handshake to finish first. @@ -90,7 +97,7 @@ void wiiuse_set_ir(struct wiimote_t* wm, int status) { * again to actually enable IR. */ if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE)) { - if(status) { + if (status) { WIIUSE_DEBUG("Tried to enable IR, will wait until handshake finishes."); WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_IR); } /* else ignoring request to turn off, since it's turned off by default */ @@ -108,13 +115,15 @@ void wiiuse_set_ir(struct wiimote_t* wm, int status) { if (status) { /* if already enabled then stop */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { return; + } WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_IR); } else { /* if already disabled then stop */ - if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) + if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { return; + } WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); } @@ -141,10 +150,11 @@ void wiiuse_set_ir(struct wiimote_t* wm, int status) { wiiuse_write_data(wm, WM_REG_IR_BLOCK2, (byte*)block2, 2); /* set the IR mode */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) { buf = WM_IR_TYPE_BASIC; - else + } else { buf = WM_IR_TYPE_EXTENDED; + } wiiuse_write_data(wm, WM_REG_IR_MODENUM, &buf, 1); wiiuse_millisleep(50); @@ -201,10 +211,12 @@ static int get_ir_sens(struct wiimote_t* wm, const byte** block1, const byte** b * @param status 1 to enable, 0 to disable. */ void wiiuse_set_ir_vres(struct wiimote_t* wm, unsigned int x, unsigned int y) { - if (!wm) return; + if (!wm) { + return; + } - wm->ir.vres[0] = (x-1); - wm->ir.vres[1] = (y-1); + wm->ir.vres[0] = (x - 1); + wm->ir.vres[1] = (y - 1); } @@ -214,7 +226,9 @@ void wiiuse_set_ir_vres(struct wiimote_t* wm, unsigned int x, unsigned int y) { * @param wm Pointer to a wiimote_t structure. */ void wiiuse_set_ir_position(struct wiimote_t* wm, enum ir_position_t pos) { - if (!wm) return; + if (!wm) { + return; + } wm->ir.pos = pos; @@ -223,20 +237,22 @@ void wiiuse_set_ir_position(struct wiimote_t* wm, enum ir_position_t pos) { case WIIUSE_IR_ABOVE: wm->ir.offset[0] = 0; - if (wm->ir.aspect == WIIUSE_ASPECT_16_9) - wm->ir.offset[1] = WM_ASPECT_16_9_Y/2 - 70; - else if (wm->ir.aspect == WIIUSE_ASPECT_4_3) - wm->ir.offset[1] = WM_ASPECT_4_3_Y/2 - 100; + if (wm->ir.aspect == WIIUSE_ASPECT_16_9) { + wm->ir.offset[1] = WM_ASPECT_16_9_Y / 2 - 70; + } else if (wm->ir.aspect == WIIUSE_ASPECT_4_3) { + wm->ir.offset[1] = WM_ASPECT_4_3_Y / 2 - 100; + } return; case WIIUSE_IR_BELOW: wm->ir.offset[0] = 0; - if (wm->ir.aspect == WIIUSE_ASPECT_16_9) - wm->ir.offset[1] = -WM_ASPECT_16_9_Y/2 + 100; - else if (wm->ir.aspect == WIIUSE_ASPECT_4_3) - wm->ir.offset[1] = -WM_ASPECT_4_3_Y/2 + 70; + if (wm->ir.aspect == WIIUSE_ASPECT_16_9) { + wm->ir.offset[1] = -WM_ASPECT_16_9_Y / 2 + 100; + } else if (wm->ir.aspect == WIIUSE_ASPECT_4_3) { + wm->ir.offset[1] = -WM_ASPECT_4_3_Y / 2 + 70; + } return; @@ -253,7 +269,9 @@ void wiiuse_set_ir_position(struct wiimote_t* wm, enum ir_position_t pos) { * @param aspect Either WIIUSE_ASPECT_16_9 or WIIUSE_ASPECT_4_3 */ void wiiuse_set_aspect_ratio(struct wiimote_t* wm, enum aspect_t aspect) { - if (!wm) return; + if (!wm) { + return; + } wm->ir.aspect = aspect; @@ -283,16 +301,22 @@ void wiiuse_set_ir_sensitivity(struct wiimote_t* wm, int level) { const byte* block1 = NULL; const byte* block2 = NULL; - if (!wm) return; + if (!wm) { + return; + } - if (level > 5) level = 5; - if (level < 1) level = 1; + if (level > 5) { + level = 5; + } + if (level < 1) { + level = 1; + } WIIMOTE_DISABLE_STATE(wm, (WIIMOTE_STATE_IR_SENS_LVL1 | - WIIMOTE_STATE_IR_SENS_LVL2 | - WIIMOTE_STATE_IR_SENS_LVL3 | - WIIMOTE_STATE_IR_SENS_LVL4 | - WIIMOTE_STATE_IR_SENS_LVL5)); + WIIMOTE_STATE_IR_SENS_LVL2 | + WIIMOTE_STATE_IR_SENS_LVL3 | + WIIMOTE_STATE_IR_SENS_LVL4 | + WIIMOTE_STATE_IR_SENS_LVL5)); switch (level) { case 1: @@ -348,9 +372,9 @@ void calculate_basic_ir(struct wiimote_t* wm, byte* data) { /* set each IR spot to visible if spot is in range */ for (i = 0; i < 4; ++i) { - if (dot[i].ry == 1023) + if (dot[i].ry == 1023) { dot[i].visible = 0; - else { + } else { dot[i].visible = 1; dot[i].size = 0; /* since we don't know the size, set it as 0 */ } @@ -371,16 +395,17 @@ void calculate_extended_ir(struct wiimote_t* wm, byte* data) { int i; for (i = 0; i < 4; ++i) { - dot[i].rx = 1023 - (data[3*i] | ((data[(3*i)+2] & 0x30) << 4)); - dot[i].ry = data[(3*i)+1] | ((data[(3*i)+2] & 0xC0) << 2); + dot[i].rx = 1023 - (data[3 * i] | ((data[(3 * i) + 2] & 0x30) << 4)); + dot[i].ry = data[(3 * i) + 1] | ((data[(3 * i) + 2] & 0xC0) << 2); - dot[i].size = data[(3*i)+2] & 0x0F; + dot[i].size = data[(3 * i) + 2] & 0x0F; /* if in range set to visible */ - if (dot[i].ry == 1023) + if (dot[i].ry == 1023) { dot[i].visible = 0; - else + } else { dot[i].visible = 1; + } } interpret_ir_data(wm); @@ -398,147 +423,150 @@ static void interpret_ir_data(struct wiimote_t* wm) { float roll = 0.0f; int last_num_dots = wm->ir.num_dots; - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_ACC)) + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_ACC)) { roll = wm->orient.roll; + } /* count visible dots */ wm->ir.num_dots = 0; for (i = 0; i < 4; ++i) { - if (dot[i].visible) + if (dot[i].visible) { wm->ir.num_dots++; + } } switch (wm->ir.num_dots) { - case 0: - { - wm->ir.state = 0; + case 0: { + wm->ir.state = 0; - /* reset the dot ordering */ - for (i = 0; i < 4; ++i) - dot[i].order = 0; - - wm->ir.x = 0; - wm->ir.y = 0; - wm->ir.z = 0.0f; - - return; - } - case 1: - { - fix_rotated_ir_dots(wm->ir.dot, roll); - - if (wm->ir.state < 2) { - /* - * Only 1 known dot, so use just that. - */ + /* reset the dot ordering */ for (i = 0; i < 4; ++i) { - if (dot[i].visible) { - wm->ir.x = dot[i].x; - wm->ir.y = dot[i].y; - - wm->ir.ax = wm->ir.x; - wm->ir.ay = wm->ir.y; - - /* can't calculate yaw because we don't have the distance */ - /* wm->orient.yaw = calc_yaw(&wm->ir); */ - - ir_convert_to_vres(&wm->ir.x, &wm->ir.y, wm->ir.aspect, wm->ir.vres[0], wm->ir.vres[1]); - break; - } + dot[i].order = 0; } - } else { - /* - * Only see 1 dot but know theres 2. - * Try to estimate where the other one - * should be and use that. - */ - for (i = 0; i < 4; ++i) { - if (dot[i].visible) { - int ox = 0; - int x, y; - if (dot[i].order == 1) - /* visible is the left dot - estimate where the right is */ - ox = (int32_t)(dot[i].x + wm->ir.distance); - else if (dot[i].order == 2) - /* visible is the right dot - estimate where the left is */ - ox = (int32_t)(dot[i].x - wm->ir.distance); - - x = ((signed int)dot[i].x + ox) / 2; - y = dot[i].y; - - wm->ir.ax = x; - wm->ir.ay = y; - wm->orient.yaw = calc_yaw(&wm->ir); - - if (ir_correct_for_bounds(&x, &y, wm->ir.aspect, wm->ir.offset[0], wm->ir.offset[1])) { - ir_convert_to_vres(&x, &y, wm->ir.aspect, wm->ir.vres[0], wm->ir.vres[1]); - wm->ir.x = x; - wm->ir.y = y; - } - - break; - } - } - } - - break; - } - case 2: - case 3: - case 4: - { - /* - * Two (or more) dots known and seen. - * Average them together to estimate the true location. - */ - int x, y; - wm->ir.state = 2; - - fix_rotated_ir_dots(wm->ir.dot, roll); - - /* if there is at least 1 new dot, reorder them all */ - if (wm->ir.num_dots > last_num_dots) { - reorder_ir_dots(dot); wm->ir.x = 0; wm->ir.y = 0; + wm->ir.z = 0.0f; + + return; } + case 1: { + fix_rotated_ir_dots(wm->ir.dot, roll); - wm->ir.distance = ir_distance(dot); - wm->ir.z = 1023 - wm->ir.distance; + if (wm->ir.state < 2) { + /* + * Only 1 known dot, so use just that. + */ + for (i = 0; i < 4; ++i) { + if (dot[i].visible) { + wm->ir.x = dot[i].x; + wm->ir.y = dot[i].y; - get_ir_dot_avg(wm->ir.dot, &x, &y); + wm->ir.ax = wm->ir.x; + wm->ir.ay = wm->ir.y; - wm->ir.ax = x; - wm->ir.ay = y; - wm->orient.yaw = calc_yaw(&wm->ir); + /* can't calculate yaw because we don't have the distance */ + /* wm->orient.yaw = calc_yaw(&wm->ir); */ - if (ir_correct_for_bounds(&x, &y, wm->ir.aspect, wm->ir.offset[0], wm->ir.offset[1])) { - ir_convert_to_vres(&x, &y, wm->ir.aspect, wm->ir.vres[0], wm->ir.vres[1]); - wm->ir.x = x; - wm->ir.y = y; + ir_convert_to_vres(&wm->ir.x, &wm->ir.y, wm->ir.aspect, wm->ir.vres[0], wm->ir.vres[1]); + break; + } + } + } else { + /* + * Only see 1 dot but know theres 2. + * Try to estimate where the other one + * should be and use that. + */ + for (i = 0; i < 4; ++i) { + if (dot[i].visible) { + int ox = 0; + int x, y; + + if (dot[i].order == 1) + /* visible is the left dot - estimate where the right is */ + { + ox = (int32_t)(dot[i].x + wm->ir.distance); + } else if (dot[i].order == 2) + /* visible is the right dot - estimate where the left is */ + { + ox = (int32_t)(dot[i].x - wm->ir.distance); + } + + x = ((signed int)dot[i].x + ox) / 2; + y = dot[i].y; + + wm->ir.ax = x; + wm->ir.ay = y; + wm->orient.yaw = calc_yaw(&wm->ir); + + if (ir_correct_for_bounds(&x, &y, wm->ir.aspect, wm->ir.offset[0], wm->ir.offset[1])) { + ir_convert_to_vres(&x, &y, wm->ir.aspect, wm->ir.vres[0], wm->ir.vres[1]); + wm->ir.x = x; + wm->ir.y = y; + } + + break; + } + } + } + + break; } + case 2: + case 3: + case 4: { + /* + * Two (or more) dots known and seen. + * Average them together to estimate the true location. + */ + int x, y; + wm->ir.state = 2; - break; - } - default: - { - break; - } + fix_rotated_ir_dots(wm->ir.dot, roll); + + /* if there is at least 1 new dot, reorder them all */ + if (wm->ir.num_dots > last_num_dots) { + reorder_ir_dots(dot); + wm->ir.x = 0; + wm->ir.y = 0; + } + + wm->ir.distance = ir_distance(dot); + wm->ir.z = 1023 - wm->ir.distance; + + get_ir_dot_avg(wm->ir.dot, &x, &y); + + wm->ir.ax = x; + wm->ir.ay = y; + wm->orient.yaw = calc_yaw(&wm->ir); + + if (ir_correct_for_bounds(&x, &y, wm->ir.aspect, wm->ir.offset[0], wm->ir.offset[1])) { + ir_convert_to_vres(&x, &y, wm->ir.aspect, wm->ir.vres[0], wm->ir.vres[1]); + wm->ir.x = x; + wm->ir.y = y; + } + + break; + } + default: { + break; + } } - #ifdef WITH_WIIUSE_DEBUG +#ifdef WITH_WIIUSE_DEBUG { - int ir_level; - WIIUSE_GET_IR_SENSITIVITY(wm, &ir_level); - WIIUSE_DEBUG("IR sensitivity: %i", ir_level); - WIIUSE_DEBUG("IR visible dots: %i", wm->ir.num_dots); - for (i = 0; i < 4; ++i) - if (dot[i].visible) - WIIUSE_DEBUG("IR[%i][order %i] (%.3i, %.3i) -> (%.3i, %.3i)", i, dot[i].order, dot[i].rx, dot[i].ry, dot[i].x, dot[i].y); - WIIUSE_DEBUG("IR[absolute]: (%i, %i)", wm->ir.x, wm->ir.y); + int ir_level; + WIIUSE_GET_IR_SENSITIVITY(wm, &ir_level); + WIIUSE_DEBUG("IR sensitivity: %i", ir_level); + WIIUSE_DEBUG("IR visible dots: %i", wm->ir.num_dots); + for (i = 0; i < 4; ++i) + if (dot[i].visible) { + WIIUSE_DEBUG("IR[%i][order %i] (%.3i, %.3i) -> (%.3i, %.3i)", i, dot[i].order, dot[i].rx, dot[i].ry, dot[i].x, dot[i].y); + } + WIIUSE_DEBUG("IR[absolute]: (%i, %i)", wm->ir.x, wm->ir.y); } - #endif +#endif } @@ -579,17 +607,18 @@ static void fix_rotated_ir_dots(struct ir_dot_t* dot, float ang) { */ for (i = 0; i < 4; ++i) { - if (!dot[i].visible) + if (!dot[i].visible) { continue; + } - x = dot[i].rx - (1024/2); - y = dot[i].ry - (768/2); + x = dot[i].rx - (1024 / 2); + y = dot[i].ry - (768 / 2); dot[i].x = (uint32_t)((c * x) + (-s * y)); dot[i].y = (uint32_t)((s * x) + (c * y)); - dot[i].x += (1024/2); - dot[i].y += (768/2); + dot[i].x += (1024 / 2); + dot[i].y += (768 / 2); } } @@ -629,19 +658,22 @@ static void reorder_ir_dots(struct ir_dot_t* dot) { int i, j, order; /* reset the dot ordering */ - for (i = 0; i < 4; ++i) + for (i = 0; i < 4; ++i) { dot[i].order = 0; + } for (order = 1; order < 5; ++order) { i = 0; for (; !dot[i].visible || dot[i].order; ++i) - if (i >= 3) - return; + if (i >= 3) { + return; + } for (j = 0; j < 4; ++j) { - if (dot[j].visible && !dot[j].order && (dot[j].x < dot[i].x)) + if (dot[j].visible && !dot[j].order && (dot[j].x < dot[i].x)) { i = j; + } } dot[i].order = order; @@ -659,21 +691,25 @@ static float ir_distance(struct ir_dot_t* dot) { int xd, yd; for (i1 = 0; i1 < 4; ++i1) - if (dot[i1].visible) + if (dot[i1].visible) { break; - if (i1 == 4) + } + if (i1 == 4) { return 0.0f; + } - for (i2 = i1+1; i2 < 4; ++i2) - if (dot[i2].visible) + for (i2 = i1 + 1; i2 < 4; ++i2) + if (dot[i2].visible) { break; - if (i2 == 4) + } + if (i2 == 4) { return 0.0f; + } xd = dot[i2].x - dot[i1].x; yd = dot[i2].y - dot[i1].y; - return sqrtf(xd*xd + yd*yd); + return sqrtf(xd * xd + yd * yd); } @@ -707,10 +743,9 @@ static int ir_correct_for_bounds(int* x, int* y, enum aspect_t aspect, int offse y0 = ((768 - ys) / 2) + offset_y; if ((*x >= x0) - && (*x <= (x0 + xs)) - && (*y >= y0) - && (*y <= (y0 + ys))) - { + && (*x <= (x0 + xs)) + && (*y >= y0) + && (*y <= (y0 + ys))) { *x -= offset_x; *y -= offset_y; @@ -735,8 +770,8 @@ static void ir_convert_to_vres(int* x, int* y, enum aspect_t aspect, int vx, int ys = WM_ASPECT_4_3_Y; } - *x -= ((1024-xs)/2); - *y -= ((768-ys)/2); + *x -= ((1024 - xs) / 2); + *y -= ((768 - ys) / 2); *x = (int)((*x / (float)xs) * vx); *y = (int)((*y / (float)ys) * vy); @@ -754,5 +789,5 @@ float calc_yaw(struct ir_t* ir) { x = (float)(ir->ax - 512); x = x * (ir->z / 1024.0f); - return RAD_TO_DEGREE( atanf(x / ir->z) ); + return RAD_TO_DEGREE(atanf(x / ir->z)); } diff --git a/src/ir.h b/src/ir.h index 03e982a..7e3dbc5 100644 --- a/src/ir.h +++ b/src/ir.h @@ -44,13 +44,13 @@ extern "C" { #endif -/** @defgroup internal_ir Internal: IR Sensor */ -/** @{ */ -void wiiuse_set_ir_mode(struct wiimote_t *wm); -void calculate_basic_ir(struct wiimote_t* wm, byte* data); -void calculate_extended_ir(struct wiimote_t* wm, byte* data); -float calc_yaw(struct ir_t* ir); -/** @} */ + /** @defgroup internal_ir Internal: IR Sensor */ + /** @{ */ + void wiiuse_set_ir_mode(struct wiimote_t *wm); + void calculate_basic_ir(struct wiimote_t* wm, byte* data); + void calculate_extended_ir(struct wiimote_t* wm, byte* data); + float calc_yaw(struct ir_t* ir); + /** @} */ #ifdef __cplusplus } diff --git a/src/motion_plus.c b/src/motion_plus.c index 84a3699..117832b 100644 --- a/src/motion_plus.c +++ b/src/motion_plus.c @@ -43,92 +43,83 @@ static void wiiuse_calibrate_motion_plus(struct motion_plus_t *mp); static void calculate_gyro_rates(struct motion_plus_t* mp); -void wiiuse_probe_motion_plus(struct wiimote_t *wm) -{ - byte buf[MAX_PAYLOAD]; - unsigned id; +void wiiuse_probe_motion_plus(struct wiimote_t *wm) { + byte buf[MAX_PAYLOAD]; + unsigned id; - wiiuse_read_data_sync(wm, 0, WM_EXP_MOTION_PLUS_IDENT, 6, buf); + wiiuse_read_data_sync(wm, 0, WM_EXP_MOTION_PLUS_IDENT, 6, buf); - /* check error code */ - if(buf[4] & 0x0f) - { - WIIUSE_DEBUG("No Motion+ available, stopping probe."); - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_MPLUS_PRESENT); - return; - } + /* check error code */ + if (buf[4] & 0x0f) { + WIIUSE_DEBUG("No Motion+ available, stopping probe."); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_MPLUS_PRESENT); + return; + } - /* decode the id */ - id = from_big_endian_uint32_t(buf + 2); + /* decode the id */ + id = from_big_endian_uint32_t(buf + 2); - if(id != EXP_ID_CODE_INACTIVE_MOTION_PLUS && - id != EXP_ID_CODE_NLA_MOTION_PLUS && - id != EXP_ID_CODE_NLA_MOTION_PLUS_NUNCHUK && - id != EXP_ID_CODE_NLA_MOTION_PLUS_CLASSIC) - { - /* we have read something weird */ - WIIUSE_DEBUG("Motion+ ID doesn't match, probably not connected."); - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_MPLUS_PRESENT); - return; - } + if (id != EXP_ID_CODE_INACTIVE_MOTION_PLUS && + id != EXP_ID_CODE_NLA_MOTION_PLUS && + id != EXP_ID_CODE_NLA_MOTION_PLUS_NUNCHUK && + id != EXP_ID_CODE_NLA_MOTION_PLUS_CLASSIC) { + /* we have read something weird */ + WIIUSE_DEBUG("Motion+ ID doesn't match, probably not connected."); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_MPLUS_PRESENT); + return; + } - WIIUSE_DEBUG("Detected inactive Motion+!"); - WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_MPLUS_PRESENT); + WIIUSE_DEBUG("Detected inactive Motion+!"); + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_MPLUS_PRESENT); - /* init M+ */ - buf[0] = 0x55; - wiiuse_write_data(wm, WM_EXP_MOTION_PLUS_INIT, buf, 1); + /* init M+ */ + buf[0] = 0x55; + wiiuse_write_data(wm, WM_EXP_MOTION_PLUS_INIT, buf, 1); - /* Init whatever is hanging on the pass-through port */ - buf[0] = 0x55; - wiiuse_write_data(wm, WM_EXP_MEM_ENABLE1, buf, 1); + /* Init whatever is hanging on the pass-through port */ + buf[0] = 0x55; + wiiuse_write_data(wm, WM_EXP_MEM_ENABLE1, buf, 1); - buf[0] = 0x00; - wiiuse_write_data(wm, WM_EXP_MEM_ENABLE2, buf, 1); + buf[0] = 0x00; + wiiuse_write_data(wm, WM_EXP_MEM_ENABLE2, buf, 1); - /* Init gyroscope data */ - 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; - wm->exp.mp.raw_gyro_threshold = 10; + /* Init gyroscope data */ + 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; + 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; + wm->exp.mp.nc = &(wm->exp.nunchuk); + wm->exp.mp.classic = &(wm->exp.classic); + wm->exp.nunchuk.flags = &wm->flags; - wm->exp.mp.ext = 0; + wm->exp.mp.ext = 0; - wiiuse_set_ir_mode(wm); - wiiuse_set_report_type(wm); + wiiuse_set_ir_mode(wm); + wiiuse_set_report_type(wm); } -void wiiuse_motion_plus_handshake(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) - { + if (data == NULL) { wiiuse_read_data_cb(wm, wiiuse_motion_plus_handshake, wm->motion_plus_id, WM_EXP_ID, 6); - } - else - { + } else { 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); - if(val == EXP_ID_CODE_MOTION_PLUS || - val == EXP_ID_CODE_MOTION_PLUS_NUNCHUK || - val == EXP_ID_CODE_MOTION_PLUS_CLASSIC) - { + 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; - switch(val) - { + switch (val) { case EXP_ID_CODE_MOTION_PLUS: wm->exp.type = EXP_MOTION_PLUS; break; @@ -171,16 +162,14 @@ void wiiuse_motion_plus_handshake(struct wiimote_t *wm,byte *data,unsigned short } } -static void wiiuse_set_motion_plus_clear2(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_FAILED); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE); wiiuse_set_ir_mode(wm); wiiuse_status(wm); } -static void wiiuse_set_motion_plus_clear1(struct wiimote_t *wm,byte *data,unsigned short len) -{ +static void wiiuse_set_motion_plus_clear1(struct wiimote_t *wm, byte *data, unsigned short len) { byte val = 0x00; wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &val, 1, wiiuse_set_motion_plus_clear2); } @@ -188,40 +177,35 @@ static void wiiuse_set_motion_plus_clear1(struct wiimote_t *wm,byte *data,unsign /** * @brief Enable/disable Motion+ expansion * - * @param wm Pointer to the wiimote with Motion+ + * @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) -{ +void wiiuse_set_motion_plus(struct wiimote_t *wm, int status) { byte val; - if(!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_MPLUS_PRESENT) || - WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP_HANDSHAKE)) - return; + if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_MPLUS_PRESENT) || + WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP_HANDSHAKE)) { + return; + } - if(status) - { + if (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_handshake); - } - else - { + } else { disable_expansion(wm); val = 0x55; wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &val, 1, wiiuse_set_motion_plus_clear1); } } -void motion_plus_disconnected(struct motion_plus_t* mp) -{ +void motion_plus_disconnected(struct motion_plus_t* mp) { WIIUSE_DEBUG("Motion plus disconnected"); memset(mp, 0, sizeof(struct motion_plus_t)); } -void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) -{ +void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) { /* * Pass-through modes interleave data from the gyro * with the expansion data. This extracts the tag @@ -230,8 +214,7 @@ void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) int isMPFrame = (1 << 1) & msg[5]; mp->ext = msg[4] & 0x1; /* extension attached to pass-through port? */ - if (mp->ext == 0 || isMPFrame) /* reading gyro frame */ - { + 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); @@ -241,15 +224,14 @@ void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) /* First calibration */ 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)) - { + (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); } @@ -257,11 +239,9 @@ void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) calculate_gyro_rates(mp); } - else - { + else { /* expansion frame */ - if (exp_type == EXP_MOTION_PLUS_NUNCHUK) - { + if (exp_type == EXP_MOTION_PLUS_NUNCHUK) { /* ok, this is nunchuck, re-encode it as regular nunchuck packet */ /* get button states */ @@ -276,23 +256,21 @@ void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) 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)); + &(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)); + &(mp->nc->accel), + &(mp->nc->gforce)); } - else if (exp_type == EXP_MOTION_PLUS_CLASSIC) - { + else if (exp_type == EXP_MOTION_PLUS_CLASSIC) { WIIUSE_ERROR("Classic controller pass-through is not implemented!\n"); } - else - { + else { WIIUSE_ERROR("Unsupported mode passed to motion_plus_event() !\n"); } } @@ -306,8 +284,7 @@ void motion_plus_event(struct motion_plus_t* mp, int exp_type, byte* msg) * This should be called only after receiving the first values * from the Motion Plus. */ -void wiiuse_calibrate_motion_plus(struct motion_plus_t *mp) -{ +void wiiuse_calibrate_motion_plus(struct motion_plus_t *mp) { mp->cal_gyro.roll = mp->raw_gyro.roll; mp->cal_gyro.pitch = mp->raw_gyro.pitch; mp->cal_gyro.yaw = mp->raw_gyro.yaw; @@ -316,8 +293,7 @@ void wiiuse_calibrate_motion_plus(struct motion_plus_t *mp) mp->orient.yaw = 0.0; } -static void calculate_gyro_rates(struct motion_plus_t* mp) -{ +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; @@ -327,28 +303,34 @@ static void calculate_gyro_rates(struct motion_plus_t* mp) 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) + if (mp->acc_mode & 0x04) { tmp_roll = (float)tmp_r / 20.0f; - else + } else { tmp_roll = (float)tmp_r / 4.0f; + } - if (mp->acc_mode & 0x02) + if (mp->acc_mode & 0x02) { tmp_pitch = (float)tmp_p / 20.0f; - else + } else { tmp_pitch = (float)tmp_p / 4.0f; + } - if (mp->acc_mode & 0x01) + if (mp->acc_mode & 0x01) { tmp_yaw = (float)tmp_y / 20.0f; - else + } else { tmp_yaw = (float)tmp_y / 4.0f; + } /* Simple filtering */ - if (fabs(tmp_roll) < 0.5f) + if (fabs(tmp_roll) < 0.5f) { tmp_roll = 0.0f; - if (fabs(tmp_pitch) < 0.5f) + } + if (fabs(tmp_pitch) < 0.5f) { tmp_pitch = 0.0f; - if (fabs(tmp_yaw) < 0.5f) + } + if (fabs(tmp_yaw) < 0.5f) { tmp_yaw = 0.0f; + } mp->angle_rate_gyro.roll = tmp_roll; mp->angle_rate_gyro.pitch = tmp_pitch; diff --git a/src/motion_plus.h b/src/motion_plus.h index b0c0401..5aa7534 100644 --- a/src/motion_plus.h +++ b/src/motion_plus.h @@ -27,10 +27,10 @@ * $Header$ * */ - /** - * @file - * @brief Motion plus extension - */ +/** +* @file +* @brief Motion plus extension +*/ #ifndef MOTION_PLUS_H_INCLUDED #define MOTION_PLUS_H_INCLUDED @@ -41,17 +41,17 @@ extern "C" { #endif -/** @defgroup internal_mp Internal: MotionPlus */ -/** @{ */ -void motion_plus_disconnected(struct motion_plus_t* mp); + /** @defgroup internal_mp Internal: MotionPlus */ + /** @{ */ + void motion_plus_disconnected(struct motion_plus_t* mp); -void motion_plus_event(struct motion_plus_t* mp, int exp_type, 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); + void wiiuse_motion_plus_handshake(struct wiimote_t *wm, byte *data, unsigned short len); -void wiiuse_probe_motion_plus(struct wiimote_t *wm); + void wiiuse_probe_motion_plus(struct wiimote_t *wm); -/** @} */ + /** @} */ #ifdef __cplusplus } diff --git a/src/nunchuk.c b/src/nunchuk.c index d34b668..12ec0aa 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -77,8 +77,9 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); return 0; - } else + } else { data += 16; + } } nc->accel_calib.cal_zero.x = data[0]; @@ -94,8 +95,8 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un nc->js.min.y = data[12]; nc->js.center.y = data[13]; WIIUSE_DEBUG("Nunchuk calibration X: min %x, max %x, center %x Y: min %x, max %x, center %x", - nc->js.min.x, nc->js.max.x, nc->js.center.x, - nc->js.min.y, nc->js.max.y, nc->js.center.y); + nc->js.min.x, nc->js.max.x, nc->js.center.x, + nc->js.min.y, nc->js.max.y, nc->js.center.y); /* default the thresholds to the same as the wiimote */ nc->orient_threshold = wm->orient_threshold; @@ -104,9 +105,9 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un /* handshake done */ wm->exp.type = EXP_NUNCHUK; - #ifdef WIIUSE_WIN32 +#ifdef WIIUSE_WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; - #endif +#endif return 1; } @@ -177,7 +178,9 @@ void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now) { * See wiiuse_set_orient_threshold() for details. */ void wiiuse_set_nunchuk_orient_threshold(struct wiimote_t* wm, float threshold) { - if (!wm) return; + if (!wm) { + return; + } wm->exp.nunchuk.orient_threshold = threshold; } @@ -192,7 +195,9 @@ void wiiuse_set_nunchuk_orient_threshold(struct wiimote_t* wm, float threshold) * See wiiuse_set_orient_threshold() for details. */ void wiiuse_set_nunchuk_accel_threshold(struct wiimote_t* wm, int threshold) { - if (!wm) return; + if (!wm) { + return; + } wm->exp.nunchuk.accel_threshold = threshold; } diff --git a/src/nunchuk.h b/src/nunchuk.h index 951749d..289a546 100644 --- a/src/nunchuk.h +++ b/src/nunchuk.h @@ -41,16 +41,16 @@ extern "C" { #endif -/** @defgroup internal_nunchuk Internal: Nunchuk */ -/** @{ */ -int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, unsigned short len); + /** @defgroup internal_nunchuk Internal: Nunchuk */ + /** @{ */ + int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, unsigned short len); -void nunchuk_disconnected(struct nunchuk_t* nc); + void nunchuk_disconnected(struct nunchuk_t* nc); -void nunchuk_event(struct nunchuk_t* nc, byte* msg); + void nunchuk_event(struct nunchuk_t* nc, byte* msg); -void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now); -/** @} */ + void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now); + /** @} */ #ifdef __cplusplus } diff --git a/src/os.h b/src/os.h index a1b6b60..e77ebf9 100644 --- a/src/os.h +++ b/src/os.h @@ -41,21 +41,21 @@ extern "C" { #endif -/** @defgroup internal_io Internal: Platform-specific Device I/O */ -/** @{ */ -void wiiuse_init_platform_fields(struct wiimote_t* wm); -void wiiuse_cleanup_platform_fields(struct wiimote_t* wm); + /** @defgroup internal_io Internal: Platform-specific Device I/O */ + /** @{ */ + void wiiuse_init_platform_fields(struct wiimote_t* wm); + void wiiuse_cleanup_platform_fields(struct wiimote_t* wm); -int wiiuse_os_find(struct wiimote_t** wm, int max_wiimotes, int timeout); + int wiiuse_os_find(struct wiimote_t** wm, int max_wiimotes, int timeout); -int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes); -void wiiuse_os_disconnect(struct wiimote_t* wm); + int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes); + void wiiuse_os_disconnect(struct wiimote_t* wm); -int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes); -/* buf[0] will be the report type, buf+1 the rest of the report */ -int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len); -int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len); -/** @} */ + int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes); + /* buf[0] will be the report type, buf+1 the rest of the report */ + int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len); + int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len); + /** @} */ #ifdef __cplusplus } diff --git a/src/os_nix.c b/src/os_nix.c index 4df8a27..ffacf94 100644 --- a/src/os_nix.c +++ b/src/os_nix.c @@ -99,9 +99,8 @@ int wiiuse_os_find(struct wiimote_t** wm, int max_wiimotes, int timeout) { /* display discovered devices */ for (i = 0; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i) { if ((scan_info[i].dev_class[0] == WM_DEV_CLASS_0) && - (scan_info[i].dev_class[1] == WM_DEV_CLASS_1) && - (scan_info[i].dev_class[2] == WM_DEV_CLASS_2)) - { + (scan_info[i].dev_class[1] == WM_DEV_CLASS_1) && + (scan_info[i].dev_class[2] == WM_DEV_CLASS_2)) { /* found a device */ ba2str(&scan_info[i].bdaddr, wm[found_wiimotes]->bdaddr_str); @@ -129,10 +128,13 @@ int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes) { for (; i < wiimotes; ++i) { if (!WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND)) /* if the device address is not set, skip it */ + { continue; + } - if (wiiuse_os_connect_single(wm[i], NULL)) + if (wiiuse_os_connect_single(wm[i], NULL)) { ++connected; + } } return connected; @@ -150,18 +152,19 @@ int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes) { */ static int wiiuse_os_connect_single(struct wiimote_t* wm, char* address) { struct sockaddr_l2 addr; - memset(&addr, 0, sizeof (addr)); + memset(&addr, 0, sizeof(addr)); - if (!wm || WIIMOTE_IS_CONNECTED(wm)) + if (!wm || WIIMOTE_IS_CONNECTED(wm)) { return 0; + } addr.l2_family = AF_BLUETOOTH; bdaddr_t *bdaddr = &wm->bdaddr; if (address) /* use provided address */ - str2ba(address, &addr.l2_bdaddr); - else { + str2ba(address, &addr.l2_bdaddr); + } else { /** @todo this line doesn't make sense bacmp(bdaddr, BDADDR_ANY);*/ /* use address of device discovered */ @@ -173,8 +176,9 @@ static int wiiuse_os_connect_single(struct wiimote_t* wm, char* address) { * OUTPUT CHANNEL */ wm->out_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); - if (wm->out_sock == -1) + if (wm->out_sock == -1) { return 0; + } addr.l2_psm = htobs(WM_OUTPUT_CHANNEL); @@ -216,8 +220,9 @@ static int wiiuse_os_connect_single(struct wiimote_t* wm, char* address) { } void wiiuse_os_disconnect(struct wiimote_t* wm) { - if (!wm || WIIMOTE_IS_CONNECTED(wm)) + if (!wm || WIIMOTE_IS_CONNECTED(wm)) { return; + } close(wm->out_sock); close(wm->in_sock); @@ -241,7 +246,9 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { int highest_fd = -1; evnt = 0; - if (!wm) return 0; + if (!wm) { + return 0; + } /* block select() for 1/2000th of a second */ tv.tv_sec = 0; @@ -255,8 +262,9 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { FD_SET(wm[i]->in_sock, &fds); /* find the highest fd of the connected wiimotes */ - if (wm[i]->in_sock > highest_fd) + if (wm[i]->in_sock > highest_fd) { highest_fd = wm[i]->in_sock; + } } wm[i]->event = WIIUSE_NONE; @@ -264,7 +272,9 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { if (highest_fd == -1) /* nothing to poll */ + { return 0; + } if (select(highest_fd + 1, &fds, NULL, NULL, &tv) == -1) { WIIUSE_ERROR("Unable to select() the wiimote interrupt socket(s)."); @@ -275,8 +285,9 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { /* check each socket for an event */ for (i = 0; i < wiimotes; ++i) { /* if this wiimote is not connected, skip it */ - if (!WIIMOTE_IS_CONNECTED(wm[i])) + if (!WIIMOTE_IS_CONNECTED(wm[i])) { continue; + } if (FD_ISSET(wm[i]->in_sock, &fds)) { /* clear out the event buffer */ @@ -289,7 +300,7 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { r = wiiuse_os_read(wm[i], read_buffer, sizeof(read_buffer)); if (r > 0) { /* propagate the event */ - propagate_event(wm[i], read_buffer[0], read_buffer+1); + propagate_event(wm[i], read_buffer[0], read_buffer + 1); evnt += (wm[i]->event != WIIUSE_NONE); } } else { @@ -319,20 +330,20 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { wiiuse_os_disconnect(wm); wiiuse_disconnected(wm); } - } else if(rc == 0) { + } else if (rc == 0) { /* remote disconnect */ wiiuse_disconnected(wm); } else { /* read successful */ /* on *nix we ignore the first byte */ - memmove(buf, buf+1, len-1); + memmove(buf, buf + 1, len - 1); /* log the received data */ #ifdef WITH_WIIUSE_DEBUG { int i; printf("[DEBUG] (id %i) RECV: (%.2x) ", wm->unid, buf[0]); - for(i = 1; i < rc; i++) { + for (i = 1; i < rc; i++) { printf("%.2x ", buf[i]); } printf("\n"); @@ -349,11 +360,12 @@ int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len) write_buffer[0] = WM_SET_REPORT | WM_BT_OUTPUT; write_buffer[1] = report_type; - memcpy(write_buffer+2, buf, len); - rc = write(wm->out_sock, write_buffer, len+2); + memcpy(write_buffer + 2, buf, len); + rc = write(wm->out_sock, write_buffer, len + 2); - if(rc < 0) + if (rc < 0) { wiiuse_disconnected(wm); + } return rc; } diff --git a/src/os_win.c b/src/os_win.c index b8d7f90..c0f0010 100644 --- a/src/os_win.c +++ b/src/os_win.c @@ -84,8 +84,9 @@ int wiiuse_os_find(struct wiimote_t** wm, int max_wiimotes, int timeout) { } /* query the next hid device info */ - if (!SetupDiEnumDeviceInterfaces(device_info, NULL, &device_id, index, &device_data)) + if (!SetupDiEnumDeviceInterfaces(device_info, NULL, &device_id, index, &device_data)) { break; + } /* get the size of the data block required */ i = SetupDiGetDeviceInterfaceDetail(device_info, &device_data, NULL, 0, &len, NULL); @@ -93,16 +94,18 @@ int wiiuse_os_find(struct wiimote_t** wm, int max_wiimotes, int timeout) { detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); /* query the data for this device */ - if (!SetupDiGetDeviceInterfaceDetail(device_info, &device_data, detail_data, len, NULL, NULL)) + if (!SetupDiGetDeviceInterfaceDetail(device_info, &device_data, detail_data, len, NULL, NULL)) { continue; + } /* open the device */ dev = CreateFile(detail_data->DevicePath, - (GENERIC_READ | GENERIC_WRITE), - (FILE_SHARE_READ | FILE_SHARE_WRITE), - NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); - if (dev == INVALID_HANDLE_VALUE) + (GENERIC_READ | GENERIC_WRITE), + (FILE_SHARE_READ | FILE_SHARE_WRITE), + NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); + if (dev == INVALID_HANDLE_VALUE) { continue; + } /* get device attributes */ attr.Size = sizeof(attr); @@ -131,16 +134,18 @@ int wiiuse_os_find(struct wiimote_t** wm, int max_wiimotes, int timeout) { WIIUSE_INFO("Connected to wiimote [id %i].", wm[found]->unid); ++found; - if (found >= max_wiimotes) + if (found >= max_wiimotes) { break; + } } else { /* not a wiimote */ CloseHandle(dev); } } - if (detail_data) + if (detail_data) { free(detail_data); + } SetupDiDestroyDeviceInfoList(device_info); @@ -153,10 +158,12 @@ int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes) { int i = 0; for (; i < wiimotes; ++i) { - if (!wm[i]) + if (!wm[i]) { continue; - if (WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_CONNECTED)) + } + if (WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_CONNECTED)) { ++connected; + } } return connected; @@ -164,8 +171,9 @@ int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes) { void wiiuse_os_disconnect(struct wiimote_t* wm) { - if (!wm || WIIMOTE_IS_CONNECTED(wm)) + if (!wm || WIIMOTE_IS_CONNECTED(wm)) { return; + } CloseHandle(wm->dev_handle); wm->dev_handle = 0; @@ -184,7 +192,9 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { byte read_buffer[MAX_PAYLOAD]; int evnt = 0; - if (!wm) return 0; + if (!wm) { + return 0; + } for (i = 0; i < wiimotes; ++i) { wm[i]->event = WIIUSE_NONE; @@ -194,7 +204,7 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { /* read */ if (wiiuse_os_read(wm[i], read_buffer, sizeof(read_buffer))) { /* propagate the event */ - propagate_event(wm[i], read_buffer[0], read_buffer+1); + propagate_event(wm[i], read_buffer[0], read_buffer + 1); evnt += (wm[i]->event != WIIUSE_NONE); } else { /* send out any waiting writes */ @@ -209,8 +219,9 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { DWORD b, r; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return 0; + } if (!ReadFile(wm->dev_handle, buf, len, &b, &wm->hid_overlap)) { /* partial read */ @@ -226,8 +237,9 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { if (r == WAIT_TIMEOUT) { /* timeout - cancel and continue */ - if (*buf) + if (*buf) { WIIUSE_WARNING("Packet ignored. This may indicate a problem (timeout is %i ms).", wm->timeout); + } CancelIo(wm->dev_handle); ResetEvent(wm->hid_overlap.hEvent); @@ -237,15 +249,16 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { return 0; } - if (!GetOverlappedResult(wm->dev_handle, &wm->hid_overlap, &b, 0)) + if (!GetOverlappedResult(wm->dev_handle, &wm->hid_overlap, &b, 0)) { return 0; + } /* log the received data */ #ifdef WITH_WIIUSE_DEBUG { DWORD i; printf("[DEBUG] (id %i) RECV: (%.2x) ", wm->unid, buf[0]); - for(i = 1; i < b; i++) { + for (i = 1; i < b; i++) { printf("%.2x ", buf[i]); } printf("\n"); @@ -263,33 +276,33 @@ int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len) int i; byte write_buffer[MAX_PAYLOAD]; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return 0; + } write_buffer[0] = report_type; - memcpy(write_buffer+1, buf, len); + memcpy(write_buffer + 1, buf, len); switch (wm->stack) { - case WIIUSE_STACK_UNKNOWN: - { - /* try to auto-detect the stack type */ - if (i = WriteFile(wm->dev_handle, write_buffer, 22, &bytes, &wm->hid_overlap)) { - /* bluesoleil will always return 1 here, even if it's not connected */ - wm->stack = WIIUSE_STACK_BLUESOLEIL; - return i; - } + case WIIUSE_STACK_UNKNOWN: { + /* try to auto-detect the stack type */ + if (i = WriteFile(wm->dev_handle, write_buffer, 22, &bytes, &wm->hid_overlap)) { + /* bluesoleil will always return 1 here, even if it's not connected */ + wm->stack = WIIUSE_STACK_BLUESOLEIL; + return i; + } - if (i = HidD_SetOutputReport(wm->dev_handle, write_buffer, len+1)) { - wm->stack = WIIUSE_STACK_MS; - return i; - } + if (i = HidD_SetOutputReport(wm->dev_handle, write_buffer, len + 1)) { + wm->stack = WIIUSE_STACK_MS; + return i; + } - WIIUSE_ERROR("Unable to determine bluetooth stack type."); - return 0; - } + WIIUSE_ERROR("Unable to determine bluetooth stack type."); + return 0; + } case WIIUSE_STACK_MS: - return HidD_SetOutputReport(wm->dev_handle, write_buffer, len+1); + return HidD_SetOutputReport(wm->dev_handle, write_buffer, len + 1); case WIIUSE_STACK_BLUESOLEIL: return WriteFile(wm->dev_handle, write_buffer, 22, &bytes, &wm->hid_overlap); diff --git a/src/wiiboard.c b/src/wiiboard.c index 275745b..8c4f9f0 100644 --- a/src/wiiboard.c +++ b/src/wiiboard.c @@ -53,14 +53,13 @@ int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data #ifdef WITH_WIIUSE_DEBUG int i; printf("DECRYPTED DATA WIIBOARD\n"); - for (i = 0; i < len; ++i) - { - if(i%16==0) - { - if(i!=0) + for (i = 0; i < len; ++i) { + if (i % 16 == 0) { + if (i != 0) { printf("\n"); + } - printf("%X: ",0x4a40000+32+i); + printf("%X: ", 0x4a40000 + 32 + i); } printf("%02X ", data[i]); } @@ -87,9 +86,9 @@ int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED; wm->exp.type = EXP_WII_BOARD; - #ifdef WIIUSE_WIN32 +#ifdef WIIUSE_WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; - #endif +#endif return 1; } @@ -109,9 +108,9 @@ static float do_interpolate(uint16_t raw, uint16_t cal[3]) { if (raw < cal[0]) { return 0.0f; } else if (raw < cal[1]) { - return ((float)(raw-cal[0]) * WIIBOARD_MIDDLE_CALIB)/(float)(cal[1] - cal[0]); + return ((float)(raw - cal[0]) * WIIBOARD_MIDDLE_CALIB) / (float)(cal[1] - cal[0]); } else if (raw < cal[2]) { - return ((float)(raw-cal[1]) * WIIBOARD_MIDDLE_CALIB)/(float)(cal[2] - cal[1]) + WIIBOARD_MIDDLE_CALIB; + return ((float)(raw - cal[1]) * WIIBOARD_MIDDLE_CALIB) / (float)(cal[2] - cal[1]) + WIIBOARD_MIDDLE_CALIB; } else { return WIIBOARD_MIDDLE_CALIB * 2.0f; } @@ -143,6 +142,5 @@ void wii_board_event(struct wii_board_t* wb, byte* msg) { /** @todo not implemented! */ -void wiiuse_set_wii_board_calib(struct wiimote_t *wm) -{ +void wiiuse_set_wii_board_calib(struct wiimote_t *wm) { } diff --git a/src/wiiboard.h b/src/wiiboard.h index 0b175e2..e0d7616 100644 --- a/src/wiiboard.h +++ b/src/wiiboard.h @@ -40,14 +40,14 @@ extern "C" { #endif -/** @defgroup internal_wiiboard Internal: Wii Balance Board */ -/** @{ */ -int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data, uint16_t len); + /** @defgroup internal_wiiboard Internal: Wii Balance Board */ + /** @{ */ + int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data, uint16_t len); -void wii_board_disconnected(struct wii_board_t* wb); + void wii_board_disconnected(struct wii_board_t* wb); -void wii_board_event(struct wii_board_t* wb, byte* msg); -/** @} */ + void wii_board_event(struct wii_board_t* wb, byte* msg); + /** @} */ #ifdef __cplusplus } #endif diff --git a/src/wiiuse.c b/src/wiiuse.c index 839f0b6..5aa5b2d 100644 --- a/src/wiiuse.c +++ b/src/wiiuse.c @@ -67,8 +67,7 @@ FILE* logtarget[4]; * * The default FILE* for all loglevels is stderr */ -void wiiuse_set_output(enum wiiuse_loglevel loglevel, FILE *logfile) -{ +void wiiuse_set_output(enum wiiuse_loglevel loglevel, FILE *logfile) { logtarget[(int)loglevel] = logfile; } @@ -78,8 +77,9 @@ void wiiuse_set_output(enum wiiuse_loglevel loglevel, FILE *logfile) void wiiuse_cleanup(struct wiimote_t** wm, int wiimotes) { int i = 0; - if (!wm) + if (!wm) { return; + } WIIUSE_INFO("wiiuse clean up..."); @@ -120,9 +120,9 @@ struct wiimote_t** wiiuse_init(int wiimotes) { * to call this function again it won't be intrusive. */ if (!g_banner) { - printf( "wiiuse v" WIIUSE_VERSION " loaded.\n" - " Fork at http://github.com/rpavlik/wiiuse\n" - " Original By: Michael Laforest http://wiiuse.net\n"); + printf("wiiuse v" WIIUSE_VERSION " loaded.\n" + " Fork at http://github.com/rpavlik/wiiuse\n" + " Original By: Michael Laforest http://wiiuse.net\n"); g_banner = 1; } @@ -131,8 +131,9 @@ struct wiimote_t** wiiuse_init(int wiimotes) { logtarget[2] = stderr; logtarget[3] = stderr; - if (!wiimotes) + if (!wiimotes) { return NULL; + } wm = malloc(sizeof(struct wiimote_t*) * wiimotes); @@ -140,7 +141,7 @@ struct wiimote_t** wiiuse_init(int wiimotes) { wm[i] = malloc(sizeof(struct wiimote_t)); memset(wm[i], 0, sizeof(struct wiimote_t)); - wm[i]->unid = i+1; + wm[i]->unid = i + 1; wiiuse_init_platform_fields(wm[i]); wm[i]->state = WIIMOTE_INIT_STATES; @@ -170,7 +171,9 @@ struct wiimote_t** wiiuse_init(int wiimotes) { * @param wm Pointer to a wiimote_t structure. */ void wiiuse_disconnected(struct wiimote_t* wm) { - if (!wm) return; + if (!wm) { + return; + } WIIUSE_INFO("Wiimote disconnected [id %i].", wm->unid); @@ -201,8 +204,9 @@ void wiiuse_disconnected(struct wiimote_t* wm) { void wiiuse_rumble(struct wiimote_t* wm, int status) { byte buf; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return; + } /* make sure to keep the current lit leds */ buf = wm->leds; @@ -218,8 +222,9 @@ void wiiuse_rumble(struct wiimote_t* wm, int status) { } /* preserve IR state */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { buf |= 0x04; + } wiiuse_send(wm, WM_CMD_RUMBLE, &buf, 1); } @@ -231,7 +236,9 @@ void wiiuse_rumble(struct wiimote_t* wm, int status) { * @param wm Pointer to a wiimote_t structure. */ void wiiuse_toggle_rumble(struct wiimote_t* wm) { - if (!wm) return; + if (!wm) { + return; + } wiiuse_rumble(wm, !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)); } @@ -248,15 +255,17 @@ void wiiuse_toggle_rumble(struct wiimote_t* wm) { void wiiuse_set_leds(struct wiimote_t* wm, int leds) { byte buf; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return; + } /* remove the lower 4 bits because they control rumble */ wm->leds = (leds & 0xF0); /* make sure if the rumble is on that we keep it on */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) { wm->leds |= 0x01; + } buf = wm->leds; @@ -275,10 +284,11 @@ void wiiuse_set_leds(struct wiimote_t* wm, int leds) { * by default. */ void wiiuse_motion_sensing(struct wiimote_t* wm, int status) { - if (status) + if (status) { WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_ACC); - else + } else { WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_ACC); + } wiiuse_set_report_type(wm); } @@ -300,34 +310,46 @@ int wiiuse_set_report_type(struct wiimote_t* wm) { byte buf[2]; int motion, exp, ir; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return 0; + } buf[0] = (WIIMOTE_IS_FLAG_SET(wm, WIIUSE_CONTINUOUS) ? 0x04 : 0x00); /* set to 0x04 for continuous reporting */ buf[1] = 0x00; /* if rumble is enabled, make sure we keep it */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) { buf[0] |= 0x01; + } motion = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_ACC); exp = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP); ir = WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR); - if (motion && ir && exp) buf[1] = WM_RPT_BTN_ACC_IR_EXP; - else if (motion && exp) buf[1] = WM_RPT_BTN_ACC_EXP; - else if (motion && ir) buf[1] = WM_RPT_BTN_ACC_IR; - else if (ir && exp) buf[1] = WM_RPT_BTN_IR_EXP; - else if (ir) buf[1] = WM_RPT_BTN_ACC_IR; - else if (exp) buf[1] = WM_RPT_BTN_EXP; - else if (motion) buf[1] = WM_RPT_BTN_ACC; - else buf[1] = WM_RPT_BTN; + if (motion && ir && exp) { + buf[1] = WM_RPT_BTN_ACC_IR_EXP; + } else if (motion && exp) { + buf[1] = WM_RPT_BTN_ACC_EXP; + } else if (motion && ir) { + buf[1] = WM_RPT_BTN_ACC_IR; + } else if (ir && exp) { + buf[1] = WM_RPT_BTN_IR_EXP; + } else if (ir) { + buf[1] = WM_RPT_BTN_ACC_IR; + } else if (exp) { + buf[1] = WM_RPT_BTN_EXP; + } else if (motion) { + buf[1] = WM_RPT_BTN_ACC; + } else { + buf[1] = WM_RPT_BTN; + } WIIUSE_DEBUG("Setting report type: 0x%x", buf[1]); exp = wiiuse_send(wm, WM_CMD_REPORT_TYPE, buf, 2); - if (exp <= 0) + if (exp <= 0) { return exp; + } return buf[1]; } @@ -353,15 +375,18 @@ int wiiuse_set_report_type(struct wiimote_t* wm) { int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int addr, uint16_t len) { struct read_req_t* req; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return 0; - if (!buffer || !len) + } + if (!buffer || !len) { return 0; + } /* make this request structure */ req = (struct read_req_t*)malloc(sizeof(struct read_req_t)); - if (req == NULL) + if (req == NULL) { return 0; + } req->cb = read_cb; req->buf = buffer; req->addr = addr; @@ -381,7 +406,9 @@ int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buff wiiuse_send_next_pending_read_request(wm); } else { struct read_req_t* nptr = wm->read_req; - for (; nptr->next; nptr = nptr->next); + for (; nptr->next; nptr = nptr->next) { + ; + } nptr->next = req; WIIUSE_DEBUG("Added pending data read request."); @@ -425,16 +452,21 @@ void wiiuse_send_next_pending_read_request(struct wiimote_t* wm) { byte buf[6]; struct read_req_t* req; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return; - if (!wm->read_req) return; + } + if (!wm->read_req) { + return; + } /* skip over dirty ones since they have already been read */ req = wm->read_req; - while (req && req->dirty) + while (req && req->dirty) { req = req->next; - if (!req) + } + if (!req) { return; + } /* the offset is in big endian */ to_big_endian_uint32_t(buf, req->addr); @@ -457,8 +489,9 @@ void wiiuse_send_next_pending_read_request(struct wiimote_t* wm) { void wiiuse_status(struct wiimote_t* wm) { byte buf = 0; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return; + } WIIUSE_DEBUG("Requested wiimote status."); @@ -477,14 +510,17 @@ void wiiuse_status(struct wiimote_t* wm) { */ struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid) { int i = 0; - if (!wm) + if (!wm) { return NULL; + } for (; i < wiimotes; ++i) { - if (!wm[i]) + if (!wm[i]) { continue; - if (wm[i]->unid == unid) + } + if (wm[i]->unid == unid) { return wm[i]; + } } return NULL; @@ -503,22 +539,25 @@ int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, const byte* data, byte buf[21] = {0}; /* the payload is always 23 */ byte * bufPtr = buf; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return 0; - if (!data || !len) + } + if (!data || !len) { return 0; + } WIIUSE_DEBUG("Writing %i bytes to memory location 0x%x...", len, addr); - #ifdef WITH_WIIUSE_DEBUG +#ifdef WITH_WIIUSE_DEBUG { int i = 0; printf("Write data is: "); - for (; i < len; ++i) + for (; i < len; ++i) { printf("%x ", data[i]); + } printf("\n"); } - #endif +#endif /* the offset is in big endian */ buffer_big_endian_uint32_t(&bufPtr, (uint32_t)addr); @@ -549,17 +588,20 @@ int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, const byte* data, * to a pending list and be sent out when the previous * finishes. */ -int wiiuse_write_data_cb(struct wiimote_t *wm, unsigned int addr, byte *data, byte 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) { struct data_req_t* req; - if(!wm || !WIIMOTE_IS_CONNECTED(wm)) return 0; - if( !data || !len ) return 0; + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { + return 0; + } + if (!data || !len) { + return 0; + } req = (struct data_req_t*)malloc(sizeof(struct data_req_t)); req->cb = write_cb; req->len = len; - memcpy(req->data,data,req->len); + memcpy(req->data, data, req->len); req->state = REQ_READY; req->addr = addr;/* BIG_ENDIAN_LONG(addr); */ req->next = NULL; @@ -574,8 +616,10 @@ int wiiuse_write_data_cb(struct wiimote_t *wm, unsigned int addr, byte *data, by wiiuse_send_next_pending_write_request(wm); } else { struct data_req_t* nptr = wm->data_req; -WIIUSE_DEBUG("chaud2fois"); - for (; nptr->next; nptr = nptr->next); + WIIUSE_DEBUG("chaud2fois"); + for (; nptr->next; nptr = nptr->next) { + ; + } nptr->next = req; WIIUSE_DEBUG("Added pending data write request."); @@ -596,14 +640,19 @@ WIIUSE_DEBUG("chaud2fois"); void wiiuse_send_next_pending_write_request(struct wiimote_t* wm) { struct data_req_t* req; - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return; + } req = wm->data_req; - if (!req) + if (!req) { return; - if (!req->data || !req->len) + } + if (!req->data || !req->len) { return; - if(req->state!=REQ_READY) return; + } + if (req->state != REQ_READY) { + return; + } wiiuse_write_data(wm, req->addr, req->data, req->len); @@ -625,26 +674,27 @@ int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { switch (report_type) { case WM_CMD_LED: case WM_CMD_RUMBLE: - case WM_CMD_CTRL_STATUS: - { - /* Rumble flag for: 0x11, 0x13, 0x14, 0x15, 0x19 or 0x1a */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) - msg[0] |= 0x01; - break; - } + case WM_CMD_CTRL_STATUS: { + /* Rumble flag for: 0x11, 0x13, 0x14, 0x15, 0x19 or 0x1a */ + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) { + msg[0] |= 0x01; + } + break; + } default: break; } - #ifdef WITH_WIIUSE_DEBUG +#ifdef WITH_WIIUSE_DEBUG { int x; printf("[DEBUG] (id %i) SEND: (%.2x) %.2x ", wm->unid, report_type, msg[0]); - for (x = 1; x < len; ++x) + for (x = 1; x < len; ++x) { printf("%.2x ", msg[x]); + } printf("\n"); } - #endif +#endif return wiiuse_os_write(wm, report_type, msg, len); } @@ -663,7 +713,9 @@ int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { * Flags are defined in wiiuse.h. */ int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable) { - if (!wm) return 0; + if (!wm) { + return 0; + } /* remove mutually exclusive flags */ enable &= ~disable; @@ -692,15 +744,18 @@ int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable) { float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha) { float old; - if (!wm) return 0.0f; + if (!wm) { + return 0.0f; + } old = wm->accel_calib.st_alpha; wm->accel_calib.st_alpha = alpha; /* if there is a nunchuk set that too */ - if (wm->exp.type == EXP_NUNCHUK) + if (wm->exp.type == EXP_NUNCHUK) { wm->exp.nunchuk.accel_calib.st_alpha = alpha; + } return old; } @@ -714,14 +769,17 @@ float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha) { * @param type The type of bluetooth stack to use. */ void wiiuse_set_bluetooth_stack(struct wiimote_t** wm, int wiimotes, enum win_bt_stack_t type) { - #ifdef WIIUSE_WIN32 +#ifdef WIIUSE_WIN32 int i; - if (!wm) return; + if (!wm) { + return; + } - for (i = 0; i < wiimotes; ++i) + for (i = 0; i < wiimotes; ++i) { wm[i]->stack = type; - #endif + } +#endif } @@ -736,7 +794,9 @@ void wiiuse_set_bluetooth_stack(struct wiimote_t** wm, int wiimotes, enum win_bt * the angle has to change by a full degree to generate an event. */ void wiiuse_set_orient_threshold(struct wiimote_t* wm, float threshold) { - if (!wm) return; + if (!wm) { + return; + } wm->orient_threshold = threshold; } @@ -749,7 +809,9 @@ void wiiuse_set_orient_threshold(struct wiimote_t* wm, float threshold) { * @param threshold The decimal place that should be considered a significant change. */ void wiiuse_set_accel_threshold(struct wiimote_t* wm, int threshold) { - if (!wm) return; + if (!wm) { + return; + } wm->accel_threshold = threshold; } @@ -761,7 +823,9 @@ void wiiuse_set_accel_threshold(struct wiimote_t* wm, int threshold) { * @param wm Pointer to a wiimote_t structure. */ void wiiuse_resync(struct wiimote_t* wm) { - if (!wm) return; + if (!wm) { + return; + } #ifndef WIIUSE_SYNC_HANDSHAKE wm->handshake_state = 0; @@ -779,14 +843,16 @@ void wiiuse_resync(struct wiimote_t* wm) { * @param exp_timeout The timeout in millisecondsd to wait for an expansion handshake. */ void wiiuse_set_timeout(struct wiimote_t** wm, int wiimotes, byte normal_timeout, byte exp_timeout) { - #ifdef WIIUSE_WIN32 +#ifdef WIIUSE_WIN32 int i; - if (!wm) return; + if (!wm) { + return; + } for (i = 0; i < wiimotes; ++i) { wm[i]->normal_timeout = normal_timeout; wm[i]->exp_timeout = exp_timeout; } - #endif +#endif } diff --git a/src/wiiuse.h b/src/wiiuse.h index 84affea..0c108e2 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -363,7 +363,7 @@ struct read_req_t { * @brief Roll/Pitch/Yaw short angles. */ typedef struct ang3s_t { - int16_t roll, pitch, yaw; + int16_t roll, pitch, yaw; } ang3s_t; /** @@ -371,7 +371,7 @@ typedef struct ang3s_t { * @brief Roll/Pitch/Yaw float angles. */ typedef struct ang3f_t { - float roll, pitch, yaw; + float roll, pitch, yaw; } ang3f_t; /** @@ -566,9 +566,8 @@ typedef struct guitar_hero_3_t { /** * @brief Motion Plus expansion device */ -typedef struct motion_plus_t -{ - byte ext; /**< is there a device on the pass-through port? */ +typedef struct motion_plus_t { + 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 */ @@ -577,7 +576,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; @@ -718,34 +717,34 @@ typedef enum WIIUSE_EVENT_TYPE { typedef struct wiimote_t { WCONST int unid; /**< user specified id */ - #ifdef WIIUSE_BLUEZ +#ifdef WIIUSE_BLUEZ /** @name Linux-specific (BlueZ) members */ /** @{ */ - WCONST char bdaddr_str[18]; /**< readable bt address */ - WCONST bdaddr_t bdaddr; /**< bt address */ - WCONST int out_sock; /**< output socket */ - WCONST int in_sock; /**< input socket */ + WCONST char bdaddr_str[18]; /**< readable bt address */ + WCONST bdaddr_t bdaddr; /**< bt address */ + WCONST int out_sock; /**< output socket */ + WCONST int in_sock; /**< input socket */ /** @} */ - #endif +#endif - #ifdef WIIUSE_WIN32 +#ifdef WIIUSE_WIN32 /** @name Windows-specific members */ /** @{ */ - WCONST HANDLE dev_handle; /**< HID handle */ - WCONST OVERLAPPED hid_overlap; /**< overlap handle */ - WCONST enum win_bt_stack_t stack; /**< type of bluetooth stack to use */ - WCONST int timeout; /**< read timeout */ - WCONST byte normal_timeout; /**< normal timeout */ - WCONST byte exp_timeout; /**< timeout for expansion handshake */ + WCONST HANDLE dev_handle; /**< HID handle */ + WCONST OVERLAPPED hid_overlap; /**< overlap handle */ + WCONST enum win_bt_stack_t stack; /**< type of bluetooth stack to use */ + WCONST int timeout; /**< read timeout */ + WCONST byte normal_timeout; /**< normal timeout */ + WCONST byte exp_timeout; /**< timeout for expansion handshake */ /** @} */ - #endif - - #ifdef WIIUSE_MAC +#endif + +#ifdef WIIUSE_MAC /** @name Mac OS X-specific members */ /** @{ */ - WCONST void* objc_wm; /** WiiuseWiimote* as opaque pointer */ + WCONST void* objc_wm; /** WiiuseWiimote* as opaque pointer */ /** @} */ - #endif +#endif WCONST int state; /**< various state flags */ WCONST byte leds; /**< currently lit leds */ @@ -817,8 +816,7 @@ typedef void (*wiiuse_update_cb)(struct wiimote_callback_data_t* wm); */ typedef void (*wiiuse_write_cb)(struct wiimote_t* wm, unsigned char* data, unsigned short len); -typedef enum data_req_s -{ +typedef enum data_req_s { REQ_READY = 0, REQ_SENT, REQ_DONE @@ -877,69 +875,69 @@ typedef enum wiiuse_loglevel { extern "C" { #endif -/* wiiuse.c */ -WIIUSE_EXPORT extern const char* wiiuse_version(); + /* wiiuse.c */ + WIIUSE_EXPORT extern const char* wiiuse_version(); -/** @brief Define indicating the presence of the feature allowing you to - * redirect output for one or more logging levels within the library. - */ + /** @brief Define indicating the presence of the feature allowing you to + * redirect output for one or more logging levels within the library. + */ #define WIIUSE_HAS_OUTPUT_REDIRECTION -WIIUSE_EXPORT extern void wiiuse_set_output(enum wiiuse_loglevel loglevel, FILE *logtarget); + WIIUSE_EXPORT extern void wiiuse_set_output(enum wiiuse_loglevel loglevel, FILE *logtarget); -WIIUSE_EXPORT extern struct wiimote_t** wiiuse_init(int wiimotes); -WIIUSE_EXPORT extern void wiiuse_disconnected(struct wiimote_t* wm); -WIIUSE_EXPORT extern void wiiuse_cleanup(struct wiimote_t** wm, int wiimotes); -WIIUSE_EXPORT extern void wiiuse_rumble(struct wiimote_t* wm, int status); -WIIUSE_EXPORT extern void wiiuse_toggle_rumble(struct wiimote_t* wm); -WIIUSE_EXPORT extern void wiiuse_set_leds(struct wiimote_t* wm, int leds); -WIIUSE_EXPORT extern void wiiuse_motion_sensing(struct wiimote_t* wm, int status); -WIIUSE_EXPORT extern int wiiuse_read_data(struct wiimote_t* wm, byte* buffer, unsigned int offset, uint16_t len); -WIIUSE_EXPORT extern int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, const byte* data, byte len); -WIIUSE_EXPORT extern void wiiuse_status(struct wiimote_t* wm); -WIIUSE_EXPORT extern struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid); -WIIUSE_EXPORT extern int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable); -WIIUSE_EXPORT extern float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha); -WIIUSE_EXPORT extern void wiiuse_set_bluetooth_stack(struct wiimote_t** wm, int wiimotes, enum win_bt_stack_t type); -WIIUSE_EXPORT extern void wiiuse_set_orient_threshold(struct wiimote_t* wm, float threshold); -WIIUSE_EXPORT extern void wiiuse_resync(struct wiimote_t* wm); -WIIUSE_EXPORT extern void wiiuse_set_timeout(struct wiimote_t** wm, int wiimotes, byte normal_timeout, byte exp_timeout); -WIIUSE_EXPORT extern void wiiuse_set_accel_threshold(struct wiimote_t* wm, int threshold); + WIIUSE_EXPORT extern struct wiimote_t** wiiuse_init(int wiimotes); + WIIUSE_EXPORT extern void wiiuse_disconnected(struct wiimote_t* wm); + WIIUSE_EXPORT extern void wiiuse_cleanup(struct wiimote_t** wm, int wiimotes); + WIIUSE_EXPORT extern void wiiuse_rumble(struct wiimote_t* wm, int status); + WIIUSE_EXPORT extern void wiiuse_toggle_rumble(struct wiimote_t* wm); + WIIUSE_EXPORT extern void wiiuse_set_leds(struct wiimote_t* wm, int leds); + WIIUSE_EXPORT extern void wiiuse_motion_sensing(struct wiimote_t* wm, int status); + WIIUSE_EXPORT extern int wiiuse_read_data(struct wiimote_t* wm, byte* buffer, unsigned int offset, uint16_t len); + WIIUSE_EXPORT extern int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, const byte* data, byte len); + WIIUSE_EXPORT extern void wiiuse_status(struct wiimote_t* wm); + WIIUSE_EXPORT extern struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid); + WIIUSE_EXPORT extern int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable); + WIIUSE_EXPORT extern float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha); + WIIUSE_EXPORT extern void wiiuse_set_bluetooth_stack(struct wiimote_t** wm, int wiimotes, enum win_bt_stack_t type); + WIIUSE_EXPORT extern void wiiuse_set_orient_threshold(struct wiimote_t* wm, float threshold); + WIIUSE_EXPORT extern void wiiuse_resync(struct wiimote_t* wm); + WIIUSE_EXPORT extern void wiiuse_set_timeout(struct wiimote_t** wm, int wiimotes, byte normal_timeout, byte exp_timeout); + WIIUSE_EXPORT extern void wiiuse_set_accel_threshold(struct wiimote_t* wm, int threshold); -/* io.c */ -WIIUSE_EXPORT extern int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout); -WIIUSE_EXPORT extern int wiiuse_connect(struct wiimote_t** wm, int wiimotes); -WIIUSE_EXPORT extern void wiiuse_disconnect(struct wiimote_t* wm); + /* io.c */ + WIIUSE_EXPORT extern int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout); + WIIUSE_EXPORT extern int wiiuse_connect(struct wiimote_t** wm, int wiimotes); + WIIUSE_EXPORT extern void wiiuse_disconnect(struct wiimote_t* wm); -/* events.c */ -WIIUSE_EXPORT extern int wiiuse_poll(struct wiimote_t** wm, int wiimotes); + /* events.c */ + WIIUSE_EXPORT extern int wiiuse_poll(struct wiimote_t** wm, int wiimotes); -/** - * @brief Poll Wiimotes, and call the provided callback with information - * on each Wiimote that had an event. - * - * Alternative to calling wiiuse_poll yourself, and provides the same - * information struct on all platforms. - * - * @return Number of wiimotes that had an event. - */ -WIIUSE_EXPORT extern int wiiuse_update(struct wiimote_t** wm, int wiimotes, wiiuse_update_cb callback); + /** + * @brief Poll Wiimotes, and call the provided callback with information + * on each Wiimote that had an event. + * + * Alternative to calling wiiuse_poll yourself, and provides the same + * information struct on all platforms. + * + * @return Number of wiimotes that had an event. + */ + WIIUSE_EXPORT extern int wiiuse_update(struct wiimote_t** wm, int wiimotes, wiiuse_update_cb callback); -/* ir.c */ -WIIUSE_EXPORT extern void wiiuse_set_ir(struct wiimote_t* wm, int status); -WIIUSE_EXPORT extern void wiiuse_set_ir_vres(struct wiimote_t* wm, unsigned int x, unsigned int y); -WIIUSE_EXPORT extern void wiiuse_set_ir_position(struct wiimote_t* wm, enum ir_position_t pos); -WIIUSE_EXPORT extern void wiiuse_set_aspect_ratio(struct wiimote_t* wm, enum aspect_t aspect); -WIIUSE_EXPORT extern void wiiuse_set_ir_sensitivity(struct wiimote_t* wm, int level); + /* ir.c */ + WIIUSE_EXPORT extern void wiiuse_set_ir(struct wiimote_t* wm, int status); + WIIUSE_EXPORT extern void wiiuse_set_ir_vres(struct wiimote_t* wm, unsigned int x, unsigned int y); + WIIUSE_EXPORT extern void wiiuse_set_ir_position(struct wiimote_t* wm, enum ir_position_t pos); + WIIUSE_EXPORT extern void wiiuse_set_aspect_ratio(struct wiimote_t* wm, enum aspect_t aspect); + WIIUSE_EXPORT extern void wiiuse_set_ir_sensitivity(struct wiimote_t* wm, int level); -/* nunchuk.c */ -WIIUSE_EXPORT extern void wiiuse_set_nunchuk_orient_threshold(struct wiimote_t* wm, float threshold); -WIIUSE_EXPORT extern void wiiuse_set_nunchuk_accel_threshold(struct wiimote_t* wm, int threshold); + /* nunchuk.c */ + WIIUSE_EXPORT extern void wiiuse_set_nunchuk_orient_threshold(struct wiimote_t* wm, float threshold); + WIIUSE_EXPORT extern void wiiuse_set_nunchuk_accel_threshold(struct wiimote_t* wm, int threshold); -/* wiiboard.c */ -/* this function not currently implemented... */ -WIIUSE_EXPORT extern void wiiuse_set_wii_board_calib(struct wiimote_t *wm); + /* wiiboard.c */ + /* this function not currently implemented... */ + WIIUSE_EXPORT extern void wiiuse_set_wii_board_calib(struct wiimote_t *wm); -WIIUSE_EXPORT extern void wiiuse_set_motion_plus(struct wiimote_t *wm, int status); + WIIUSE_EXPORT extern void wiiuse_set_motion_plus(struct wiimote_t *wm, int status); #ifdef __cplusplus } diff --git a/src/wiiuse_internal.h b/src/wiiuse_internal.h index e4830c0..b484409 100644 --- a/src/wiiuse_internal.h +++ b/src/wiiuse_internal.h @@ -143,12 +143,12 @@ * not store them in such a way, rather it uses * the concept of major service, major class, * and minor class, that are respectivelly - * 11bit, 5bit, and 6bit long. Hence, the + * 11bit, 5bit, and 6bit long. Hence, the * numbers are different. * The Wiimote CoD Bluetooth division is the following: * 00000000001 00101 000001 00 (major service - major class - minor class - format type) * This can also be seen in the WiiC Linux way: - * 00000000 00100101 00000100 + * 00000000 00100101 00000100 */ #ifdef WIIUSE_MAC #define WM_DEV_MINOR_CLASS 0x01 @@ -280,121 +280,121 @@ extern "C" { #endif -/* not part of the api */ + /* not part of the api */ -/** @brief Cross-platform call to sleep for at least the specified number - * of milliseconds. - * - * Use instead of Sleep(), usleep(), or similar functions. - * Defined in util.c - */ -void wiiuse_millisleep(int durationMilliseconds); + /** @brief Cross-platform call to sleep for at least the specified number + * of milliseconds. + * + * Use instead of Sleep(), usleep(), or similar functions. + * Defined in util.c + */ + void wiiuse_millisleep(int durationMilliseconds); -int wiiuse_set_report_type(struct wiimote_t* wm); -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, unsigned int addr, byte* data, byte len, wiiuse_write_cb write_cb); + int wiiuse_set_report_type(struct wiimote_t* wm); + 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, unsigned int addr, byte* data, byte len, wiiuse_write_cb write_cb); #ifdef WIIUSE_DOXYGEN_PARSING -/** @addtogroup betosystem Big-endian buffer to system-byte-order value - @{ */ + /** @addtogroup betosystem Big-endian buffer to system-byte-order value + @{ */ -/** @brief Given a buffer buf, copy and return a value of type uint8_t. -*/ -uint8_t from_big_endian_uint8_t(byte * buf); -/** @brief Given a buffer buf, copy out a uint16_t, convert it from big-endian - to system byte order, and return it. + /** @brief Given a buffer buf, copy and return a value of type uint8_t. + */ + uint8_t from_big_endian_uint8_t(byte * buf); + /** @brief Given a buffer buf, copy out a uint16_t, convert it from big-endian + to system byte order, and return it. - @note Requires that at least 2 bytes be available in buf, but does not - check this - it is your responsibility. -*/ -uint16_t from_big_endian_uint16_t(byte * buf); + @note Requires that at least 2 bytes be available in buf, but does not + check this - it is your responsibility. + */ + uint16_t from_big_endian_uint16_t(byte * buf); -/** @brief Given a buffer buf, copy out a uint32_t, convert it from big-endian - to system byte order, and return it. + /** @brief Given a buffer buf, copy out a uint32_t, convert it from big-endian + to system byte order, and return it. - @note Requires that at least 4 bytes be available in buf, but does not - check this - it is your responsibility. -*/ -uint32_t from_big_endian_uint32_t(byte * buf); -/** @} */ + @note Requires that at least 4 bytes be available in buf, but does not + check this - it is your responsibility. + */ + uint32_t from_big_endian_uint32_t(byte * buf); + /** @} */ -/** @addtogroup systemtobe System-byte-order value to big-endian buffer - @{ -*/ + /** @addtogroup systemtobe System-byte-order value to big-endian buffer + @{ + */ -/** @brief Copies the value val into the buffer buf. - @note Requires that at least 1 byte is available in buf, but does not - check this - it is your responsibility. -*/ -void to_big_endian_uint8_t(byte * buf, uint8_t val); + /** @brief Copies the value val into the buffer buf. + @note Requires that at least 1 byte is available in buf, but does not + check this - it is your responsibility. + */ + void to_big_endian_uint8_t(byte * buf, uint8_t val); -/** @brief Converts the value val from system byte order to big endian, - and copies it into the given buffer starting at buf. + /** @brief Converts the value val from system byte order to big endian, + and copies it into the given buffer starting at buf. - @note Requires that at least 2 bytes be available in buf, but does not - check this - it is your responsibility. -*/ -void to_big_endian_uint16_t(byte * buf, uint16_t val); + @note Requires that at least 2 bytes be available in buf, but does not + check this - it is your responsibility. + */ + void to_big_endian_uint16_t(byte * buf, uint16_t val); -/** @brief Converts the value val from system byte order to big endian, - and copies it into the given buffer starting at buf. + /** @brief Converts the value val from system byte order to big endian, + and copies it into the given buffer starting at buf. - @note Requires that at least 4 bytes be available in buf, but does not - check this - it is your responsibility. -*/ -void to_big_endian_uint32_t(byte * buf, uint32_t val); -/** @} -*/ + @note Requires that at least 4 bytes be available in buf, but does not + check this - it is your responsibility. + */ + void to_big_endian_uint32_t(byte * buf, uint32_t val); + /** @} + */ -/** @addtogroup bufferfunc Buffering functions - @brief These wrap around from/to_big_endian_TYPE, but take a byte** so that - they can advance the input/output pointer appropriately. - @{ -*/ -/** @brief Converts the value val from system byte order to big endian, - copies it into the given buffer starting at *buf, and advances buf by - sizeof(uint16_t). -*/ -void buffer_big_endian_uint16_t(byte ** buf, uint16_t val); + /** @addtogroup bufferfunc Buffering functions + @brief These wrap around from/to_big_endian_TYPE, but take a byte** so that + they can advance the input/output pointer appropriately. + @{ + */ + /** @brief Converts the value val from system byte order to big endian, + copies it into the given buffer starting at *buf, and advances buf by + sizeof(uint16_t). + */ + void buffer_big_endian_uint16_t(byte ** buf, uint16_t val); -/** @brief Given the address of a buffer pointer buf, copy out a uint16_t - from *buf, convert it from big-endian to system byte order, advance - buf by sizeof(uint16_t), and return the value retrieved. -*/ -uint16_t unbuffer_big_endian_uint16_t(byte ** buf); + /** @brief Given the address of a buffer pointer buf, copy out a uint16_t + from *buf, convert it from big-endian to system byte order, advance + buf by sizeof(uint16_t), and return the value retrieved. + */ + uint16_t unbuffer_big_endian_uint16_t(byte ** buf); -/** @sa buffer_big_endian_uint16_t() -*/ -void buffer_big_endian_uint8_t(byte ** buf, uint8_t val); + /** @sa buffer_big_endian_uint16_t() + */ + void buffer_big_endian_uint8_t(byte ** buf, uint8_t val); -/** @sa unbuffer_big_endian_uint8_t -*/ -uint8_t unbuffer_big_endian_uint8_t(byte ** buf); + /** @sa unbuffer_big_endian_uint8_t + */ + uint8_t unbuffer_big_endian_uint8_t(byte ** buf); -/** @sa buffer_big_endian_uint16_t -*/ -void buffer_big_endian_uint32_t(byte ** buf, uint32_t val); + /** @sa buffer_big_endian_uint16_t + */ + void buffer_big_endian_uint32_t(byte ** buf, uint32_t val); -/** @sa unbuffer_big_endian_uint32_t -*/ -uint8_t unbuffer_big_endian_uint32_t(byte ** buf) + /** @sa unbuffer_big_endian_uint32_t + */ + uint8_t unbuffer_big_endian_uint32_t(byte ** buf) -/** @} */ + /** @} */ #else /* this else is true when not in doxygen */ -INLINE_UTIL void to_big_endian_uint8_t(byte * buf, uint8_t val) { - memcpy(buf, &val, 1); -} + INLINE_UTIL void to_big_endian_uint8_t(byte * buf, uint8_t val) { + memcpy(buf, &val, 1); + } -INLINE_UTIL uint8_t from_big_endian_uint8_t(byte * buf) { - uint8_t beVal; - memcpy(&beVal, buf, 1); - return beVal; -} + INLINE_UTIL uint8_t from_big_endian_uint8_t(byte * buf) { + uint8_t beVal; + memcpy(&beVal, buf, 1); + return beVal; + } #define WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(_TYPE, _TOBE, _FROMBE) \ INLINE_UTIL void to_big_endian_##_TYPE(byte * buf, _TYPE val) { \ @@ -407,8 +407,8 @@ INLINE_UTIL _TYPE from_big_endian_##_TYPE(byte * buf) { \ return _FROMBE(beVal); \ } -WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint16_t, htons, ntohs) -WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint32_t, htonl, ntohl) + WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint16_t, htons, ntohs) + WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint32_t, htonl, ntohl) #undef WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS @@ -423,9 +423,9 @@ INLINE_UTIL _TYPE unbuffer_big_endian_##_TYPE (byte ** buf) { \ return from_big_endian_##_TYPE(current); \ } -WIIUSE_DECLARE_BUFFERING_OPS(uint8_t) -WIIUSE_DECLARE_BUFFERING_OPS(uint16_t) -WIIUSE_DECLARE_BUFFERING_OPS(uint32_t) + WIIUSE_DECLARE_BUFFERING_OPS(uint8_t) + WIIUSE_DECLARE_BUFFERING_OPS(uint16_t) + WIIUSE_DECLARE_BUFFERING_OPS(uint32_t) #undef WIIUSE_DECLARE_BUFFERING_OPS