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.4KB

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