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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /*
  18. * Deadzone around the axis center, in both directions.
  19. * You can try to counteract a faulty Yaw-Potentiometer
  20. * with this, but it won't be enough in extreme cases.
  21. * X, Y, Z, Rx, Ry, Rz, Slider
  22. */
  23. const GamePadEventData JoystickEventsDeadZone::deadZone(
  24. 4, 4, 2, 2, 5, 25, 2
  25. );
  26. const uint8_t JoystickEventsDeadZone::deadZoneMouseX = 1;
  27. const uint8_t JoystickEventsDeadZone::deadZoneMouseY = 1;
  28. /*
  29. * Absolute values of the axis center. Deadzone will be applied
  30. * in both directions around these values.
  31. * X 11bit, Y 11bit, Z 8bit, Rx 8bit, Ry 8bit, Rz 10bit, Slider 8bit
  32. */
  33. const GamePadEventData JoystickEventsDeadZone::centerValue(
  34. 0x3FF, 0x3FF, 0x7F, 0x7F, 0x7F, 0x1FF, 0x7F
  35. );
  36. const uint8_t JoystickEventsDeadZone::centerMouseX = 0x07;
  37. const uint8_t JoystickEventsDeadZone::centerMouseY = 0x07;
  38. void JoystickEventsDeadZone::OnGamePadChanged(const GamePadEventData& evt) {
  39. #ifdef DEBUG_OUTPUT_RAW
  40. Serial.print("Raw X: ");
  41. PrintHex<uint16_t > (evt.X, 0x80);
  42. Serial.print(" Y: ");
  43. PrintHex<uint16_t > (evt.Y, 0x80);
  44. Serial.print(" Z: ");
  45. PrintHex<uint8_t > (evt.Z, 0x80);
  46. Serial.print(" Rx: ");
  47. PrintHex<uint8_t > (evt.Rx, 0x80);
  48. Serial.print(" Ry: ");
  49. PrintHex<uint8_t > (evt.Ry, 0x80);
  50. Serial.print(" Rz: ");
  51. PrintHex<uint16_t > (evt.Rz, 0x80);
  52. Serial.print(" S: ");
  53. PrintHex<uint8_t > (evt.Slider, 0x80);
  54. Serial.println("");
  55. #endif
  56. GamePadEventData newData = centerValue;
  57. #ifdef DEBUG_OUTPUT
  58. uint8_t updated = 0;
  59. #endif
  60. if ((evt.X > (centerValue.X + deadZone.X))
  61. || (evt.X < (centerValue.X - deadZone.X))) {
  62. newData.X = evt.X;
  63. #ifdef DEBUG_OUTPUT
  64. updated = 1;
  65. #endif
  66. }
  67. if ((evt.Y > (centerValue.Y + deadZone.Y))
  68. || (evt.Y < (centerValue.Y - deadZone.Y))) {
  69. newData.Y = evt.Y;
  70. #ifdef DEBUG_OUTPUT
  71. updated = 1;
  72. #endif
  73. }
  74. if ((evt.Z > (centerValue.Z + deadZone.Z))
  75. || (evt.Z < (centerValue.Z - deadZone.Z))) {
  76. newData.Z = evt.Z;
  77. #ifdef DEBUG_OUTPUT
  78. updated = 1;
  79. #endif
  80. }
  81. if ((evt.Rx > (centerValue.Rx + deadZone.Rx))
  82. || (evt.Rx < (centerValue.Rx - deadZone.Rx))) {
  83. newData.Rx = evt.Rx;
  84. #ifdef DEBUG_OUTPUT
  85. updated = 1;
  86. #endif
  87. }
  88. if ((evt.Ry > (centerValue.Ry + deadZone.Ry))
  89. || (evt.Ry < (centerValue.Ry - deadZone.Ry))) {
  90. newData.Ry = evt.Ry;
  91. #ifdef DEBUG_OUTPUT
  92. updated = 1;
  93. #endif
  94. }
  95. if ((evt.Rz > (centerValue.Rz + deadZone.Rz))
  96. || (evt.Rz < (centerValue.Rz - deadZone.Rz))) {
  97. newData.Rz = evt.Rz;
  98. #ifdef DEBUG_OUTPUT
  99. updated = 1;
  100. #endif
  101. }
  102. if ((evt.Slider > (centerValue.Slider + deadZone.Slider))
  103. || (evt.Slider < (centerValue.Slider - deadZone.Slider))) {
  104. newData.Slider = evt.Slider;
  105. #ifdef DEBUG_OUTPUT
  106. updated = 1;
  107. #endif
  108. }
  109. #ifdef DEBUG_OUTPUT
  110. if (updated) {
  111. Serial.print("X: ");
  112. PrintHex<uint16_t > (newData.X, 0x80);
  113. Serial.print(" Y: ");
  114. PrintHex<uint16_t > (newData.Y, 0x80);
  115. Serial.print(" Z: ");
  116. PrintHex<uint8_t > (newData.Z, 0x80);
  117. Serial.print(" Rx: ");
  118. PrintHex<uint8_t > (newData.Rx, 0x80);
  119. Serial.print(" Ry: ");
  120. PrintHex<uint8_t > (newData.Ry, 0x80);
  121. Serial.print(" Rz: ");
  122. PrintHex<uint16_t > (newData.Rz, 0x80);
  123. Serial.print(" S: ");
  124. PrintHex<uint8_t > (newData.Slider, 0x80);
  125. Serial.println("");
  126. }
  127. #endif
  128. if (client) {
  129. client->OnGamePadChanged(newData);
  130. }
  131. }
  132. void JoystickEventsDeadZone::OnMouseMoved(uint8_t x, uint8_t y) {
  133. #ifdef DEBUG_OUTPUT_RAW
  134. Serial.print("Raw Mouse X: ");
  135. PrintHex<uint8_t >(x, 0x80);
  136. Serial.print("\tY: ");
  137. PrintHex<uint8_t >(y, 0x80);
  138. Serial.println("");
  139. #endif
  140. uint8_t newX = centerMouseX;
  141. uint8_t newY = centerMouseY;
  142. #ifdef DEBUG_OUTPUT
  143. uint8_t updated = 0;
  144. #endif
  145. if ((x > (centerMouseX + deadZoneMouseX))
  146. || (x < (centerMouseX - deadZoneMouseX))) {
  147. newX = x;
  148. #ifdef DEBUG_OUTPUT
  149. updated = 1;
  150. #endif
  151. }
  152. if ((y > (centerMouseY + deadZoneMouseY))
  153. || (y < (centerMouseY - deadZoneMouseY))) {
  154. newY = y;
  155. #ifdef DEBUG_OUTPUT
  156. updated = 1;
  157. #endif
  158. }
  159. #ifdef DEBUG_OUTPUT
  160. if (updated) {
  161. Serial.print("Mouse X: ");
  162. PrintHex<uint8_t >(newX, 0x80);
  163. Serial.print("\tY: ");
  164. PrintHex<uint8_t >(newY, 0x80);
  165. Serial.println("");
  166. }
  167. #endif
  168. if (client) {
  169. client->OnMouseMoved(x, y);
  170. }
  171. }