wiiuse.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * wiiuse
  3. *
  4. * Written By:
  5. * Michael Laforest < para >
  6. * Email: < thepara (--AT--) g m a i l [--DOT--] com >
  7. *
  8. * Copyright 2006-2007
  9. *
  10. * Mac fields based on wiic_structs.h from WiiC, written By:
  11. * Gabriele Randelli
  12. * Email: < randelli (--AT--) dis [--DOT--] uniroma1 [--DOT--] it >
  13. *
  14. * Copyright 2010
  15. *
  16. * This file is part of wiiuse.
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. * $Header$
  32. *
  33. */
  34. /**
  35. * @file
  36. *
  37. * @brief API header file.
  38. *
  39. * If this file is included from inside the wiiuse source
  40. * and not from a third party program, then wiimote_internal.h
  41. * is also included which extends this file.
  42. */
  43. /**
  44. * @mainpage
  45. *
  46. * @section intro Introduction
  47. *
  48. * WiiUse is a cross-platform C library for accessing the Nintendo Wii
  49. * Remote and its related expansions and variations.
  50. *
  51. * @section project Project
  52. *
  53. * This is a friendly fork of the original WiiUse project, which seems
  54. * to have gone defunct. This updated version incorporates improvements
  55. * from a number of internal forks found across the Internet, and is
  56. * intended to be the new "upstream" of the project. The new homepage is
  57. * on GitHub, where the source is maintained:
  58. *
  59. * - http://github.com/rpavlik/wiiuse
  60. *
  61. * Contributions (under the GPL 3+) are welcome and encouraged!
  62. *
  63. * @section publicapisec Public API
  64. *
  65. * - @ref publicapi "Public API" - entirely within @ref wiiuse.h
  66. * - @ref wiimote "Wiimote device structure"
  67. *
  68. */
  69. #ifndef WIIUSE_H_INCLUDED
  70. #define WIIUSE_H_INCLUDED
  71. #define WIIUSE_MAJOR 0
  72. #define WIIUSE_MINOR 14
  73. #define WIIUSE_MICRO 0
  74. #ifndef WIIUSE_PLATFORM
  75. #if defined(_WIN32)
  76. #define WIIUSE_PLATFORM
  77. #define WIIUSE_WIN32
  78. #elif defined(__linux)
  79. #define WIIUSE_PLATFORM
  80. #define WIIUSE_BLUEZ
  81. #elif defined(__APPLE__)
  82. #define WIIUSE_PLATFORM
  83. #define WIIUSE_MAC
  84. #else
  85. #error "Platform not yet supported!"
  86. #endif
  87. #endif
  88. #ifdef WIIUSE_WIN32
  89. /* windows */
  90. #ifndef WIN32_LEAN_AND_MEAN
  91. #define WIN32_LEAN_AND_MEAN 1
  92. #endif
  93. #include <windows.h>
  94. #endif
  95. #ifdef WIIUSE_BLUEZ
  96. /* nix */
  97. #include <bluetooth/bluetooth.h>
  98. #endif
  99. #ifndef WCONST
  100. #define WCONST const
  101. #endif
  102. #if defined(_MSC_VER)
  103. /* MS compilers of pre-VC2010 versions don't have stdint.h
  104. * and I can't get VC2010's stdint.h to compile nicely in
  105. * WiiUse
  106. */
  107. #include "wiiuse_msvcstdint.h"
  108. #else
  109. #include <stdint.h>
  110. #endif
  111. #include <stdio.h> /* for FILE */
  112. /** @defgroup publicapi External API */
  113. /** @{ */
  114. /** @name Wiimote state flags and macros */
  115. /** @{ */
  116. #define WIIMOTE_STATE_DEV_FOUND 0x0001
  117. #define WIIMOTE_STATE_HANDSHAKE 0x0002 /* actual connection exists but no handshake yet */
  118. #define WIIMOTE_STATE_HANDSHAKE_COMPLETE 0x0004 /* actual connection exists but no handshake yet */
  119. #define WIIMOTE_STATE_CONNECTED 0x0008
  120. #define WIIMOTE_STATE_RUMBLE 0x0010
  121. #define WIIMOTE_STATE_ACC 0x0020
  122. #define WIIMOTE_STATE_EXP 0x0040
  123. #define WIIMOTE_STATE_IR 0x0080
  124. #define WIIMOTE_STATE_SPEAKER 0x0100
  125. #define WIIMOTE_STATE_IR_SENS_LVL1 0x0200
  126. #define WIIMOTE_STATE_IR_SENS_LVL2 0x0400
  127. #define WIIMOTE_STATE_IR_SENS_LVL3 0x0800
  128. #define WIIMOTE_STATE_IR_SENS_LVL4 0x1000
  129. #define WIIMOTE_STATE_IR_SENS_LVL5 0x2000
  130. #define WIIMOTE_STATE_EXP_HANDSHAKE 0x10000 /* actual M+ connection exists but no handshake yet */
  131. #define WIIMOTE_STATE_EXP_EXTERN 0x20000 /* actual M+ connection exists but handshake failed */
  132. #define WIIMOTE_STATE_EXP_FAILED 0x40000 /* actual M+ connection exists but handshake failed */
  133. #define WIIMOTE_STATE_MPLUS_PRESENT 0x80000 /* Motion+ is connected */
  134. #define WIIMOTE_ID(wm) (wm->unid)
  135. #define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s))
  136. #define WIIMOTE_IS_CONNECTED(wm) (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED))
  137. /** @} */
  138. /** @name LED bit masks */
  139. /** @{ */
  140. #define WIIMOTE_LED_NONE 0x00
  141. #define WIIMOTE_LED_1 0x10
  142. #define WIIMOTE_LED_2 0x20
  143. #define WIIMOTE_LED_3 0x40
  144. #define WIIMOTE_LED_4 0x80
  145. /** @} */
  146. /** @name Button codes */
  147. /** @{ */
  148. #define WIIMOTE_BUTTON_TWO 0x0001
  149. #define WIIMOTE_BUTTON_ONE 0x0002
  150. #define WIIMOTE_BUTTON_B 0x0004
  151. #define WIIMOTE_BUTTON_A 0x0008
  152. #define WIIMOTE_BUTTON_MINUS 0x0010
  153. #define WIIMOTE_BUTTON_ZACCEL_BIT6 0x0020
  154. #define WIIMOTE_BUTTON_ZACCEL_BIT7 0x0040
  155. #define WIIMOTE_BUTTON_HOME 0x0080
  156. #define WIIMOTE_BUTTON_LEFT 0x0100
  157. #define WIIMOTE_BUTTON_RIGHT 0x0200
  158. #define WIIMOTE_BUTTON_DOWN 0x0400
  159. #define WIIMOTE_BUTTON_UP 0x0800
  160. #define WIIMOTE_BUTTON_PLUS 0x1000
  161. #define WIIMOTE_BUTTON_ZACCEL_BIT4 0x2000
  162. #define WIIMOTE_BUTTON_ZACCEL_BIT5 0x4000
  163. #define WIIMOTE_BUTTON_UNKNOWN 0x8000
  164. #define WIIMOTE_BUTTON_ALL 0x1F9F
  165. /** @} */
  166. /** @name Nunchuk button codes */
  167. /** @{ */
  168. #define NUNCHUK_BUTTON_Z 0x01
  169. #define NUNCHUK_BUTTON_C 0x02
  170. #define NUNCHUK_BUTTON_ALL 0x03
  171. /** @} */
  172. /** @name Classic controller button codes */
  173. /** @{ */
  174. #define CLASSIC_CTRL_BUTTON_UP 0x0001
  175. #define CLASSIC_CTRL_BUTTON_LEFT 0x0002
  176. #define CLASSIC_CTRL_BUTTON_ZR 0x0004
  177. #define CLASSIC_CTRL_BUTTON_X 0x0008
  178. #define CLASSIC_CTRL_BUTTON_A 0x0010
  179. #define CLASSIC_CTRL_BUTTON_Y 0x0020
  180. #define CLASSIC_CTRL_BUTTON_B 0x0040
  181. #define CLASSIC_CTRL_BUTTON_ZL 0x0080
  182. #define CLASSIC_CTRL_BUTTON_FULL_R 0x0200
  183. #define CLASSIC_CTRL_BUTTON_PLUS 0x0400
  184. #define CLASSIC_CTRL_BUTTON_HOME 0x0800
  185. #define CLASSIC_CTRL_BUTTON_MINUS 0x1000
  186. #define CLASSIC_CTRL_BUTTON_FULL_L 0x2000
  187. #define CLASSIC_CTRL_BUTTON_DOWN 0x4000
  188. #define CLASSIC_CTRL_BUTTON_RIGHT 0x8000
  189. #define CLASSIC_CTRL_BUTTON_ALL 0xFEFF
  190. /** @} */
  191. /** @name Guitar Hero 3 button codes */
  192. /** @{ */
  193. #define GUITAR_HERO_3_BUTTON_STRUM_UP 0x0001
  194. #define GUITAR_HERO_3_BUTTON_YELLOW 0x0008
  195. #define GUITAR_HERO_3_BUTTON_GREEN 0x0010
  196. #define GUITAR_HERO_3_BUTTON_BLUE 0x0020
  197. #define GUITAR_HERO_3_BUTTON_RED 0x0040
  198. #define GUITAR_HERO_3_BUTTON_ORANGE 0x0080
  199. #define GUITAR_HERO_3_BUTTON_PLUS 0x0400
  200. #define GUITAR_HERO_3_BUTTON_MINUS 0x1000
  201. #define GUITAR_HERO_3_BUTTON_STRUM_DOWN 0x4000
  202. #define GUITAR_HERO_3_BUTTON_ALL 0xFEFF
  203. /** @} */
  204. /** @name Wiimote option flags */
  205. /** @{ */
  206. #define WIIUSE_SMOOTHING 0x01
  207. #define WIIUSE_CONTINUOUS 0x02
  208. #define WIIUSE_ORIENT_THRESH 0x04
  209. #define WIIUSE_INIT_FLAGS (WIIUSE_SMOOTHING | WIIUSE_ORIENT_THRESH)
  210. #define WIIUSE_ORIENT_PRECISION 100.0f
  211. /** @} */
  212. /** @name Expansion codes */
  213. /** @{ */
  214. #define EXP_NONE 0
  215. #define EXP_NUNCHUK 1
  216. #define EXP_CLASSIC 2
  217. #define EXP_GUITAR_HERO_3 3
  218. #define EXP_WII_BOARD 4
  219. #define EXP_MOTION_PLUS 5
  220. #define EXP_MOTION_PLUS_NUNCHUK 6 /* Motion+ in nunchuk pass-through mode */
  221. #define EXP_MOTION_PLUS_CLASSIC 7 /* Motion+ in classic ctr. pass-through mode */
  222. /** @} */
  223. /** @brief IR correction types */
  224. typedef enum ir_position_t {
  225. WIIUSE_IR_ABOVE,
  226. WIIUSE_IR_BELOW
  227. } ir_position_t;
  228. /** @name Device Inquiry Macros */
  229. /** @{ */
  230. /**
  231. * @brief Check if a button is pressed.
  232. * @param dev Pointer to a wiimote_t or expansion structure.
  233. * @param button The button you are interested in.
  234. * @return 1 if the button is pressed, 0 if not.
  235. */
  236. #define IS_PRESSED(dev, button) ((dev->btns & button) == button)
  237. /**
  238. * @brief Check if a button is being held.
  239. * @param dev Pointer to a wiimote_t or expansion structure.
  240. * @param button The button you are interested in.
  241. * @return 1 if the button is held, 0 if not.
  242. */
  243. #define IS_HELD(dev, button) ((dev->btns_held & button) == button)
  244. /**
  245. * @brief Check if a button is released on this event. \n\n
  246. * This does not mean the button is not pressed, it means \n
  247. * this button was just now released.
  248. * @param dev Pointer to a wiimote_t or expansion structure.
  249. * @param button The button you are interested in.
  250. * @return 1 if the button is released, 0 if not.
  251. *
  252. */
  253. #define IS_RELEASED(dev, button) ((dev->btns_released & button) == button)
  254. /**
  255. * @brief Check if a button has just been pressed this event.
  256. * @param dev Pointer to a wiimote_t or expansion structure.
  257. * @param button The button you are interested in.
  258. * @return 1 if the button is pressed, 0 if not.
  259. */
  260. #define IS_JUST_PRESSED(dev, button) (IS_PRESSED(dev, button) && !IS_HELD(dev, button))
  261. /**
  262. * @brief Return the IR sensitivity level.
  263. * @param wm Pointer to a wiimote_t structure.
  264. * @param lvl [out] Pointer to an int that will hold the level setting.
  265. * If no level is set 'lvl' will be set to 0.
  266. */
  267. #define WIIUSE_GET_IR_SENSITIVITY(dev, lvl) \
  268. do { \
  269. if ((wm->state & 0x0200) == 0x0200) *lvl = 1; \
  270. else if ((wm->state & 0x0400) == 0x0400) *lvl = 2; \
  271. else if ((wm->state & 0x0800) == 0x0800) *lvl = 3; \
  272. else if ((wm->state & 0x1000) == 0x1000) *lvl = 4; \
  273. else if ((wm->state & 0x2000) == 0x2000) *lvl = 5; \
  274. else *lvl = 0; \
  275. } while (0)
  276. #define WIIUSE_USING_ACC(wm) ((wm->state & 0x020) == 0x020)
  277. #define WIIUSE_USING_EXP(wm) ((wm->state & 0x040) == 0x040)
  278. #define WIIUSE_USING_IR(wm) ((wm->state & 0x080) == 0x080)
  279. #define WIIUSE_USING_SPEAKER(wm) ((wm->state & 0x100) == 0x100)
  280. #define WIIUSE_IS_LED_SET(wm, num) ((wm->leds & WIIMOTE_LED_##num) == WIIMOTE_LED_##num)
  281. /** @} */
  282. /*
  283. * This is left over from an old hack, but it may actually
  284. * be a useful feature to keep so it wasn't removed.
  285. */
  286. #ifdef WIIUSE_WIN32
  287. #define WIIMOTE_DEFAULT_TIMEOUT 10
  288. #define WIIMOTE_EXP_TIMEOUT 10
  289. #endif
  290. #define WIIUSE_SYNC_HANDSHAKE
  291. typedef unsigned char byte;
  292. typedef char sbyte;
  293. struct wiimote_t;
  294. struct vec3b_t;
  295. struct orient_t;
  296. struct gforce_t;
  297. /**
  298. * @brief Callback that handles a read event.
  299. *
  300. * @param wm Pointer to a wiimote_t structure.
  301. * @param data Pointer to the filled data block.
  302. * @param len Length in bytes of the data block.
  303. *
  304. * @see wiiuse_init()
  305. *
  306. * A registered function of this type is called automatically by the wiiuse
  307. * library when the wiimote has returned the full data requested by a previous
  308. * call to wiiuse_read_data().
  309. */
  310. typedef void (*wiiuse_read_cb)(struct wiimote_t* wm, byte* data, uint16_t len);
  311. /**
  312. * @brief Data read request structure.
  313. */
  314. struct read_req_t {
  315. wiiuse_read_cb cb; /**< read data callback */
  316. byte* buf; /**< buffer where read data is written */
  317. uint32_t addr; /**< the offset that the read started at */
  318. uint16_t size; /**< the length of the data read */
  319. uint16_t wait; /**< num bytes still needed to finish read */
  320. byte dirty; /**< set to 1 if not using callback and needs to be cleaned up */
  321. struct read_req_t* next; /**< next read request in the queue */
  322. };
  323. /**
  324. * @struct ang3s_t
  325. * @brief Roll/Pitch/Yaw short angles.
  326. */
  327. typedef struct ang3s_t {
  328. int16_t roll, pitch, yaw;
  329. } ang3s_t;
  330. /**
  331. * @struct ang3f_t
  332. * @brief Roll/Pitch/Yaw float angles.
  333. */
  334. typedef struct ang3f_t {
  335. float roll, pitch, yaw;
  336. } ang3f_t;
  337. /**
  338. * @brief Unsigned x,y byte vector.
  339. */
  340. typedef struct vec2b_t {
  341. byte x, y;
  342. } vec2b_t;
  343. /**
  344. * @brief Unsigned x,y,z byte vector.
  345. */
  346. typedef struct vec3b_t {
  347. byte x, y, z;
  348. } vec3b_t;
  349. /**
  350. * @brief Signed x,y,z float struct.
  351. */
  352. typedef struct vec3f_t {
  353. float x, y, z;
  354. } vec3f_t;
  355. /**
  356. * @brief Orientation struct.
  357. *
  358. * Yaw, pitch, and roll range from -180 to 180 degrees.
  359. */
  360. typedef struct orient_t {
  361. float roll; /**< roll, this may be smoothed if enabled */
  362. float pitch; /**< pitch, this may be smoothed if enabled */
  363. float yaw;
  364. float a_roll; /**< absolute roll, unsmoothed */
  365. float a_pitch; /**< absolute pitch, unsmoothed */
  366. } orient_t;
  367. /**
  368. * @brief Gravity force struct.
  369. */
  370. typedef struct gforce_t {
  371. float x, y, z;
  372. } gforce_t;
  373. /**
  374. * @brief Accelerometer struct. For any device with an accelerometer.
  375. */
  376. typedef struct accel_t {
  377. struct vec3b_t cal_zero; /**< zero calibration */
  378. struct vec3b_t cal_g; /**< 1g difference around 0cal */
  379. float st_roll; /**< last smoothed roll value */
  380. float st_pitch; /**< last smoothed roll pitch */
  381. float st_alpha; /**< alpha value for smoothing [0-1] */
  382. } accel_t;
  383. /**
  384. * @brief A single IR source.
  385. */
  386. typedef struct ir_dot_t {
  387. byte visible; /**< if the IR source is visible */
  388. unsigned int x; /**< interpolated X coordinate */
  389. unsigned int y; /**< interpolated Y coordinate */
  390. int16_t rx; /**< raw X coordinate (0-1023) */
  391. int16_t ry; /**< raw Y coordinate (0-767) */
  392. byte order; /**< increasing order by x-axis value */
  393. byte size; /**< size of the IR dot (0-15) */
  394. } ir_dot_t;
  395. /**
  396. * @brief Screen aspect ratio.
  397. */
  398. typedef enum aspect_t {
  399. WIIUSE_ASPECT_4_3,
  400. WIIUSE_ASPECT_16_9
  401. } aspect_t;
  402. /**
  403. * @brief IR struct. Hold all data related to the IR tracking.
  404. */
  405. typedef struct ir_t {
  406. struct ir_dot_t dot[4]; /**< IR dots */
  407. byte num_dots; /**< number of dots at this time */
  408. enum aspect_t aspect; /**< aspect ratio of the screen */
  409. enum ir_position_t pos; /**< IR sensor bar position */
  410. unsigned int vres[2]; /**< IR virtual screen resolution */
  411. int offset[2]; /**< IR XY correction offset */
  412. int state; /**< keeps track of the IR state */
  413. int ax; /**< absolute X coordinate */
  414. int ay; /**< absolute Y coordinate */
  415. int x; /**< calculated X coordinate */
  416. int y; /**< calculated Y coordinate */
  417. float distance; /**< pixel distance between first 2 dots*/
  418. float z; /**< calculated distance */
  419. } ir_t;
  420. /**
  421. * @brief Joystick calibration structure.
  422. *
  423. * The angle \a ang is relative to the positive y-axis into quadrant I
  424. * and ranges from 0 to 360 degrees. So if the joystick is held straight
  425. * upwards then angle is 0 degrees. If it is held to the right it is 90,
  426. * down is 180, and left is 270.
  427. *
  428. * The magnitude \a mag is the distance from the center to where the
  429. * joystick is being held. The magnitude ranges from 0 to 1.
  430. * If the joystick is only slightly tilted from the center the magnitude
  431. * will be low, but if it is closer to the outter edge the value will
  432. * be higher.
  433. */
  434. typedef struct joystick_t {
  435. struct vec2b_t max; /**< maximum joystick values */
  436. struct vec2b_t min; /**< minimum joystick values */
  437. struct vec2b_t center; /**< center joystick values */
  438. float ang; /**< angle the joystick is being held */
  439. float mag; /**< magnitude of the joystick (range 0-1) */
  440. } joystick_t;
  441. /**
  442. * @brief Nunchuk expansion device.
  443. */
  444. typedef struct nunchuk_t {
  445. struct accel_t accel_calib; /**< nunchuk accelerometer calibration */
  446. struct joystick_t js; /**< joystick calibration */
  447. int* flags; /**< options flag (points to wiimote_t.flags) */
  448. byte btns; /**< what buttons have just been pressed */
  449. byte btns_held; /**< what buttons are being held down */
  450. byte btns_released; /**< what buttons were just released this */
  451. float orient_threshold; /**< threshold for orient to generate an event */
  452. int accel_threshold; /**< threshold for accel to generate an event */
  453. struct vec3b_t accel; /**< current raw acceleration data */
  454. struct orient_t orient; /**< current orientation on each axis */
  455. struct gforce_t gforce; /**< current gravity forces on each axis */
  456. } nunchuk_t;
  457. /**
  458. * @brief Classic controller expansion device.
  459. */
  460. typedef struct classic_ctrl_t {
  461. int16_t btns; /**< what buttons have just been pressed */
  462. int16_t btns_held; /**< what buttons are being held down */
  463. int16_t btns_released; /**< what buttons were just released this */
  464. float r_shoulder; /**< right shoulder button (range 0-1) */
  465. float l_shoulder; /**< left shoulder button (range 0-1) */
  466. struct joystick_t ljs; /**< left joystick calibration */
  467. struct joystick_t rjs; /**< right joystick calibration */
  468. } classic_ctrl_t;
  469. /**
  470. * @brief Guitar Hero 3 expansion device.
  471. */
  472. typedef struct guitar_hero_3_t {
  473. int16_t btns; /**< what buttons have just been pressed */
  474. int16_t btns_held; /**< what buttons are being held down */
  475. int16_t btns_released; /**< what buttons were just released this */
  476. float whammy_bar; /**< whammy bar (range 0-1) */
  477. struct joystick_t js; /**< joystick calibration */
  478. } guitar_hero_3_t;
  479. /**
  480. * @brief Motion Plus expansion device
  481. */
  482. typedef struct motion_plus_t {
  483. byte ext; /**< is there a device on the pass-through port? */
  484. struct ang3s_t raw_gyro; /**< current raw gyroscope data */
  485. struct ang3s_t cal_gyro; /**< calibration raw gyroscope data */
  486. struct ang3f_t angle_rate_gyro; /**< current gyro angle rate */
  487. struct orient_t orient; /**< current orientation on each axis using Motion Plus gyroscopes */
  488. byte acc_mode; /**< Fast/slow rotation mode for roll, pitch and yaw (0 if rotating fast, 1 if slow or still) */
  489. int raw_gyro_threshold; /**< threshold for gyroscopes to generate an event */
  490. struct nunchuk_t *nc; /**< pointers to nunchuk & classic in pass-through-mode */
  491. struct classic_ctrl_t *classic;
  492. } motion_plus_t;
  493. /**
  494. * @brief Wii Balance Board "expansion" device.
  495. *
  496. * A Balance Board presents itself as a Wiimote with a permanently-attached
  497. * Balance Board expansion device.
  498. */
  499. typedef struct wii_board_t {
  500. /** @name Interpolated weight per sensor (kg)
  501. *
  502. * These are the values you're most likely to use.
  503. *
  504. * See example.c for how to compute total weight and center of gravity
  505. * from these values.
  506. */
  507. /** @{ */
  508. float tl;
  509. float tr;
  510. float bl;
  511. float br;
  512. /** @} */
  513. /** @name Raw sensor values */
  514. /** @{ */
  515. uint16_t rtl;
  516. uint16_t rtr;
  517. uint16_t rbl;
  518. uint16_t rbr;
  519. /** @} */
  520. /** @name Sensor calibration values */
  521. /** @{ */
  522. uint16_t ctl[3]; /* Calibration */
  523. uint16_t ctr[3];
  524. uint16_t cbl[3];
  525. uint16_t cbr[3]; /* /Calibration */
  526. /** @} */
  527. uint8_t update_calib;
  528. } wii_board_t;
  529. /**
  530. * @brief Generic expansion device plugged into wiimote.
  531. */
  532. typedef struct expansion_t {
  533. int type; /**< type of expansion attached */
  534. struct motion_plus_t mp;
  535. union {
  536. struct nunchuk_t nunchuk;
  537. struct classic_ctrl_t classic;
  538. struct guitar_hero_3_t gh3;
  539. struct wii_board_t wb;
  540. };
  541. } expansion_t;
  542. /**
  543. * @brief Available bluetooth stacks for Windows.
  544. */
  545. typedef enum win_bt_stack_t {
  546. WIIUSE_STACK_UNKNOWN,
  547. WIIUSE_STACK_MS,
  548. WIIUSE_STACK_BLUESOLEIL
  549. } win_bt_stack_t;
  550. /**
  551. * @brief Significant data from the previous event.
  552. */
  553. typedef struct wiimote_state_t {
  554. /* expansion_t */
  555. float exp_ljs_ang;
  556. float exp_rjs_ang;
  557. float exp_ljs_mag;
  558. float exp_rjs_mag;
  559. uint16_t exp_btns;
  560. struct orient_t exp_orient;
  561. struct vec3b_t exp_accel;
  562. float exp_r_shoulder;
  563. float exp_l_shoulder;
  564. /* motion plus */
  565. short drx;
  566. short dry;
  567. short drz;
  568. /* wiiboard */
  569. uint16_t exp_wb_rtr;
  570. uint16_t exp_wb_rtl;
  571. uint16_t exp_wb_rbr;
  572. uint16_t exp_wb_rbl;
  573. /* ir_t */
  574. int ir_ax;
  575. int ir_ay;
  576. float ir_distance;
  577. struct orient_t orient;
  578. uint16_t btns;
  579. struct vec3b_t accel;
  580. } wiimote_state_t;
  581. /**
  582. * @brief Events that wiiuse can generate from a poll.
  583. */
  584. typedef enum WIIUSE_EVENT_TYPE {
  585. WIIUSE_NONE = 0,
  586. WIIUSE_EVENT,
  587. WIIUSE_STATUS,
  588. WIIUSE_CONNECT,
  589. WIIUSE_DISCONNECT,
  590. WIIUSE_UNEXPECTED_DISCONNECT,
  591. WIIUSE_READ_DATA,
  592. WIIUSE_WRITE_DATA,
  593. WIIUSE_NUNCHUK_INSERTED,
  594. WIIUSE_NUNCHUK_REMOVED,
  595. WIIUSE_CLASSIC_CTRL_INSERTED,
  596. WIIUSE_CLASSIC_CTRL_REMOVED,
  597. WIIUSE_GUITAR_HERO_3_CTRL_INSERTED,
  598. WIIUSE_GUITAR_HERO_3_CTRL_REMOVED,
  599. WIIUSE_WII_BOARD_CTRL_INSERTED,
  600. WIIUSE_WII_BOARD_CTRL_REMOVED,
  601. WIIUSE_MOTION_PLUS_ACTIVATED,
  602. WIIUSE_MOTION_PLUS_REMOVED
  603. } WIIUSE_EVENT_TYPE;
  604. /**
  605. * @brief Type of wiimote peripheral
  606. */
  607. typedef enum WIIUSE_WIIMOTE_TYPE {
  608. WIIUSE_WIIMOTE_REGULAR = 0,
  609. WIIUSE_WIIMOTE_MOTION_PLUS_INSIDE,
  610. } WIIUSE_WIIMOTE_TYPE;
  611. /**
  612. * @brief Main Wiimote device structure.
  613. *
  614. * You need one of these to do pretty much anything with this library.
  615. */
  616. typedef struct wiimote_t {
  617. WCONST int unid; /**< user specified id */
  618. #ifdef WIIUSE_BLUEZ
  619. /** @name Linux-specific (BlueZ) members */
  620. /** @{ */
  621. WCONST char bdaddr_str[18]; /**< readable bt address */
  622. WCONST bdaddr_t bdaddr; /**< bt address */
  623. WCONST int out_sock; /**< output socket */
  624. WCONST int in_sock; /**< input socket */
  625. /** @} */
  626. #endif
  627. #ifdef WIIUSE_WIN32
  628. /** @name Windows-specific members */
  629. /** @{ */
  630. WCONST HANDLE dev_handle; /**< HID handle */
  631. WCONST OVERLAPPED hid_overlap; /**< overlap handle */
  632. WCONST enum win_bt_stack_t stack; /**< type of bluetooth stack to use */
  633. WCONST int timeout; /**< read timeout */
  634. WCONST byte normal_timeout; /**< normal timeout */
  635. WCONST byte exp_timeout; /**< timeout for expansion handshake */
  636. /** @} */
  637. #endif
  638. #ifdef WIIUSE_MAC
  639. /** @name Mac OS X-specific members */
  640. /** @{ */
  641. WCONST void* objc_wm; /** WiiuseWiimote* as opaque pointer */
  642. /** @} */
  643. #endif
  644. WCONST int state; /**< various state flags */
  645. WCONST byte leds; /**< currently lit leds */
  646. WCONST float battery_level; /**< battery level */
  647. WCONST int flags; /**< options flag */
  648. #ifndef WIIUSE_SYNC_HANDSHAKE
  649. WCONST byte handshake_state; /**< the state of the connection handshake */
  650. #endif
  651. WCONST byte expansion_state; /**< the state of the expansion handshake */
  652. WCONST struct data_req_t* data_req; /**< list of data read requests */
  653. WCONST struct read_req_t* read_req; /**< list of data read requests */
  654. WCONST struct accel_t accel_calib; /**< wiimote accelerometer calibration */
  655. WCONST struct expansion_t exp; /**< wiimote expansion device */
  656. WCONST struct vec3b_t accel; /**< current raw acceleration data */
  657. WCONST struct orient_t orient; /**< current orientation on each axis */
  658. WCONST struct gforce_t gforce; /**< current gravity forces on each axis */
  659. WCONST struct ir_t ir; /**< IR data */
  660. WCONST uint16_t btns; /**< what buttons have just been pressed */
  661. WCONST uint16_t btns_held; /**< what buttons are being held down */
  662. WCONST uint16_t btns_released; /**< what buttons were just released this */
  663. WCONST float orient_threshold; /**< threshold for orient to generate an event */
  664. WCONST int32_t accel_threshold; /**< threshold for accel to generate an event */
  665. WCONST struct wiimote_state_t lstate; /**< last saved state */
  666. WCONST WIIUSE_EVENT_TYPE event; /**< type of event that occurred */
  667. WCONST byte motion_plus_id[6];
  668. WCONST WIIUSE_WIIMOTE_TYPE type;
  669. } wiimote;
  670. /** @brief Data passed to a callback during wiiuse_update() */
  671. typedef struct wiimote_callback_data_t {
  672. WCONST int uid;
  673. WCONST byte leds;
  674. WCONST float battery_level;
  675. WCONST struct vec3b_t accel;
  676. WCONST struct orient_t orient;
  677. WCONST struct gforce_t gforce;
  678. WCONST struct ir_t ir;
  679. WCONST uint16_t buttons;
  680. WCONST uint16_t buttons_held;
  681. WCONST uint16_t buttons_released;
  682. WCONST WIIUSE_EVENT_TYPE event;
  683. WCONST int state;
  684. WCONST struct expansion_t expansion;
  685. } wiimote_callback_data_t;
  686. /** @brief Callback type */
  687. typedef void (*wiiuse_update_cb)(struct wiimote_callback_data_t* wm);
  688. /**
  689. * @brief Callback that handles a write event.
  690. *
  691. * @param wm Pointer to a wiimote_t structure.
  692. * @param data Pointer to the sent data block.
  693. * @param len Length in bytes of the data block.
  694. *
  695. * @see wiiuse_init()
  696. *
  697. * A registered function of this type is called automatically by the wiiuse
  698. * library when the wiimote has returned the full data requested by a previous
  699. * call to wiiuse_write_data().
  700. */
  701. typedef void (*wiiuse_write_cb)(struct wiimote_t* wm, unsigned char* data, unsigned short len);
  702. typedef enum data_req_s {
  703. REQ_READY = 0,
  704. REQ_SENT,
  705. REQ_DONE
  706. } data_req_s;
  707. /**
  708. * @struct data_req_t
  709. * @brief Data write request structure.
  710. */
  711. struct data_req_t {
  712. byte data[21]; /**< buffer where read data is written */
  713. byte len;
  714. unsigned int addr;
  715. data_req_s state; /**< set to 1 if not using callback and needs to be cleaned up */
  716. wiiuse_write_cb cb; /**< read data callback */
  717. struct data_req_t *next;
  718. };
  719. /**
  720. * @brief Loglevels supported by wiiuse.
  721. */
  722. typedef enum wiiuse_loglevel {
  723. LOGLEVEL_ERROR = 0,
  724. LOGLEVEL_WARNING = 1,
  725. LOGLEVEL_INFO = 2,
  726. LOGLEVEL_DEBUG = 3
  727. } wiiuse_loglevel;
  728. /*****************************************
  729. *
  730. * Include API specific stuff
  731. *
  732. *****************************************/
  733. #ifdef WIIUSE_WIN32
  734. #ifdef WIIUSE_STATIC
  735. #define WIIUSE_EXPORT_DECL
  736. #define WIIUSE_IMPORT_DECL
  737. #else
  738. #define WIIUSE_EXPORT_DECL __declspec(dllexport)
  739. #define WIIUSE_IMPORT_DECL __declspec(dllimport)
  740. #endif
  741. #else
  742. #define WIIUSE_EXPORT_DECL
  743. #define WIIUSE_IMPORT_DECL
  744. #endif
  745. #ifdef WIIUSE_COMPILE_LIB
  746. #define WIIUSE_EXPORT WIIUSE_EXPORT_DECL
  747. #else
  748. #define WIIUSE_EXPORT WIIUSE_IMPORT_DECL
  749. #endif
  750. #ifdef __cplusplus
  751. extern "C" {
  752. #endif
  753. /* wiiuse.c */
  754. WIIUSE_EXPORT extern const char* wiiuse_version();
  755. /** @brief Define indicating the presence of the feature allowing you to
  756. * redirect output for one or more logging levels within the library.
  757. */
  758. #define WIIUSE_HAS_OUTPUT_REDIRECTION
  759. WIIUSE_EXPORT extern void wiiuse_set_output(enum wiiuse_loglevel loglevel, FILE *logtarget);
  760. WIIUSE_EXPORT extern struct wiimote_t** wiiuse_init(int wiimotes);
  761. WIIUSE_EXPORT extern void wiiuse_disconnected(struct wiimote_t* wm);
  762. WIIUSE_EXPORT extern void wiiuse_cleanup(struct wiimote_t** wm, int wiimotes);
  763. WIIUSE_EXPORT extern void wiiuse_rumble(struct wiimote_t* wm, int status);
  764. WIIUSE_EXPORT extern void wiiuse_toggle_rumble(struct wiimote_t* wm);
  765. WIIUSE_EXPORT extern void wiiuse_set_leds(struct wiimote_t* wm, int leds);
  766. WIIUSE_EXPORT extern void wiiuse_motion_sensing(struct wiimote_t* wm, int status);
  767. WIIUSE_EXPORT extern int wiiuse_read_data(struct wiimote_t* wm, byte* buffer, unsigned int offset, uint16_t len);
  768. WIIUSE_EXPORT extern int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, const byte* data, byte len);
  769. WIIUSE_EXPORT extern void wiiuse_status(struct wiimote_t* wm);
  770. WIIUSE_EXPORT extern struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid);
  771. WIIUSE_EXPORT extern int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable);
  772. WIIUSE_EXPORT extern float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha);
  773. WIIUSE_EXPORT extern void wiiuse_set_bluetooth_stack(struct wiimote_t** wm, int wiimotes, enum win_bt_stack_t type);
  774. WIIUSE_EXPORT extern void wiiuse_set_orient_threshold(struct wiimote_t* wm, float threshold);
  775. WIIUSE_EXPORT extern void wiiuse_resync(struct wiimote_t* wm);
  776. WIIUSE_EXPORT extern void wiiuse_set_timeout(struct wiimote_t** wm, int wiimotes, byte normal_timeout, byte exp_timeout);
  777. WIIUSE_EXPORT extern void wiiuse_set_accel_threshold(struct wiimote_t* wm, int threshold);
  778. /* io.c */
  779. WIIUSE_EXPORT extern int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout);
  780. WIIUSE_EXPORT extern int wiiuse_connect(struct wiimote_t** wm, int wiimotes);
  781. WIIUSE_EXPORT extern void wiiuse_disconnect(struct wiimote_t* wm);
  782. /* events.c */
  783. WIIUSE_EXPORT extern int wiiuse_poll(struct wiimote_t** wm, int wiimotes);
  784. /**
  785. * @brief Poll Wiimotes, and call the provided callback with information
  786. * on each Wiimote that had an event.
  787. *
  788. * Alternative to calling wiiuse_poll yourself, and provides the same
  789. * information struct on all platforms.
  790. *
  791. * @return Number of wiimotes that had an event.
  792. */
  793. WIIUSE_EXPORT extern int wiiuse_update(struct wiimote_t** wm, int wiimotes, wiiuse_update_cb callback);
  794. /* ir.c */
  795. WIIUSE_EXPORT extern void wiiuse_set_ir(struct wiimote_t* wm, int status);
  796. WIIUSE_EXPORT extern void wiiuse_set_ir_vres(struct wiimote_t* wm, unsigned int x, unsigned int y);
  797. WIIUSE_EXPORT extern void wiiuse_set_ir_position(struct wiimote_t* wm, enum ir_position_t pos);
  798. WIIUSE_EXPORT extern void wiiuse_set_aspect_ratio(struct wiimote_t* wm, enum aspect_t aspect);
  799. WIIUSE_EXPORT extern void wiiuse_set_ir_sensitivity(struct wiimote_t* wm, int level);
  800. /* nunchuk.c */
  801. WIIUSE_EXPORT extern void wiiuse_set_nunchuk_orient_threshold(struct wiimote_t* wm, float threshold);
  802. WIIUSE_EXPORT extern void wiiuse_set_nunchuk_accel_threshold(struct wiimote_t* wm, int threshold);
  803. /* wiiboard.c */
  804. /* this function not currently implemented... */
  805. WIIUSE_EXPORT extern void wiiuse_set_wii_board_calib(struct wiimote_t *wm);
  806. WIIUSE_EXPORT extern void wiiuse_set_motion_plus(struct wiimote_t *wm, int status);
  807. #ifdef __cplusplus
  808. }
  809. #endif
  810. /** @} */
  811. #endif /* WIIUSE_H_INCLUDED */