Control drones with a proper joystick
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Saitek X52 Arduino USB Host Shield Library.
  3. * Copyright 2016 by Thomas Buck <xythobuz@xythobuz.de>
  4. *
  5. * Based on "x52pro-linux" by Nirenjan Krishnan
  6. * https://github.com/nirenjan/x52pro-linux
  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 "x52.h"
  13. //#define DEBUG_OUTPUT Serial
  14. #define TIME_24H_FORMAT
  15. X52::X52(USB* u, USBHID* h) : usb(u), hid(h), ready(0) { }
  16. void X52::initialize() {
  17. ready = 0;
  18. if ((!usb) || (!hid)) {
  19. return;
  20. }
  21. // Get the USB device descriptor so we can check for a valid PID/VID
  22. uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
  23. USB_DEVICE_DESCRIPTOR* udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
  24. uint8_t ret = usb->getDevDescr(hid->GetAddress(), 0, sizeof(USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
  25. if (ret) {
  26. #ifdef DEBUG_OUTPUT
  27. DEBUG_OUTPUT.print("Error getting descriptor: ");
  28. DEBUG_OUTPUT.println(ret, DEC);
  29. #endif
  30. }
  31. uint16_t vid = udd->idVendor;
  32. uint16_t pid = udd->idProduct;
  33. #ifdef DEBUG_OUTPUT
  34. DEBUG_OUTPUT.print("VID: ");
  35. DEBUG_OUTPUT.print(vid, DEC);
  36. DEBUG_OUTPUT.print(" PID: ");
  37. DEBUG_OUTPUT.println(pid, DEC);
  38. #endif
  39. if ((vid == 0x06A3) && (pid == 0x0255)) {
  40. // Saitek X52
  41. ready = 1;
  42. } else if ((vid == 0x06A3) && (pid == 0x0762)) {
  43. // Saitek X52 Pro
  44. ready = 1;
  45. } else {
  46. #ifdef DEBUG_OUTPUT
  47. DEBUG_OUTPUT.println("No valid VID/PID found!");
  48. #endif
  49. }
  50. }
  51. void X52::setTime(uint8_t h, uint8_t m) {
  52. uint8_t ret = sendCommand(X52_TIME_CLOCK1, m | ((h & 0x7F) << 8)
  53. #ifdef TIME_24H_FORMAT
  54. | (1 << 15)
  55. #endif
  56. );
  57. if (ret) {
  58. #ifdef DEBUG_OUTPUT
  59. DEBUG_OUTPUT.print("Error setting time: ");
  60. DEBUG_OUTPUT.println(ret, DEC);
  61. #endif
  62. }
  63. }
  64. void X52::setTimeOffset(uint8_t cl, int16_t offset) {
  65. if (offset < -1023) {
  66. offset = -1023;
  67. }
  68. if (offset > 1023) {
  69. offset = 1023;
  70. }
  71. uint8_t negative = 0;
  72. if (offset < 0) {
  73. negative = 1;
  74. offset = -offset;
  75. }
  76. uint8_t ret = sendCommand(cl ? X52_OFFS_CLOCK3 : X52_OFFS_CLOCK2,
  77. (negative << 10) | (offset & 0x03FF)
  78. #ifdef TIME_24H_FORMAT
  79. | (1 << 15)
  80. #endif
  81. );
  82. if (ret) {
  83. #ifdef DEBUG_OUTPUT
  84. DEBUG_OUTPUT.print("Error setting offset: ");
  85. DEBUG_OUTPUT.println(ret, DEC);
  86. #endif
  87. }
  88. }
  89. void X52::setDate(uint8_t dd, uint8_t mm, uint8_t yy) {
  90. uint8_t ret = sendCommand(X52_DATE_DDMM, dd | (mm << 8));
  91. if (!ret) {
  92. ret = sendCommand(X52_DATE_YEAR, yy);
  93. }
  94. if (ret) {
  95. #ifdef DEBUG_OUTPUT
  96. DEBUG_OUTPUT.print("Error setting date: ");
  97. DEBUG_OUTPUT.println(ret, DEC);
  98. #endif
  99. }
  100. }
  101. void X52::setBlink(uint8_t state) {
  102. uint8_t ret = sendCommand(X52_BLINK_INDICATOR, state ? X52_BLINK_ON : X52_BLINK_OFF);
  103. if (ret != 0) {
  104. #ifdef DEBUG_OUTPUT
  105. DEBUG_OUTPUT.print("Error setting blink: ");
  106. DEBUG_OUTPUT.println(ret, DEC);
  107. #endif
  108. }
  109. }
  110. void X52::setShift(uint8_t state) {
  111. uint8_t ret = sendCommand(X52_SHIFT_INDICATOR, state ? X52_SHIFT_ON : X52_SHIFT_OFF);
  112. if (ret != 0) {
  113. #ifdef DEBUG_OUTPUT
  114. DEBUG_OUTPUT.print("Error setting shift: ");
  115. DEBUG_OUTPUT.println(ret, DEC);
  116. #endif
  117. }
  118. }
  119. void X52::setMFDText(uint8_t line, const char* text) {
  120. const static uint16_t lines[3] = {
  121. X52_MFD_LINE1,
  122. X52_MFD_LINE2,
  123. X52_MFD_LINE3
  124. };
  125. if (line >= 3) {
  126. #ifdef DEBUG_OUTPUT
  127. DEBUG_OUTPUT.print("Invalid line: ");
  128. DEBUG_OUTPUT.println(line, DEC);
  129. #endif
  130. return;
  131. }
  132. uint8_t ret = sendCommand(X52_MFD_CLEAR_LINE | lines[line], 0);
  133. if (ret) {
  134. #ifdef DEBUG_OUTPUT
  135. DEBUG_OUTPUT.print("Error clearing line: ");
  136. DEBUG_OUTPUT.println(ret, DEC);
  137. #endif
  138. return;
  139. }
  140. for (uint8_t i = 0; i < 16; i += 2) {
  141. if (text[i] == '\0') {
  142. break;
  143. }
  144. uint16_t value = text[i];
  145. if (text[i + 1] != '\0') {
  146. value |= text[i + 1] << 8;
  147. }
  148. ret = sendCommand(X52_MFD_WRITE_LINE | lines[line], value);
  149. if (ret) {
  150. #ifdef DEBUG_OUTPUT
  151. DEBUG_OUTPUT.print("Error writing to line: ");
  152. DEBUG_OUTPUT.println(ret, DEC);
  153. #endif
  154. return;
  155. }
  156. if (text[i + 1] == '\0') {
  157. break;
  158. }
  159. }
  160. }
  161. uint8_t X52::sendCommand(uint16_t command, uint16_t val) {
  162. if (!ready) {
  163. #ifdef DEBUG_OUTPUT
  164. DEBUG_OUTPUT.println("Invalid state!");
  165. #endif
  166. return 23;
  167. }
  168. if ((!usb) || (!hid)) {
  169. #ifdef DEBUG_OUTPUT
  170. DEBUG_OUTPUT.println("Invalid objects!");
  171. #endif
  172. return 42;
  173. }
  174. const uint8_t valLo = (val & 0x00FF);
  175. const uint8_t valHi = (val & 0xFF00) >> 8;
  176. #ifdef DEBUG_OUTPUT
  177. DEBUG_OUTPUT.println("Sending X52 Ctrl-Req...");
  178. #endif
  179. uint8_t ret = usb->ctrlReq(hid->GetAddress(), 0,
  180. USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE,
  181. X52_VENDOR_REQUEST, valLo, valHi, command,
  182. 0, 0, NULL, NULL);
  183. if (ret != 0) {
  184. #ifdef DEBUG_OUTPUT
  185. DEBUG_OUTPUT.print("Ctrl-Req Error Code: ");
  186. DEBUG_OUTPUT.println(val, DEC);
  187. #endif
  188. }
  189. return ret;
  190. }