From 8aaf572de7b262d29e0a67ac5cba1a6333a9f809 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Tue, 15 Feb 2011 14:59:52 +0000 Subject: [PATCH] Calibratie toegevoegd aan WiiuseJ --- cpp/wiiuse/definitions.h | 20 +- cpp/wiiuse/io_win.c | 30 +- cpp/wiiuse/msvc/wiiuse.suo | Bin 29696 -> 32768 bytes cpp/wiiuse/wiiuse.c | 1406 +++++++++++----------- cpp/wiiusej/lib/wiiuse.lib | Bin 10344 -> 10344 bytes cpp/wiiusej/msvc/WiiUseJ.suo | Bin 19456 -> 19968 bytes cpp/wiiusej/msvc/WiiUseJ.vcxproj | 1 + cpp/wiiusej/msvc/WiiUseJ.vcxproj.filters | 3 + cpp/wiiusej/wiiusej_Calibration.c | 21 + cpp/wiiusej/wiiusej_WiiUseApi.c | 2 +- cpp/wiiusej/wiiusej_WiiUseApi.def | 34 - cpp/wiiusej/wiiusej_WiiUseApi.h | 38 +- java/native/WiiUseJ.dll | Bin 14336 -> 14848 bytes 13 files changed, 757 insertions(+), 798 deletions(-) create mode 100644 cpp/wiiusej/wiiusej_Calibration.c delete mode 100644 cpp/wiiusej/wiiusej_WiiUseApi.def diff --git a/cpp/wiiuse/definitions.h b/cpp/wiiuse/definitions.h index 5a8da85..d1d3e6c 100644 --- a/cpp/wiiuse/definitions.h +++ b/cpp/wiiuse/definitions.h @@ -39,7 +39,7 @@ #define WIIMOTE_PI 3.14159265f -//#define WITH_WIIUSE_DEBUG +#define WITH_WIIUSE_DEBUG /* Error output macros */ #define WIIUSE_ERROR(fmt, ...) fprintf(stderr, "[ERROR] " fmt "\n", ##__VA_ARGS__) @@ -50,16 +50,16 @@ /* Information output macros */ #define WIIUSE_INFO(fmt, ...) fprintf(stderr, "[INFO] " fmt "\n", ##__VA_ARGS__) -#ifdef WITH_WIIUSE_DEBUG - #ifdef WIN32 - #define WIIUSE_DEBUG(fmt, ...) do { \ - char* file = __FILE__; \ - int i = strlen(file) - 1; \ - for (; i && (file[i] != '\\'); --i); \ - fprintf(stderr, "[DEBUG] %s:%i: " fmt "\n", file+i+1, __LINE__, ##__VA_ARGS__); \ - } while (0) +#ifdef WITH_WIIUSE_DEBUG + #ifdef WIN32 + #define WIIUSE_DEBUG(fmt, ...) do { \ + char* file = __FILE__; \ + int i = strlen(file) - 1; \ + for (; i && (file[i] != '\\'); --i); \ + fprintf(stderr, "[DEBUG] %s:%i: " fmt "\n", file+i+1, __LINE__, ##__VA_ARGS__); \ + } while (0) #else - #define WIIUSE_DEBUG(fmt, ...) fprintf(stderr, "[DEBUG] " __FILE__ ":%i: " fmt "\n", __LINE__, ##__VA_ARGS__) + #define WIIUSE_DEBUG(fmt, ...) fprintf(stderr, "[DEBUG] " __FILE__ ":%i: " fmt "\n", __LINE__, ##__VA_ARGS__) #endif #else #define WIIUSE_DEBUG(fmt, ...) diff --git a/cpp/wiiuse/io_win.c b/cpp/wiiuse/io_win.c index 17fac36..85ddd10 100644 --- a/cpp/wiiuse/io_win.c +++ b/cpp/wiiuse/io_win.c @@ -32,14 +32,14 @@ */ #ifdef WIN32 - + #include #include #include #include #include - + #include "definitions.h" #include "wiiuse_internal.h" #include "io.h" @@ -176,8 +176,8 @@ int wiiuse_io_read(struct wiimote_t* wm) { if (!ReadFile(wm->dev_handle, wm->event_buf, sizeof(wm->event_buf), &b, &wm->hid_overlap)) { /* partial read */ - b = GetLastError(); - + b = GetLastError(); + if ((b == ERROR_HANDLE_EOF) || (b == ERROR_DEVICE_NOT_CONNECTED)) { /* remote disconnect */ wiiuse_disconnected(wm); @@ -186,11 +186,11 @@ int wiiuse_io_read(struct wiimote_t* wm) { r = WaitForSingleObject(wm->hid_overlap.hEvent, wm->timeout); if (r == WAIT_TIMEOUT) { - /* timeout - cancel and continue */ - - if (*wm->event_buf) - WIIUSE_WARNING("Packet ignored. This may indicate a problem (timeout is %i ms).", wm->timeout); - + /* timeout - cancel and continue */ + + if (*wm->event_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); return 0; @@ -203,25 +203,25 @@ int wiiuse_io_read(struct wiimote_t* wm) { return 0; } - ResetEvent(wm->hid_overlap.hEvent); + ResetEvent(wm->hid_overlap.hEvent); return 1; } int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len) { DWORD bytes; - int i; - + int i; + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) - return 0; - + return 0; + switch (wm->stack) { case WIIUSE_STACK_UNKNOWN: { /* try to auto-detect the stack type */ if (i = WriteFile(wm->dev_handle, buf, 22, &bytes, &wm->hid_overlap)) { /* bluesoleil will always return 1 here, even if it's not connected */ - wm->stack = WIIUSE_STACK_BLUESOLEIL; + wm->stack = WIIUSE_STACK_BLUESOLEIL; return i; } diff --git a/cpp/wiiuse/msvc/wiiuse.suo b/cpp/wiiuse/msvc/wiiuse.suo index 982849a6005d31d0a1ba97bb4ccf4a647cd0acf8..574cebf15f695e4af11e60266bd3e9c9b63c12ee 100644 GIT binary patch delta 3471 zcmcInYfO_@7(VA~3oTTvfD^emD58KZ^a>Q6wotqvyZ$C}e1s8uUo5_~>XUndMF)kwuG1Fvo?`iu{bcpS;9rEP7=hE{% z=Q-zn-^1uFIy%ZtSgGWSt(cP<2qEC*?6z_={(2B8qHV_hQp5&C8N!0th+yNJuzm)y z8Bq*5&Xh^l+?2F4&IRKR?sK6>dY8&gIJOr7-EmIkb7-&yYgot4xCS3vWhXXvAsmQW z#BM|#q8`zRU~?E6(B2nr@5kDScmZ($(S&dznh`G|S`e)WH{vCP7jY25BYF`Xh)(2i zDLyB=4{f&o0M>(HdkE_zh?59mHvT3j%|Hvr*5|`q!WRl+!&cIKPohIE!-gW|t=LKr_rw{|xD^lgRn^i&)^{UcBY0P=lh6q`tjU8wss=Qu&KL>g)9Khup1vv(wwt=jm{^bay*LM8Z{3%kc0KyRJ%aGgp>qD~x7~ z)>LKEYb_NeI<3)IVzw9zl?I!^F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p)bAvi?1U`e@_J+TWt_ols@WI$jx%%hNroD8*&4A4$QoJXa z2ekJ!j0EMSzGZ44>eD=x(KP~sI0hbg9GvBfSJA-tvkma2W*=N1zQ~jPNd^yEZSZP( zG;w~}KzuQq~PbmE)IMck0G`zHOTmxf(0Q)@@}xU<8~ s{- - * Email: < thepara (--AT--) g m a i l [--DOT--] com > - * - * Copyright 2006-2007 - * - * This file is part of wiiuse. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * $Header$ - * - */ - -/** - * @file - * @brief General wiimote operations. - * - * The file includes functions that handle general - * tasks. Most of these are functions that are part - * of the API. - */ - -#include -#include - -#ifndef WIN32 - #include -#else - #include -#endif - -#include "definitions.h" -#include "wiiuse_internal.h" -#include "events.h" -#include "io.h" - -static int g_banner = 0; - -/** - * @breif Returns the version of the library. - */ -const char* wiiuse_version() { - return WIIUSE_VERSION; -} - - -/** - * @brief Clean up wiimote_t array created by wiiuse_init() - */ -void wiiuse_cleanup(struct wiimote_t** wm, int wiimotes) { - int i = 0; - - if (!wm) - return; - - WIIUSE_INFO("wiiuse clean up..."); - - for (; i < wiimotes; ++i) { - wiiuse_disconnect(wm[i]); - free(wm[i]); - } - - free(wm); - - return; -} - - -/** - * @brief Initialize an array of wiimote structures. - * - * @param wiimotes Number of wiimote_t structures to create. - * - * @return An array of initialized wiimote_t structures. - * - * @see wiiuse_connect() - * - * The array returned by this function can be passed to various - * functions, including wiiuse_connect(). - */ -struct wiimote_t** wiiuse_init(int wiimotes) { - int i = 0; - struct wiimote_t** wm = NULL; - - /* - * Please do not remove this banner. - * GPL asks that you please leave output credits intact. - * Thank you. - * - * This banner is only displayed once so that if you need - * to call this function again it won't be intrusive. - */ - if (!g_banner) { - printf( "wiiuse v" WIIUSE_VERSION " loaded.\n" - " By: Michael Laforest \n" - " http://wiiuse.net http://wiiuse.sf.net\n"); - g_banner = 1; - } - - if (!wiimotes) - return NULL; - - wm = malloc(sizeof(struct wiimote_t*) * wiimotes); - - for (i = 0; i < wiimotes; ++i) { - wm[i] = malloc(sizeof(struct wiimote_t)); - memset(wm[i], 0, sizeof(struct wiimote_t)); - - wm[i]->unid = i+1; - - #ifndef WIN32 - wm[i]->bdaddr = *BDADDR_ANY; - wm[i]->out_sock = -1; - wm[i]->in_sock = -1; - #else - wm[i]->dev_handle = 0; - wm[i]->stack = WIIUSE_STACK_UNKNOWN; - wm[i]->normal_timeout = WIIMOTE_DEFAULT_TIMEOUT; - wm[i]->exp_timeout = WIIMOTE_EXP_TIMEOUT; - wm[i]->timeout = wm[i]->normal_timeout; - #endif - - wm[i]->state = WIIMOTE_INIT_STATES; - wm[i]->flags = WIIUSE_INIT_FLAGS; - - wm[i]->event = WIIUSE_NONE; - - wm[i]->exp.type = EXP_NONE; - - wiiuse_set_aspect_ratio(wm[i], WIIUSE_ASPECT_4_3); - wiiuse_set_ir_position(wm[i], WIIUSE_IR_ABOVE); - - wm[i]->orient_threshold = 0.5f; - wm[i]->accel_threshold = 5; - - wm[i]->accel_calib.st_alpha = WIIUSE_DEFAULT_SMOOTH_ALPHA; - } - - return wm; -} - - -/** - * @brief The wiimote disconnected. - * - * @param wm Pointer to a wiimote_t structure. - */ -void wiiuse_disconnected(struct wiimote_t* wm) { - if (!wm) return; - - WIIUSE_INFO("Wiimote disconnected [id %i].", wm->unid); - - /* disable the connected flag */ - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED); - - /* reset a bunch of stuff */ - #ifndef WIN32 - wm->out_sock = -1; - wm->in_sock = -1; - #else - wm->dev_handle = 0; - #endif - - wm->leds = 0; - wm->state = WIIMOTE_INIT_STATES; - wm->read_req = NULL; - wm->handshake_state = 0; - wm->btns = 0; - wm->btns_held = 0; - wm->btns_released = 0; - memset(wm->event_buf, 0, sizeof(wm->event_buf)); - - wm->event = WIIUSE_DISCONNECT; -} - - -/** - * @brief Enable or disable the rumble. - * - * @param wm Pointer to a wiimote_t structure. - * @param status 1 to enable, 0 to disable. - */ -void wiiuse_rumble(struct wiimote_t* wm, int status) { - byte buf; - - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) - return; - - /* make sure to keep the current lit leds */ - buf = wm->leds; - - if (status) { - WIIUSE_DEBUG("Starting rumble..."); - WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_RUMBLE); - buf |= 0x01; - } else { - WIIUSE_DEBUG("Stopping rumble..."); - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_RUMBLE); - } - - /* preserve IR state */ - if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) - buf |= 0x04; - - wiiuse_send(wm, WM_CMD_RUMBLE, &buf, 1); -} - - -/** - * @brief Toggle the state of the rumble. - * - * @param wm Pointer to a wiimote_t structure. - */ -void wiiuse_toggle_rumble(struct wiimote_t* wm) { - if (!wm) return; - - wiiuse_rumble(wm, !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)); -} - - -/** - * @brief Set the enabled LEDs. - * - * @param wm Pointer to a wiimote_t structure. - * @param leds What LEDs to enable. - * - * \a leds is a bitwise or of WIIMOTE_LED_1, WIIMOTE_LED_2, WIIMOTE_LED_3, or WIIMOTE_LED_4. - */ -void wiiuse_set_leds(struct wiimote_t* wm, int leds) { - byte buf; - - 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)) - wm->leds |= 0x01; - - buf = wm->leds; - - wiiuse_send(wm, WM_CMD_LED, &buf, 1); -} - - -/** - * @brief Set if the wiimote should report motion sensing. - * - * @param wm Pointer to a wiimote_t structure. - * @param status 1 to enable, 0 to disable. - * - * Since reporting motion sensing sends a lot of data, - * the wiimote saves power by not transmitting it - * by default. - */ -void wiiuse_motion_sensing(struct wiimote_t* wm, int status) { - if (status) - WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_ACC); - else - WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_ACC); - - wiiuse_set_report_type(wm); -} - - -/** - * @brief Set the report type based on the current wiimote state. - * - * @param wm Pointer to a wiimote_t structure. - * - * @return The report type sent. - * - * The wiimote reports formatted packets depending on the - * report type that was last requested. This function will - * update the type of report that should be sent based on - * the current state of the device. - */ -int wiiuse_set_report_type(struct wiimote_t* wm) { - byte buf[2]; - int motion, exp, ir; - - 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)) - 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; - - WIIUSE_DEBUG("Setting report type: 0x%x", buf[1]); - - exp = wiiuse_send(wm, WM_CMD_REPORT_TYPE, buf, 2); - if (exp <= 0) - return exp; - - return buf[1]; -} - - -/** - * @brief Read data from the wiimote (callback version). - * - * @param wm Pointer to a wiimote_t structure. - * @param read_cb Function pointer to call when the data arrives from the wiimote. - * @param buffer An allocated buffer to store the data as it arrives from the wiimote. - * Must be persistent in memory and large enough to hold the data. - * @param addr The address of wiimote memory to read from. - * @param len The length of the block to be read. - * - * The library can only handle one data read request at a time - * because it must keep track of the buffer and other - * events that are specific to that request. So if a request - * has already been made, subsequent requests will be added - * to a pending list and be sent out when the previous - * finishes. - */ -int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int addr, unsigned short len) { - struct read_req_t* req; - - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) - return 0; - if (!buffer || !len || !read_cb) - return 0; - - /* make this request structure */ - req = (struct read_req_t*)malloc(sizeof(struct read_req_t)); - req->cb = read_cb; - req->buf = buffer; - req->addr = addr; - req->size = len; +/* + * wiiuse + * + * Written By: + * Michael Laforest < para > + * Email: < thepara (--AT--) g m a i l [--DOT--] com > + * + * Copyright 2006-2007 + * + * This file is part of wiiuse. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * $Header$ + * + */ + +/** + * @file + * @brief General wiimote operations. + * + * The file includes functions that handle general + * tasks. Most of these are functions that are part + * of the API. + */ + +#include +#include + +#ifndef WIN32 + #include +#else + #include +#endif + +#include "definitions.h" +#include "wiiuse_internal.h" +#include "events.h" +#include "io.h" + +static int g_banner = 1; + +/** + * @breif Returns the version of the library. + */ +const char* wiiuse_version() { + return WIIUSE_VERSION; +} + + +/** + * @brief Clean up wiimote_t array created by wiiuse_init() + */ +void wiiuse_cleanup(struct wiimote_t** wm, int wiimotes) { + int i = 0; + + if (!wm) + return; + + WIIUSE_INFO("wiiuse clean up..."); + + for (; i < wiimotes; ++i) { + wiiuse_disconnect(wm[i]); + free(wm[i]); + } + + free(wm); + + return; +} + + +/** + * @brief Initialize an array of wiimote structures. + * + * @param wiimotes Number of wiimote_t structures to create. + * + * @return An array of initialized wiimote_t structures. + * + * @see wiiuse_connect() + * + * The array returned by this function can be passed to various + * functions, including wiiuse_connect(). + */ +struct wiimote_t** wiiuse_init(int wiimotes) { + int i = 0; + struct wiimote_t** wm = NULL; + + /* + * Please do not remove this banner. + * GPL asks that you please leave output credits intact. + * Thank you. + * + * This banner is only displayed once so that if you need + * to call this function again it won't be intrusive. + */ + if (!g_banner) { + printf( "wiiuse v" WIIUSE_VERSION " loaded.\n" + " By: Michael Laforest \n" + " http://wiiuse.net http://wiiuse.sf.net\n"); + g_banner = 1; + } + + if (!wiimotes) + return NULL; + + wm = malloc(sizeof(struct wiimote_t*) * wiimotes); + + for (i = 0; i < wiimotes; ++i) { + wm[i] = malloc(sizeof(struct wiimote_t)); + memset(wm[i], 0, sizeof(struct wiimote_t)); + + wm[i]->unid = i+1; + + #ifndef WIN32 + wm[i]->bdaddr = *BDADDR_ANY; + wm[i]->out_sock = -1; + wm[i]->in_sock = -1; + #else + wm[i]->dev_handle = 0; + wm[i]->stack = WIIUSE_STACK_UNKNOWN; + wm[i]->normal_timeout = WIIMOTE_DEFAULT_TIMEOUT; + wm[i]->exp_timeout = WIIMOTE_EXP_TIMEOUT; + wm[i]->timeout = wm[i]->normal_timeout; + #endif + + wm[i]->state = WIIMOTE_INIT_STATES; + wm[i]->flags = WIIUSE_INIT_FLAGS; + + wm[i]->event = WIIUSE_NONE; + + wm[i]->exp.type = EXP_NONE; + + wiiuse_set_aspect_ratio(wm[i], WIIUSE_ASPECT_4_3); + wiiuse_set_ir_position(wm[i], WIIUSE_IR_ABOVE); + + wm[i]->orient_threshold = 0.5f; + wm[i]->accel_threshold = 5; + + wm[i]->accel_calib.st_alpha = WIIUSE_DEFAULT_SMOOTH_ALPHA; + } + + return wm; +} + + +/** + * @brief The wiimote disconnected. + * + * @param wm Pointer to a wiimote_t structure. + */ +void wiiuse_disconnected(struct wiimote_t* wm) { + if (!wm) return; + + WIIUSE_INFO("Wiimote disconnected [id %i].", wm->unid); + + /* disable the connected flag */ + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED); + + /* reset a bunch of stuff */ + #ifndef WIN32 + wm->out_sock = -1; + wm->in_sock = -1; + #else + wm->dev_handle = 0; + #endif + + wm->leds = 0; + wm->state = WIIMOTE_INIT_STATES; + wm->read_req = NULL; + wm->handshake_state = 0; + wm->btns = 0; + wm->btns_held = 0; + wm->btns_released = 0; + memset(wm->event_buf, 0, sizeof(wm->event_buf)); + + wm->event = WIIUSE_DISCONNECT; +} + + +/** + * @brief Enable or disable the rumble. + * + * @param wm Pointer to a wiimote_t structure. + * @param status 1 to enable, 0 to disable. + */ +void wiiuse_rumble(struct wiimote_t* wm, int status) { + byte buf; + + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + return; + + /* make sure to keep the current lit leds */ + buf = wm->leds; + + if (status) { + WIIUSE_DEBUG("Starting rumble..."); + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_RUMBLE); + buf |= 0x01; + } else { + WIIUSE_DEBUG("Stopping rumble..."); + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_RUMBLE); + } + + /* preserve IR state */ + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) + buf |= 0x04; + + wiiuse_send(wm, WM_CMD_RUMBLE, &buf, 1); +} + + +/** + * @brief Toggle the state of the rumble. + * + * @param wm Pointer to a wiimote_t structure. + */ +void wiiuse_toggle_rumble(struct wiimote_t* wm) { + if (!wm) return; + + wiiuse_rumble(wm, !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)); +} + + +/** + * @brief Set the enabled LEDs. + * + * @param wm Pointer to a wiimote_t structure. + * @param leds What LEDs to enable. + * + * \a leds is a bitwise or of WIIMOTE_LED_1, WIIMOTE_LED_2, WIIMOTE_LED_3, or WIIMOTE_LED_4. + */ +void wiiuse_set_leds(struct wiimote_t* wm, int leds) { + byte buf; + + 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)) + wm->leds |= 0x01; + + buf = wm->leds; + + wiiuse_send(wm, WM_CMD_LED, &buf, 1); +} + + +/** + * @brief Set if the wiimote should report motion sensing. + * + * @param wm Pointer to a wiimote_t structure. + * @param status 1 to enable, 0 to disable. + * + * Since reporting motion sensing sends a lot of data, + * the wiimote saves power by not transmitting it + * by default. + */ +void wiiuse_motion_sensing(struct wiimote_t* wm, int status) { + if (status) + WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_ACC); + else + WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_ACC); + + wiiuse_set_report_type(wm); +} + + +/** + * @brief Set the report type based on the current wiimote state. + * + * @param wm Pointer to a wiimote_t structure. + * + * @return The report type sent. + * + * The wiimote reports formatted packets depending on the + * report type that was last requested. This function will + * update the type of report that should be sent based on + * the current state of the device. + */ +int wiiuse_set_report_type(struct wiimote_t* wm) { + byte buf[2]; + int motion, exp, ir; + + 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)) + 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; + + WIIUSE_DEBUG("Setting report type: 0x%x", buf[1]); + + exp = wiiuse_send(wm, WM_CMD_REPORT_TYPE, buf, 2); + if (exp <= 0) + return exp; + + return buf[1]; +} + + +/** + * @brief Read data from the wiimote (callback version). + * + * @param wm Pointer to a wiimote_t structure. + * @param read_cb Function pointer to call when the data arrives from the wiimote. + * @param buffer An allocated buffer to store the data as it arrives from the wiimote. + * Must be persistent in memory and large enough to hold the data. + * @param addr The address of wiimote memory to read from. + * @param len The length of the block to be read. + * + * The library can only handle one data read request at a time + * because it must keep track of the buffer and other + * events that are specific to that request. So if a request + * has already been made, subsequent requests will be added + * to a pending list and be sent out when the previous + * finishes. + */ +int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int addr, unsigned short len) { + struct read_req_t* req; + + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + return 0; + if (!buffer || !len || !read_cb) + return 0; + + /* make this request structure */ + req = (struct read_req_t*)malloc(sizeof(struct read_req_t)); + req->cb = read_cb; + req->buf = buffer; + req->addr = addr; + req->size = len; req->wait = len; - req->dirty = 0; - req->next = NULL; - - /* add this to the request list */ - if (!wm->read_req) { - /* root node */ - wm->read_req = req; - - WIIUSE_DEBUG("Data read request can be sent out immediately."); - - /* send the request out immediately */ - wiiuse_send_next_pending_read_request(wm); - } else { - struct read_req_t* nptr = wm->read_req; - for (; nptr->next; nptr = nptr->next); - nptr->next = req; - - WIIUSE_DEBUG("Added pending data read request."); - } - - return 1; -} - - + req->dirty = 0; + req->next = NULL; + + /* add this to the request list */ + if (!wm->read_req) { + /* root node */ + wm->read_req = req; + + WIIUSE_DEBUG("Data read request can be sent out immediately."); + + /* send the request out immediately */ + wiiuse_send_next_pending_read_request(wm); + } else { + struct read_req_t* nptr = wm->read_req; + for (; nptr->next; nptr = nptr->next); + nptr->next = req; + + WIIUSE_DEBUG("Added pending data read request."); + } + + return 1; +} + + /** * @brief Read data from the wiimote (event version). * @@ -440,325 +440,325 @@ int wiiuse_read_data(struct wiimote_t* wm, byte* buffer, unsigned int addr, unsi } -/** - * @brief Send the next pending data read request to the wiimote. - * - * @param wm Pointer to a wiimote_t structure. - * - * @see wiiuse_read_data() - * - * This function is not part of the wiiuse API. - */ -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)) - return; - if (!wm->read_req) return; - +/** + * @brief Send the next pending data read request to the wiimote. + * + * @param wm Pointer to a wiimote_t structure. + * + * @see wiiuse_read_data() + * + * This function is not part of the wiiuse API. + */ +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)) + return; + if (!wm->read_req) return; + /* skip over dirty ones since they have already been read */ req = wm->read_req; while (req && req->dirty) req = req->next; if (!req) - return; - - /* the offset is in big endian */ - *(int*)(buf) = BIG_ENDIAN_LONG(req->addr); - - /* the length is in big endian */ - *(short*)(buf + 4) = BIG_ENDIAN_SHORT(req->size); - - WIIUSE_DEBUG("Request read at address: 0x%x length: %i", req->addr, req->size); - wiiuse_send(wm, WM_CMD_READ_DATA, buf, 6); -} - - -/** - * @brief Request the wiimote controller status. - * - * @param wm Pointer to a wiimote_t structure. - * - * Controller status includes: battery level, LED status, expansions - */ -void wiiuse_status(struct wiimote_t* wm) { - byte buf = 0; - - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) - return; - - WIIUSE_DEBUG("Requested wiimote status."); - - wiiuse_send(wm, WM_CMD_CTRL_STATUS, &buf, 1); -} - - -/** - * @brief Find a wiimote_t structure by its unique identifier. - * - * @param wm Pointer to a wiimote_t structure. - * @param wiimotes The number of wiimote_t structures in \a wm. - * @param unid The unique identifier to search for. - * - * @return Pointer to a wiimote_t structure, or NULL if not found. - */ -struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid) { - int i = 0; - - for (; i < wiimotes; ++i) { - if (wm[i]->unid == unid) - return wm[i]; - } - - return NULL; -} - - -/** - * @brief Write data to the wiimote. - * - * @param wm Pointer to a wiimote_t structure. - * @param addr The address to write to. - * @param data The data to be written to the memory location. - * @param len The length of the block to be written. - */ -int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, byte* data, byte len) { - byte buf[21] = {0}; /* the payload is always 23 */ - - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) - return 0; - if (!data || !len) - return 0; - - WIIUSE_DEBUG("Writing %i bytes to memory location 0x%x...", len, addr); - - #ifdef WITH_WIIUSE_DEBUG - { - int i = 0; - printf("Write data is: "); - for (; i < len; ++i) - printf("%x ", data[i]); - printf("\n"); - } - #endif - - /* the offset is in big endian */ - *(int*)(buf) = BIG_ENDIAN_LONG(addr); - - /* length */ - *(byte*)(buf + 4) = len; - - /* data */ - memcpy(buf + 5, data, len); - - wiiuse_send(wm, WM_CMD_WRITE_DATA, buf, 21); - return 1; -} - - -/** - * @brief Send a packet to the wiimote. - * - * @param wm Pointer to a wiimote_t structure. - * @param report_type The report type to send (WIIMOTE_CMD_LED, WIIMOTE_CMD_RUMBLE, etc). Found in wiiuse.h - * @param msg The payload. - * @param len Length of the payload in bytes. - * - * This function should replace any write()s directly to the wiimote device. - */ -int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { - byte buf[32]; /* no payload is better than this */ - int rumble = 0; - - #ifndef WIN32 - buf[0] = WM_SET_REPORT | WM_BT_OUTPUT; - buf[1] = report_type; - #else - buf[0] = report_type; - #endif - - 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)) - rumble = 1; - break; - } - default: - break; - } - - #ifndef WIN32 - memcpy(buf+2, msg, len); - if (rumble) - buf[2] |= 0x01; - #else - memcpy(buf+1, msg, len); - if (rumble) - buf[1] |= 0x01; - #endif - - #ifdef WITH_WIIUSE_DEBUG - { - int x = 2; - printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[0], buf[1]); - #ifndef WIN32 - for (; x < len+2; ++x) - #else - for (; x < len+1; ++x) - #endif - printf("%.2x ", buf[x]); - printf("\n"); - } - #endif - - #ifndef WIN32 - return wiiuse_io_write(wm, buf, len+2); - #else - return wiiuse_io_write(wm, buf, len+1); - #endif -} - - -/** - * @brief Set flags for the specified wiimote. - * - * @param wm Pointer to a wiimote_t structure. - * @param enable Flags to enable. - * @param disable Flags to disable. - * - * @return The flags set after 'enable' and 'disable' have been applied. - * - * The values 'enable' and 'disable' may be any flags OR'ed together. - * Flags are defined in wiiuse.h. - */ -int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable) { - if (!wm) return 0; - - /* remove mutually exclusive flags */ - enable &= ~disable; - disable &= ~enable; - - wm->flags |= enable; - wm->flags &= ~disable; - - return wm->flags; -} - - -/** - * @brief Set the wiimote smoothing alpha value. - * - * @param wm Pointer to a wiimote_t structure. - * @param alpha The alpha value to set. Between 0 and 1. - * - * @return Returns the old alpha value. - * - * The alpha value is between 0 and 1 and is used in an exponential - * smoothing algorithm. - * - * Smoothing is only performed if the WIIMOTE_USE_SMOOTHING is set. - */ -float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha) { - float old; - - 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) - wm->exp.nunchuk.accel_calib.st_alpha = alpha; - - return old; -} - - -/** - * @brief Set the bluetooth stack type to use. - * - * @param wm Array of wiimote_t structures. - * @param wiimotes Number of objects in the wm array. - * @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 WIN32 - int i; - - if (!wm) return; - - for (i = 0; i < wiimotes; ++i) - wm[i]->stack = type; - #endif -} - - -/** - * @brief Set the orientation event threshold. - * - * @param wm Pointer to a wiimote_t structure. - * @param threshold The decimal place that should be considered a significant change. - * - * If threshold is 0.01, and any angle changes by 0.01 then a significant change - * has occured and the event callback will be invoked. If threshold is 1 then - * 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; - - wm->orient_threshold = threshold; -} - - -/** - * @brief Set the accelerometer event threshold. - * - * @param wm Pointer to a wiimote_t structure. - * @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; - - wm->accel_threshold = threshold; -} - - -/** - * @brief Try to resync with the wiimote by starting a new handshake. - * - * @param wm Pointer to a wiimote_t structure. - */ -void wiiuse_resync(struct wiimote_t* wm) { - if (!wm) return; - - wm->handshake_state = 0; - wiiuse_handshake(wm, NULL, 0); -} - - -/** - * @brief Set the normal and expansion handshake timeouts. - * - * @param wm Array of wiimote_t structures. - * @param wiimotes Number of objects in the wm array. - * @param normal_timeout The timeout in milliseconds for a normal read. - * @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 WIN32 - int i; - - if (!wm) return; - - for (i = 0; i < wiimotes; ++i) { - wm[i]->normal_timeout = normal_timeout; - wm[i]->exp_timeout = exp_timeout; - } - #endif -} + return; + + /* the offset is in big endian */ + *(int*)(buf) = BIG_ENDIAN_LONG(req->addr); + + /* the length is in big endian */ + *(short*)(buf + 4) = BIG_ENDIAN_SHORT(req->size); + + WIIUSE_DEBUG("Request read at address: 0x%x length: %i", req->addr, req->size); + wiiuse_send(wm, WM_CMD_READ_DATA, buf, 6); +} + + +/** + * @brief Request the wiimote controller status. + * + * @param wm Pointer to a wiimote_t structure. + * + * Controller status includes: battery level, LED status, expansions + */ +void wiiuse_status(struct wiimote_t* wm) { + byte buf = 0; + + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + return; + + WIIUSE_DEBUG("Requested wiimote status."); + + wiiuse_send(wm, WM_CMD_CTRL_STATUS, &buf, 1); +} + + +/** + * @brief Find a wiimote_t structure by its unique identifier. + * + * @param wm Pointer to a wiimote_t structure. + * @param wiimotes The number of wiimote_t structures in \a wm. + * @param unid The unique identifier to search for. + * + * @return Pointer to a wiimote_t structure, or NULL if not found. + */ +struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid) { + int i = 0; + + for (; i < wiimotes; ++i) { + if (wm[i]->unid == unid) + return wm[i]; + } + + return NULL; +} + + +/** + * @brief Write data to the wiimote. + * + * @param wm Pointer to a wiimote_t structure. + * @param addr The address to write to. + * @param data The data to be written to the memory location. + * @param len The length of the block to be written. + */ +int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, byte* data, byte len) { + byte buf[21] = {0}; /* the payload is always 23 */ + + if (!wm || !WIIMOTE_IS_CONNECTED(wm)) + return 0; + if (!data || !len) + return 0; + + WIIUSE_DEBUG("Writing %i bytes to memory location 0x%x...", len, addr); + + #ifdef WITH_WIIUSE_DEBUG + { + int i = 0; + printf("Write data is: "); + for (; i < len; ++i) + printf("%x ", data[i]); + printf("\n"); + } + #endif + + /* the offset is in big endian */ + *(int*)(buf) = BIG_ENDIAN_LONG(addr); + + /* length */ + *(byte*)(buf + 4) = len; + + /* data */ + memcpy(buf + 5, data, len); + + wiiuse_send(wm, WM_CMD_WRITE_DATA, buf, 21); + return 1; +} + + +/** + * @brief Send a packet to the wiimote. + * + * @param wm Pointer to a wiimote_t structure. + * @param report_type The report type to send (WIIMOTE_CMD_LED, WIIMOTE_CMD_RUMBLE, etc). Found in wiiuse.h + * @param msg The payload. + * @param len Length of the payload in bytes. + * + * This function should replace any write()s directly to the wiimote device. + */ +int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { + byte buf[32]; /* no payload is better than this */ + int rumble = 0; + + #ifndef WIN32 + buf[0] = WM_SET_REPORT | WM_BT_OUTPUT; + buf[1] = report_type; + #else + buf[0] = report_type; + #endif + + 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)) + rumble = 1; + break; + } + default: + break; + } + + #ifndef WIN32 + memcpy(buf+2, msg, len); + if (rumble) + buf[2] |= 0x01; + #else + memcpy(buf+1, msg, len); + if (rumble) + buf[1] |= 0x01; + #endif + + #ifdef WITH_WIIUSE_DEBUG + { + int x = 2; + printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[0], buf[1]); + #ifndef WIN32 + for (; x < len+2; ++x) + #else + for (; x < len+1; ++x) + #endif + printf("%.2x ", buf[x]); + printf("\n"); + } + #endif + + #ifndef WIN32 + return wiiuse_io_write(wm, buf, len+2); + #else + return wiiuse_io_write(wm, buf, len+1); + #endif +} + + +/** + * @brief Set flags for the specified wiimote. + * + * @param wm Pointer to a wiimote_t structure. + * @param enable Flags to enable. + * @param disable Flags to disable. + * + * @return The flags set after 'enable' and 'disable' have been applied. + * + * The values 'enable' and 'disable' may be any flags OR'ed together. + * Flags are defined in wiiuse.h. + */ +int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable) { + if (!wm) return 0; + + /* remove mutually exclusive flags */ + enable &= ~disable; + disable &= ~enable; + + wm->flags |= enable; + wm->flags &= ~disable; + + return wm->flags; +} + + +/** + * @brief Set the wiimote smoothing alpha value. + * + * @param wm Pointer to a wiimote_t structure. + * @param alpha The alpha value to set. Between 0 and 1. + * + * @return Returns the old alpha value. + * + * The alpha value is between 0 and 1 and is used in an exponential + * smoothing algorithm. + * + * Smoothing is only performed if the WIIMOTE_USE_SMOOTHING is set. + */ +float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha) { + float old; + + 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) + wm->exp.nunchuk.accel_calib.st_alpha = alpha; + + return old; +} + + +/** + * @brief Set the bluetooth stack type to use. + * + * @param wm Array of wiimote_t structures. + * @param wiimotes Number of objects in the wm array. + * @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 WIN32 + int i; + + if (!wm) return; + + for (i = 0; i < wiimotes; ++i) + wm[i]->stack = type; + #endif +} + + +/** + * @brief Set the orientation event threshold. + * + * @param wm Pointer to a wiimote_t structure. + * @param threshold The decimal place that should be considered a significant change. + * + * If threshold is 0.01, and any angle changes by 0.01 then a significant change + * has occured and the event callback will be invoked. If threshold is 1 then + * 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; + + wm->orient_threshold = threshold; +} + + +/** + * @brief Set the accelerometer event threshold. + * + * @param wm Pointer to a wiimote_t structure. + * @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; + + wm->accel_threshold = threshold; +} + + +/** + * @brief Try to resync with the wiimote by starting a new handshake. + * + * @param wm Pointer to a wiimote_t structure. + */ +void wiiuse_resync(struct wiimote_t* wm) { + if (!wm) return; + + wm->handshake_state = 0; + wiiuse_handshake(wm, NULL, 0); +} + + +/** + * @brief Set the normal and expansion handshake timeouts. + * + * @param wm Array of wiimote_t structures. + * @param wiimotes Number of objects in the wm array. + * @param normal_timeout The timeout in milliseconds for a normal read. + * @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 WIN32 + int i; + + if (!wm) return; + + for (i = 0; i < wiimotes; ++i) { + wm[i]->normal_timeout = normal_timeout; + wm[i]->exp_timeout = exp_timeout; + } + #endif +} diff --git a/cpp/wiiusej/lib/wiiuse.lib b/cpp/wiiusej/lib/wiiuse.lib index 98aeaa3243b335cc2e7ca93cbc7f43e7f7c8d5f3..93409c6339e05df5ad1faf58cac35cf661205275 100644 GIT binary patch delta 1057 zcmY+Dzi-n(6vtgfEwu_wTgN`9j^neN*b%5oJEWl?1OYKHRCtAjNKA|jY(XXP`nLb> zinPaH_xtuu`V5Ccz=xG`=~Msy{+E@rnTbTe&q{d~=R5DA?M2|bzluKwo)N8kF^dkx zMP&rf#WtZd`ZF2jp@2&*N0aybt3;~t%cf(iL}^)82_E<^{O}V3j;qGKTkX(Dwf`O( ztJhVwBJ#p2lY@p4KUuv_VxoResG{H9fc*x65s!i{iD{J8c$9~RdR4nkl-lWAV*1%( zZ!$LI(~!w=@`Bnv#O=%0JbavUV5u%iMUfBd9678TqtEq%w}8=_iR?wUNUWMXN*wJU zp}&Lv!zCAvBKB7mmZecXWi^@UCg|~8p%oXx)jBFs{X(WY%I4UB$ zapk<)uZ4)P<}Jw2g+-=#S_{e4Mp#|HmuJZ}axBuP(=aekcZoJpz#%Q236U>ZN=5SF! wC*@2Ec#(1d-TB{??_6c>WTt(LP7URBy6C~1uyB>;U3|^>8kLOxb-h;h9~z-1-T(jq delta 1057 zcmY+Dzi-n(6vvICmRg0Ttz(~4$MM-s?5GGO3{_eXf`AwpD$K~#NKA|!y0xm1kQf{E z{(weE?4T|%M3g^(0SN{s5E9S@b*zvWcz2ig;%v#w=lkQ`T~71Ud{CRcw%O@!Y(U@k z-&~jW`0H`szC)kkLXG_lQzEeN9Y1 zJM2xyhTIRC94F7K?L*wYY|X)kSqB#Dl2jD=xXzKox-t4#FL(|D7o-jcF0^I|HeoM5lKAWcT8R@zbg97Zbv*bZfau=n< zBhp<`L~5$b?U?}1SDB%cz2D#-^n^wYenQHr+BDVL&8fr24!ZHHCO1X{ota=}tTKlS y0@hQ`q=4rs2hg4WP5I7M=2m9f$LQ2hPG<`qybKFhY2L+`jIUA2=wH`sW&Z*B_#t!v diff --git a/cpp/wiiusej/msvc/WiiUseJ.suo b/cpp/wiiusej/msvc/WiiUseJ.suo index 93a5e57ea0f5759a6291a6e27f769bad4415bb2e..1032800f8d61a04d593b70d4a02625513febd4bb 100644 GIT binary patch delta 474 zcmZpe!Pqc|aYGLirz!&j!@vLk|Nq+H}ptfq3#n zeT~UqcmgOdYFTtsFW3uD>jjmpxK zHz;{cPSp|s8pZ;YtpSTIkn)?XX(TjxnxfL=1yT-^Z|h2J?o%@3;}8KVQG*%s#UO-* zg$v4?Tw)qBSx`@B^A}Tg7RITQ|5zqS&H&2I1mamh4D#C?AUzkv-dt)W#Ll{ak%2*U zbC0756XU$ejLzAU3&go5w43JPQ}*-=YuvYx9NV-L{K>dBWhg(ml@s!Uc;3Yq*sif{57CyC8( zTw|EPjxF&BVC2vN8e#y%pupYy#gm6=l8FMN(PTw?Wfnt_Fwj@r(}9d5Knw(v|Enrb iRB3_l+5@z*nryAU`Bi~2w`F2f$}Don1-;3z@<#tSr|`G z{$rUiF+^o^gB3eFE68Tq%{`7LOp`U7#JNF^g@Vb7c5;(+u1$8*>6vVx&BDky+1{yj b^BdO~CKi355|IBkmv{s)f}Q)tlZOca|2|L5 diff --git a/cpp/wiiusej/msvc/WiiUseJ.vcxproj b/cpp/wiiusej/msvc/WiiUseJ.vcxproj index 65fe5f8..ea82aed 100644 --- a/cpp/wiiusej/msvc/WiiUseJ.vcxproj +++ b/cpp/wiiusej/msvc/WiiUseJ.vcxproj @@ -92,6 +92,7 @@ + diff --git a/cpp/wiiusej/msvc/WiiUseJ.vcxproj.filters b/cpp/wiiusej/msvc/WiiUseJ.vcxproj.filters index cc1e8b3..380ac77 100644 --- a/cpp/wiiusej/msvc/WiiUseJ.vcxproj.filters +++ b/cpp/wiiusej/msvc/WiiUseJ.vcxproj.filters @@ -29,5 +29,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/cpp/wiiusej/wiiusej_Calibration.c b/cpp/wiiusej/wiiusej_Calibration.c new file mode 100644 index 0000000..2456fee --- /dev/null +++ b/cpp/wiiusej/wiiusej_Calibration.c @@ -0,0 +1,21 @@ +#include "wiiusej_WiiUseApi.h" +#include "wiiuse.h" + +static wiimote** wiimotes; +static int nbMaxWiimotes; + + +JNIEXPORT jshortArray JNICALL Java_wiiusej_WiiUseApi_getCalibration +(JNIEnv *env, jobject obj, jint id) { + struct wiimote_t* wm = wiiuse_get_by_id(wiimotes, nbMaxWiimotes, id); + const accel_t* accel = &wm->accel_calib; + const vec3b_t* zero = &accel->cal_zero; + const vec3b_t* g = &accel->cal_g; + short calibration[] = { + zero->x, zero->y, zero->z, + g->x, g->y, g->z}; + int size = 6; + jshortArray jShorts = (*env)->NewShortArray(env, size); + (*env)->SetShortArrayRegion(env, jShorts, 0, size, calibration); + return jShorts; +} \ No newline at end of file diff --git a/cpp/wiiusej/wiiusej_WiiUseApi.c b/cpp/wiiusej/wiiusej_WiiUseApi.c index 8d43e93..d5215ae 100644 --- a/cpp/wiiusej/wiiusej_WiiUseApi.c +++ b/cpp/wiiusej/wiiusej_WiiUseApi.c @@ -339,7 +339,7 @@ JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_getStatus */ JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setTimeout (JNIEnv *env, jobject obj, jint id, jshort normalTimeout, jshort expansionTimeout) { - wiiuse_set_timeout(wiimotes, nbMaxWiimotes, normalTimeout, expansionTimeout); + wiiuse_set_timeout(wiimotes, nbMaxWiimotes, (byte) normalTimeout, (byte) expansionTimeout); } /** diff --git a/cpp/wiiusej/wiiusej_WiiUseApi.def b/cpp/wiiusej/wiiusej_WiiUseApi.def deleted file mode 100644 index e7aebd3..0000000 --- a/cpp/wiiusej/wiiusej_WiiUseApi.def +++ /dev/null @@ -1,34 +0,0 @@ -EXPORTS -Java_wiiusej_WiiUseApi_connect -Java_wiiusej_WiiUseApi_find -Java_wiiusej_WiiUseApi_init -Java_wiiusej_WiiUseApi_closeConnection -Java_wiiusej_WiiUseApi_getUnId -Java_wiiusej_WiiUseApi_cleanUp -Java_wiiusej_WiiUseApi_activateRumble -Java_wiiusej_WiiUseApi_deactivateRumble -Java_wiiusej_WiiUseApi_activateIRTracking -Java_wiiusej_WiiUseApi_deactivateIRTracking -Java_wiiusej_WiiUseApi_activateMotionSensing -Java_wiiusej_WiiUseApi_deactivateMotionSensing -Java_wiiusej_WiiUseApi_setLeds -Java_wiiusej_WiiUseApi_setOrientThreshold -Java_wiiusej_WiiUseApi_setAccelThreshold -Java_wiiusej_WiiUseApi_setAlphaSmoothing -Java_wiiusej_WiiUseApi_reSync -Java_wiiusej_WiiUseApi_activateSmoothing -Java_wiiusej_WiiUseApi_deactivateSmoothing -Java_wiiusej_WiiUseApi_activateContinuous -Java_wiiusej_WiiUseApi_deactivateContinuous -Java_wiiusej_WiiUseApi_setScreenRatio43 -Java_wiiusej_WiiUseApi_setScreenRatio169 -Java_wiiusej_WiiUseApi_setSensorBarAboveScreen -Java_wiiusej_WiiUseApi_setSensorBarBelowScreen -Java_wiiusej_WiiUseApi_setVirtualScreenResolution -Java_wiiusej_WiiUseApi_getStatus -Java_wiiusej_WiiUseApi_setTimeout -Java_wiiusej_WiiUseApi_setIrSensitivity -Java_wiiusej_WiiUseApi_setNunchukOrientationThreshold -Java_wiiusej_WiiUseApi_setNunchukAccelerationThreshold -Java_wiiusej_WiiUseApi_windowsSetBluetoothStack -Java_wiiusej_WiiUseApi_specialPoll diff --git a/cpp/wiiusej/wiiusej_WiiUseApi.h b/cpp/wiiusej/wiiusej_WiiUseApi.h index 031958b..29dde3f 100644 --- a/cpp/wiiusej/wiiusej_WiiUseApi.h +++ b/cpp/wiiusej/wiiusej_WiiUseApi.h @@ -273,44 +273,12 @@ JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_specialPoll /* * Class: wiiusej_WiiUseApi - * Method: activateSpeaker - * Signature: (I)V + * Method: getCalibration + * Signature: (I)[S */ -JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateSpeaker +JNIEXPORT jshortArray JNICALL Java_wiiusej_WiiUseApi_getCalibration (JNIEnv *, jobject, jint); -/* - * Class: wiiusej_WiiUseApi - * Method: deactivateSpeaker - * Signature: (I)V - */ -JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateSpeaker - (JNIEnv *, jobject, jint); - -/* - * Class: wiiusej_WiiUseApi - * Method: setSpeakerVolume - * Signature: (IS)V - */ -JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setSpeakerVolume - (JNIEnv *, jobject, jint, jshort); - -/* - * Class: wiiusej_WiiUseApi - * Method: setSpeakerFrequency - * Signature: (II)V - */ -JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setSpeakerFrequency - (JNIEnv *, jobject, jint, jint); - -/* - * Class: wiiusej_WiiUseApi - * Method: streamSpeakerData - * Signature: (I[S)V - */ -JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_streamSpeakerData - (JNIEnv *, jobject, jint, jshortArray); - #ifdef __cplusplus } #endif diff --git a/java/native/WiiUseJ.dll b/java/native/WiiUseJ.dll index d00ef094eb50314d5bc9fcd01b2661b4b67efe18..2f48ea0454a43a25aff1fb68d6fbad71b01d6181 100644 GIT binary patch delta 2903 zcmZ{l4Nz3q702&ecVS^!c;c=uh_DFi;^+Ig?*oCii^9l+pvx*6@e@U}?4VMG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