Control drones with a proper joystick
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

joystick_events.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Saitek X52 Arduino USB Host Shield Library.
  3. * Copyright 2016 by Thomas Buck <xythobuz@xythobuz.de>
  4. *
  5. * Based on the USB Host Library HID Joystick example
  6. * https://www.circuitsathome.com/mcu/hid-joystick-code-sample
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2.
  11. */
  12. #include "hid_parser.h"
  13. //#define DEBUG_OUTPUT_RAW
  14. #define DEBUG_OUTPUT
  15. const GamePadEventData JoystickEventsDeadZone::deadZone(
  16. 0, 0, 0, 0, 0, 50, 0
  17. );
  18. const uint8_t JoystickEventsDeadZone::deadZoneMouseX = 0;
  19. const uint8_t JoystickEventsDeadZone::deadZoneMouseY = 0;
  20. const GamePadEventData JoystickEventsDeadZone::centerValue(
  21. 0x3FF, 0x3FF, 0x7F,
  22. 0x7F, 0x7F, 0x1FF, 0x7F
  23. );
  24. const uint8_t JoystickEventsDeadZone::centerMouseX = 0x7F;
  25. const uint8_t JoystickEventsDeadZone::centerMouseY = 0x7F;
  26. void JoystickEventsDeadZone::OnGamePadChanged(const GamePadEventData& evt) {
  27. #ifdef DEBUG_OUTPUT_RAW
  28. Serial.print("X: ");
  29. PrintHex<uint16_t > (evt.X, 0x80);
  30. Serial.print(" Y: ");
  31. PrintHex<uint16_t > (evt.Y, 0x80);
  32. Serial.print(" Z: ");
  33. PrintHex<uint8_t > (evt.Z, 0x80);
  34. Serial.print(" Rx: ");
  35. PrintHex<uint8_t > (evt.Rx, 0x80);
  36. Serial.print(" Ry: ");
  37. PrintHex<uint8_t > (evt.Ry, 0x80);
  38. Serial.print(" Rz: ");
  39. PrintHex<uint16_t > (evt.Rz, 0x80);
  40. Serial.print(" S: ");
  41. PrintHex<uint8_t > (evt.Slider, 0x80);
  42. Serial.println("");
  43. #endif
  44. GamePadEventData newData = centerValue;
  45. #ifdef DEBUG_OUTPUT
  46. uint8_t updated = 0;
  47. #endif
  48. if ((evt.X > (centerValue.X + deadZone.X))
  49. || (evt.X < (centerValue.X - deadZone.X))) {
  50. newData.X = evt.X;
  51. #ifdef DEBUG_OUTPUT
  52. updated = 1;
  53. #endif
  54. }
  55. if ((evt.Y > (centerValue.Y + deadZone.Y))
  56. || (evt.Y < (centerValue.Y - deadZone.Y))) {
  57. newData.Y = evt.Y;
  58. #ifdef DEBUG_OUTPUT
  59. updated = 1;
  60. #endif
  61. }
  62. if ((evt.Z > (centerValue.Z + deadZone.Z))
  63. || (evt.Z < (centerValue.Z - deadZone.Z))) {
  64. newData.Z = evt.Z;
  65. #ifdef DEBUG_OUTPUT
  66. updated = 1;
  67. #endif
  68. }
  69. if ((evt.Rx > (centerValue.Rx + deadZone.Rx))
  70. || (evt.Rx < (centerValue.Rx - deadZone.Rx))) {
  71. newData.Rx = evt.Rx;
  72. #ifdef DEBUG_OUTPUT
  73. updated = 1;
  74. #endif
  75. }
  76. if ((evt.Ry > (centerValue.Ry + deadZone.Ry))
  77. || (evt.Ry < (centerValue.Ry - deadZone.Ry))) {
  78. newData.Ry = evt.Ry;
  79. #ifdef DEBUG_OUTPUT
  80. updated = 1;
  81. #endif
  82. }
  83. if ((evt.Rz > (centerValue.Rz + deadZone.Rz))
  84. || (evt.Rz < (centerValue.Rz - deadZone.Rz))) {
  85. newData.Rz = evt.Rz;
  86. #ifdef DEBUG_OUTPUT
  87. updated = 1;
  88. #endif
  89. }
  90. if ((evt.Slider > (centerValue.Slider + deadZone.Slider))
  91. || (evt.Slider < (centerValue.Slider - deadZone.Slider))) {
  92. newData.Slider = evt.Slider;
  93. #ifdef DEBUG_OUTPUT
  94. updated = 1;
  95. #endif
  96. }
  97. #ifdef DEBUG_OUTPUT
  98. if (updated) {
  99. Serial.print("X: ");
  100. PrintHex<uint16_t > (newData.X, 0x80);
  101. Serial.print(" Y: ");
  102. PrintHex<uint16_t > (newData.Y, 0x80);
  103. Serial.print(" Z: ");
  104. PrintHex<uint8_t > (newData.Z, 0x80);
  105. Serial.print(" Rx: ");
  106. PrintHex<uint8_t > (newData.Rx, 0x80);
  107. Serial.print(" Ry: ");
  108. PrintHex<uint8_t > (newData.Ry, 0x80);
  109. Serial.print(" Rz: ");
  110. PrintHex<uint16_t > (newData.Rz, 0x80);
  111. Serial.print(" S: ");
  112. PrintHex<uint8_t > (newData.Slider, 0x80);
  113. Serial.println("");
  114. }
  115. #endif
  116. if (client) {
  117. client->OnGamePadChanged(newData);
  118. }
  119. }
  120. void JoystickEventsDeadZone::OnHatSwitch(uint8_t hat) {
  121. #ifdef DEBUG_OUTPUT
  122. Serial.print("Hat Switch: ");
  123. PrintHex<uint8_t > (hat, 0x80);
  124. Serial.println("");
  125. #endif
  126. if (client) {
  127. client->OnHatSwitch(hat);
  128. }
  129. }
  130. void JoystickEventsDeadZone::OnButtonUp(uint8_t but_id) {
  131. #ifdef DEBUG_OUTPUT
  132. Serial.print("Up: ");
  133. Serial.println(but_id, DEC);
  134. #endif
  135. if (client) {
  136. client->OnButtonUp(but_id);
  137. }
  138. }
  139. void JoystickEventsDeadZone::OnButtonDn(uint8_t but_id) {
  140. #ifdef DEBUG_OUTPUT
  141. Serial.print("Down: ");
  142. Serial.println(but_id, DEC);
  143. #endif
  144. if (client) {
  145. client->OnButtonDn(but_id);
  146. }
  147. }
  148. void JoystickEventsDeadZone::OnMouseMoved(uint8_t x, uint8_t y) {
  149. #ifdef DEBUG_OUTPUT_RAW
  150. Serial.print("Mouse X: ");
  151. PrintHex<uint8_t >(x, 0x80);
  152. Serial.print("\tY: ");
  153. PrintHex<uint8_t >(y, 0x80);
  154. Serial.println("");
  155. #endif
  156. uint8_t newX = centerMouseX;
  157. uint8_t newY = centerMouseY;
  158. #ifdef DEBUG_OUTPUT
  159. uint8_t updated = 0;
  160. #endif
  161. if ((x > (centerMouseX + deadZoneMouseX))
  162. || (x < (centerMouseX - deadZoneMouseX))) {
  163. newX = x;
  164. #ifdef DEBUG_OUTPUT
  165. updated = 1;
  166. #endif
  167. }
  168. if ((y > (centerMouseY + deadZoneMouseY))
  169. || (y < (centerMouseY - deadZoneMouseY))) {
  170. newY = y;
  171. #ifdef DEBUG_OUTPUT
  172. updated = 1;
  173. #endif
  174. }
  175. #ifdef DEBUG_OUTPUT
  176. if (updated) {
  177. Serial.print("Mouse X: ");
  178. PrintHex<uint8_t >(newX, 0x80);
  179. Serial.print("\tY: ");
  180. PrintHex<uint8_t >(newY, 0x80);
  181. Serial.println("");
  182. }
  183. #endif
  184. if (client) {
  185. client->OnMouseMoved(x, y);
  186. }
  187. }