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.

x52.cpp 4.8KB

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