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.

events_deadzone.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <usb.h>
  13. #include "data.h"
  14. #include "events.h"
  15. //#define DEBUG_OUTPUT_RAW
  16. #define DEBUG_OUTPUT
  17. const GamePadEventData JoystickEventsDeadZone::deadZone(
  18. 0, 0, 0, 0, 0, 50, 0
  19. );
  20. const uint8_t JoystickEventsDeadZone::deadZoneMouseX = 0;
  21. const uint8_t JoystickEventsDeadZone::deadZoneMouseY = 0;
  22. const GamePadEventData JoystickEventsDeadZone::centerValue(
  23. 0x3FF, 0x3FF, 0x7F,
  24. 0x7F, 0x7F, 0x1FF, 0x7F
  25. );
  26. const uint8_t JoystickEventsDeadZone::centerMouseX = 0x7F;
  27. const uint8_t JoystickEventsDeadZone::centerMouseY = 0x7F;
  28. void JoystickEventsDeadZone::OnGamePadChanged(const GamePadEventData& evt) {
  29. #ifdef DEBUG_OUTPUT_RAW
  30. Serial.print("X: ");
  31. PrintHex<uint16_t > (evt.X, 0x80);
  32. Serial.print(" Y: ");
  33. PrintHex<uint16_t > (evt.Y, 0x80);
  34. Serial.print(" Z: ");
  35. PrintHex<uint8_t > (evt.Z, 0x80);
  36. Serial.print(" Rx: ");
  37. PrintHex<uint8_t > (evt.Rx, 0x80);
  38. Serial.print(" Ry: ");
  39. PrintHex<uint8_t > (evt.Ry, 0x80);
  40. Serial.print(" Rz: ");
  41. PrintHex<uint16_t > (evt.Rz, 0x80);
  42. Serial.print(" S: ");
  43. PrintHex<uint8_t > (evt.Slider, 0x80);
  44. Serial.println("");
  45. #endif
  46. GamePadEventData newData = centerValue;
  47. #ifdef DEBUG_OUTPUT
  48. uint8_t updated = 0;
  49. #endif
  50. if ((evt.X > (centerValue.X + deadZone.X))
  51. || (evt.X < (centerValue.X - deadZone.X))) {
  52. newData.X = evt.X;
  53. #ifdef DEBUG_OUTPUT
  54. updated = 1;
  55. #endif
  56. }
  57. if ((evt.Y > (centerValue.Y + deadZone.Y))
  58. || (evt.Y < (centerValue.Y - deadZone.Y))) {
  59. newData.Y = evt.Y;
  60. #ifdef DEBUG_OUTPUT
  61. updated = 1;
  62. #endif
  63. }
  64. if ((evt.Z > (centerValue.Z + deadZone.Z))
  65. || (evt.Z < (centerValue.Z - deadZone.Z))) {
  66. newData.Z = evt.Z;
  67. #ifdef DEBUG_OUTPUT
  68. updated = 1;
  69. #endif
  70. }
  71. if ((evt.Rx > (centerValue.Rx + deadZone.Rx))
  72. || (evt.Rx < (centerValue.Rx - deadZone.Rx))) {
  73. newData.Rx = evt.Rx;
  74. #ifdef DEBUG_OUTPUT
  75. updated = 1;
  76. #endif
  77. }
  78. if ((evt.Ry > (centerValue.Ry + deadZone.Ry))
  79. || (evt.Ry < (centerValue.Ry - deadZone.Ry))) {
  80. newData.Ry = evt.Ry;
  81. #ifdef DEBUG_OUTPUT
  82. updated = 1;
  83. #endif
  84. }
  85. if ((evt.Rz > (centerValue.Rz + deadZone.Rz))
  86. || (evt.Rz < (centerValue.Rz - deadZone.Rz))) {
  87. newData.Rz = evt.Rz;
  88. #ifdef DEBUG_OUTPUT
  89. updated = 1;
  90. #endif
  91. }
  92. if ((evt.Slider > (centerValue.Slider + deadZone.Slider))
  93. || (evt.Slider < (centerValue.Slider - deadZone.Slider))) {
  94. newData.Slider = evt.Slider;
  95. #ifdef DEBUG_OUTPUT
  96. updated = 1;
  97. #endif
  98. }
  99. #ifdef DEBUG_OUTPUT
  100. if (updated) {
  101. Serial.print("X: ");
  102. PrintHex<uint16_t > (newData.X, 0x80);
  103. Serial.print(" Y: ");
  104. PrintHex<uint16_t > (newData.Y, 0x80);
  105. Serial.print(" Z: ");
  106. PrintHex<uint8_t > (newData.Z, 0x80);
  107. Serial.print(" Rx: ");
  108. PrintHex<uint8_t > (newData.Rx, 0x80);
  109. Serial.print(" Ry: ");
  110. PrintHex<uint8_t > (newData.Ry, 0x80);
  111. Serial.print(" Rz: ");
  112. PrintHex<uint16_t > (newData.Rz, 0x80);
  113. Serial.print(" S: ");
  114. PrintHex<uint8_t > (newData.Slider, 0x80);
  115. Serial.println("");
  116. }
  117. #endif
  118. if (client) {
  119. client->OnGamePadChanged(newData);
  120. }
  121. }
  122. void JoystickEventsDeadZone::OnHatSwitch(uint8_t hat) {
  123. #ifdef DEBUG_OUTPUT
  124. Serial.print("Hat Switch: ");
  125. PrintHex<uint8_t > (hat, 0x80);
  126. Serial.println("");
  127. #endif
  128. if (client) {
  129. client->OnHatSwitch(hat);
  130. }
  131. }
  132. void JoystickEventsDeadZone::OnButtonUp(uint8_t but_id) {
  133. #ifdef DEBUG_OUTPUT
  134. Serial.print("Up: ");
  135. Serial.println(but_id, DEC);
  136. #endif
  137. if (client) {
  138. client->OnButtonUp(but_id);
  139. }
  140. }
  141. void JoystickEventsDeadZone::OnButtonDn(uint8_t but_id) {
  142. #ifdef DEBUG_OUTPUT
  143. Serial.print("Down: ");
  144. Serial.println(but_id, DEC);
  145. #endif
  146. if (client) {
  147. client->OnButtonDn(but_id);
  148. }
  149. }
  150. void JoystickEventsDeadZone::OnMouseMoved(uint8_t x, uint8_t y) {
  151. #ifdef DEBUG_OUTPUT_RAW
  152. Serial.print("Mouse X: ");
  153. PrintHex<uint8_t >(x, 0x80);
  154. Serial.print("\tY: ");
  155. PrintHex<uint8_t >(y, 0x80);
  156. Serial.println("");
  157. #endif
  158. uint8_t newX = centerMouseX;
  159. uint8_t newY = centerMouseY;
  160. #ifdef DEBUG_OUTPUT
  161. uint8_t updated = 0;
  162. #endif
  163. if ((x > (centerMouseX + deadZoneMouseX))
  164. || (x < (centerMouseX - deadZoneMouseX))) {
  165. newX = x;
  166. #ifdef DEBUG_OUTPUT
  167. updated = 1;
  168. #endif
  169. }
  170. if ((y > (centerMouseY + deadZoneMouseY))
  171. || (y < (centerMouseY - deadZoneMouseY))) {
  172. newY = y;
  173. #ifdef DEBUG_OUTPUT
  174. updated = 1;
  175. #endif
  176. }
  177. #ifdef DEBUG_OUTPUT
  178. if (updated) {
  179. Serial.print("Mouse X: ");
  180. PrintHex<uint8_t >(newX, 0x80);
  181. Serial.print("\tY: ");
  182. PrintHex<uint8_t >(newY, 0x80);
  183. Serial.println("");
  184. }
  185. #endif
  186. if (client) {
  187. client->OnMouseMoved(x, y);
  188. }
  189. }