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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. /*
  16. * Uuuhhh TODO!!
  17. * This is not a deadzone, this is something like sensitivity.
  18. * For deadzone, we need to only apply it from the center outwards.
  19. */
  20. JoystickEvents::JoystickEvents()
  21. : lastData(0), lastMouseX(0), lastMouseY(0), deadZone(0) {
  22. deadZone.X = 0;
  23. deadZone.Y = 0;
  24. deadZone.Z = 0;
  25. deadZone.Rx = 0;
  26. deadZone.Ry = 0;
  27. deadZone.Rz = 50;
  28. deadZoneMouseX = 0;
  29. deadZoneMouseY = 0;
  30. }
  31. void JoystickEvents::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 = lastData;
  50. uint8_t updated = 0;
  51. if ((evt->X > (lastData.X + deadZone.X))
  52. || (evt->X < (lastData.X - deadZone.X))) {
  53. newData.X = evt->X;
  54. updated = 1;
  55. }
  56. if ((evt->Y > (lastData.Y + deadZone.Y))
  57. || (evt->Y < (lastData.Y - deadZone.Y))) {
  58. newData.Y = evt->Y;
  59. updated = 1;
  60. }
  61. if ((evt->Z > (lastData.Z + deadZone.Z))
  62. || (evt->Z < (lastData.Z - deadZone.Z))) {
  63. newData.Z = evt->Z;
  64. updated = 1;
  65. }
  66. if ((evt->Rx > (lastData.Rx + deadZone.Rx))
  67. || (evt->Rx < (lastData.Rx - deadZone.Rx))) {
  68. newData.Rx = evt->Rx;
  69. updated = 1;
  70. }
  71. if ((evt->Ry > (lastData.Ry + deadZone.Ry))
  72. || (evt->Ry < (lastData.Ry - deadZone.Ry))) {
  73. newData.Ry = evt->Ry;
  74. updated = 1;
  75. }
  76. if ((evt->Rz > (lastData.Rz + deadZone.Rz))
  77. || (evt->Rz < (lastData.Rz - deadZone.Rz))) {
  78. newData.Rz = evt->Rz;
  79. updated = 1;
  80. }
  81. if ((evt->Slider > (lastData.Slider + deadZone.Slider))
  82. || (evt->Slider < (lastData.Slider - deadZone.Slider))) {
  83. newData.Slider = evt->Slider;
  84. updated = 1;
  85. }
  86. if (updated) {
  87. #ifdef DEBUG_OUTPUT
  88. Serial.print("X: ");
  89. PrintHex<uint16_t > (newData.X, 0x80);
  90. Serial.print(" Y: ");
  91. PrintHex<uint16_t > (newData.Y, 0x80);
  92. Serial.print(" Z: ");
  93. PrintHex<uint8_t > (newData.Z, 0x80);
  94. Serial.print(" Rx: ");
  95. PrintHex<uint8_t > (newData.Rx, 0x80);
  96. Serial.print(" Ry: ");
  97. PrintHex<uint8_t > (newData.Ry, 0x80);
  98. Serial.print(" Rz: ");
  99. PrintHex<uint16_t > (newData.Rz, 0x80);
  100. Serial.print(" S: ");
  101. PrintHex<uint8_t > (newData.Slider, 0x80);
  102. Serial.println("");
  103. #endif
  104. }
  105. lastData = *evt;
  106. }
  107. void JoystickEvents::OnHatSwitch(uint8_t hat) {
  108. #ifdef DEBUG_OUTPUT
  109. Serial.print("Hat Switch: ");
  110. PrintHex<uint8_t > (hat, 0x80);
  111. Serial.println("");
  112. #endif
  113. }
  114. void JoystickEvents::OnButtonUp(uint8_t but_id) {
  115. #ifdef DEBUG_OUTPUT
  116. Serial.print("Up: ");
  117. Serial.println(but_id, DEC);
  118. #endif
  119. }
  120. void JoystickEvents::OnButtonDn(uint8_t but_id) {
  121. #ifdef DEBUG_OUTPUT
  122. Serial.print("Down: ");
  123. Serial.println(but_id, DEC);
  124. #endif
  125. }
  126. void JoystickEvents::OnMouseMoved(uint8_t x, uint8_t y) {
  127. #ifdef DEBUG_OUTPUT_RAW
  128. Serial.print("Mouse X: ");
  129. PrintHex<uint8_t >(x, 0x80);
  130. Serial.print("\tY: ");
  131. PrintHex<uint8_t >(y, 0x80);
  132. Serial.println("");
  133. #endif
  134. uint8_t newX = lastMouseX;
  135. uint8_t newY = lastMouseY;
  136. uint8_t updated = 0;
  137. if ((x > (lastMouseX + deadZoneMouseX))
  138. || (x < (lastMouseX - deadZoneMouseX))) {
  139. newX = x;
  140. updated = 1;
  141. }
  142. if ((y > (lastMouseY + deadZoneMouseY))
  143. || (y < (lastMouseY - deadZoneMouseY))) {
  144. newY = y;
  145. updated = 1;
  146. }
  147. if (updated) {
  148. #ifdef DEBUG_OUTPUT
  149. Serial.print("Mouse X: ");
  150. PrintHex<uint8_t >(newX, 0x80);
  151. Serial.print("\tY: ");
  152. PrintHex<uint8_t >(newY, 0x80);
  153. Serial.println("");
  154. #endif
  155. }
  156. lastMouseX = x;
  157. lastMouseY = y;
  158. }