io.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. * This file is part of wiiuse.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. * $Header$
  26. *
  27. */
  28. /**
  29. * @file
  30. * @brief Handles device I/O (non-OS specific).
  31. */
  32. #include "io.h"
  33. #include "ir.h" /* for wiiuse_set_ir_mode */
  34. #include "wiiuse_internal.h"
  35. #include "os.h" /* for wiiuse_os_* */
  36. #include <stdlib.h> /* for free, malloc */
  37. /**
  38. * @brief Find a wiimote or wiimotes.
  39. *
  40. * @param wm An array of wiimote_t structures.
  41. * @param max_wiimotes The number of wiimote structures in \a wm.
  42. * @param timeout The number of seconds before the search times out.
  43. *
  44. * @return The number of wiimotes found.
  45. *
  46. * @see wiiuse_connect()
  47. * @see wiiuse_os_find()
  48. *
  49. * This function will only look for wiimote devices. \n
  50. * When a device is found the address in the structures will be set. \n
  51. * You can then call wiiuse_connect() to connect to the found \n
  52. * devices.
  53. *
  54. * This function only delegates to the platform-specific implementation
  55. * wiiuse_os_find.
  56. *
  57. * This function is declared in wiiuse.h
  58. */
  59. int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout) {
  60. return wiiuse_os_find(wm, max_wiimotes, timeout);
  61. }
  62. /**
  63. * @brief Connect to a wiimote or wiimotes once an address is known.
  64. *
  65. * @param wm An array of wiimote_t structures.
  66. * @param wiimotes The number of wiimote structures in \a wm.
  67. *
  68. * @return The number of wiimotes that successfully connected.
  69. *
  70. * @see wiiuse_find()
  71. * @see wiiuse_disconnect()
  72. * @see wiiuse_os_connect()
  73. *
  74. * Connect to a number of wiimotes when the address is already set
  75. * in the wiimote_t structures. These addresses are normally set
  76. * by the wiiuse_find() function, but can also be set manually.
  77. *
  78. * This function only delegates to the platform-specific implementation
  79. * wiiuse_os_connect.
  80. *
  81. * This function is declared in wiiuse.h
  82. */
  83. int wiiuse_connect(struct wiimote_t** wm, int wiimotes) {
  84. return wiiuse_os_connect(wm, wiimotes);
  85. }
  86. /**
  87. * @brief Disconnect a wiimote.
  88. *
  89. * @param wm Pointer to a wiimote_t structure.
  90. *
  91. * @see wiiuse_connect()
  92. * @see wiiuse_os_disconnect()
  93. *
  94. * Note that this will not free the wiimote structure.
  95. *
  96. * This function only delegates to the platform-specific implementation
  97. * wiiuse_os_disconnect.
  98. *
  99. * This function is declared in wiiuse.h
  100. */
  101. void wiiuse_disconnect(struct wiimote_t* wm) {
  102. wiiuse_os_disconnect(wm);
  103. }
  104. /**
  105. * @brief Wait until specified report arrives and return it
  106. *
  107. * @param wm Pointer to a wiimote_t structure.
  108. * @param buffer Pre-allocated memory to store the received data
  109. * @param bufferLength size of buffer in bytes
  110. *
  111. * Synchronous/blocking, this function will not return until it receives the specified
  112. * report from the Wiimote.
  113. *
  114. */
  115. void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength) {
  116. for (;;) {
  117. if (wiiuse_os_read(wm, buffer, bufferLength) > 0) {
  118. if (buffer[0] == report) {
  119. break;
  120. } else {
  121. WIIUSE_DEBUG("(id %i) dropping report 0x%x, waiting for 0x%x", wm->unid, buffer[0], report);
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * @brief Read memory/register data synchronously
  128. *
  129. * @param wm Pointer to a wiimote_t structure.
  130. * @param memory If set to non-zero, reads EEPROM, otherwise registers
  131. * @param addr Address offset to read from
  132. * @param size How many bytes to read
  133. * @param data Pre-allocated memory to store the received data
  134. *
  135. * Synchronous/blocking read, this function will not return until it receives the specified
  136. * amount of data from the Wiimote.
  137. *
  138. */
  139. void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data) {
  140. byte pkt[6];
  141. byte buf[MAX_PAYLOAD];
  142. unsigned n_full_reports;
  143. unsigned last_report;
  144. byte *output;
  145. unsigned int i;
  146. /*
  147. * address in big endian first, the leading byte will
  148. * be overwritten (only 3 bytes are sent)
  149. */
  150. to_big_endian_uint32_t(pkt, addr);
  151. /* read from registers or memory */
  152. pkt[0] = (memory != 0) ? 0x00 : 0x04;
  153. /* length in big endian */
  154. to_big_endian_uint16_t(pkt + 4, size);
  155. /* send */
  156. wiiuse_send(wm, WM_CMD_READ_DATA, pkt, sizeof(pkt));
  157. /* calculate how many 16B packets we have to get back */
  158. n_full_reports = size / 16;
  159. last_report = size % 16;
  160. output = data;
  161. for (i = 0; i < n_full_reports; ++i) {
  162. wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD);
  163. memmove(output, buf + 6, 16);
  164. output += 16;
  165. }
  166. /* read the last incomplete packet */
  167. if (last_report) {
  168. wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD);
  169. memmove(output, buf + 6, last_report);
  170. }
  171. }
  172. /**
  173. * @brief Get initialization data from the wiimote.
  174. *
  175. * @param wm Pointer to a wiimote_t structure.
  176. * @param data unused
  177. * @param len unused
  178. *
  179. * When first called for a wiimote_t structure, a request
  180. * is sent to the wiimote for initialization information.
  181. * This includes factory set accelerometer data.
  182. * The handshake will be concluded when the wiimote responds
  183. * with this data.
  184. */
  185. #ifdef WIIUSE_SYNC_HANDSHAKE
  186. void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) {
  187. /* send request to wiimote for accelerometer calibration */
  188. byte buf[MAX_PAYLOAD];
  189. /* step 0 - Reset wiimote */
  190. {
  191. //wiiuse_set_leds(wm, WIIMOTE_LED_NONE);
  192. WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
  193. WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
  194. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_ACC);
  195. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR);
  196. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_RUMBLE);
  197. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP);
  198. WIIMOTE_DISABLE_FLAG(wm, WIIUSE_CONTINUOUS);
  199. wiiuse_set_report_type(wm);
  200. wiiuse_millisleep(500);
  201. WIIUSE_DEBUG("Wiimote reset!\n");
  202. }
  203. /* step 1 - calibration of accelerometers */
  204. if(wm->type != WIIUSE_WIIMOTE_MOTION_PLUS_INSIDE) // MotionPlus Inside wiimotes don't answer to that
  205. {
  206. struct accel_t* accel = &wm->accel_calib;
  207. wiiuse_read_data_sync(wm, 1, WM_MEM_OFFSET_CALIBRATION, 8, buf);
  208. /* received read data */
  209. accel->cal_zero.x = buf[0];
  210. accel->cal_zero.y = buf[1];
  211. accel->cal_zero.z = buf[2];
  212. accel->cal_g.x = buf[4] - accel->cal_zero.x;
  213. accel->cal_g.y = buf[5] - accel->cal_zero.y;
  214. accel->cal_g.z = buf[6] - accel->cal_zero.z;
  215. WIIUSE_DEBUG("Calibrated wiimote acc\n");
  216. }
  217. /* step 2 - re-enable IR and ask for status */
  218. {
  219. WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
  220. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
  221. /* now enable IR if it was set before the handshake completed */
  222. if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) {
  223. WIIUSE_DEBUG("Handshake finished, enabling IR.");
  224. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR);
  225. wiiuse_set_ir(wm, 1);
  226. }
  227. WIIUSE_DEBUG("Asking for status ...\n");
  228. wm->event = WIIUSE_CONNECT;
  229. wiiuse_status(wm);
  230. }
  231. }
  232. #else
  233. static void wiiuse_disable_motion_plus1(struct wiimote_t *wm, byte *data, unsigned short len);
  234. static void wiiuse_disable_motion_plus2(struct wiimote_t *wm, byte *data, unsigned short len);
  235. void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) {
  236. if (!wm) {
  237. return;
  238. }
  239. switch (wm->handshake_state) {
  240. case 0: {
  241. byte* buf;
  242. /* continuous reporting off, report to buttons only */
  243. WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
  244. wiiuse_set_leds(wm, WIIMOTE_LED_NONE);
  245. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_ACC);
  246. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR);
  247. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_RUMBLE);
  248. WIIMOTE_DISABLE_FLAG(wm, WIIUSE_CONTINUOUS);
  249. wiiuse_set_report_type(wm);
  250. /* send request to wiimote for accelerometer calibration */
  251. buf = (byte*)malloc(sizeof(byte) * 8);
  252. wiiuse_read_data_cb(wm, wiiuse_handshake, buf, WM_MEM_OFFSET_CALIBRATION, 7);
  253. wm->handshake_state++;
  254. wiiuse_set_leds(wm, WIIMOTE_LED_NONE);
  255. break;
  256. }
  257. case 1: {
  258. struct read_req_t* req = wm->read_req;
  259. struct accel_t* accel = &wm->accel_calib;
  260. byte val;
  261. /* received read data */
  262. accel->cal_zero.x = req->buf[0];
  263. accel->cal_zero.y = req->buf[1];
  264. accel->cal_zero.z = req->buf[2];
  265. accel->cal_g.x = req->buf[4] - accel->cal_zero.x;
  266. accel->cal_g.y = req->buf[5] - accel->cal_zero.y;
  267. accel->cal_g.z = req->buf[6] - accel->cal_zero.z;
  268. /* done with the buffer */
  269. free(req->buf);
  270. /* handshake is done */
  271. WIIUSE_DEBUG("Handshake finished. Calibration: Idle: X=%x Y=%x Z=%x\t+1g: X=%x Y=%x Z=%x",
  272. accel->cal_zero.x, accel->cal_zero.y, accel->cal_zero.z,
  273. accel->cal_g.x, accel->cal_g.y, accel->cal_g.z);
  274. /* M+ off */
  275. val = 0x55;
  276. wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &val, 1, wiiuse_disable_motion_plus1);
  277. break;
  278. }
  279. case 2: {
  280. /* request the status of the wiimote to check for any expansion */
  281. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
  282. WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
  283. wm->handshake_state++;
  284. /* now enable IR if it was set before the handshake completed */
  285. if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) {
  286. WIIUSE_DEBUG("Handshake finished, enabling IR.");
  287. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR);
  288. wiiuse_set_ir(wm, 1);
  289. }
  290. wm->event = WIIUSE_CONNECT;
  291. wiiuse_status(wm);
  292. break;
  293. }
  294. default: {
  295. break;
  296. }
  297. }
  298. }
  299. static void wiiuse_disable_motion_plus1(struct wiimote_t *wm, byte *data, unsigned short len) {
  300. byte val = 0x00;
  301. wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &val, 1, wiiuse_disable_motion_plus2);
  302. }
  303. static void wiiuse_disable_motion_plus2(struct wiimote_t *wm, byte *data, unsigned short len) {
  304. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_FAILED);
  305. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE);
  306. wiiuse_set_ir_mode(wm);
  307. wm->handshake_state++;
  308. wiiuse_handshake(wm, NULL, 0);
  309. }
  310. #endif