From 61d26d509c5d2722d78a06c6b142538322ab8946 Mon Sep 17 00:00:00 2001 From: Lysann Schlegel Date: Sun, 11 Nov 2012 18:53:37 +0100 Subject: [PATCH] stop wiiuseexample when all wiimotes are disconnected --- example/example.c | 12 +++++++++++- src/wiiuse.h | 26 ++++++++++++++++++++++++++ src/wiiuse_internal.h | 26 -------------------------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/example/example.c b/example/example.c index 4eaeed6..1db8bc5 100644 --- a/example/example.c +++ b/example/example.c @@ -294,6 +294,16 @@ void test(struct wiimote_t* wm, byte* data, unsigned short len) { printf("test: %i [%x %x %x %x]\n", len, data[0], data[1], data[2], data[3]); } +short any_wiimote_connected(wiimote** wm, int wiimotes) { + if(!wm) return 0; + + for(int i = 0; i < wiimotes; i++) { + if(wm[i] && WIIMOTE_IS_CONNECTED(wm[i])) + return 1; + } + + return 0; +} /** @@ -390,7 +400,7 @@ int main(int argc, char** argv) { * This function will set the event flag for each wiimote * when the wiimote has things to report. */ - while (1) { + while (any_wiimote_connected(wiimotes, MAX_WIIMOTES)) { if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) { /* * This happens if something happened on any wiimote. diff --git a/src/wiiuse.h b/src/wiiuse.h index 4676b27..ec22a24 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -123,6 +123,32 @@ /** @defgroup publicapi External API */ /** @{ */ +/** @name Wiimote state flags and macros */ +/** @{ */ +#define WIIMOTE_STATE_DEV_FOUND 0x0001 +#define WIIMOTE_STATE_HANDSHAKE 0x0002 /* actual connection exists but no handshake yet */ +#define WIIMOTE_STATE_HANDSHAKE_COMPLETE 0x0004 /* actual connection exists but no handshake yet */ +#define WIIMOTE_STATE_CONNECTED 0x0008 +#define WIIMOTE_STATE_RUMBLE 0x0010 +#define WIIMOTE_STATE_ACC 0x0020 +#define WIIMOTE_STATE_EXP 0x0040 +#define WIIMOTE_STATE_IR 0x0080 +#define WIIMOTE_STATE_SPEAKER 0x0100 +#define WIIMOTE_STATE_IR_SENS_LVL1 0x0200 +#define WIIMOTE_STATE_IR_SENS_LVL2 0x0400 +#define WIIMOTE_STATE_IR_SENS_LVL3 0x0800 +#define WIIMOTE_STATE_IR_SENS_LVL4 0x1000 +#define WIIMOTE_STATE_IR_SENS_LVL5 0x2000 +#define WIIMOTE_STATE_EXP_HANDSHAKE 0x10000 /* actual M+ connection exists but no handshake yet */ +#define WIIMOTE_STATE_EXP_EXTERN 0x20000 /* actual M+ connection exists but handshake failed */ +#define WIIMOTE_STATE_EXP_FAILED 0x40000 /* actual M+ connection exists but handshake failed */ + +#define WIIMOTE_ID(wm) (wm->unid) + +#define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s)) +#define WIIMOTE_IS_CONNECTED(wm) (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED)) +/** @} */ + /** @name LED bit masks */ /** @{ */ #define WIIMOTE_LED_NONE 0x00 diff --git a/src/wiiuse_internal.h b/src/wiiuse_internal.h index 6aed90c..7c02113 100644 --- a/src/wiiuse_internal.h +++ b/src/wiiuse_internal.h @@ -221,31 +221,9 @@ * ********************/ -/* wiimote state flags - (some duplicated in wiiuse.h)*/ -#define WIIMOTE_STATE_DEV_FOUND 0x0001 -#define WIIMOTE_STATE_HANDSHAKE 0x0002 /* actual connection exists but no handshake yet */ -#define WIIMOTE_STATE_HANDSHAKE_COMPLETE 0x0004 /* actual connection exists but no handshake yet */ -#define WIIMOTE_STATE_CONNECTED 0x0008 -#define WIIMOTE_STATE_RUMBLE 0x0010 -#define WIIMOTE_STATE_ACC 0x0020 -#define WIIMOTE_STATE_EXP 0x0040 -#define WIIMOTE_STATE_IR 0x0080 -#define WIIMOTE_STATE_SPEAKER 0x0100 -#define WIIMOTE_STATE_IR_SENS_LVL1 0x0200 -#define WIIMOTE_STATE_IR_SENS_LVL2 0x0400 -#define WIIMOTE_STATE_IR_SENS_LVL3 0x0800 -#define WIIMOTE_STATE_IR_SENS_LVL4 0x1000 -#define WIIMOTE_STATE_IR_SENS_LVL5 0x2000 -#define WIIMOTE_STATE_EXP_HANDSHAKE 0x10000 /* actual M+ connection exists but no handshake yet */ -#define WIIMOTE_STATE_EXP_EXTERN 0x20000 /* actual M+ connection exists but handshake failed */ -#define WIIMOTE_STATE_EXP_FAILED 0x40000 /* actual M+ connection exists but handshake failed */ - - - #define WIIMOTE_INIT_STATES (WIIMOTE_STATE_IR_SENS_LVL3) /* macro to manage states */ -#define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s)) #define WIIMOTE_ENABLE_STATE(wm, s) (wm->state |= (s)) #define WIIMOTE_DISABLE_STATE(wm, s) (wm->state &= ~(s)) #define WIIMOTE_TOGGLE_STATE(wm, s) ((wm->state & (s)) ? WIIMOTE_DISABLE_STATE(wm, s) : WIIMOTE_ENABLE_STATE(wm, s)) @@ -257,10 +235,6 @@ #define NUNCHUK_IS_FLAG_SET(wm, s) ((*(wm->flags) & (s)) == (s)) -/* misc macros */ -#define WIIMOTE_ID(wm) (wm->unid) -#define WIIMOTE_IS_CONNECTED(wm) (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED)) - /* * Smooth tilt calculations are computed with the * exponential moving average formula: