guitar_hero_3.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 Guitar Hero 3 expansion device.
  31. */
  32. #include "guitar_hero_3.h"
  33. #include "dynamics.h" /* for calc_joystick_state */
  34. #include "events.h" /* for handshake_expansion */
  35. #include <stdlib.h> /* for malloc */
  36. #include <string.h> /* for memset */
  37. static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now);
  38. /**
  39. * @brief Handle the handshake data from the guitar.
  40. *
  41. * @param cc A pointer to a classic_ctrl_t structure.
  42. * @param data The data read in from the device.
  43. * @param len The length of the data block, in bytes.
  44. *
  45. * @return Returns 1 if handshake was successful, 0 if not.
  46. */
  47. int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, byte* data, unsigned short len) {
  48. /*
  49. * The good fellows that made the Guitar Hero 3 controller
  50. * failed to factory calibrate the devices. There is no
  51. * calibration data on the device.
  52. */
  53. gh3->btns = 0;
  54. gh3->btns_held = 0;
  55. gh3->btns_released = 0;
  56. gh3->whammy_bar = 0.0f;
  57. /*
  58. TODO: If we're not using anything from calibration data, why are we
  59. even bothering here?
  60. */
  61. if (data[0] == 0xFF) {
  62. /*
  63. * Sometimes the data returned here is not correct.
  64. * This might happen because the wiimote is lagging
  65. * behind our initialization sequence.
  66. * To fix this just request the handshake again.
  67. *
  68. * Other times it's just the first 16 bytes are 0xFF,
  69. * but since the next 16 bytes are the same, just use
  70. * those.
  71. */
  72. if (data[16] == 0xFF) {
  73. /* get the calibration data */
  74. byte* handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte));
  75. WIIUSE_DEBUG("Guitar Hero 3 handshake appears invalid, trying again.");
  76. wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN);
  77. return 0;
  78. } else {
  79. data += 16;
  80. }
  81. }
  82. /* joystick stuff */
  83. gh3->js.max.x = GUITAR_HERO_3_JS_MAX_X;
  84. gh3->js.min.x = GUITAR_HERO_3_JS_MIN_X;
  85. gh3->js.center.x = GUITAR_HERO_3_JS_CENTER_X;
  86. gh3->js.max.y = GUITAR_HERO_3_JS_MAX_Y;
  87. gh3->js.min.y = GUITAR_HERO_3_JS_MIN_Y;
  88. gh3->js.center.y = GUITAR_HERO_3_JS_CENTER_Y;
  89. /* handshake done */
  90. wm->exp.type = EXP_GUITAR_HERO_3;
  91. #ifdef WIIUSE_WIN32
  92. wm->timeout = WIIMOTE_DEFAULT_TIMEOUT;
  93. #endif
  94. return 1;
  95. }
  96. /**
  97. * @brief The guitar disconnected.
  98. *
  99. * @param cc A pointer to a classic_ctrl_t structure.
  100. */
  101. void guitar_hero_3_disconnected(struct guitar_hero_3_t* gh3) {
  102. memset(gh3, 0, sizeof(struct guitar_hero_3_t));
  103. }
  104. /**
  105. * @brief Handle guitar event.
  106. *
  107. * @param cc A pointer to a classic_ctrl_t structure.
  108. * @param msg The message specified in the event packet.
  109. */
  110. void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg) {
  111. guitar_hero_3_pressed_buttons(gh3, from_big_endian_uint16_t(msg + 4));
  112. /* whammy bar */
  113. gh3->whammy_bar = (msg[3] - GUITAR_HERO_3_WHAMMY_BAR_MIN) / (float)(GUITAR_HERO_3_WHAMMY_BAR_MAX - GUITAR_HERO_3_WHAMMY_BAR_MIN);
  114. /* joy stick */
  115. calc_joystick_state(&gh3->js, msg[0], msg[1]);
  116. }
  117. /**
  118. * @brief Find what buttons are pressed.
  119. *
  120. * @param cc A pointer to a classic_ctrl_t structure.
  121. * @param msg The message byte specified in the event packet.
  122. */
  123. static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now) {
  124. /* message is inverted (0 is active, 1 is inactive) */
  125. now = ~now & GUITAR_HERO_3_BUTTON_ALL;
  126. /* pressed now & were pressed, then held */
  127. gh3->btns_held = (now & gh3->btns);
  128. /* were pressed or were held & not pressed now, then released */
  129. gh3->btns_released = ((gh3->btns | gh3->btns_held) & ~now);
  130. /* buttons pressed now */
  131. gh3->btns = now;
  132. }