events.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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 wiimote events.
  31. *
  32. * The file includes functions that handle the events
  33. * that are sent from the wiimote to us.
  34. */
  35. #include "wiiuse_internal.h"
  36. #include "events.h"
  37. #include "classic.h" /* for classic_ctrl_disconnected, etc */
  38. #include "dynamics.h" /* for calculate_gforce, etc */
  39. #include "guitar_hero_3.h" /* for guitar_hero_3_disconnected, etc */
  40. #include "ir.h" /* for calculate_basic_ir, etc */
  41. #include "nunchuk.h" /* for nunchuk_disconnected, etc */
  42. #include "wiiboard.h" /* for wii_board_disconnected, etc */
  43. #include "motion_plus.h" /* for motion_plus_disconnected, etc */
  44. #include "os.h" /* for wiiuse_os_poll */
  45. #include <stdio.h> /* for printf, perror */
  46. #include <stdlib.h> /* for free, malloc */
  47. #include <string.h> /* for memcpy, memset */
  48. static void event_data_read(struct wiimote_t* wm, byte* msg);
  49. static void event_data_write(struct wiimote_t *wm, byte *msg);
  50. static void event_status(struct wiimote_t* wm, byte* msg);
  51. static void handle_expansion(struct wiimote_t* wm, byte* msg);
  52. static void save_state(struct wiimote_t* wm);
  53. static int state_changed(struct wiimote_t* wm);
  54. /**
  55. * @brief Poll the wiimotes for any events.
  56. *
  57. * @param wm An array of pointers to wiimote_t structures.
  58. * @param wiimotes The number of wiimote_t structures in the \a wm array.
  59. *
  60. * @return Returns number of wiimotes that an event has occurred on.
  61. *
  62. * It is necessary to poll the wiimote devices for events
  63. * that occur. If an event occurs on a particular wiimote,
  64. * the event variable will be set.
  65. */
  66. int wiiuse_poll(struct wiimote_t** wm, int wiimotes) {
  67. return wiiuse_os_poll(wm, wiimotes);
  68. }
  69. int wiiuse_update(struct wiimote_t** wiimotes, int nwiimotes, wiiuse_update_cb callback) {
  70. int evnt = 0;
  71. if (wiiuse_poll(wiimotes, nwiimotes)) {
  72. static struct wiimote_callback_data_t s;
  73. int i = 0;
  74. for (; i < nwiimotes; ++i) {
  75. switch (wiimotes[i]->event) {
  76. case WIIUSE_NONE:
  77. break;
  78. default:
  79. /* this could be: WIIUSE_EVENT, WIIUSE_STATUS, WIIUSE_CONNECT, etc.. */
  80. s.uid = wiimotes[i]->unid;
  81. s.leds = wiimotes[i]->leds;
  82. s.battery_level = wiimotes[i]->battery_level;
  83. s.accel = wiimotes[i]->accel;
  84. s.orient = wiimotes[i]->orient;
  85. s.gforce = wiimotes[i]->gforce;
  86. s.ir = wiimotes[i]->ir;
  87. s.buttons = wiimotes[i]->btns;
  88. s.buttons_held = wiimotes[i]->btns_held;
  89. s.buttons_released = wiimotes[i]->btns_released;
  90. s.event = wiimotes[i]->event;
  91. s.state = wiimotes[i]->state;
  92. s.expansion = wiimotes[i]->exp;
  93. callback(&s);
  94. evnt++;
  95. break;
  96. }
  97. }
  98. }
  99. return evnt;
  100. }
  101. /**
  102. * @brief Called on a cycle where no significant change occurs.
  103. *
  104. * @param wm Pointer to a wiimote_t structure.
  105. */
  106. void idle_cycle(struct wiimote_t* wm) {
  107. /*
  108. * Smooth the angles.
  109. *
  110. * This is done to make sure that on every cycle the orientation
  111. * angles are smoothed. Normally when an event occurs the angles
  112. * are updated and smoothed, but if no packet comes in then the
  113. * angles remain the same. This means the angle wiiuse reports
  114. * is still an old value. Smoothing needs to be applied in this
  115. * case in order for the angle it reports to converge to the true
  116. * angle of the device.
  117. */
  118. if (WIIUSE_USING_ACC(wm) && WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)) {
  119. apply_smoothing(&wm->accel_calib, &wm->orient, SMOOTH_ROLL);
  120. apply_smoothing(&wm->accel_calib, &wm->orient, SMOOTH_PITCH);
  121. }
  122. /* clear out any old read requests */
  123. clear_dirty_reads(wm);
  124. }
  125. /**
  126. * @brief Clear out all old 'dirty' read requests.
  127. *
  128. * @param wm Pointer to a wiimote_t structure.
  129. */
  130. void clear_dirty_reads(struct wiimote_t* wm) {
  131. struct read_req_t* req = wm->read_req;
  132. while (req && req->dirty) {
  133. WIIUSE_DEBUG("Cleared old read request for address: %x", req->addr);
  134. wm->read_req = req->next;
  135. free(req);
  136. req = wm->read_req;
  137. }
  138. }
  139. /**
  140. * @brief Handle accel data in a wiimote message.
  141. *
  142. * @param wm Pointer to a wiimote_t structure.
  143. * @param msg The message specified in the event packet.
  144. */
  145. static void handle_wm_accel(struct wiimote_t* wm, byte* msg) {
  146. wm->accel.x = msg[2];
  147. wm->accel.y = msg[3];
  148. wm->accel.z = msg[4];
  149. /* calculate the remote orientation */
  150. calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING));
  151. /* calculate the gforces on each axis */
  152. calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce);
  153. }
  154. /**
  155. * @brief Analyze the event that occurred on a wiimote.
  156. *
  157. * @param wm Pointer to a wiimote_t structure.
  158. * @param event The event that occurred.
  159. * @param msg The message specified in the event packet.
  160. *
  161. * Pass the event to the registered event callback.
  162. */
  163. void propagate_event(struct wiimote_t* wm, byte event, byte* msg) {
  164. save_state(wm);
  165. switch (event) {
  166. case WM_RPT_BTN: {
  167. /* button */
  168. wiiuse_pressed_buttons(wm, msg);
  169. break;
  170. }
  171. case WM_RPT_BTN_ACC: {
  172. /* button - motion */
  173. wiiuse_pressed_buttons(wm, msg);
  174. handle_wm_accel(wm, msg);
  175. break;
  176. }
  177. case WM_RPT_READ: {
  178. /* data read */
  179. event_data_read(wm, msg);
  180. /* yeah buttons may be pressed, but this wasn't an "event" */
  181. return;
  182. }
  183. case WM_RPT_CTRL_STATUS: {
  184. /* controller status */
  185. event_status(wm, msg);
  186. /* don't execute the event callback */
  187. return;
  188. }
  189. case WM_RPT_BTN_EXP: {
  190. /* button - expansion */
  191. wiiuse_pressed_buttons(wm, msg);
  192. handle_expansion(wm, msg + 2);
  193. break;
  194. }
  195. case WM_RPT_BTN_ACC_EXP: {
  196. /* button - motion - expansion */
  197. wiiuse_pressed_buttons(wm, msg);
  198. handle_wm_accel(wm, msg);
  199. handle_expansion(wm, msg + 5);
  200. break;
  201. }
  202. case WM_RPT_BTN_ACC_IR: {
  203. /* button - motion - ir */
  204. wiiuse_pressed_buttons(wm, msg);
  205. handle_wm_accel(wm, msg);
  206. /* ir */
  207. calculate_extended_ir(wm, msg + 5);
  208. break;
  209. }
  210. case WM_RPT_BTN_IR_EXP: {
  211. /* button - ir - expansion */
  212. wiiuse_pressed_buttons(wm, msg);
  213. handle_expansion(wm, msg + 12);
  214. /* ir */
  215. calculate_basic_ir(wm, msg + 2);
  216. break;
  217. }
  218. case WM_RPT_BTN_ACC_IR_EXP: {
  219. /* button - motion - ir - expansion */
  220. wiiuse_pressed_buttons(wm, msg);
  221. handle_wm_accel(wm, msg);
  222. handle_expansion(wm, msg + 15);
  223. /* ir */
  224. calculate_basic_ir(wm, msg + 5);
  225. break;
  226. }
  227. case WM_RPT_WRITE: {
  228. event_data_write(wm, msg);
  229. break;
  230. }
  231. default: {
  232. WIIUSE_WARNING("Unknown event, can not handle it [Code 0x%x].", event);
  233. return;
  234. }
  235. }
  236. /* was there an event? */
  237. if (state_changed(wm)) {
  238. wm->event = WIIUSE_EVENT;
  239. }
  240. }
  241. /**
  242. * @brief Find what buttons are pressed.
  243. *
  244. * @param wm Pointer to a wiimote_t structure.
  245. * @param msg The message specified in the event packet.
  246. */
  247. void wiiuse_pressed_buttons(struct wiimote_t* wm, byte* msg) {
  248. int16_t now;
  249. /* convert from big endian */
  250. now = from_big_endian_uint16_t(msg) & WIIMOTE_BUTTON_ALL;
  251. /* pressed now & were pressed, then held */
  252. wm->btns_held = (now & wm->btns);
  253. /* were pressed or were held & not pressed now, then released */
  254. wm->btns_released = ((wm->btns | wm->btns_held) & ~now);
  255. /* buttons pressed now */
  256. wm->btns = now;
  257. }
  258. /**
  259. * @brief Received a data packet from a read request.
  260. *
  261. * @param wm Pointer to a wiimote_t structure.
  262. * @param msg The message specified in the event packet.
  263. *
  264. * Data from the wiimote comes in packets. If the requested
  265. * data segment size is bigger than one packet can hold then
  266. * several packets will be received. These packets are first
  267. * reassembled into one, then the registered callback function
  268. * that handles data reads is invoked.
  269. */
  270. static void event_data_read(struct wiimote_t* wm, byte* msg) {
  271. /* we must always assume the packet received is from the most recent request */
  272. byte err;
  273. byte len;
  274. uint16_t offset;
  275. struct read_req_t* req = wm->read_req;
  276. wiiuse_pressed_buttons(wm, msg);
  277. /* find the next non-dirty request */
  278. while (req && req->dirty) {
  279. req = req->next;
  280. }
  281. /* if we don't have a request out then we didn't ask for this packet */
  282. if (!req) {
  283. WIIUSE_WARNING("Received data packet when no request was made.");
  284. return;
  285. }
  286. err = msg[2] & 0x0F;
  287. if (err == 0x08) {
  288. WIIUSE_WARNING("Unable to read data - address does not exist.");
  289. } else if (err == 0x07) {
  290. WIIUSE_WARNING("Unable to read data - address is for write-only registers.");
  291. } else if (err) {
  292. WIIUSE_WARNING("Unable to read data - unknown error code %x.", err);
  293. }
  294. if (err) {
  295. /* this request errored out, so skip it and go to the next one */
  296. /* delete this request */
  297. wm->read_req = req->next;
  298. free(req);
  299. /* if another request exists send it to the wiimote */
  300. if (wm->read_req) {
  301. wiiuse_send_next_pending_read_request(wm);
  302. }
  303. return;
  304. }
  305. len = ((msg[2] & 0xF0) >> 4) + 1;
  306. offset = from_big_endian_uint16_t(msg + 3);
  307. req->addr = (req->addr & 0xFFFF);
  308. req->wait -= len;
  309. if (req->wait >= req->size)
  310. /* this should never happen */
  311. {
  312. req->wait = 0;
  313. }
  314. WIIUSE_DEBUG("Received read packet:");
  315. WIIUSE_DEBUG(" Packet read offset: %i bytes", offset);
  316. WIIUSE_DEBUG(" Request read offset: %i bytes", req->addr);
  317. WIIUSE_DEBUG(" Read offset into buf: %i bytes", offset - req->addr);
  318. WIIUSE_DEBUG(" Read data size: %i bytes", len);
  319. WIIUSE_DEBUG(" Still need: %i bytes", req->wait);
  320. /* reconstruct this part of the data */
  321. memcpy((req->buf + offset - req->addr), (msg + 5), len);
  322. #ifdef WITH_WIIUSE_DEBUG
  323. {
  324. int i = 0;
  325. printf("Read: ");
  326. for (; i < req->size - req->wait; ++i) {
  327. printf("%x ", req->buf[i]);
  328. }
  329. printf("\n");
  330. }
  331. #endif
  332. /* if all data has been received, execute the read event callback or generate event */
  333. if (!req->wait) {
  334. if (req->cb) {
  335. /* this was a callback, so invoke it now */
  336. req->cb(wm, req->buf, req->size);
  337. /* delete this request */
  338. wm->read_req = req->next;
  339. free(req);
  340. } else {
  341. /*
  342. * This should generate an event.
  343. * We need to leave the event in the array so the client
  344. * can access it still. We'll flag is as being 'dirty'
  345. * and give the client one cycle to use it. Next event
  346. * we will remove it from the list.
  347. */
  348. wm->event = WIIUSE_READ_DATA;
  349. req->dirty = 1;
  350. }
  351. /* if another request exists send it to the wiimote */
  352. if (wm->read_req) {
  353. wiiuse_send_next_pending_read_request(wm);
  354. }
  355. }
  356. }
  357. static void event_data_write(struct wiimote_t *wm, byte *msg) {
  358. struct data_req_t* req = wm->data_req;
  359. wiiuse_pressed_buttons(wm, msg);
  360. /* if we don't have a request out then we didn't ask for this packet */
  361. if (!req) {
  362. WIIUSE_WARNING("Transmitting data packet when no request was made.");
  363. return;
  364. }
  365. if (!(req->state == REQ_SENT)) {
  366. WIIUSE_WARNING("Transmission is not necessary");
  367. /* delete this request */
  368. wm->data_req = req->next;
  369. free(req);
  370. return;
  371. }
  372. req->state = REQ_DONE;
  373. if (req->cb) {
  374. /* this was a callback, so invoke it now */
  375. req->cb(wm, NULL, 0);
  376. /* delete this request */
  377. wm->data_req = req->next;
  378. free(req);
  379. } else {
  380. /*
  381. * This should generate an event.
  382. * We need to leave the event in the array so the client
  383. * can access it still. We'll flag is as being 'REQ_DONE'
  384. * and give the client one cycle to use it. Next event
  385. * we will remove it from the list.
  386. */
  387. wm->event = WIIUSE_WRITE_DATA;
  388. }
  389. /* if another request exists send it to the wiimote */
  390. if (wm->data_req) {
  391. wiiuse_send_next_pending_write_request(wm);
  392. }
  393. }
  394. /**
  395. * @brief Read the controller status.
  396. *
  397. * @param wm Pointer to a wiimote_t structure.
  398. * @param msg The message specified in the event packet.
  399. *
  400. * Read the controller status and execute the registered status callback.
  401. */
  402. static void event_status(struct wiimote_t* wm, byte* msg) {
  403. int led[4] = {0, 0, 0, 0};
  404. int attachment = 0;
  405. int ir = 0;
  406. int exp_changed = 0;
  407. struct data_req_t* req = wm->data_req;
  408. /* initial handshake is not finished yet, ignore this */
  409. if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_HANDSHAKE)) {
  410. return;
  411. }
  412. /*
  413. * An event occurred.
  414. * This event can be overwritten by a more specific
  415. * event type during a handshake or expansion removal.
  416. */
  417. wm->event = WIIUSE_STATUS;
  418. wiiuse_pressed_buttons(wm, msg);
  419. /* find what LEDs are lit */
  420. if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_1) {
  421. led[0] = 1;
  422. }
  423. if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_2) {
  424. led[1] = 1;
  425. }
  426. if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_3) {
  427. led[2] = 1;
  428. }
  429. if (msg[2] & WM_CTRL_STATUS_BYTE1_LED_4) {
  430. led[3] = 1;
  431. }
  432. /* probe for Motion+ */
  433. if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_MPLUS_PRESENT)) {
  434. wiiuse_probe_motion_plus(wm);
  435. }
  436. /* is an attachment connected to the expansion port? */
  437. if ((msg[2] & WM_CTRL_STATUS_BYTE1_ATTACHMENT) == WM_CTRL_STATUS_BYTE1_ATTACHMENT) {
  438. WIIUSE_DEBUG("Attachment detected!");
  439. attachment = 1;
  440. }
  441. /* is the speaker enabled? */
  442. if ((msg[2] & WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED) == WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED) {
  443. WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_SPEAKER);
  444. }
  445. /* is IR sensing enabled? */
  446. if ((msg[2] & WM_CTRL_STATUS_BYTE1_IR_ENABLED) == WM_CTRL_STATUS_BYTE1_IR_ENABLED) {
  447. ir = 1;
  448. }
  449. /* find the battery level and normalize between 0 and 1 */
  450. wm->battery_level = (msg[5] / (float)WM_MAX_BATTERY_CODE);
  451. /* expansion port */
  452. if (attachment && !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP) && !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP_HANDSHAKE)) {
  453. /* send the initialization code for the attachment */
  454. handshake_expansion(wm, NULL, 0);
  455. exp_changed = 1;
  456. } else if (!attachment && WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) {
  457. /* attachment removed */
  458. disable_expansion(wm);
  459. exp_changed = 1;
  460. }
  461. #ifdef WIIUSE_WIN32
  462. if (!attachment) {
  463. WIIUSE_DEBUG("Setting timeout to normal %i ms.", wm->normal_timeout);
  464. wm->timeout = wm->normal_timeout;
  465. }
  466. #endif
  467. /*
  468. * From now on the remote will only send status packets.
  469. * We need to send a WIIMOTE_CMD_REPORT_TYPE packet to
  470. * reenable other incoming reports.
  471. */
  472. if (exp_changed && WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) {
  473. /*
  474. * Since the expansion status changed IR needs to
  475. * be reset for the new IR report mode.
  476. */
  477. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR);
  478. wiiuse_set_ir(wm, 1);
  479. } else {
  480. wiiuse_set_report_type(wm);
  481. return;
  482. }
  483. /* handling new Tx for changed exp */
  484. if (!req) {
  485. return;
  486. }
  487. if (!(req->state == REQ_SENT)) {
  488. return;
  489. }
  490. wm->data_req = req->next;
  491. req->state = REQ_DONE;
  492. /* if(req->cb!=NULL) req->cb(wm,msg,6); */
  493. free(req);
  494. }
  495. /**
  496. * @brief Handle data from the expansion.
  497. *
  498. * @param wm A pointer to a wiimote_t structure.
  499. * @param msg The message specified in the event packet for the expansion.
  500. */
  501. static void handle_expansion(struct wiimote_t* wm, byte* msg) {
  502. switch (wm->exp.type) {
  503. case EXP_NUNCHUK:
  504. nunchuk_event(&wm->exp.nunchuk, msg);
  505. break;
  506. case EXP_CLASSIC:
  507. classic_ctrl_event(&wm->exp.classic, msg);
  508. break;
  509. case EXP_GUITAR_HERO_3:
  510. guitar_hero_3_event(&wm->exp.gh3, msg);
  511. break;
  512. case EXP_WII_BOARD:
  513. wii_board_event(&wm->exp.wb, msg);
  514. break;
  515. case EXP_MOTION_PLUS:
  516. case EXP_MOTION_PLUS_CLASSIC:
  517. case EXP_MOTION_PLUS_NUNCHUK:
  518. motion_plus_event(&wm->exp.mp, wm->exp.type, msg);
  519. break;
  520. default:
  521. break;
  522. }
  523. }
  524. /**
  525. * @brief Handle the handshake data from the expansion device.
  526. *
  527. * @param wm A pointer to a wiimote_t structure.
  528. * @param data The data read in from the device.
  529. * @param len The length of the data block, in bytes.
  530. *
  531. * Tries to determine what kind of expansion was attached
  532. * and invoke the correct handshake function.
  533. *
  534. * If the data is NULL then this function will try to start
  535. * a handshake with the expansion.
  536. */
  537. void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) {
  538. int id;
  539. byte val = 0;
  540. byte buf = 0x00;
  541. byte* handshake_buf;
  542. int gotIt = 0;
  543. WIIUSE_DEBUG("handshake_expansion with state %d", wm->expansion_state);
  544. switch (wm->expansion_state) {
  545. /* These two initialization writes disable the encryption */
  546. case 0:
  547. wm->expansion_state = 1;
  548. /* increase the timeout until the handshake completes */
  549. #ifdef WIIUSE_WIN32
  550. WIIUSE_DEBUG("write 0x55 - Setting timeout to expansion %i ms.", wm->exp_timeout);
  551. wm->timeout = wm->exp_timeout;
  552. #endif
  553. buf = 0x55;
  554. wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE1, &buf, 1, handshake_expansion);
  555. break;
  556. case 1:
  557. wm->expansion_state = 2;
  558. /* increase the timeout until the handshake completes */
  559. #ifdef WIIUSE_WIN32
  560. WIIUSE_DEBUG("write 0x00 - Setting timeout to expansion %i ms.", wm->exp_timeout);
  561. wm->timeout = wm->exp_timeout;
  562. #endif
  563. val = 0x00;
  564. wiiuse_write_data_cb(wm, WM_EXP_MEM_ENABLE2, &buf, 1, handshake_expansion);
  565. break;
  566. case 2:
  567. wm->expansion_state = 3;
  568. /* get the calibration data */
  569. if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) {
  570. disable_expansion(wm);
  571. }
  572. handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte));
  573. /* tell the wiimote to send expansion data */
  574. WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP);
  575. wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN);
  576. break;
  577. case 3:
  578. if (!data || !len) {
  579. WIIUSE_DEBUG("no handshake data received from expansion");
  580. disable_expansion(wm);
  581. return;
  582. }
  583. wm->expansion_state = 0;
  584. id = from_big_endian_uint32_t(data + 220);
  585. switch (id) {
  586. case EXP_ID_CODE_NUNCHUK:
  587. if (nunchuk_handshake(wm, &wm->exp.nunchuk, data, len)) {
  588. wm->event = WIIUSE_NUNCHUK_INSERTED;
  589. gotIt = 1;
  590. }
  591. break;
  592. case EXP_ID_CODE_CLASSIC_CONTROLLER:
  593. if (classic_ctrl_handshake(wm, &wm->exp.classic, data, len)) {
  594. wm->event = WIIUSE_CLASSIC_CTRL_INSERTED;
  595. gotIt = 1;
  596. }
  597. break;
  598. case EXP_ID_CODE_GUITAR:
  599. if (guitar_hero_3_handshake(wm, &wm->exp.gh3, data, len)) {
  600. wm->event = WIIUSE_GUITAR_HERO_3_CTRL_INSERTED;
  601. gotIt = 1;
  602. }
  603. break;
  604. case EXP_ID_CODE_MOTION_PLUS:
  605. case EXP_ID_CODE_MOTION_PLUS_CLASSIC:
  606. case EXP_ID_CODE_MOTION_PLUS_NUNCHUK:
  607. /* wiiuse_motion_plus_handshake(wm, data, len); */
  608. wm->event = WIIUSE_MOTION_PLUS_ACTIVATED;
  609. gotIt = 1;
  610. break;
  611. case EXP_ID_CODE_WII_BOARD:
  612. if (wii_board_handshake(wm, &wm->exp.wb, data, len)) {
  613. wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED;
  614. gotIt = 1;
  615. }
  616. break;
  617. default:
  618. WIIUSE_WARNING("Unknown expansion type. Code: 0x%x", id);
  619. break;
  620. }
  621. free(data);
  622. if (gotIt) {
  623. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE);
  624. WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP);
  625. } else {
  626. WIIUSE_WARNING("Could not handshake with expansion id: 0x%x", id);
  627. }
  628. wiiuse_set_ir_mode(wm);
  629. wiiuse_status(wm);
  630. break;
  631. }
  632. }
  633. /**
  634. * @brief Disable the expansion device if it was enabled.
  635. *
  636. * @param wm A pointer to a wiimote_t structure.
  637. * @param data The data read in from the device.
  638. * @param len The length of the data block, in bytes.
  639. *
  640. * If the data is NULL then this function will try to start
  641. * a handshake with the expansion.
  642. */
  643. void disable_expansion(struct wiimote_t* wm) {
  644. WIIUSE_DEBUG("Disabling expansion");
  645. if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) {
  646. return;
  647. }
  648. /* tell the associated module the expansion was removed */
  649. switch (wm->exp.type) {
  650. case EXP_NUNCHUK:
  651. nunchuk_disconnected(&wm->exp.nunchuk);
  652. wm->event = WIIUSE_NUNCHUK_REMOVED;
  653. break;
  654. case EXP_CLASSIC:
  655. classic_ctrl_disconnected(&wm->exp.classic);
  656. wm->event = WIIUSE_CLASSIC_CTRL_REMOVED;
  657. break;
  658. case EXP_GUITAR_HERO_3:
  659. guitar_hero_3_disconnected(&wm->exp.gh3);
  660. wm->event = WIIUSE_GUITAR_HERO_3_CTRL_REMOVED;
  661. break;
  662. case EXP_WII_BOARD:
  663. wii_board_disconnected(&wm->exp.wb);
  664. wm->event = WIIUSE_WII_BOARD_CTRL_REMOVED;
  665. break;
  666. case EXP_MOTION_PLUS:
  667. case EXP_MOTION_PLUS_CLASSIC:
  668. case EXP_MOTION_PLUS_NUNCHUK:
  669. motion_plus_disconnected(&wm->exp.mp);
  670. wm->event = WIIUSE_MOTION_PLUS_REMOVED;
  671. break;
  672. default:
  673. break;
  674. }
  675. WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP);
  676. wm->exp.type = EXP_NONE;
  677. wm->expansion_state = 0;
  678. }
  679. /**
  680. * @brief Save important state data.
  681. * @param wm A pointer to a wiimote_t structure.
  682. */
  683. static void save_state(struct wiimote_t* wm) {
  684. /* wiimote */
  685. wm->lstate.btns = wm->btns;
  686. wm->lstate.accel = wm->accel;
  687. /* ir */
  688. if (WIIUSE_USING_IR(wm)) {
  689. wm->lstate.ir_ax = wm->ir.ax;
  690. wm->lstate.ir_ay = wm->ir.ay;
  691. wm->lstate.ir_distance = wm->ir.distance;
  692. }
  693. /* expansion */
  694. switch (wm->exp.type) {
  695. case EXP_NUNCHUK:
  696. wm->lstate.exp_ljs_ang = wm->exp.nunchuk.js.ang;
  697. wm->lstate.exp_ljs_mag = wm->exp.nunchuk.js.mag;
  698. wm->lstate.exp_btns = wm->exp.nunchuk.btns;
  699. wm->lstate.exp_accel = wm->exp.nunchuk.accel;
  700. break;
  701. case EXP_CLASSIC:
  702. wm->lstate.exp_ljs_ang = wm->exp.classic.ljs.ang;
  703. wm->lstate.exp_ljs_mag = wm->exp.classic.ljs.mag;
  704. wm->lstate.exp_rjs_ang = wm->exp.classic.rjs.ang;
  705. wm->lstate.exp_rjs_mag = wm->exp.classic.rjs.mag;
  706. wm->lstate.exp_r_shoulder = wm->exp.classic.r_shoulder;
  707. wm->lstate.exp_l_shoulder = wm->exp.classic.l_shoulder;
  708. wm->lstate.exp_btns = wm->exp.classic.btns;
  709. break;
  710. case EXP_GUITAR_HERO_3:
  711. wm->lstate.exp_ljs_ang = wm->exp.gh3.js.ang;
  712. wm->lstate.exp_ljs_mag = wm->exp.gh3.js.mag;
  713. wm->lstate.exp_r_shoulder = wm->exp.gh3.whammy_bar;
  714. wm->lstate.exp_btns = wm->exp.gh3.btns;
  715. break;
  716. case EXP_WII_BOARD:
  717. wm->lstate.exp_wb_rtr = wm->exp.wb.rtr;
  718. wm->lstate.exp_wb_rtl = wm->exp.wb.rtl;
  719. wm->lstate.exp_wb_rbr = wm->exp.wb.rbr;
  720. wm->lstate.exp_wb_rbl = wm->exp.wb.rbl;
  721. break;
  722. case EXP_MOTION_PLUS:
  723. case EXP_MOTION_PLUS_CLASSIC:
  724. case EXP_MOTION_PLUS_NUNCHUK: {
  725. wm->lstate.drx = wm->exp.mp.raw_gyro.pitch;
  726. wm->lstate.dry = wm->exp.mp.raw_gyro.roll;
  727. wm->lstate.drz = wm->exp.mp.raw_gyro.yaw;
  728. if (wm->exp.type == EXP_MOTION_PLUS_CLASSIC) {
  729. wm->lstate.exp_ljs_ang = wm->exp.classic.ljs.ang;
  730. wm->lstate.exp_ljs_mag = wm->exp.classic.ljs.mag;
  731. wm->lstate.exp_rjs_ang = wm->exp.classic.rjs.ang;
  732. wm->lstate.exp_rjs_mag = wm->exp.classic.rjs.mag;
  733. wm->lstate.exp_r_shoulder = wm->exp.classic.r_shoulder;
  734. wm->lstate.exp_l_shoulder = wm->exp.classic.l_shoulder;
  735. wm->lstate.exp_btns = wm->exp.classic.btns;
  736. } else {
  737. wm->lstate.exp_ljs_ang = wm->exp.nunchuk.js.ang;
  738. wm->lstate.exp_ljs_mag = wm->exp.nunchuk.js.mag;
  739. wm->lstate.exp_btns = wm->exp.nunchuk.btns;
  740. wm->lstate.exp_accel = wm->exp.nunchuk.accel;
  741. }
  742. break;
  743. }
  744. case EXP_NONE:
  745. break;
  746. }
  747. }
  748. /**
  749. * @brief Determine if the current state differs significantly from the previous.
  750. * @param wm A pointer to a wiimote_t structure.
  751. * @return 1 if a significant change occurred, 0 if not.
  752. */
  753. static int state_changed(struct wiimote_t* wm) {
  754. #define STATE_CHANGED(a, b) if (a != b) return 1
  755. #define CROSS_THRESH(last, now, thresh) \
  756. do { \
  757. if (WIIMOTE_IS_FLAG_SET(wm, WIIUSE_ORIENT_THRESH)) { \
  758. if ((diff_f(last.roll, now.roll) >= thresh) || \
  759. (diff_f(last.pitch, now.pitch) >= thresh) || \
  760. (diff_f(last.yaw, now.yaw) >= thresh)) \
  761. { \
  762. last = now; \
  763. return 1; \
  764. } \
  765. } else { \
  766. if (last.roll != now.roll) return 1; \
  767. if (last.pitch != now.pitch) return 1; \
  768. if (last.yaw != now.yaw) return 1; \
  769. } \
  770. } while (0)
  771. #define CROSS_THRESH_XYZ(last, now, thresh) \
  772. do { \
  773. if (WIIMOTE_IS_FLAG_SET(wm, WIIUSE_ORIENT_THRESH)) { \
  774. if ((diff_f(last.x, now.x) >= thresh) || \
  775. (diff_f(last.y, now.y) >= thresh) || \
  776. (diff_f(last.z, now.z) >= thresh)) \
  777. { \
  778. last = now; \
  779. return 1; \
  780. } \
  781. } else { \
  782. if (last.x != now.x) return 1; \
  783. if (last.y != now.y) return 1; \
  784. if (last.z != now.z) return 1; \
  785. } \
  786. } while (0)
  787. /* ir */
  788. if (WIIUSE_USING_IR(wm)) {
  789. STATE_CHANGED(wm->lstate.ir_ax, wm->ir.ax);
  790. STATE_CHANGED(wm->lstate.ir_ay, wm->ir.ay);
  791. STATE_CHANGED(wm->lstate.ir_distance, wm->ir.distance);
  792. }
  793. /* accelerometer */
  794. if (WIIUSE_USING_ACC(wm)) {
  795. /* raw accelerometer */
  796. CROSS_THRESH_XYZ(wm->lstate.accel, wm->accel, wm->accel_threshold);
  797. /* orientation */
  798. CROSS_THRESH(wm->lstate.orient, wm->orient, wm->orient_threshold);
  799. }
  800. /* expansion */
  801. switch (wm->exp.type) {
  802. case EXP_NUNCHUK: {
  803. STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.nunchuk.js.ang);
  804. STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.nunchuk.js.mag);
  805. STATE_CHANGED(wm->lstate.exp_btns, wm->exp.nunchuk.btns);
  806. CROSS_THRESH(wm->lstate.exp_orient, wm->exp.nunchuk.orient, wm->exp.nunchuk.orient_threshold);
  807. CROSS_THRESH_XYZ(wm->lstate.exp_accel, wm->exp.nunchuk.accel, wm->exp.nunchuk.accel_threshold);
  808. break;
  809. }
  810. case EXP_CLASSIC: {
  811. STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.classic.ljs.ang);
  812. STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.classic.ljs.mag);
  813. STATE_CHANGED(wm->lstate.exp_rjs_ang, wm->exp.classic.rjs.ang);
  814. STATE_CHANGED(wm->lstate.exp_rjs_mag, wm->exp.classic.rjs.mag);
  815. STATE_CHANGED(wm->lstate.exp_r_shoulder, wm->exp.classic.r_shoulder);
  816. STATE_CHANGED(wm->lstate.exp_l_shoulder, wm->exp.classic.l_shoulder);
  817. STATE_CHANGED(wm->lstate.exp_btns, wm->exp.classic.btns);
  818. break;
  819. }
  820. case EXP_GUITAR_HERO_3: {
  821. STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.gh3.js.ang);
  822. STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.gh3.js.mag);
  823. STATE_CHANGED(wm->lstate.exp_r_shoulder, wm->exp.gh3.whammy_bar);
  824. STATE_CHANGED(wm->lstate.exp_btns, wm->exp.gh3.btns);
  825. break;
  826. }
  827. case EXP_WII_BOARD: {
  828. STATE_CHANGED(wm->lstate.exp_wb_rtr, wm->exp.wb.tr);
  829. STATE_CHANGED(wm->lstate.exp_wb_rtl, wm->exp.wb.tl);
  830. STATE_CHANGED(wm->lstate.exp_wb_rbr, wm->exp.wb.br);
  831. STATE_CHANGED(wm->lstate.exp_wb_rbl, wm->exp.wb.bl);
  832. break;
  833. }
  834. case EXP_MOTION_PLUS:
  835. case EXP_MOTION_PLUS_CLASSIC:
  836. case EXP_MOTION_PLUS_NUNCHUK: {
  837. STATE_CHANGED(wm->lstate.drx, wm->exp.mp.raw_gyro.pitch);
  838. STATE_CHANGED(wm->lstate.dry, wm->exp.mp.raw_gyro.roll);
  839. STATE_CHANGED(wm->lstate.drz, wm->exp.mp.raw_gyro.yaw);
  840. if (wm->exp.type == EXP_MOTION_PLUS_CLASSIC) {
  841. STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.classic.ljs.ang);
  842. STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.classic.ljs.mag);
  843. STATE_CHANGED(wm->lstate.exp_rjs_ang, wm->exp.classic.rjs.ang);
  844. STATE_CHANGED(wm->lstate.exp_rjs_mag, wm->exp.classic.rjs.mag);
  845. STATE_CHANGED(wm->lstate.exp_r_shoulder, wm->exp.classic.r_shoulder);
  846. STATE_CHANGED(wm->lstate.exp_l_shoulder, wm->exp.classic.l_shoulder);
  847. STATE_CHANGED(wm->lstate.exp_btns, wm->exp.classic.btns);
  848. } else {
  849. STATE_CHANGED(wm->lstate.exp_ljs_ang, wm->exp.nunchuk.js.ang);
  850. STATE_CHANGED(wm->lstate.exp_ljs_mag, wm->exp.nunchuk.js.mag);
  851. STATE_CHANGED(wm->lstate.exp_btns, wm->exp.nunchuk.btns);
  852. CROSS_THRESH(wm->lstate.exp_orient, wm->exp.nunchuk.orient, wm->exp.nunchuk.orient_threshold);
  853. CROSS_THRESH_XYZ(wm->lstate.exp_accel, wm->exp.nunchuk.accel, wm->exp.nunchuk.accel_threshold);
  854. }
  855. break;
  856. }
  857. case EXP_NONE: {
  858. break;
  859. }
  860. }
  861. STATE_CHANGED(wm->lstate.btns, wm->btns);
  862. return 0;
  863. }