diff --git a/CHANGELOG b/CHANGELOG index b7b02a9..0844445 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -15,7 +15,7 @@ http://sourceforge.net/projects/wiiuse/ v0.15 -- ??? --------------------------- Added: - - + - Callback API with constant-size WiimoteState_t. Thanks to bhartsho. Fixed: - Changed: diff --git a/src/events.c b/src/events.c index 1889ea4..1d8319d 100644 --- a/src/events.c +++ b/src/events.c @@ -192,6 +192,42 @@ int wiiuse_poll(struct wiimote_t** wm, int wiimotes) { } +///////////////////////////////////////////////////////////////////////////////////////////////////// +/* Harts API */ + +int wiiuse_update(struct wiimote_t** wiimotes, int nwiimotes, wiiuse_update_cb callback) { + if (wiiuse_poll(wiimotes, nwiimotes)) { + static struct WiimoteState_t s; + int i = 0; + for (; i < nwiimotes; ++i) { + switch (wiimotes[i]->event) { + case WIIUSE_NONE: + break; + default: + /* this could be: WIIUSE_EVENT, WIIUSE_STATUS, WIIUSE_CONNECT, etc.. */ + s.uid = wiimotes[i]->unid; + s.leds = wiimotes[i]->leds; + s.battery_level = wiimotes[i]->battery_level; + s.accel = wiimotes[i]->accel; + s.orient = wiimotes[i]->orient; + s.gforce = wiimotes[i]->gforce; + s.ir = wiimotes[i]->ir; + s.buttons = wiimotes[i]->btns; + s.buttons_held = wiimotes[i]->btns_held; + s.buttons_released = wiimotes[i]->btns_released; + s.event = wiimotes[i]->event; + s.state = wiimotes[i]->state; + s.expansion = wiimotes[i]->exp; + callback( &s ); + break; + } + } + return 1; + } + return 0; +} + + /** * @brief Called on a cycle where no significant change occurs. * diff --git a/src/wiiuse.h b/src/wiiuse.h index 68630f6..da1f6e0 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -773,6 +773,29 @@ WIIUSE_EXPORT extern void wiiuse_set_nunchuk_accel_threshold(struct wiimote_t* w /* this function not currently implemented... */ WIIUSE_EXPORT extern void wiiuse_set_wii_board_calib(struct wiimote_t *wm); +/* wiiuse.c harts API */ +typedef struct WiimoteState_t { + WCONST int uid; + WCONST byte leds; + WCONST float battery_level; + WCONST struct vec3b_t accel; + WCONST struct orient_t orient; + WCONST struct gforce_t gforce; + WCONST struct ir_t ir; + WCONST uint16_t buttons; + WCONST uint16_t buttons_held; + WCONST uint16_t buttons_released; + WCONST WIIUSE_EVENT_TYPE event; + WCONST int state; + WCONST struct expansion_t expansion; +} WiimoteState; + +typedef void (*wiiuse_update_cb)(struct WiimoteState_t* wm); + +WIIUSE_EXPORT extern int wiiuse_update(struct wiimote_t** wm, int wiimotes, wiiuse_update_cb callback); + +/* end harts API */ + #ifdef __cplusplus } #endif