From 9124fa7cc7fa5aa67494295201416d25d9c977ad Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:24:41 -0500 Subject: [PATCH] De-duplicate handling of wiimote accelerometer data. --- src/events.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src/events.c b/src/events.c index 9d063a5..d53faa4 100644 --- a/src/events.c +++ b/src/events.c @@ -294,6 +294,24 @@ static void clear_dirty_reads(struct wiimote_t* wm) { } } +/** + * @brief Handle accel data in a wiimote message. + * + * @param wm Pointer to a wiimote_t structure. + * @param msg The message specified in the event packet. + */ +static void handle_wm_accel(struct wiimote_t* wm, byte* msg) { + wm->accel.x = msg[2]; + wm->accel.y = msg[3]; + wm->accel.z = msg[4]; + + /* calculate the remote orientation */ + calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); + + /* calculate the gforces on each axis */ + calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); +} + /** * @brief Analyze the event that occurred on a wiimote. @@ -319,15 +337,7 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { /* button - motion */ wiiuse_pressed_buttons(wm, msg); - wm->accel.x = msg[2]; - wm->accel.y = msg[3]; - wm->accel.z = msg[4]; - - /* calculate the remote orientation */ - calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); - - /* calculate the gforces on each axis */ - calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); + handle_wm_accel(wm, msg); break; } @@ -360,12 +370,7 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { /* button - motion - expansion */ wiiuse_pressed_buttons(wm, msg); - wm->accel.x = msg[2]; - wm->accel.y = msg[3]; - wm->accel.z = msg[4]; - - calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); - calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); + handle_wm_accel(wm, msg); handle_expansion(wm, msg+5); @@ -376,12 +381,7 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { /* button - motion - ir */ wiiuse_pressed_buttons(wm, msg); - wm->accel.x = msg[2]; - wm->accel.y = msg[3]; - wm->accel.z = msg[4]; - - calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); - calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); + handle_wm_accel(wm, msg); /* ir */ calculate_extended_ir(wm, msg+5); @@ -404,12 +404,7 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { /* button - motion - ir - expansion */ wiiuse_pressed_buttons(wm, msg); - wm->accel.x = msg[2]; - wm->accel.y = msg[3]; - wm->accel.z = msg[4]; - - calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); - calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); + handle_wm_accel(wm, msg); handle_expansion(wm, msg+15);